Skip to content

Commit

Permalink
fix: imported variables are replaced in exports if followed by a comma (
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored and michael-ciniawsky committed Apr 20, 2017
1 parent f5f88bb commit 956bad7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/processCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var localByDefault = require("postcss-modules-local-by-default");
var extractImports = require("postcss-modules-extract-imports");
var modulesScope = require("postcss-modules-scope");
var modulesValues = require("postcss-modules-values");
var valueParser = require('postcss-value-parser');

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

function replaceImportsInString(str) {
if(options.import) {
var tokens = str.split(/(\S+)/);
tokens = tokens.map(function (token) {
var tokens = valueParser(str);
tokens.walk(function (node) {
if (node.type !== 'word') {
return;
}
var token = node.value;
var importIndex = imports["$" + token];
if(typeof importIndex === "number") {
return "___CSS_LOADER_IMPORT___" + importIndex + "___";
node.value = "___CSS_LOADER_IMPORT___" + importIndex + "___";
}
return token;
});
return tokens.join("");
})
return tokens.toString();
}
return str;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"postcss-modules-local-by-default": "^1.0.1",
"postcss-modules-scope": "^1.0.0",
"postcss-modules-values": "^1.1.0",
"postcss-value-parser": "^3.3.0",
"source-list-map": "^0.1.7"
},
"devDependencies": {
Expand Down
51 changes: 51 additions & 0 deletions test/valuesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,55 @@ describe("values", function() {
})()
}
);
testLocal("should import values contain comma",
"@value color from './file1'; @value shadow: 0 0 color,0 0 color; .ghi { box-shadow: shadow; }", [
[ 2, "", "" ],
[ 1, ".ghi { box-shadow: 0 0 red,0 0 red; }", "" ]
], {
color: "red",
shadow: "0 0 red,0 0 red"
}, "", {
"./file1": (function() {
var a = [[2, "", ""]];
a.locals = {
color: "red",
};
return a;
})()
}
);
testLocal("should import values contain comma and space before comma",
"@value color from './file1'; @value shadow: 0 0 color ,0 0 color; .ghi { box-shadow: shadow; }", [
[ 2, "", "" ],
[ 1, ".ghi { box-shadow: 0 0 red ,0 0 red; }", "" ]
], {
color: "red",
shadow: "0 0 red ,0 0 red"
}, "", {
"./file1": (function() {
var a = [[2, "", ""]];
a.locals = {
color: "red",
};
return a;
})()
}
);
testLocal("should import values contain tralling comma and space after comma",
"@value color from './file1'; @value shadow: 0 0 color, 0 0 color; .ghi { box-shadow: shadow; }", [
[ 2, "", "" ],
[ 1, ".ghi { box-shadow: 0 0 red, 0 0 red; }", "" ]
], {
color: "red",
shadow: "0 0 red, 0 0 red"
}, "", {
"./file1": (function() {
var a = [[2, "", ""]];
a.locals = {
color: "red",
};
return a;
})()
}
);
});

0 comments on commit 956bad7

Please sign in to comment.