@@ -5,6 +5,42 @@ const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/
55const matchValueDefinition = / (?: \s + | ^ ) ( [ \w - ] + ) : ? \s + ( .+ ?) \s * $ / g
66const matchImport = / ^ ( [ \w - ] + ) (?: \s + a s \s + ( [ \w - ] + ) ) ? /
77
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+
844let importIndex = 0
945const createImportedName = importName =>
1046 `i__const_${ importName . replace ( / \W / g, '_' ) } _${ importIndex ++ } `
@@ -29,10 +65,10 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
2965 const addImport = atRule => {
3066 let matches = matchImports . exec ( atRule . params )
3167 if ( matches ) {
32- let [ , aliases , path ] = matches
68+ let [ , aliasesString , path ] = matches
3369 // We can use constants for path names
3470 if ( definitions [ path ] ) path = definitions [ path ]
35- let imports = aliases
71+ let aliases = aliasesString
3672 . replace ( / ^ \( \s * ( [ \s \S ] + ) \s * \) $ / , '$1' )
3773 . split ( / \s * , \s * / )
3874 . map ( alias => {
@@ -46,7 +82,11 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
4682 throw new Error ( `@import statement "${ alias } " is invalid!` )
4783 }
4884 } )
49- importAliases . push ( { path, imports } )
85+ . reduce ( ( acc , { theirName, importedName } ) => {
86+ acc [ importedName ] = theirName
87+ return acc
88+ } , { } )
89+ importAliases . push ( { path, aliases } )
5090 atRule . remove ( )
5191 }
5292 }
@@ -64,46 +104,13 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
64104 }
65105 } )
66106
67- /* We want to export anything defined by now, but don't add it to the CSS yet or
68- it well get picked up by the replacement stuff */
69- let exportDeclarations = Object . keys ( definitions ) . map ( key =>
70- postcss . decl ( {
71- value : definitions [ key ] ,
72- prop : key ,
73- raws : { before : '\n ' }
74- } )
75- )
76-
77107 /* If we have no definitions, don't continue */
78- if ( ! Object . keys ( definitions ) . length ) return
108+ if ( Object . keys ( definitions ) . length === 0 ) return
79109
80110 /* Perform replacements */
81111 replaceSymbols ( css , definitions )
82112
83- /* Add export rules if any */
84- if ( exportDeclarations . length > 0 ) {
85- let exportRule = postcss . rule ( {
86- selector : `:export` ,
87- raws : { after : '\n' }
88- } )
89- exportRule . append ( exportDeclarations )
90- css . prepend ( exportRule )
91- }
113+ addExportsRule ( css , definitions )
92114
93- /* Add import rules */
94- importAliases . reverse ( ) . forEach ( ( { path, imports } ) => {
95- let importRule = postcss . rule ( {
96- selector : `:import(${ path } )` ,
97- raws : { after : '\n' }
98- } )
99- imports . forEach ( ( { theirName, importedName } ) => {
100- importRule . append ( {
101- value : theirName ,
102- prop : importedName ,
103- raws : { before : '\n ' }
104- } )
105- } )
106-
107- css . prepend ( importRule )
108- } )
115+ addImportsRules ( css , importAliases )
109116} )
0 commit comments