1- const postcss = require ( 'postcss' )
2- const { default : replaceSymbols , replaceAll } = require ( 'icss-replace-symbols' )
1+ import postcss from 'postcss'
2+ import {
3+ replaceSymbols ,
4+ replaceValueSymbols ,
5+ extractICSS ,
6+ createICSSRules
7+ } from 'icss-utils'
38
49const matchImports = / ^ ( .+ ?| \( [ \s \S ] + ?\) ) \s + f r o m \s + ( " [ ^ " ] * " | ' [ ^ ' ] * ' | [ \w - ] + ) $ /
510const matchValueDefinition = / (?: \s + | ^ ) ( [ \w - ] + ) : ? \s + ( .+ ?) \s * $ / g
611const matchImport = / ^ ( [ \w - ] + ) (?: \s + a s \s + ( [ \w - ] + ) ) ? /
712
8- const addImportsRules = ( css , imports ) => {
9- const rules = imports . map ( ( { path, aliases } ) => {
10- const declarations = Object . keys ( aliases ) . map ( key =>
11- postcss . decl ( {
12- prop : key ,
13- value : aliases [ key ] ,
14- raws : { before : '\n ' }
15- } )
16- )
17- return postcss
18- . rule ( {
19- selector : `:import(${ path } )` ,
20- raws : { after : '\n' }
21- } )
22- . append ( declarations )
23- } )
24- css . prepend ( rules )
25- }
26-
27- const addExportsRule = ( css , exports ) => {
28- const declarations = Object . keys ( exports ) . map ( key =>
29- postcss . decl ( {
30- prop : key ,
31- value : exports [ key ] ,
32- raws : { before : '\n ' }
33- } )
34- )
35- const rule = postcss
36- . rule ( {
37- selector : `:export` ,
38- raws : { after : '\n' }
39- } )
40- . append ( declarations )
41- css . prepend ( rule )
42- }
43-
44- let importIndex = 0
45- const createImportedName = importName =>
46- `i__const_${ importName . replace ( / \W / g, '_' ) } _${ importIndex ++ } `
13+ // 'i' prefix to prevent postcss parsing "_" as css hook
14+ const getAliasName = ( name , index ) =>
15+ `i__value_${ name . replace ( / \W / g, '_' ) } _${ index } `
4716
4817module . exports = postcss . plugin ( 'postcss-modules-values' , ( ) => (
4918 css ,
5019 result
5120) => {
52- let importAliases = [ ]
53- let definitions = { }
21+ const { icssImports, icssExports } = extractICSS ( css )
22+ let importIndex = 0
23+ const createImportedName = ( path , name ) => {
24+ const importedName = getAliasName ( name , importIndex )
25+ if ( icssImports [ path ] && icssImports [ path ] [ importedName ] ) {
26+ importIndex += 1
27+ return createImportedName ( path , name )
28+ }
29+ importIndex += 1
30+ return importedName
31+ }
5432
5533 const addDefinition = atRule => {
5634 let matches
5735 while ( ( matches = matchValueDefinition . exec ( atRule . params ) ) ) {
5836 let [ , key , value ] = matches
5937 // Add to the definitions, knowing that values can refer to each other
60- definitions [ key ] = replaceAll ( definitions , value )
38+ icssExports [ key ] = replaceValueSymbols ( value , icssExports )
6139 atRule . remove ( )
6240 }
6341 }
@@ -67,16 +45,16 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
6745 if ( matches ) {
6846 let [ , aliasesString , path ] = matches
6947 // We can use constants for path names
70- if ( definitions [ path ] ) path = definitions [ path ]
48+ if ( icssExports [ path ] ) path = icssExports [ path ]
7149 let aliases = aliasesString
7250 . replace ( / ^ \( \s * ( [ \s \S ] + ) \s * \) $ / , '$1' )
7351 . split ( / \s * , \s * / )
7452 . map ( alias => {
7553 let tokens = matchImport . exec ( alias )
7654 if ( tokens ) {
77- let [ , /*match*/ theirName , myName = theirName ] = tokens
78- let importedName = createImportedName ( myName )
79- definitions [ myName ] = importedName
55+ let [ , theirName , myName = theirName ] = tokens
56+ let importedName = createImportedName ( path , myName )
57+ icssExports [ myName ] = importedName
8058 return { theirName, importedName }
8159 } else {
8260 throw new Error ( `@import statement "${ alias } " is invalid!` )
@@ -86,7 +64,7 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
8664 acc [ importedName ] = theirName
8765 return acc
8866 } , { } )
89- importAliases . push ( { path, aliases } )
67+ icssImports [ path ] = Object . assign ( { } , icssImports [ path ] , aliases )
9068 atRule . remove ( )
9169 }
9270 }
@@ -104,13 +82,9 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
10482 }
10583 } )
10684
107- /* If we have no definitions, don't continue */
108- if ( Object . keys ( definitions ) . length === 0 ) return
109-
110- /* Perform replacements */
111- replaceSymbols ( css , definitions )
85+ if ( Object . keys ( icssExports ) . length === 0 ) return
11286
113- addExportsRule ( css , definitions )
87+ replaceSymbols ( css , icssExports )
11488
115- addImportsRules ( css , importAliases )
89+ css . prepend ( createICSSRules ( icssImports , icssExports ) )
11690} )
0 commit comments