Skip to content

Commit fbedce9

Browse files
committed
allowed existing icss imports to be added-to rather than duplicated
1 parent 278b5c7 commit fbedce9

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
"lint": "eslint src",
88
"build": "babel --out-dir lib src",
99
"watch": "chokidar src -c 'npm run build'",
10-
"pretest": "npm run lint && npm run build",
11-
"test": "mocha",
10+
"posttest": "npm run lint && npm run build",
11+
"test": "mocha --compilers js:babel/register",
1212
"autotest": "chokidar src test -c 'npm test'",
1313
"precover": "npm run lint && npm run build",
14-
"cover": "istanbul cover node_modules/mocha/bin/_mocha",
14+
"cover": "babel-istanbul cover node_modules/.bin/_mocha",
1515
"travis": "npm run cover -- --report lcovonly",
1616
"prepublish": "npm run build"
1717
},
@@ -39,12 +39,12 @@
3939
"devDependencies": {
4040
"babel": "^5.4.7",
4141
"babel-eslint": "^3.1.9",
42+
"babel-istanbul": "^0.2.10",
4243
"babelify": "^6.1.2",
4344
"chokidar-cli": "^0.2.1",
4445
"codecov.io": "^0.1.2",
4546
"coveralls": "^2.11.2",
4647
"eslint": "^0.22.1",
47-
"istanbul": "^0.3.14",
4848
"mocha": "^2.2.5"
4949
}
5050
}

src/index.js

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

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

78
const processor = postcss.plugin( 'modules-extract-imports', function ( options ) {
89
return ( css ) => {
@@ -28,19 +29,33 @@ const processor = postcss.plugin( 'modules-extract-imports', function ( options
2829
}
2930
} );
3031

31-
// If we've found any imports, insert :import rules
32+
// If we've found any imports, insert or append :import rules
33+
let existingImports = {};
34+
css.eachRule(rule => {
35+
let matches = icssImport.exec(rule.selector);
36+
if (matches) {
37+
let [/*match*/, doubleQuotePath, singleQuotePath] = matches;
38+
existingImports[doubleQuotePath || singleQuotePath] = rule;
39+
}
40+
});
41+
3242
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( {
43+
let rule = existingImports[path];
44+
if (!rule) {
45+
rule = postcss.rule( {
46+
selector: `:import("${path}")`,
47+
after: "\n"
48+
} );
49+
css.prepend( rule );
50+
}
51+
Object.keys( imports[path] ).forEach( importedSymbol => {
52+
rule.push(postcss.decl( {
3853
value: importedSymbol,
39-
prop: pathImports[importedSymbol],
54+
prop: imports[path][importedSymbol],
4055
before: "\n ",
4156
_autoprefixerDisabled: true
42-
} ) )
43-
} ) );
57+
} ) );
58+
} );
4459
} );
4560
};
4661
} );

test/test-cases.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var assert = require("assert");
66
var fs = require("fs");
77
var path = require("path");
88
var postcss = require("postcss");
9-
var processor = require("../");
9+
var processor = require("../src");
1010

1111
var pipeline = postcss([processor]);
1212

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:import("path/library.css") {
2+
something: else;
3+
i__imported_importName_0: importName;
4+
}
5+
6+
:local(.exportName) {
7+
composes: i__imported_importName_0;
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:import("path/library.css") {
2+
something: else;
3+
}
4+
5+
:local(.exportName) {
6+
composes: importName from 'path/library.css';
7+
}

0 commit comments

Comments
 (0)