Skip to content

Commit 9fdde94

Browse files
committed
BREAKING: Rewrite imported file parsing
1 parent 883669d commit 9fdde94

18 files changed

+178
-6
lines changed

index.js

Lines changed: 7 additions & 5 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

@@ -322,11 +323,12 @@ function loadImportContent(
322323
return
323324
}
324325

325-
return postcss(options.plugins).process(content, {
326-
from: filename,
327-
syntax: result.opts.syntax,
328-
parser: result.opts.parser,
329-
})
326+
return processContent(
327+
result,
328+
content,
329+
filename,
330+
options
331+
)
330332
.then(function(importedResult) {
331333
var styles = importedResult.root
332334
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +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"
33+
"postcss-scss": "^0.1.3",
34+
"sugarss": "^0.2.0"
3435
},
3536
"jspm": {
3637
"name": "postcss-import",

test/custom-syntax-parser.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import test from "ava"
22
import scss from "postcss-scss"
3+
import sugarss from "sugarss"
34
import compareFixtures from "./helpers/compare-fixtures"
5+
import compareFixturesExt from "./helpers/compare-fixtures-ext"
46

57
test("should process custom syntax", t => {
68
return compareFixtures(t, "scss-syntax", null, {
@@ -13,3 +15,29 @@ test("should process custom syntax by parser", t => {
1315
parser: scss,
1416
})
1517
})
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+
}

test/fixtures/import-sss.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "sugarbar.sss";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.sugarbar {
2+
color: blue
3+
}

0 commit comments

Comments
 (0)