Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ Default: `null`

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

#### `plugins`

Type: `Array`
Default: `undefined`

An array of plugins to be applied on each imported file.

#### `encoding`

Type: `String`
Expand Down
82 changes: 53 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ var helpers = require("postcss-message-helpers")
var hash = require("string-hash")
var glob = require("glob")

var Promise = global.Promise || require("es6-promise").Promise
var resolvedPromise = new Promise(function(resolvePromise) {
resolvePromise()
})

/**
* Constants
*/
Expand Down Expand Up @@ -72,22 +77,33 @@ function AtImport(options) {

var hashFiles = {}

parseStyles(
return parseStyles(
result,
styles,
opts,
insertRules,
importedFiles,
ignoredAtRules,
null,
hashFiles
)
hashFiles,
createProcessor(result, options.plugins)
).then(function() {
addIgnoredAtRulesOnTop(styles, ignoredAtRules)

if (typeof opts.onImport === "function") {
opts.onImport(Object.keys(importedFiles))
}
})
}
}

function createProcessor(result, plugins) {
if (plugins) {
if (!Array.isArray(plugins)) {
throw new Error("plugins option must be an array")
}
return postcss(plugins)
}
return postcss()
}

/**
Expand All @@ -100,11 +116,11 @@ function parseStyles(
result,
styles,
options,
cb,
importedFiles,
ignoredAtRules,
media,
hashFiles
hashFiles,
processor
) {
var imports = []
styles.eachAtRule("import", function checkAtRule(atRule) {
Expand All @@ -118,20 +134,20 @@ function parseStyles(
imports.push(atRule)
}
})
imports.forEach(function(atRule) {
helpers.try(function transformAtImport() {
readAtImport(
return Promise.all(imports.map(function(atRule) {
return helpers.try(function transformAtImport() {
return readAtImport(
result,
atRule,
options,
cb,
importedFiles,
ignoredAtRules,
media,
hashFiles
hashFiles,
processor
)
}, atRule.source)
})
}))
}

/**
Expand Down Expand Up @@ -220,11 +236,11 @@ function readAtImport(
result,
atRule,
options,
cb,
importedFiles,
ignoredAtRules,
media,
hashFiles
hashFiles,
processor
) {
// parse-import module parse entire line
// @todo extract what can be interesting from this one
Expand All @@ -246,7 +262,7 @@ function readAtImport(
// detach
detach(atRule)

return
return resolvedPromise
}

addInputToPath(options)
Expand All @@ -264,7 +280,7 @@ function readAtImport(
importedFiles[resolvedFilename][media]
) {
detach(atRule)
return
return resolvedPromise
}

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

readImportedContent(
return readImportedContent(
result,
atRule,
parsedAtImport,
clone(options),
resolvedFilename,
cb,
importedFiles,
ignoredAtRules,
media,
hashFiles
hashFiles,
processor
)
}

Expand All @@ -294,19 +310,18 @@ function readAtImport(
* @param {Object} parsedAtImport
* @param {Object} options
* @param {String} resolvedFilename
* @param {Function} cb
*/
function readImportedContent(
result,
atRule,
parsedAtImport,
options,
resolvedFilename,
cb,
importedFiles,
ignoredAtRules,
media,
hashFiles
hashFiles,
processor
) {
// add directory containing the @imported file in the paths
// to allow local import from this file
Expand All @@ -328,7 +343,7 @@ function readImportedContent(
if (fileContent.trim() === "") {
result.warn(resolvedFilename + " is empty", {node: atRule})
detach(atRule)
return
return resolvedPromise
}

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

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

// recursion: import @import from imported file
parseStyles(
return parseStyles(
result,
newStyles,
options,
cb,
importedFiles,
ignoredAtRules,
parsedAtImport.media,
hashFiles
hashFiles,
processor
)

cb(atRule, parsedAtImport, newStyles, resolvedFilename)
.then(function() {
return processor.process(newStyles)
.then(function(newResult) {
newResult.warnings().forEach(function(message) {
result.warn(message)
})
})
})
.then(function() {
insertRules(atRule, parsedAtImport, newStyles)
})
}

/**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
],
"dependencies": {
"clone": "^0.1.17",
"es6-promise": "^2.3.0",
"glob": "^5.0.1",
"object-assign": "^3.0.0",
"postcss": "^4.1.4",
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/imports/bar-decl.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
bar: bar;
qux: qux;
}
4 changes: 4 additions & 0 deletions test/fixtures/imports/foo-decl.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
foo: foo;
baz: baz;
}
2 changes: 2 additions & 0 deletions test/fixtures/plugins.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import foo-decl;
@import bar-decl;
6 changes: 6 additions & 0 deletions test/fixtures/plugins.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
body {
baz: baz;
}
body {
qux: qux;
}
Loading