|
1 | 1 | import postcss from 'postcss'; |
2 | 2 |
|
3 | 3 | 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+(?:"([^"]+)"|'([^']+)')$/; |
6 | 6 |
|
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++}`); |
12 | 12 |
|
13 | 13 | // 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 ) { |
17 | 17 | let [/*match*/, symbols, doubleQuotePath, singleQuotePath] = matches; |
18 | 18 | let path = doubleQuotePath || singleQuotePath; |
19 | 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); |
| 20 | + let tmpSymbols = symbols.split( /\s+/ ) |
| 21 | + .map( s => { |
| 22 | + if ( !imports[path][s] ) { |
| 23 | + imports[path][s] = createImportedName( s, path ); |
24 | 24 | } |
25 | 25 | return imports[path][s]; |
26 | | - }); |
27 | | - decl.value = tmpSymbols.join(' '); |
| 26 | + } ); |
| 27 | + decl.value = tmpSymbols.join( ' ' ); |
28 | 28 | } |
29 | | - }); |
| 29 | + } ); |
30 | 30 |
|
31 | 31 | // If we've found any imports, insert :import rules |
32 | | - Object.keys(imports).reverse().forEach(path => { |
| 32 | + Object.keys( imports ).reverse().forEach( path => { |
33 | 33 | let pathImports = imports[path]; |
34 | | - css.prepend(postcss.rule({ |
| 34 | + css.prepend( postcss.rule( { |
35 | 35 | selector: `:import("${path}")`, |
36 | 36 | after: "\n", |
37 | | - nodes: Object.keys(pathImports).map(importedSymbol => postcss.decl({ |
| 37 | + nodes: Object.keys( pathImports ).map( importedSymbol => postcss.decl( { |
38 | 38 | value: importedSymbol, |
39 | 39 | prop: pathImports[importedSymbol], |
40 | 40 | before: "\n " |
41 | | - })) |
42 | | - })); |
43 | | - }); |
| 41 | + } ) ) |
| 42 | + } ) ); |
| 43 | + } ); |
44 | 44 | }; |
45 | | -}); |
| 45 | +} ); |
46 | 46 |
|
47 | 47 | export default processor; |
0 commit comments