@@ -5,6 +5,42 @@ const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/
5
5
const matchValueDefinition = / (?: \s + | ^ ) ( [ \w - ] + ) : ? \s + ( .+ ?) \s * $ / g
6
6
const matchImport = / ^ ( [ \w - ] + ) (?: \s + a s \s + ( [ \w - ] + ) ) ? /
7
7
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
+
8
44
let importIndex = 0
9
45
const createImportedName = importName =>
10
46
`i__const_${ importName . replace ( / \W / g, '_' ) } _${ importIndex ++ } `
@@ -29,10 +65,10 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
29
65
const addImport = atRule => {
30
66
let matches = matchImports . exec ( atRule . params )
31
67
if ( matches ) {
32
- let [ , aliases , path ] = matches
68
+ let [ , aliasesString , path ] = matches
33
69
// We can use constants for path names
34
70
if ( definitions [ path ] ) path = definitions [ path ]
35
- let imports = aliases
71
+ let aliases = aliasesString
36
72
. replace ( / ^ \( \s * ( [ \s \S ] + ) \s * \) $ / , '$1' )
37
73
. split ( / \s * , \s * / )
38
74
. map ( alias => {
@@ -46,7 +82,11 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
46
82
throw new Error ( `@import statement "${ alias } " is invalid!` )
47
83
}
48
84
} )
49
- importAliases . push ( { path, imports } )
85
+ . reduce ( ( acc , { theirName, importedName } ) => {
86
+ acc [ importedName ] = theirName
87
+ return acc
88
+ } , { } )
89
+ importAliases . push ( { path, aliases } )
50
90
atRule . remove ( )
51
91
}
52
92
}
@@ -64,46 +104,13 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
64
104
}
65
105
} )
66
106
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
-
77
107
/* If we have no definitions, don't continue */
78
- if ( ! Object . keys ( definitions ) . length ) return
108
+ if ( Object . keys ( definitions ) . length === 0 ) return
79
109
80
110
/* Perform replacements */
81
111
replaceSymbols ( css , definitions )
82
112
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 )
92
114
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 )
109
116
} )
0 commit comments