11const postcss = require ( 'postcss' )
2- const { default : replaceSymbols , replaceAll} = require ( 'icss-replace-symbols' )
2+ const { default : replaceSymbols , replaceAll } = require ( 'icss-replace-symbols' )
33
44const matchImports = / ^ ( .+ ?| \( [ \s \S ] + ?\) ) \s + f r o m \s + ( " [ ^ " ] * " | ' [ ^ ' ] * ' | [ \w - ] + ) $ /
55const matchValueDefinition = / (?: \s + | ^ ) ( [ \w - ] + ) : ? \s + ( .+ ?) \s * $ / g
66const matchImport = / ^ ( [ \w - ] + ) (?: \s + a s \s + ( [ \w - ] + ) ) ? /
77let options = { }
88let importIndex = 0
9- let createImportedName = options && options . createImportedName || ( ( importName /*, path*/ ) => `i__const_${ importName . replace ( / \W / g, '_' ) } _${ importIndex ++ } ` )
9+ let createImportedName =
10+ ( options && options . createImportedName ) ||
11+ ( ( importName /*, path*/ ) =>
12+ `i__const_${ importName . replace ( / \W / g, '_' ) } _${ importIndex ++ } ` )
1013
11- module . exports = postcss . plugin ( 'postcss-modules-values' , ( ) => ( css , result ) => {
14+ module . exports = postcss . plugin ( 'postcss-modules-values' , ( ) => (
15+ css ,
16+ result
17+ ) => {
1218 let importAliases = [ ]
1319 let definitions = { }
1420
1521 const addDefinition = atRule => {
1622 let matches
17- while ( matches = matchValueDefinition . exec ( atRule . params ) ) {
18- let [ /*match*/ , key , value ] = matches
23+ while ( ( matches = matchValueDefinition . exec ( atRule . params ) ) ) {
24+ let [ , key , value ] = matches
1925 // Add to the definitions, knowing that values can refer to each other
2026 definitions [ key ] = replaceAll ( definitions , value )
2127 atRule . remove ( )
@@ -25,20 +31,23 @@ module.exports = postcss.plugin('postcss-modules-values', () => (css, result) =>
2531 const addImport = atRule => {
2632 let matches = matchImports . exec ( atRule . params )
2733 if ( matches ) {
28- let [ /*match*/ , aliases , path ] = matches
34+ let [ , aliases , path ] = matches
2935 // We can use constants for path names
3036 if ( definitions [ path ] ) path = definitions [ path ]
31- let imports = aliases . replace ( / ^ \( \s * ( [ \s \S ] + ) \s * \) $ / , '$1' ) . split ( / \s * , \s * / ) . map ( alias => {
32- let tokens = matchImport . exec ( alias )
33- if ( tokens ) {
34- let [ /*match*/ , theirName , myName = theirName ] = tokens
35- let importedName = createImportedName ( myName )
36- definitions [ myName ] = importedName
37- return { theirName, importedName }
38- } else {
39- throw new Error ( `@import statement "${ alias } " is invalid!` )
40- }
41- } )
37+ let imports = aliases
38+ . replace ( / ^ \( \s * ( [ \s \S ] + ) \s * \) $ / , '$1' )
39+ . split ( / \s * , \s * / )
40+ . map ( alias => {
41+ let tokens = matchImport . exec ( alias )
42+ if ( tokens ) {
43+ let [ , /*match*/ theirName , myName = theirName ] = tokens
44+ let importedName = createImportedName ( myName )
45+ definitions [ myName ] = importedName
46+ return { theirName, importedName }
47+ } else {
48+ throw new Error ( `@import statement "${ alias } " is invalid!` )
49+ }
50+ } )
4251 importAliases . push ( { path, imports } )
4352 atRule . remove ( )
4453 }
@@ -59,11 +68,13 @@ module.exports = postcss.plugin('postcss-modules-values', () => (css, result) =>
5968
6069 /* We want to export anything defined by now, but don't add it to the CSS yet or
6170 it well get picked up by the replacement stuff */
62- let exportDeclarations = Object . keys ( definitions ) . map ( key => postcss . decl ( {
63- value : definitions [ key ] ,
64- prop : key ,
65- raws : { before : "\n " }
66- } ) )
71+ let exportDeclarations = Object . keys ( definitions ) . map ( key =>
72+ postcss . decl ( {
73+ value : definitions [ key ] ,
74+ prop : key ,
75+ raws : { before : '\n ' }
76+ } )
77+ )
6778
6879 /* If we have no definitions, don't continue */
6980 if ( ! Object . keys ( definitions ) . length ) return
@@ -75,7 +86,7 @@ module.exports = postcss.plugin('postcss-modules-values', () => (css, result) =>
7586 if ( exportDeclarations . length > 0 ) {
7687 let exportRule = postcss . rule ( {
7788 selector : `:export` ,
78- raws : { after : "\n" }
89+ raws : { after : '\n' }
7990 } )
8091 exportRule . append ( exportDeclarations )
8192 css . prepend ( exportRule )
@@ -85,13 +96,13 @@ module.exports = postcss.plugin('postcss-modules-values', () => (css, result) =>
8596 importAliases . reverse ( ) . forEach ( ( { path, imports } ) => {
8697 let importRule = postcss . rule ( {
8798 selector : `:import(${ path } )` ,
88- raws : { after : "\n" }
99+ raws : { after : '\n' }
89100 } )
90101 imports . forEach ( ( { theirName, importedName } ) => {
91102 importRule . append ( {
92103 value : theirName ,
93104 prop : importedName ,
94- raws : { before : " \n " }
105+ raws : { before : ' \n ' }
95106 } )
96107 } )
97108
0 commit comments