Skip to content

Commit 7af006e

Browse files
author
Jed Mao
committed
Introduce plugins option
1 parent 88b0b69 commit 7af006e

File tree

8 files changed

+261
-143
lines changed

8 files changed

+261
-143
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 to be applied on each imported file.
109+
103110
#### `encoding`
104111

105112
Type: `String`

index.js

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ var helpers = require("postcss-message-helpers")
1212
var hash = require("string-hash")
1313
var glob = require("glob")
1414

15+
var Promise = global.Promise || require("es6-promise").Promise
16+
var resolvedPromise = new Promise(function(resolvePromise) {
17+
resolvePromise()
18+
})
19+
1520
/**
1621
* Constants
1722
*/
@@ -72,22 +77,33 @@ function AtImport(options) {
7277

7378
var hashFiles = {}
7479

75-
parseStyles(
80+
return parseStyles(
7681
result,
7782
styles,
7883
opts,
79-
insertRules,
8084
importedFiles,
8185
ignoredAtRules,
8286
null,
83-
hashFiles
84-
)
85-
addIgnoredAtRulesOnTop(styles, ignoredAtRules)
87+
hashFiles,
88+
createProcessor(result, options.plugins)
89+
).then(function() {
90+
addIgnoredAtRulesOnTop(styles, ignoredAtRules)
8691

87-
if (typeof opts.onImport === "function") {
88-
opts.onImport(Object.keys(importedFiles))
92+
if (typeof opts.onImport === "function") {
93+
opts.onImport(Object.keys(importedFiles))
94+
}
95+
})
96+
}
97+
}
98+
99+
function createProcessor(result, plugins) {
100+
if (plugins) {
101+
if (!Array.isArray(plugins)) {
102+
throw new Error("plugins option must be an array")
89103
}
104+
return postcss(plugins)
90105
}
106+
return postcss()
91107
}
92108

93109
/**
@@ -100,11 +116,11 @@ function parseStyles(
100116
result,
101117
styles,
102118
options,
103-
cb,
104119
importedFiles,
105120
ignoredAtRules,
106121
media,
107-
hashFiles
122+
hashFiles,
123+
processor
108124
) {
109125
var imports = []
110126
styles.eachAtRule("import", function checkAtRule(atRule) {
@@ -118,20 +134,20 @@ function parseStyles(
118134
imports.push(atRule)
119135
}
120136
})
121-
imports.forEach(function(atRule) {
122-
helpers.try(function transformAtImport() {
123-
readAtImport(
137+
return Promise.all(imports.map(function(atRule) {
138+
return helpers.try(function transformAtImport() {
139+
return readAtImport(
124140
result,
125141
atRule,
126142
options,
127-
cb,
128143
importedFiles,
129144
ignoredAtRules,
130145
media,
131-
hashFiles
146+
hashFiles,
147+
processor
132148
)
133149
}, atRule.source)
134-
})
150+
}))
135151
}
136152

137153
/**
@@ -220,11 +236,11 @@ function readAtImport(
220236
result,
221237
atRule,
222238
options,
223-
cb,
224239
importedFiles,
225240
ignoredAtRules,
226241
media,
227-
hashFiles
242+
hashFiles,
243+
processor
228244
) {
229245
// parse-import module parse entire line
230246
// @todo extract what can be interesting from this one
@@ -246,7 +262,7 @@ function readAtImport(
246262
// detach
247263
detach(atRule)
248264

249-
return
265+
return resolvedPromise
250266
}
251267

252268
addInputToPath(options)
@@ -264,7 +280,7 @@ function readAtImport(
264280
importedFiles[resolvedFilename][media]
265281
) {
266282
detach(atRule)
267-
return
283+
return resolvedPromise
268284
}
269285

270286
// save imported files to skip them next time
@@ -273,17 +289,17 @@ function readAtImport(
273289
}
274290
importedFiles[resolvedFilename][media] = true
275291

276-
readImportedContent(
292+
return readImportedContent(
277293
result,
278294
atRule,
279295
parsedAtImport,
280296
clone(options),
281297
resolvedFilename,
282-
cb,
283298
importedFiles,
284299
ignoredAtRules,
285300
media,
286-
hashFiles
301+
hashFiles,
302+
processor
287303
)
288304
}
289305

@@ -294,19 +310,18 @@ function readAtImport(
294310
* @param {Object} parsedAtImport
295311
* @param {Object} options
296312
* @param {String} resolvedFilename
297-
* @param {Function} cb
298313
*/
299314
function readImportedContent(
300315
result,
301316
atRule,
302317
parsedAtImport,
303318
options,
304319
resolvedFilename,
305-
cb,
306320
importedFiles,
307321
ignoredAtRules,
308322
media,
309-
hashFiles
323+
hashFiles,
324+
processor
310325
) {
311326
// add directory containing the @imported file in the paths
312327
// to allow local import from this file
@@ -328,7 +343,7 @@ function readImportedContent(
328343
if (fileContent.trim() === "") {
329344
result.warn(resolvedFilename + " is empty", {node: atRule})
330345
detach(atRule)
331-
return
346+
return resolvedPromise
332347
}
333348

334349
// skip files wich only contain @import rules
@@ -339,7 +354,7 @@ function readImportedContent(
339354
// skip files already imported at the same scope and same hash
340355
if (hashFiles[fileContentHash] && hashFiles[fileContentHash][media]) {
341356
detach(atRule)
342-
return
357+
return resolvedPromise
343358
}
344359

345360
// save hash files to skip them next time
@@ -352,18 +367,27 @@ function readImportedContent(
352367
var newStyles = postcss.parse(fileContent, options)
353368

354369
// recursion: import @import from imported file
355-
parseStyles(
370+
return parseStyles(
356371
result,
357372
newStyles,
358373
options,
359-
cb,
360374
importedFiles,
361375
ignoredAtRules,
362376
parsedAtImport.media,
363-
hashFiles
377+
hashFiles,
378+
processor
364379
)
365-
366-
cb(atRule, parsedAtImport, newStyles, resolvedFilename)
380+
.then(function() {
381+
return processor.process(newStyles)
382+
.then(function(newResult) {
383+
newResult.warnings().forEach(function(message) {
384+
result.warn(message)
385+
})
386+
})
387+
})
388+
.then(function() {
389+
insertRules(atRule, parsedAtImport, newStyles)
390+
})
367391
}
368392

369393
/**

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
],
2424
"dependencies": {
2525
"clone": "^0.1.17",
26+
"es6-promise": "^2.3.0",
2627
"glob": "^5.0.1",
2728
"object-assign": "^3.0.0",
2829
"postcss": "^4.1.4",

test/fixtures/imports/bar-decl.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
bar: bar;
3+
qux: qux;
4+
}

test/fixtures/imports/foo-decl.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
foo: foo;
3+
baz: baz;
4+
}

test/fixtures/plugins.css

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

test/fixtures/plugins.expected.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body {
2+
baz: baz;
3+
}
4+
body {
5+
qux: qux;
6+
}

0 commit comments

Comments
 (0)