Skip to content

Commit dad07fd

Browse files
committed
Merge pull request #2 from css-modules/tests-and-fixes
Tests and fixes
2 parents 72e90a2 + 6f4d499 commit dad07fd

File tree

13 files changed

+98
-13
lines changed

13 files changed

+98
-13
lines changed

src/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import postcss from 'postcss';
22

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

77
const processor = (css) => {
88
let imports = {};
@@ -14,18 +14,23 @@ const processor = (css) => {
1414
let [/*match*/, symbols, doubleQuotePath, singleQuotePath] = matches;
1515
let path = doubleQuotePath || singleQuotePath;
1616
imports[path] = imports[path] || {};
17-
let tmpSymbols = symbols.split(' ')
18-
.map(s => imports[path][s] = `__tmp_${s}_${processor.getRandomStr()}`);
17+
let tmpSymbols = symbols.split(/\s+/)
18+
.map(s => {
19+
if(!imports[path][s]) {
20+
imports[path][s] = `__tmp_${s}_${processor.getRandomStr()}`;
21+
}
22+
return imports[path][s];
23+
});
1924
decl.value = tmpSymbols.join(' ');
2025
}
2126
});
2227

2328
// If we've found any imports, insert :import rules
24-
Object.keys(imports).forEach(path => {
29+
Object.keys(imports).reverse().forEach(path => {
2530
let pathImports = imports[path];
2631
css.prepend(postcss.rule({
2732
selector: `:import("${path}")`,
28-
before: "\n",
33+
after: "\n",
2934
nodes: Object.keys(pathImports).map(importedSymbol => postcss.decl({
3035
prop: importedSymbol,
3136
value: pathImports[importedSymbol],
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
:local(.exportName) {
3+
extends: importName from "path/library.css";
4+
other: rule;
5+
}
6+
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
:local(.exportName) {
3+
extends: importName from "path/library.css";
4+
other: rule;
5+
}
6+
*/

test/test-cases/import-consolidate/expected.css

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
2-
:import("path/other-lib.css") {
3-
otherLibImport: __tmp_otherLibImport_rand3;
4-
}
51
:import("path/library.css") {
62
importName: __tmp_importName_rand0;
73
secondImport: __tmp_secondImport_rand1;
84
thirdImport: __tmp_thirdImport_rand2;
95
}
6+
:import("path/other-lib.css") {
7+
otherLibImport: __tmp_otherLibImport_rand3;
8+
}
109
:local(.exportName) {
1110
extends: __tmp_importName_rand0 __tmp_secondImport_rand1;
1211
other: rule;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
:import("path/library.css") {
2+
importName: __tmp_importName_rand0;
3+
importName2: __tmp_importName2_rand1;
4+
}
5+
6+
@media screen {
7+
:local(.exportName) {
8+
extends: __tmp_importName_rand0;
9+
extends: __tmp_importName2_rand1;
10+
other: rule2;
11+
}
12+
}
13+
14+
:local(.exportName) {
15+
extends: __tmp_importName_rand0;
16+
other: rule;
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@media screen {
2+
:local(.exportName) {
3+
extends: importName from "path/library.css";
4+
extends: importName2 from "path/library.css";
5+
other: rule2;
6+
}
7+
}
8+
9+
:local(.exportName) {
10+
extends: importName from "path/library.css";
11+
other: rule;
12+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
21
:import("path/library.css") {
32
importName: __tmp_importName_rand0;
4-
secondImport: __tmp_secondImport_rand1; }
3+
secondImport: __tmp_secondImport_rand1;
4+
}
55
:local(.exportName) { extends: __tmp_importName_rand0 __tmp_secondImport_rand1; other: rule; }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
:import("path/library.css") {
2+
importName: __tmp_importName_rand0;
3+
secondImport: __tmp_secondImport_rand1;
4+
importName2: __tmp_importName2_rand3;
5+
}
6+
:import("path/library2.css") {
7+
importName: __tmp_importName_rand2;
8+
}
9+
:local(.exportName) {
10+
extends: __tmp_importName_rand0 __tmp_secondImport_rand1;
11+
extends: __tmp_importName_rand2;
12+
extends: __tmp_importName2_rand3;
13+
}
14+
:local(.exportName2) {
15+
extends: __tmp_secondImport_rand1;
16+
extends: __tmp_secondImport_rand1;
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
:local(.exportName) {
2+
extends: importName secondImport from 'path/library.css';
3+
extends: importName from 'path/library2.css';
4+
extends: importName2 from 'path/library.css';
5+
}
6+
:local(.exportName2) {
7+
extends: secondImport from 'path/library.css';
8+
extends: secondImport from 'path/library.css';
9+
}

test/test-cases/import-single-quotes/expected.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
:import("path/library.css") {
32
importName: __tmp_importName_rand0;
43
}

0 commit comments

Comments
 (0)