Skip to content

Commit 9dc91c9

Browse files
committed
added a test case to cover dashes and other chars in imported classes
1 parent dd2ae0e commit 9dc91c9

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

src/index.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
import postcss from 'postcss';
22

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

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*/) => `i__imported_${importName}_${importIndex++}`);
7+
const processor = postcss.plugin( 'modules-extract-imports', function ( options ) {
8+
return ( css ) => {
9+
let imports = {},
10+
importIndex = 0,
11+
createImportedName = options && options.createImportedName || (( importName/*, path*/ ) => `i__imported_${importName.replace( /\W/g, '_' )}_${importIndex++}`);
1212

1313
// Find any declaration that supports imports
14-
css.eachDecl(declFilter, (decl) => {
15-
let matches = decl.value.match(matchImports);
16-
if (matches) {
14+
css.eachDecl( declFilter, ( decl ) => {
15+
let matches = decl.value.match( matchImports );
16+
if ( matches ) {
1717
let [/*match*/, symbols, doubleQuotePath, singleQuotePath] = matches;
1818
let path = doubleQuotePath || singleQuotePath;
1919
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);
20+
let tmpSymbols = symbols.split( /\s+/ )
21+
.map( s => {
22+
if ( !imports[path][s] ) {
23+
imports[path][s] = createImportedName( s, path );
2424
}
2525
return imports[path][s];
26-
});
27-
decl.value = tmpSymbols.join(' ');
26+
} );
27+
decl.value = tmpSymbols.join( ' ' );
2828
}
29-
});
29+
} );
3030

3131
// If we've found any imports, insert :import rules
32-
Object.keys(imports).reverse().forEach(path => {
32+
Object.keys( imports ).reverse().forEach( path => {
3333
let pathImports = imports[path];
34-
css.prepend(postcss.rule({
34+
css.prepend( postcss.rule( {
3535
selector: `:import("${path}")`,
3636
after: "\n",
37-
nodes: Object.keys(pathImports).map(importedSymbol => postcss.decl({
37+
nodes: Object.keys( pathImports ).map( importedSymbol => postcss.decl( {
3838
value: importedSymbol,
3939
prop: pathImports[importedSymbol],
4040
before: "\n "
41-
}))
42-
}));
43-
});
41+
} ) )
42+
} ) );
43+
} );
4444
};
45-
});
45+
} );
4646

4747
export default processor;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
:import("path/library.css") {
2+
i__imported_a_0: a;
3+
i__imported__b_1: -b;
4+
i__imported___c_2: --c;
5+
i__imported__d_3: _d;
6+
}
7+
:import("path/library2.css") {
8+
i__imported_a__4: a_;
9+
i__imported_b__5: b-;
10+
i__imported_c___6: c--;
11+
i__imported_d___7: d\%;
12+
}
13+
:local(.exportName) {
14+
composes: i__imported_a_0 i__imported__b_1 i__imported___c_2 i__imported__d_3;
15+
composes: i__imported_a__4 i__imported_b__5 i__imported_c___6 i__imported_d___7;
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:local(.exportName) {
2+
composes: a -b --c _d from "path/library.css";
3+
composes: a_ b- c-- d\% from "path/library2.css";
4+
}

0 commit comments

Comments
 (0)