Skip to content

Commit 94ce815

Browse files
committed
Added: option addDependencyTo to make the integration with webpack more elegant (7.1.0)
Ref webpack/postcss-loader#31
1 parent ccf5cef commit 94ce815

File tree

7 files changed

+132
-69
lines changed

7 files changed

+132
-69
lines changed

.eslintrc

Lines changed: 0 additions & 35 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 7.1.0 - 2015-10-19
2+
3+
- Added: option `addDependencyTo` to make the integration with webpack more
4+
elegant
5+
(ref [postcss-loader#31](https://github.com/postcss/postcss-loader/issues/31))
6+
17
# 7.0.0 - 2015-08-25
28

39
- Removed: compatibility with postcss v4.x

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 Maxime Thirouin, Jason Campbell & Kevin Mårtensson
3+
Copyright (c) Maxime Thirouin, Jason Campbell & Kevin Mårtensson
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

README.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
> [PostCSS](https://github.com/postcss/postcss) plugin to transform `@import` rules by inlining content.
44
55
This plugin can consume local files, node modules or bower packages.
6-
To resolve path of an `@import` rule, it can look into root directory (by default `process.cwd()`), `node_modules` (Per [`package.json's style` attribute](http://stackoverflow.com/questions/32037150/style-field-in-package-json)), `web_modules`, `bower_components` or local modules.
6+
To resolve path of an `@import` rule, it can look into root directory
7+
(by default `process.cwd()`), `node_modules`, `web_modules`, `bower_components`
8+
or local modules.
9+
_When importing a module, it will looks for `index.css` or file referenced in
10+
`package.json` in the `style` field._
711
You can also provide manually multiples paths where to look at.
812

913
**Notes:**
@@ -161,6 +165,24 @@ It's to optimize output and skip similar files like `normalize.css` for example.
161165
If this behavior is not what you want, just set this option to `false` to
162166
disable it.
163167

168+
#### `addDependencyTo`
169+
170+
Type: `Function`
171+
Default: null
172+
173+
Allow to generate and call a callback that take one argument, the object from
174+
which you need to call `addDependency` from.
175+
Called whenever a file is imported, handy in a webpack workflow.
176+
It's equivalent to `onImport` with the following code:
177+
178+
```js
179+
{
180+
onImport: function (files) {
181+
files.forEach(this.addDependency)
182+
}.bind(obj) // obj = the argument you should pass to `addDependencyTo()`
183+
}
184+
```
185+
164186
#### Example with some options
165187

166188
```js
@@ -178,16 +200,11 @@ var css = postcss()
178200

179201
---
180202

181-
## Contributing
182-
183-
Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
203+
## CONTRIBUTING
184204

185-
```console
186-
$ git clone https://github.com/postcss/postcss-import.git
187-
$ git checkout -b patch-1
188-
$ npm install
189-
$ npm test
190-
```
205+
* ⇄ Pull requests and ★ Stars are always welcome.
206+
* For bugs and feature requests, please create an issue.
207+
* Pull requests must be accompanied by passing automated tests (`$ npm test`).
191208

192209
## [Changelog](CHANGELOG.md)
193210

index.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ function AtImport(options) {
4343

4444
// convert string to an array of a single element
4545
if (typeof options.path === "string") {
46-
options.path = [options.path]
46+
options.path = [ options.path ]
4747
}
4848

4949
return function(styles, result) {
50-
const opts = assign({}, options || {})
50+
var opts = assign({}, options || {})
51+
5152
// auto add from option if possible
5253
if (
5354
!opts.from &&
@@ -92,6 +93,14 @@ function AtImport(options) {
9293
function onParseEnd() {
9394
addIgnoredAtRulesOnTop(styles, state.ignoredAtRules)
9495

96+
if (
97+
typeof opts.addDependencyTo === "object" &&
98+
typeof opts.addDependencyTo.addDependency === "function"
99+
) {
100+
Object.keys(state.importedFiles)
101+
.forEach(opts.addDependencyTo.addDependency)
102+
}
103+
95104
if (typeof opts.onImport === "function") {
96105
opts.onImport(Object.keys(state.importedFiles))
97106
}
@@ -132,7 +141,7 @@ function parseStyles(
132141
var imports = []
133142
styles.walkAtRules("import", function checkAtRule(atRule) {
134143
if (atRule.nodes) {
135-
result.warn(warnNodesMessage, {node: atRule})
144+
result.warn(warnNodesMessage, { node: atRule })
136145
}
137146
if (options.glob && glob.hasMagic(atRule.params)) {
138147
imports = parseGlob(atRule, options, imports)
@@ -268,7 +277,7 @@ function readAtImport(
268277
parsedAtImport.media = media
269278

270279
// save
271-
state.ignoredAtRules.push([atRule, parsedAtImport])
280+
state.ignoredAtRules.push([ atRule, parsedAtImport ])
272281

273282
// detach
274283
detach(atRule)
@@ -350,7 +359,7 @@ function readImportedContent(
350359
)
351360

352361
if (fileContent.trim() === "") {
353-
result.warn(resolvedFilename + " is empty", {node: atRule})
362+
result.warn(resolvedFilename + " is empty", { node: atRule })
354363
detach(atRule)
355364
return resolvedPromise
356365
}
@@ -440,7 +449,7 @@ function insertRules(atRule, parsedAtImport, newStyles) {
440449

441450
// move nodes
442451
wrapper.nodes = newNodes
443-
newNodes = [wrapper]
452+
newNodes = [ wrapper ]
444453
}
445454
else if (newNodes && newNodes.length) {
446455
newNodes[0].raws.before = atRule.raws.before
@@ -453,7 +462,7 @@ function insertRules(atRule, parsedAtImport, newStyles) {
453462

454463
// replace atRule by imported nodes
455464
var nodes = atRule.parent.nodes
456-
nodes.splice.apply(nodes, [nodes.indexOf(atRule), 0].concat(newNodes))
465+
nodes.splice.apply(nodes, [ nodes.indexOf(atRule), 0 ].concat(newNodes))
457466
detach(atRule)
458467
}
459468

@@ -489,7 +498,7 @@ function resolveFilename(name, root, paths, source, resolver) {
489498
basedir: dir,
490499
moduleDirectory: moduleDirectories.concat(paths),
491500
paths: paths,
492-
extensions: [".css"],
501+
extensions: [ ".css" ],
493502
packageFilter: function processPackage(pkg) {
494503
pkg.main = pkg.style || "index.css"
495504
return pkg

package.json

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-import",
3-
"version": "7.0.0",
3+
"version": "7.1.0",
44
"description": "PostCSS plugin to import CSS files",
55
"keywords": [
66
"css",
@@ -12,10 +12,7 @@
1212
],
1313
"author": "Maxime Thirouin",
1414
"license": "MIT",
15-
"repository": {
16-
"type": "git",
17-
"url": "https://github.com/postcss/postcss-import.git"
18-
},
15+
"repository": "https://github.com/postcss/postcss-import.git",
1916
"files": [
2017
"index.js"
2118
],
@@ -33,6 +30,45 @@
3330
"tape": "^4.0.3"
3431
},
3532
"scripts": {
36-
"test": "eslint . && tape test"
33+
"eslint": "eslint .",
34+
"tape": "tape test",
35+
"test": "npm run eslint && npm run tape"
36+
},
37+
38+
"eslintConfig": {
39+
"extends": "eslint:recommended",
40+
"ecmaFeatures": {
41+
"modules": true,
42+
"experimentalObjectRestSpread": true
43+
},
44+
"env": {
45+
"es6": true,
46+
"node": true
47+
},
48+
"rules": {
49+
"indent": [ 2, 2 ],
50+
"max-len": [ 2, 80, 4 ],
51+
"no-multiple-empty-lines": [ 2, { "max": 1 } ],
52+
"quotes": [ 2, "double" ],
53+
"semi": [ 2, "never" ],
54+
"comma-dangle": [ 2, "always-multiline" ],
55+
"comma-style": [ 2, "last" ],
56+
"brace-style": [ 2, "stroustrup" ],
57+
"dot-location": [ 2, "property" ],
58+
"computed-property-spacing": [ 2, "never" ],
59+
"object-curly-spacing": [ 2, "always" ],
60+
"array-bracket-spacing": [ 2, "always" ],
61+
"space-after-keywords": [ 2, "always" ],
62+
"space-before-blocks": [ 2, "always" ],
63+
"space-before-function-paren": [ 2, "never" ],
64+
"space-in-parens": [ 2, "never" ],
65+
"space-unary-ops": [ 2, { "words": true, "nonwords": false } ],
66+
"spaced-comment": [ 2, "always" ],
67+
68+
"one-var": [ 2, "never" ],
69+
"no-bitwise": [ 2 ],
70+
"prefer-const": [ 2 ]
71+
}
3772
}
73+
3874
}

test/index.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function read(name) {
1515
}
1616

1717
function compareFixtures(t, name, msg, opts, postcssOpts) {
18-
opts = assign({path: importsDir}, opts || {})
18+
opts = assign({ path: importsDir }, opts || {})
1919
postcss(atImport(opts))
2020
.process(read("fixtures/" + name), postcssOpts)
2121
.then(trimResultCss)
@@ -96,14 +96,14 @@ test("@import", function(t) {
9696
"relative-to-source",
9797
"should not need `path` option if `source` option has been passed",
9898
null,
99-
{from: "test/fixtures/relative-to-source.css"}
99+
{ from: "test/fixtures/relative-to-source.css" }
100100
)
101101

102102
compareFixtures(
103103
t,
104104
"modules",
105105
"should be able to consume npm package or local modules",
106-
{root: __dirname}
106+
{ root: __dirname }
107107
)
108108

109109
var base = "@import url(http://)"
@@ -138,7 +138,7 @@ test("@import error output", function(t) {
138138
var file = importsDir + "/import-missing.css"
139139
postcss()
140140
.use(atImport())
141-
.process(fs.readFileSync(file), {from: file})
141+
.process(fs.readFileSync(file), { from: file })
142142
.catch(function(error) {
143143
t.throws(
144144
function() {
@@ -157,8 +157,8 @@ test("@import error output", function(t) {
157157
test("@import glob pattern matches no files", function(t) {
158158
var file = importsDir + "/glob-missing.css"
159159
postcss()
160-
.use(atImport({glob: true}))
161-
.process(fs.readFileSync(file), {from: file})
160+
.use(atImport({ glob: true }))
161+
.process(fs.readFileSync(file), { from: file })
162162
.then(trimResultCss)
163163
.then(function(css) {
164164
t.equal(
@@ -219,9 +219,39 @@ test("@import callback", function(t) {
219219
.then(trimResultCss)
220220
})
221221

222+
test("@import callback (webpack)", function(t) {
223+
var files = []
224+
var webpackMock = {
225+
addDependency: (file) => files.push(file),
226+
}
227+
228+
postcss()
229+
.use(atImport({
230+
path: importsDir,
231+
addDependencyTo: webpackMock,
232+
}))
233+
.process(read("fixtures/recursive"), {
234+
from: "./test/fixtures/recursive.css",
235+
})
236+
.then(trimResultCss)
237+
.then(() => {
238+
t.deepEqual(
239+
files,
240+
[
241+
path.join(__dirname, "fixtures", "recursive.css"),
242+
path.join(__dirname, "fixtures", "imports", "foo-recursive.css"),
243+
path.join(__dirname, "fixtures", "imports", "bar.css"),
244+
],
245+
"should have a callback shortcut for webpack"
246+
)
247+
248+
t.end()
249+
})
250+
})
251+
222252
test("import relative files using path option only", function(t) {
223253
postcss()
224-
.use(atImport({path: "test/fixtures/imports/relative"}))
254+
.use(atImport({ path: "test/fixtures/imports/relative" }))
225255
.process(read("fixtures/imports/relative/import"))
226256
.then(trimResultCss)
227257
.then(function(css) {
@@ -262,7 +292,7 @@ test("@import custom resolve", function(t) {
262292
var resolve = require("resolve")
263293
var sassResolve = function(file, opts) {
264294
opts = opts || {}
265-
opts.extensions = [".scss", ".css"]
295+
opts.extensions = [ ".scss", ".css" ]
266296
opts.packageFilter = function(pkg) {
267297
pkg.main = pkg.sass || pkg.style || "index"
268298
return pkg
@@ -273,7 +303,7 @@ test("@import custom resolve", function(t) {
273303
t,
274304
"custom-resolve-modules",
275305
"should be able to consume modules in the custom-resolve way",
276-
{root: __dirname, path: importsDir, resolve: sassResolve}
306+
{ root: __dirname, path: importsDir, resolve: sassResolve }
277307
)
278308

279309
t.end()

0 commit comments

Comments
 (0)