Skip to content

Commit

Permalink
BREAKING: Rewrite imported file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Nov 23, 2016
1 parent 883669d commit 9fdde94
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 6 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var postcss = require("postcss")
var joinMedia = require("./lib/join-media")
var resolveId = require("./lib/resolve-id")
var loadContent = require("./lib/load-content")
var processContent = require("./lib/process-content")
var parseStatements = require("./lib/parse-statements")
var promiseEach = require("promise-each")

Expand Down Expand Up @@ -322,11 +323,12 @@ function loadImportContent(
return
}

return postcss(options.plugins).process(content, {
from: filename,
syntax: result.opts.syntax,
parser: result.opts.parser,
})
return processContent(
result,
content,
filename,
options
)
.then(function(importedResult) {
var styles = importedResult.root
result.messages = result.messages.concat(importedResult.messages)
Expand Down
61 changes: 61 additions & 0 deletions lib/process-content.js
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)
})
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"eslint": "^1.10.3",
"eslint-config-i-am-meticulous": "^2.0.0",
"npmpub": "^3.0.1",
"postcss-scss": "^0.1.3"
"postcss-scss": "^0.1.3",
"sugarss": "^0.2.0"
},
"jspm": {
"name": "postcss-import",
Expand Down
28 changes: 28 additions & 0 deletions test/custom-syntax-parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import test from "ava"
import scss from "postcss-scss"
import sugarss from "sugarss"
import compareFixtures from "./helpers/compare-fixtures"
import compareFixturesExt from "./helpers/compare-fixtures-ext"

test("should process custom syntax", t => {
return compareFixtures(t, "scss-syntax", null, {
Expand All @@ -13,3 +15,29 @@ test("should process custom syntax by parser", t => {
parser: scss,
})
})

test(".css importing .sss should work", t => {
return compareFixtures(t, "import-sss")
})

test(".sss importing .sss should work", t => {
return compareFixturesExt(t, "sugar", ".sss", null, {
parser: sugarss,
})
})

test(".sss importing .css should work", t => {
return compareFixturesExt(t, "sugar-import-css", ".sss", null, {
parser: sugarss,
})
})

test(".css importing .sss importing .css should work", t => {
return compareFixtures(t, "import-sss-css")
})

test(".sss importing .css importing .sss should work", t => {
return compareFixturesExt(t, "import-css-sss", ".sss", null, {
parser: sugarss,
})
})
9 changes: 9 additions & 0 deletions test/fixtures/import-css-sss.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.sugarbar{
color: blue
}

import.sugarbar{}

.sugar{
color: white
}
4 changes: 4 additions & 0 deletions test/fixtures/import-css-sss.sss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@import "import-sugarbar.css"

.sugar
color: white
1 change: 1 addition & 0 deletions test/fixtures/import-sss-css.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "foo-recursive.sss";
5 changes: 5 additions & 0 deletions test/fixtures/import-sss-css.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bar{}

foo.recursive{
color: red
}
1 change: 1 addition & 0 deletions test/fixtures/import-sss.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "sugarbar.sss";
3 changes: 3 additions & 0 deletions test/fixtures/import-sss.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.sugarbar {
color: blue
}
4 changes: 4 additions & 0 deletions test/fixtures/imports/foo-recursive.sss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@import "bar.css"

foo.recursive
color: red
3 changes: 3 additions & 0 deletions test/fixtures/imports/import-sugarbar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@import "sugarbar.sss";

import.sugarbar{}
2 changes: 2 additions & 0 deletions test/fixtures/imports/sugarbar.sss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.sugarbar
color: blue
4 changes: 4 additions & 0 deletions test/fixtures/sugar-import-css.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bar{}
.sugar{
color: white
}
3 changes: 3 additions & 0 deletions test/fixtures/sugar-import-css.sss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@import "bar.css"
.sugar
color: white
6 changes: 6 additions & 0 deletions test/fixtures/sugar.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.sugarbar {
color: blue
}
.sugar {
color: white
}
3 changes: 3 additions & 0 deletions test/fixtures/sugar.sss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@import "sugarbar.sss"
.sugar
color: white
32 changes: 32 additions & 0 deletions test/helpers/compare-fixtures-ext.js
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 + "\""
)
})
})
}

0 comments on commit 9fdde94

Please sign in to comment.