Skip to content

Commit 7d9099c

Browse files
committed
Merge branch 'v9-dev'
2 parents 49cf9be + 7124d43 commit 7d9099c

28 files changed

+251
-121
lines changed

README.md

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,6 @@ Default: `[]`
126126

127127
A string or an array of paths in where to look for files.
128128

129-
#### `transform`
130-
131-
Type: `Function`
132-
Default: `null`
133-
134-
A function to transform the content of imported files. Take one argument (file
135-
content) and should return the modified content or a resolved promise with it.
136-
`undefined` result will be skipped.
137-
138-
```js
139-
transform: function(css) {
140-
return postcss([somePlugin]).process(css).then(function(result) {
141-
return result.css;
142-
});
143-
}
144-
```
145-
146129
#### `plugins`
147130

148131
Type: `Array`
@@ -163,10 +146,12 @@ files).
163146
Type: `Function`
164147
Default: `null`
165148

166-
You can overwrite the default path resolving way by setting this option.
167-
This function gets `(id, basedir, importOptions)` arguments and returns full
168-
path, array of paths or promise resolving paths.
169-
You can use [resolve](https://github.com/substack/node-resolve) for that.
149+
You can provide a custom path resolver with this option. This function gets
150+
`(id, basedir, importOptions)` arguments and should return a path, an array of
151+
paths or a promise resolving to the path(s). If you do not return an absolute
152+
path, your path will be resolved to an absolute path using the default
153+
resolver.
154+
You can use [resolve](https://github.com/substack/node-resolve) for this.
170155

171156
#### `load`
172157

@@ -230,6 +215,13 @@ postcss()
230215
})
231216
```
232217

218+
### jspm Usage
219+
220+
postcss-import can `@import` [jspm](http://jspm.io) dependencies if
221+
[`pkg-resolve`](https://www.npmjs.com/package/pkg-resolve) is installed by the
222+
user. Run `npm install pkg-resolve` to install it. postcss-import should then be
223+
able to import from jspm dependencies without further configuration.
224+
233225
## `dependency` Message Support
234226

235227
`postcss-import` adds a message to `result.messages` for each `@import`. Messages are in the following format:

index.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var postcss = require("postcss")
44
var joinMedia = require("./lib/join-media")
55
var resolveId = require("./lib/resolve-id")
66
var loadContent = require("./lib/load-content")
7+
var processContent = require("./lib/process-content")
78
var parseStatements = require("./lib/parse-statements")
89
var promiseEach = require("promise-each")
910

@@ -62,6 +63,14 @@ function AtImport(options) {
6263
typeof options.addDependencyTo === "object" &&
6364
typeof options.addDependencyTo.addDependency === "function"
6465
) {
66+
console.warn([
67+
"addDependencyTo is deprecated in favor of",
68+
"result.messages.dependency; postcss-loader >= v1.0.0 will",
69+
"automatically add your imported files to webpack's file watcher.",
70+
"For more information, see",
71+
"https://github.com/postcss/postcss-import\
72+
#dependency-message-support",
73+
].join("\n"))
6574
Object.keys(state.importedFiles)
6675
.forEach(options.addDependencyTo.addDependency)
6776
}
@@ -227,11 +236,18 @@ function resolveImportId(
227236
: options.root
228237

229238
return Promise.resolve(options.resolve(stmt.uri, base, options))
230-
.then(function(resolved) {
231-
if (!Array.isArray(resolved)) {
232-
resolved = [ resolved ]
239+
.then(function(paths) {
240+
if (!Array.isArray(paths)) {
241+
paths = [ paths ]
233242
}
234243

244+
return Promise.all(paths.map(function(file) {
245+
// Ensure that each path is absolute:
246+
if (!path.isAbsolute(file)) return resolveId(file, base, options)
247+
return file
248+
}))
249+
})
250+
.then(function(resolved) {
235251
// Add dependency messages:
236252
resolved.forEach(function(file) {
237253
result.messages.push({
@@ -261,6 +277,7 @@ function resolveImportId(
261277
}, [])
262278
})
263279
.catch(function(err) {
280+
if (err.message.indexOf("Failed to find") !== -1) throw err
264281
result.warn(err.message, { node: atRule })
265282
})
266283
}
@@ -291,15 +308,6 @@ function loadImportContent(
291308
}
292309

293310
return Promise.resolve(options.load(filename, options))
294-
.then(function(content) {
295-
if (typeof options.transform !== "function") {
296-
return content
297-
}
298-
return Promise.resolve(options.transform(content, filename, options))
299-
.then(function(transformed) {
300-
return typeof transformed === "string" ? transformed : content
301-
})
302-
})
303311
.then(function(content) {
304312
if (content.trim() === "") {
305313
result.warn(filename + " is empty", { node: atRule })
@@ -314,11 +322,12 @@ function loadImportContent(
314322
return
315323
}
316324

317-
return postcss(options.plugins).process(content, {
318-
from: filename,
319-
syntax: result.opts.syntax,
320-
parser: result.opts.parser,
321-
})
325+
return processContent(
326+
result,
327+
content,
328+
filename,
329+
options
330+
)
322331
.then(function(importedResult) {
323332
var styles = importedResult.root
324333
result.messages = result.messages.concat(importedResult.messages)

lib/process-content.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var path = require("path")
2+
var postcss = require("postcss")
3+
var sugarss
4+
5+
module.exports = function processContent(
6+
result,
7+
content,
8+
filename,
9+
options
10+
) {
11+
var plugins = options.plugins
12+
var ext = path.extname(filename)
13+
14+
var parserList = []
15+
16+
// SugarSS support:
17+
if (ext === ".sss") {
18+
if (!sugarss) {
19+
try {
20+
sugarss = require("sugarss")
21+
}
22+
catch (e) {
23+
// Ignore
24+
}
25+
}
26+
if (sugarss) return runPostcss(content, filename, plugins, [ sugarss ])
27+
}
28+
29+
// Syntax support:
30+
if (result.opts.syntax && result.opts.syntax.parse) {
31+
parserList.push(result.opts.syntax.parse)
32+
}
33+
34+
// Parser support:
35+
if (result.opts.parser) parserList.push(result.opts.parser)
36+
// Try the default as a last resort:
37+
parserList.push(null)
38+
39+
return runPostcss(content, filename, plugins, parserList)
40+
}
41+
42+
function runPostcss(
43+
content,
44+
filename,
45+
plugins,
46+
parsers,
47+
index
48+
) {
49+
if (!index) index = 0
50+
return postcss(plugins).process(content, {
51+
from: filename,
52+
parser: parsers[index],
53+
})
54+
.catch(function(err) {
55+
// If there's an error, try the next parser
56+
index++
57+
// If there are no parsers left, throw it
58+
if (index === parsers.length) throw err
59+
return runPostcss(content, filename, plugins, parsers, index)
60+
})
61+
}

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@
3030
"eslint": "^1.10.3",
3131
"eslint-config-i-am-meticulous": "^2.0.0",
3232
"npmpub": "^3.0.1",
33-
"postcss-scss": "^0.1.3"
34-
},
35-
"optionalDependencies": {
36-
"pkg-resolve": "^0.1.7"
33+
"postcss-scss": "^0.1.3",
34+
"sugarss": "^0.2.0"
3735
},
3836
"jspm": {
3937
"name": "postcss-import",

test/custom-resolve.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import test from "ava"
22
import compareFixtures from "./helpers/compare-fixtures"
3+
import postcss from "postcss"
4+
import atImport from ".."
35
import path from "path"
46

57
test.serial("should accept file", t => {
@@ -43,3 +45,23 @@ test.serial("should accept promised array of files", t => {
4345
},
4446
})
4547
})
48+
49+
test(
50+
"should apply default resolver when custom doesn't return an absolute path",
51+
function(t) {
52+
return postcss()
53+
.use(atImport({
54+
resolve: path => {
55+
return path.replace("foo", "imports/bar")
56+
},
57+
load: p => {
58+
t.is(p, path.resolve("fixtures/imports", "bar.css"))
59+
return "/* comment */"
60+
},
61+
}))
62+
.process(`@import "foo.css";`, { from: "fixtures/custom-resolve-file" })
63+
.then(result => {
64+
t.is(result.css, "/* comment */")
65+
})
66+
}
67+
)

test/custom-syntax-parser.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import test from "ava"
2+
import scss from "postcss-scss"
3+
import sugarss from "sugarss"
4+
import compareFixtures from "./helpers/compare-fixtures"
5+
import compareFixturesExt from "./helpers/compare-fixtures-ext"
6+
7+
test("should process custom syntax", t => {
8+
return compareFixtures(t, "scss-syntax", null, {
9+
syntax: scss,
10+
})
11+
})
12+
13+
test("should process custom syntax by parser", t => {
14+
return compareFixtures(t, "scss-parser", null, {
15+
parser: scss,
16+
})
17+
})
18+
19+
test(".css importing .sss should work", t => {
20+
return compareFixtures(t, "import-sss")
21+
})
22+
23+
test(".sss importing .sss should work", t => {
24+
return compareFixturesExt(t, "sugar", ".sss", null, {
25+
parser: sugarss,
26+
})
27+
})
28+
29+
test(".sss importing .css should work", t => {
30+
return compareFixturesExt(t, "sugar-import-css", ".sss", null, {
31+
parser: sugarss,
32+
})
33+
})
34+
35+
test(".css importing .sss importing .css should work", t => {
36+
return compareFixtures(t, "import-sss-css")
37+
})
38+
39+
test(".sss importing .css importing .sss should work", t => {
40+
return compareFixturesExt(t, "import-css-sss", ".sss", null, {
41+
parser: sugarss,
42+
})
43+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.sugarbar{
2+
color: blue
3+
}
4+
5+
import.sugarbar{}
6+
7+
.sugar{
8+
color: white
9+
}

test/fixtures/import-css-sss.sss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@import "import-sugarbar.css"
2+
3+
.sugar
4+
color: white

test/fixtures/import-sss-css.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "foo-recursive.sss";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bar{}
2+
3+
foo.recursive{
4+
color: red
5+
}

0 commit comments

Comments
 (0)