Skip to content

Commit db1e793

Browse files
committed
more tests passing!
1 parent cd913bf commit db1e793

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,25 @@ var _postcss2 = _interopRequireDefault(_postcss);
1414

1515
var declWhitelist = ['extends'],
1616
declFilter = new RegExp('^(' + declWhitelist.join('|') + ')$'),
17-
matchImports = /^([\w ]+) from "([^"]+)"$/;
17+
matchImports = /^([\w ]+) from ("([^"]+)"|'([^']+)')$/;
1818

1919
var processor = function processor(css, result) {
2020
var imports = {};
2121
css.eachDecl(declFilter, function (decl) {
2222
var matches = decl.value.match(matchImports);
2323
if (matches) {
2424
(function () {
25-
var _matches = _slicedToArray(matches, 3);
25+
var _matches = _slicedToArray(matches, 5);
2626

2727
var _ = _matches[0];
2828
var symbols = _matches[1];
29-
var path = _matches[2];
30-
29+
var _ = _matches[2];
30+
var doubleQuotePath = _matches[3];
31+
var singleQuotePath = _matches[4];
32+
var path = doubleQuotePath || singleQuotePath;
3133
imports[path] = imports[path] || {};
3234
var tmpSymbols = symbols.split(' ').map(function (s) {
33-
return imports[path][s] = '__tmp-' + processor.getRandomStr();
35+
return imports[path][s] = '__tmp_' + s + '_' + processor.getRandomStr();
3436
});
3537
decl.value = tmpSymbols.join(' ');
3638
})();

index.src.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import postcss from 'postcss'
22

33
const declWhitelist = ['extends'],
44
declFilter = new RegExp(`^(${declWhitelist.join('|')})$`),
5-
matchImports = /^([\w ]+) from "([^"]+)"$/
5+
matchImports = /^([\w ]+) from ("([^"]+)"|'([^']+)')$/
66

77
const processor = (css, result) => {
88
let imports = {}
99
css.eachDecl(declFilter, (decl) => {
1010
let matches = decl.value.match(matchImports)
1111
if (matches) {
12-
let [_, symbols, path] = matches
12+
let [_, symbols, _, doubleQuotePath, singleQuotePath] = matches,
13+
path = doubleQuotePath || singleQuotePath
1314
imports[path] = imports[path] || {}
1415
let tmpSymbols = symbols.split(' ')
15-
.map(s => imports[path][s] = `__tmp-${processor.getRandomStr()}`)
16+
.map(s => imports[path][s] = `__tmp_${s}_${processor.getRandomStr()}`)
1617
decl.value = tmpSymbols.join(' ')
1718
}
1819
})

test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,23 @@ describe("processor", () => {
1414
check(
1515
"it should extract an import within a :local",
1616
`:local(.exportName) { extends: importName from "path/library.css"; other: rule; }`,
17-
`:import("path/library.css") {importName: __tmp-rand0ml0l0l; }\n:local(.exportName) { extends: __tmp-rand0ml0l0l; other: rule; }`,
17+
`:import("path/library.css") {importName: __tmp_importName_rand0ml0l0l; }\n:local(.exportName) { extends: __tmp_importName_rand0ml0l0l; other: rule; }`,
1818
["rand0ml0l0l"]
1919
)
2020

21-
it("it should not care if single-quotes are used")
22-
it("should import multiple classes on a single line")
21+
check(
22+
"it should not care if single-quotes are used",
23+
`:local(.exportName) { extends: importName from 'path/library.css'; other: rule; }`,
24+
`:import("path/library.css") {importName: __tmp_importName_rand0ml0l0l; }\n:local(.exportName) { extends: __tmp_importName_rand0ml0l0l; other: rule; }`,
25+
["rand0ml0l0l"]
26+
)
27+
28+
check(
29+
"should import multiple classes on a single line",
30+
`:local(.exportName) { extends: importName secondImport from 'path/library.css'; other: rule; }`,
31+
`:import("path/library.css") {importName: __tmp_importName_rand0ml0l0l;secondImport: __tmp_secondImport_rand0ml1l1l; }\n:local(.exportName) { extends: __tmp_importName_rand0ml0l0l __tmp_secondImport_rand0ml1l1l; other: rule; }`,
32+
["rand0ml0l0l", "rand0ml1l1l"]
33+
)
2334
it("should consolidate imports by file for multiple files and multiple classes")
2435
it("should ignore imports not inside a :local")
2536
it("should ignore imports not inside our rule whitelist")

0 commit comments

Comments
 (0)