Skip to content

Commit 67d8ac8

Browse files
committed
createImportedName option
deterministic result (for caching) allow to pass options added createImportedName option which allows a custom naming function
1 parent dad07fd commit 67d8ac8

File tree

10 files changed

+89
-71
lines changed

10 files changed

+89
-71
lines changed

src/index.js

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,44 @@ const declWhitelist = ['extends'],
44
declFilter = new RegExp(`^(${declWhitelist.join('|')})$`),
55
matchImports = /^([\w\s]+?)\s+from\s+(?:"([^"]+)"|'([^']+)')$/;
66

7-
const processor = (css) => {
8-
let imports = {};
7+
const processor = postcss.plugin('modules-extract-imports', function(options) {
8+
return (css) => {
9+
let imports = {};
10+
let importIndex = 0;
11+
let createImportedName = options && options.createImportedName || ((importName/*, path*/) => `__imported_${importName}_${importIndex++}`);
912

10-
// Find any declaration that supports imports
11-
css.eachDecl(declFilter, (decl) => {
12-
let matches = decl.value.match(matchImports);
13-
if (matches) {
14-
let [/*match*/, symbols, doubleQuotePath, singleQuotePath] = matches;
15-
let path = doubleQuotePath || singleQuotePath;
16-
imports[path] = imports[path] || {};
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-
});
24-
decl.value = tmpSymbols.join(' ');
25-
}
26-
});
13+
// Find any declaration that supports imports
14+
css.eachDecl(declFilter, (decl) => {
15+
let matches = decl.value.match(matchImports);
16+
if (matches) {
17+
let [/*match*/, symbols, doubleQuotePath, singleQuotePath] = matches;
18+
let path = doubleQuotePath || singleQuotePath;
19+
imports[path] = imports[path] || {};
20+
let tmpSymbols = symbols.split(/\s+/)
21+
.map(s => {
22+
if(!imports[path][s]) {
23+
imports[path][s] = createImportedName(s, path);
24+
}
25+
return imports[path][s];
26+
});
27+
decl.value = tmpSymbols.join(' ');
28+
}
29+
});
2730

28-
// If we've found any imports, insert :import rules
29-
Object.keys(imports).reverse().forEach(path => {
30-
let pathImports = imports[path];
31-
css.prepend(postcss.rule({
32-
selector: `:import("${path}")`,
33-
after: "\n",
34-
nodes: Object.keys(pathImports).map(importedSymbol => postcss.decl({
35-
prop: importedSymbol,
36-
value: pathImports[importedSymbol],
37-
before: "\n "
38-
}))
39-
}));
40-
});
41-
};
42-
43-
processor.defaultRandomStr = () => Math.random().toString(36).substr(2, 8);
44-
processor.getRandomStr = processor.defaultRandomStr; // Easy to be mocked out
31+
// If we've found any imports, insert :import rules
32+
Object.keys(imports).reverse().forEach(path => {
33+
let pathImports = imports[path];
34+
css.prepend(postcss.rule({
35+
selector: `:import("${path}")`,
36+
after: "\n",
37+
nodes: Object.keys(pathImports).map(importedSymbol => postcss.decl({
38+
prop: importedSymbol,
39+
value: pathImports[importedSymbol],
40+
before: "\n "
41+
}))
42+
}));
43+
});
44+
};
45+
});
4546

4647
export default processor;

test/custom-import-name.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
3+
/*globals describe it */
4+
5+
var assert = require("assert");
6+
var postcss = require("postcss");
7+
var processor = require("../");
8+
9+
10+
describe("custom-import-name", function() {
11+
it("should allow to provide a custom imported name", function() {
12+
var input = ":local(.name) { extends: abc from \"def\"; }";
13+
var expected = ":import(\"def\") {\n abc: abc-from-def;\n}\n:local(.name) { extends: abc-from-def; }";
14+
var pipeline = postcss([processor({
15+
createImportedName: function(importName, path) { return importName + "-from-" + path; }
16+
})]);
17+
assert.equal(pipeline.process(input).css, expected);
18+
});
19+
});

test/test-cases.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ describe("test-cases", function() {
1919
fs.readdirSync(testDir).forEach(function(testCase) {
2020
if(fs.existsSync(path.join(testDir, testCase, "source.css"))) {
2121
it("should " + testCase.replace(/-/g, " "), function() {
22-
var i = 0;
23-
processor.getRandomStr = function() { return "rand" + (i++); };
2422
var input = normalize(fs.readFileSync(path.join(testDir, testCase, "source.css"), "utf-8"));
2523
var expected = normalize(fs.readFileSync(path.join(testDir, testCase, "expected.css"), "utf-8"));
2624
assert.equal(pipeline.process(input).css, expected);
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
:import("path/library.css") {
2-
importName: __tmp_importName_rand0;
3-
secondImport: __tmp_secondImport_rand1;
4-
thirdImport: __tmp_thirdImport_rand2;
2+
importName: __imported_importName_0;
3+
secondImport: __imported_secondImport_1;
4+
thirdImport: __imported_thirdImport_2;
55
}
66
:import("path/other-lib.css") {
7-
otherLibImport: __tmp_otherLibImport_rand3;
7+
otherLibImport: __imported_otherLibImport_3;
88
}
99
:local(.exportName) {
10-
extends: __tmp_importName_rand0 __tmp_secondImport_rand1;
10+
extends: __imported_importName_0 __imported_secondImport_1;
1111
other: rule;
1212
}
1313
:local(.otherExport) {
14-
extends: __tmp_thirdImport_rand2;
15-
extends: __tmp_otherLibImport_rand3;
14+
extends: __imported_thirdImport_2;
15+
extends: __imported_otherLibImport_3;
1616
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
:import("path/library.css") {
2-
importName: __tmp_importName_rand0;
3-
importName2: __tmp_importName2_rand1;
2+
importName: __imported_importName_0;
3+
importName2: __imported_importName2_1;
44
}
55

66
@media screen {
77
:local(.exportName) {
8-
extends: __tmp_importName_rand0;
9-
extends: __tmp_importName2_rand1;
8+
extends: __imported_importName_0;
9+
extends: __imported_importName2_1;
1010
other: rule2;
1111
}
1212
}
1313

1414
:local(.exportName) {
15-
extends: __tmp_importName_rand0;
15+
extends: __imported_importName_0;
1616
other: rule;
1717
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
:import("path/library.css") {
2-
importName: __tmp_importName_rand0;
3-
secondImport: __tmp_secondImport_rand1;
2+
importName: __imported_importName_0;
3+
secondImport: __imported_secondImport_1;
44
}
5-
:local(.exportName) { extends: __tmp_importName_rand0 __tmp_secondImport_rand1; other: rule; }
5+
:local(.exportName) { extends: __imported_importName_0 __imported_secondImport_1; other: rule; }
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
:import("path/library.css") {
2-
importName: __tmp_importName_rand0;
3-
secondImport: __tmp_secondImport_rand1;
4-
importName2: __tmp_importName2_rand3;
2+
importName: __imported_importName_0;
3+
secondImport: __imported_secondImport_1;
4+
importName2: __imported_importName2_3;
55
}
66
:import("path/library2.css") {
7-
importName: __tmp_importName_rand2;
7+
importName: __imported_importName_2;
88
}
99
:local(.exportName) {
10-
extends: __tmp_importName_rand0 __tmp_secondImport_rand1;
11-
extends: __tmp_importName_rand2;
12-
extends: __tmp_importName2_rand3;
10+
extends: __imported_importName_0 __imported_secondImport_1;
11+
extends: __imported_importName_2;
12+
extends: __imported_importName2_3;
1313
}
1414
:local(.exportName2) {
15-
extends: __tmp_secondImport_rand1;
16-
extends: __tmp_secondImport_rand1;
15+
extends: __imported_secondImport_1;
16+
extends: __imported_secondImport_1;
1717
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
:import("path/library.css") {
2-
importName: __tmp_importName_rand0;
2+
importName: __imported_importName_0;
33
}
44
:local(.exportName) {
5-
extends: __tmp_importName_rand0;
5+
extends: __imported_importName_0;
66
other: rule;
77
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
:import("path/library.css") {
2-
importName: __tmp_importName_rand0;
3-
importName2: __tmp_importName2_rand1;
2+
importName: __imported_importName_0;
3+
importName2: __imported_importName2_1;
44
}
55
:local(.exportName) {
6-
extends: __tmp_importName_rand0;
7-
extends: __tmp_importName2_rand1;
8-
extends: __tmp_importName_rand0 __tmp_importName2_rand1;
6+
extends: __imported_importName_0;
7+
extends: __imported_importName2_1;
8+
extends: __imported_importName_0 __imported_importName2_1;
99
other: rule;
1010
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
:import("path/library.css") {
2-
importName: __tmp_importName_rand0;
2+
importName: __imported_importName_0;
33
}
44
:local(.exportName) {
5-
extends: __tmp_importName_rand0;
5+
extends: __imported_importName_0;
66
other: rule;
77
}

0 commit comments

Comments
 (0)