Skip to content

Commit f22f405

Browse files
author
Jed Mao
committed
Introduce plugins option
1 parent 7bcbf57 commit f22f405

File tree

8 files changed

+113
-19
lines changed

8 files changed

+113
-19
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ Default: `null`
100100

101101
A function to transform the content of imported files. Take one argument (file content) & should return the modified content.
102102

103+
#### `plugins`
104+
105+
Type: `Array`
106+
Default: `undefined`
107+
108+
An array of plugins you wish you apply on each imported file.
109+
103110
#### `encoding`
104111

105112
Type: `String`

index.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ function AtImport(options) {
8383
importedFiles,
8484
ignoredAtRules,
8585
null,
86-
hashFiles
86+
hashFiles,
87+
createProcessor(result, options.plugins)
8788
).then(function() {
8889
addIgnoredAtRulesOnTop(styles, ignoredAtRules)
8990

@@ -94,6 +95,16 @@ function AtImport(options) {
9495
}
9596
}
9697

98+
function createProcessor(result, plugins) {
99+
if (plugins) {
100+
if (Array.isArray(plugins)) {
101+
return postcss(plugins)
102+
}
103+
result.warn("plugins must be an array")
104+
}
105+
return postcss()
106+
}
107+
97108
/**
98109
* lookup for @import rules
99110
*
@@ -107,7 +118,8 @@ function parseStyles(
107118
importedFiles,
108119
ignoredAtRules,
109120
media,
110-
hashFiles
121+
hashFiles,
122+
processor
111123
) {
112124
var imports = []
113125
styles.eachAtRule("import", function checkAtRule(atRule) {
@@ -130,7 +142,8 @@ function parseStyles(
130142
importedFiles,
131143
ignoredAtRules,
132144
media,
133-
hashFiles
145+
hashFiles,
146+
processor
134147
)
135148
}, atRule.source)
136149
}))
@@ -225,7 +238,8 @@ function readAtImport(
225238
importedFiles,
226239
ignoredAtRules,
227240
media,
228-
hashFiles
241+
hashFiles,
242+
processor
229243
) {
230244
// parse-import module parse entire line
231245
// @todo extract what can be interesting from this one
@@ -283,7 +297,8 @@ function readAtImport(
283297
importedFiles,
284298
ignoredAtRules,
285299
media,
286-
hashFiles
300+
hashFiles,
301+
processor
287302
)
288303
}
289304

@@ -304,7 +319,8 @@ function readImportedContent(
304319
importedFiles,
305320
ignoredAtRules,
306321
media,
307-
hashFiles
322+
hashFiles,
323+
processor
308324
) {
309325
// add directory containing the @imported file in the paths
310326
// to allow local import from this file
@@ -357,10 +373,20 @@ function readImportedContent(
357373
importedFiles,
358374
ignoredAtRules,
359375
parsedAtImport.media,
360-
hashFiles
361-
).then(function() {
362-
insertRules(atRule, parsedAtImport, newStyles, resolvedFilename)
363-
})
376+
hashFiles,
377+
processor
378+
)
379+
.then(function() {
380+
return processor.process(newStyles)
381+
.then(function(newResult) {
382+
newResult.warnings().forEach(function(message) {
383+
result.warn(message)
384+
})
385+
})
386+
})
387+
.then(function() {
388+
insertRules(atRule, parsedAtImport, newStyles)
389+
})
364390
}
365391

366392
/**

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"devDependencies": {
3535
"css-whitespace": "^1.1.0",
3636
"eslint": "^0.23.0",
37+
"postcss-bem-linter": "^0.3.0",
38+
"postcss-nested": "^0.3.2",
3739
"tape": "^3.0.0"
3840
},
3941
"scripts": {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/** @define block-bar */
2+
3+
.block-bar {
4+
color: green;
5+
6+
&--modifier {
7+
color: yellow;
8+
}
9+
10+
&__element {
11+
color: black;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/** @define block-foo */
2+
3+
.block-foo {
4+
color: red;
5+
6+
&--modifier {
7+
color: white;
8+
}
9+
10+
&__element {
11+
color: blue;
12+
}
13+
}

test/fixtures/plugins.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import block-foo;
2+
@import block-bar;

test/fixtures/plugins.expected.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/** @define block-foo */
2+
3+
.block-foo {
4+
color: red;
5+
}
6+
7+
.block-foo--modifier {
8+
color: white;
9+
}
10+
11+
.block-foo__element {
12+
color: blue;
13+
}
14+
/** @define block-bar */
15+
16+
.block-bar {
17+
color: green;
18+
}
19+
20+
.block-bar--modifier {
21+
color: yellow;
22+
}
23+
24+
.block-bar__element {
25+
color: black;
26+
}

test/index.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var test = require("tape")
22

3+
var assign = require("object-assign")
34
var path = require("path")
45
var fs = require("fs")
56

@@ -16,9 +17,8 @@ function read(name) {
1617
}
1718

1819
function compareFixtures(t, name, msg, opts, postcssOpts) {
19-
opts = opts || {path: importsDir}
20-
postcss()
21-
.use(atImport(opts))
20+
opts = assign({path: importsDir}, opts || {})
21+
postcss(atImport(opts))
2222
.process(read("fixtures/" + name), postcssOpts)
2323
.then(trimResultCss)
2424
.then(function(actual) {
@@ -42,28 +42,33 @@ test("@import", function(t) {
4242

4343
compareFixtures(t, "glob", "should handle a glob pattern", {
4444
root: __dirname,
45-
path: importsDir,
4645
glob: true,
4746
})
4847

4948
compareFixtures(
5049
t,
5150
"glob-alt",
5251
"should handle a glob pattern with single quote and/or url(...)", {
53-
path: importsDir,
54-
glob: true,
55-
})
52+
glob: true,
53+
})
54+
5655
compareFixtures(t, "recursive", "should import stylsheets recursively")
5756

5857
compareFixtures(t, "relative", "should import stylsheets relatively")
5958

6059
compareFixtures(t, "empty-and-useless", "should work with empty files")
6160

6261
compareFixtures(t, "transform", "should support transform", {
63-
path: importsDir,
6462
transform: require("css-whitespace"),
6563
})
6664

65+
compareFixtures(t, "plugins", "should support plugins", {
66+
plugins: [
67+
require("postcss-nested"),
68+
require("postcss-bem-linter"),
69+
],
70+
})
71+
6772
compareFixtures(t, "cwd", "should work without a specified path", {})
6873

6974
compareFixtures(
@@ -78,7 +83,7 @@ test("@import", function(t) {
7883
t,
7984
"modules",
8085
"should be able to consume npm package or local modules",
81-
{root: __dirname, path: importsDir}
86+
{root: __dirname}
8287
)
8388

8489
var base = "@import url(http://)"

0 commit comments

Comments
 (0)