@@ -4,12 +4,46 @@ const { default: replaceSymbols, replaceAll } = require('icss-replace-symbols')
4
4
const matchImports = / ^ ( .+ ?| \( [ \s \S ] + ?\) ) \s + f r o m \s + ( " [ ^ " ] * " | ' [ ^ ' ] * ' | [ \w - ] + ) $ /
5
5
const matchValueDefinition = / (?: \s + | ^ ) ( [ \w - ] + ) : ? \s + ( .+ ?) \s * $ / g
6
6
const matchImport = / ^ ( [ \w - ] + ) (?: \s + a s \s + ( [ \w - ] + ) ) ? /
7
- let options = { }
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
- let createImportedName =
10
- ( options && options . createImportedName ) ||
11
- ( ( importName /*, path*/ ) =>
12
- `i__const_${ importName . replace ( / \W / g, '_' ) } _${ importIndex ++ } ` )
45
+ const createImportedName = importName =>
46
+ `i__const_${ importName . replace ( / \W / g, '_' ) } _${ importIndex ++ } `
13
47
14
48
module . exports = postcss . plugin ( 'postcss-modules-values' , ( ) => (
15
49
css ,
@@ -31,10 +65,10 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
31
65
const addImport = atRule => {
32
66
let matches = matchImports . exec ( atRule . params )
33
67
if ( matches ) {
34
- let [ , aliases , path ] = matches
68
+ let [ , aliasesString , path ] = matches
35
69
// We can use constants for path names
36
70
if ( definitions [ path ] ) path = definitions [ path ]
37
- let imports = aliases
71
+ let aliases = aliasesString
38
72
. replace ( / ^ \( \s * ( [ \s \S ] + ) \s * \) $ / , '$1' )
39
73
. split ( / \s * , \s * / )
40
74
. map ( alias => {
@@ -48,7 +82,11 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
48
82
throw new Error ( `@import statement "${ alias } " is invalid!` )
49
83
}
50
84
} )
51
- importAliases . push ( { path, imports } )
85
+ . reduce ( ( acc , { theirName, importedName } ) => {
86
+ acc [ importedName ] = theirName
87
+ return acc
88
+ } , { } )
89
+ importAliases . push ( { path, aliases } )
52
90
atRule . remove ( )
53
91
}
54
92
}
@@ -66,46 +104,13 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
66
104
}
67
105
} )
68
106
69
- /* We want to export anything defined by now, but don't add it to the CSS yet or
70
- it well get picked up by the replacement stuff */
71
- let exportDeclarations = Object . keys ( definitions ) . map ( key =>
72
- postcss . decl ( {
73
- value : definitions [ key ] ,
74
- prop : key ,
75
- raws : { before : '\n ' }
76
- } )
77
- )
78
-
79
107
/* If we have no definitions, don't continue */
80
- if ( ! Object . keys ( definitions ) . length ) return
108
+ if ( Object . keys ( definitions ) . length === 0 ) return
81
109
82
110
/* Perform replacements */
83
111
replaceSymbols ( css , definitions )
84
112
85
- /* Add export rules if any */
86
- if ( exportDeclarations . length > 0 ) {
87
- let exportRule = postcss . rule ( {
88
- selector : `:export` ,
89
- raws : { after : '\n' }
90
- } )
91
- exportRule . append ( exportDeclarations )
92
- css . prepend ( exportRule )
93
- }
94
-
95
- /* Add import rules */
96
- importAliases . reverse ( ) . forEach ( ( { path, imports } ) => {
97
- let importRule = postcss . rule ( {
98
- selector : `:import(${ path } )` ,
99
- raws : { after : '\n' }
100
- } )
101
- imports . forEach ( ( { theirName, importedName } ) => {
102
- importRule . append ( {
103
- value : theirName ,
104
- prop : importedName ,
105
- raws : { before : '\n ' }
106
- } )
107
- } )
113
+ addExportsRule ( css , definitions )
108
114
109
- css . prepend ( importRule )
110
- } )
115
+ addImportsRules ( css , importAliases )
111
116
} )
0 commit comments