-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING: Rewrite imported file parsing
- Loading branch information
Showing
18 changed files
with
178 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var path = require("path") | ||
var postcss = require("postcss") | ||
var sugarss | ||
|
||
module.exports = function processContent( | ||
result, | ||
content, | ||
filename, | ||
options | ||
) { | ||
var plugins = options.plugins | ||
var ext = path.extname(filename) | ||
|
||
var parserList = [] | ||
|
||
// SugarSS support: | ||
if (ext === ".sss") { | ||
if (!sugarss) { | ||
try { | ||
sugarss = require("sugarss") | ||
} | ||
catch (e) { | ||
// Ignore | ||
} | ||
} | ||
if (sugarss) return runPostcss(content, filename, plugins, [ sugarss ]) | ||
} | ||
|
||
// Syntax support: | ||
if (result.opts.syntax && result.opts.syntax.parse) { | ||
parserList.push(result.opts.syntax.parse) | ||
} | ||
|
||
// Parser support: | ||
if (result.opts.parser) parserList.push(result.opts.parser) | ||
// Try the default as a last resort: | ||
parserList.push(null) | ||
|
||
return runPostcss(content, filename, plugins, parserList) | ||
} | ||
|
||
function runPostcss( | ||
content, | ||
filename, | ||
plugins, | ||
parsers, | ||
index | ||
) { | ||
if (!index) index = 0 | ||
return postcss(plugins).process(content, { | ||
from: filename, | ||
parser: parsers[index], | ||
}) | ||
.catch(function(err) { | ||
// If there's an error, try the next parser | ||
index++ | ||
// If there are no parsers left, throw it | ||
if (index === parsers.length) throw err | ||
return runPostcss(content, filename, plugins, parsers, index) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.sugarbar{ | ||
color: blue | ||
} | ||
|
||
import.sugarbar{} | ||
|
||
.sugar{ | ||
color: white | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@import "import-sugarbar.css" | ||
|
||
.sugar | ||
color: white |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import "foo-recursive.sss"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
bar{} | ||
|
||
foo.recursive{ | ||
color: red | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import "sugarbar.sss"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.sugarbar { | ||
color: blue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@import "bar.css" | ||
|
||
foo.recursive | ||
color: red |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@import "sugarbar.sss"; | ||
|
||
import.sugarbar{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.sugarbar | ||
color: blue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
bar{} | ||
.sugar{ | ||
color: white | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@import "bar.css" | ||
.sugar | ||
color: white |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.sugarbar { | ||
color: blue | ||
} | ||
.sugar { | ||
color: white | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@import "sugarbar.sss" | ||
.sugar | ||
color: white |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var fs = require("fs") | ||
var postcss = require("postcss") | ||
var assign = require("object-assign") | ||
var atImport = require("../..") | ||
|
||
function read(name, ext) { | ||
if (!ext) ext = ".css" | ||
return fs.readFileSync("fixtures/" + name + ext, "utf8") | ||
} | ||
|
||
module.exports = function(t, name, ext, opts, postcssOpts, warnings) { | ||
opts = assign({ path: "fixtures/imports" }, opts) | ||
return postcss(atImport(opts)) | ||
.process(read(name, ext), postcssOpts || {}) | ||
.then(function(result) { | ||
var actual = result.css | ||
var expected = read(name + ".expected") | ||
// handy thing: checkout actual in the *.actual.css file | ||
fs.writeFile("fixtures/" + name + ".actual.css", actual) | ||
t.is(actual, expected) | ||
if (!warnings) { | ||
warnings = [] | ||
} | ||
result.warnings().forEach(function(warning, index) { | ||
t.is( | ||
warning.text, | ||
warnings[index], | ||
"unexpected warning: \"" + warning.text + "\"" | ||
) | ||
}) | ||
}) | ||
} |