Skip to content

Commit 956bad7

Browse files
evilebottnawimichael-ciniawsky
authored andcommittedApr 20, 2017
fix: imported variables are replaced in exports if followed by a comma (#504)
1 parent f5f88bb commit 956bad7

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed
 

‎lib/processCss.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var localByDefault = require("postcss-modules-local-by-default");
1313
var extractImports = require("postcss-modules-extract-imports");
1414
var modulesScope = require("postcss-modules-scope");
1515
var modulesValues = require("postcss-modules-values");
16+
var valueParser = require('postcss-value-parser');
1617

1718
var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
1819
return function(css) {
@@ -23,15 +24,18 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
2324

2425
function replaceImportsInString(str) {
2526
if(options.import) {
26-
var tokens = str.split(/(\S+)/);
27-
tokens = tokens.map(function (token) {
27+
var tokens = valueParser(str);
28+
tokens.walk(function (node) {
29+
if (node.type !== 'word') {
30+
return;
31+
}
32+
var token = node.value;
2833
var importIndex = imports["$" + token];
2934
if(typeof importIndex === "number") {
30-
return "___CSS_LOADER_IMPORT___" + importIndex + "___";
35+
node.value = "___CSS_LOADER_IMPORT___" + importIndex + "___";
3136
}
32-
return token;
33-
});
34-
return tokens.join("");
37+
})
38+
return tokens.toString();
3539
}
3640
return str;
3741
}

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"postcss-modules-local-by-default": "^1.0.1",
2424
"postcss-modules-scope": "^1.0.0",
2525
"postcss-modules-values": "^1.1.0",
26+
"postcss-value-parser": "^3.3.0",
2627
"source-list-map": "^0.1.7"
2728
},
2829
"devDependencies": {

‎test/valuesTest.js

+51
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,55 @@ describe("values", function() {
6666
})()
6767
}
6868
);
69+
testLocal("should import values contain comma",
70+
"@value color from './file1'; @value shadow: 0 0 color,0 0 color; .ghi { box-shadow: shadow; }", [
71+
[ 2, "", "" ],
72+
[ 1, ".ghi { box-shadow: 0 0 red,0 0 red; }", "" ]
73+
], {
74+
color: "red",
75+
shadow: "0 0 red,0 0 red"
76+
}, "", {
77+
"./file1": (function() {
78+
var a = [[2, "", ""]];
79+
a.locals = {
80+
color: "red",
81+
};
82+
return a;
83+
})()
84+
}
85+
);
86+
testLocal("should import values contain comma and space before comma",
87+
"@value color from './file1'; @value shadow: 0 0 color ,0 0 color; .ghi { box-shadow: shadow; }", [
88+
[ 2, "", "" ],
89+
[ 1, ".ghi { box-shadow: 0 0 red ,0 0 red; }", "" ]
90+
], {
91+
color: "red",
92+
shadow: "0 0 red ,0 0 red"
93+
}, "", {
94+
"./file1": (function() {
95+
var a = [[2, "", ""]];
96+
a.locals = {
97+
color: "red",
98+
};
99+
return a;
100+
})()
101+
}
102+
);
103+
testLocal("should import values contain tralling comma and space after comma",
104+
"@value color from './file1'; @value shadow: 0 0 color, 0 0 color; .ghi { box-shadow: shadow; }", [
105+
[ 2, "", "" ],
106+
[ 1, ".ghi { box-shadow: 0 0 red, 0 0 red; }", "" ]
107+
], {
108+
color: "red",
109+
shadow: "0 0 red, 0 0 red"
110+
}, "", {
111+
"./file1": (function() {
112+
var a = [[2, "", ""]];
113+
a.locals = {
114+
color: "red",
115+
};
116+
return a;
117+
})()
118+
}
119+
);
69120
});

0 commit comments

Comments
 (0)