1
- const postcss = require ( 'postcss' )
2
- const { default : replaceSymbols , replaceAll } = require ( 'icss-replace-symbols' )
3
- const { extractICSS, createICSSRules } = require ( './icss.js' )
1
+ import postcss from 'postcss'
2
+ import {
3
+ replaceSymbols ,
4
+ replaceValueSymbols ,
5
+ extractICSS ,
6
+ createICSSRules
7
+ } from 'icss-utils'
4
8
5
9
const matchImports = / ^ ( .+ ?| \( [ \s \S ] + ?\) ) \s + f r o m \s + ( " [ ^ " ] * " | ' [ ^ ' ] * ' | [ \w - ] + ) $ /
6
10
const matchValueDefinition = / (?: \s + | ^ ) ( [ \w - ] + ) : ? \s + ( .+ ?) \s * $ / g
@@ -14,11 +18,11 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
14
18
css ,
15
19
result
16
20
) => {
17
- const { imports , exports } = extractICSS ( css )
21
+ const { icssImports , icssExports } = extractICSS ( css )
18
22
let importIndex = 0
19
23
const createImportedName = ( path , name ) => {
20
24
const importedName = getAliasName ( name , importIndex )
21
- if ( imports [ path ] && imports [ path ] [ importedName ] ) {
25
+ if ( icssImports [ path ] && icssImports [ path ] [ importedName ] ) {
22
26
importIndex += 1
23
27
return createImportedName ( path , name )
24
28
}
@@ -31,7 +35,7 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
31
35
while ( ( matches = matchValueDefinition . exec ( atRule . params ) ) ) {
32
36
let [ , key , value ] = matches
33
37
// Add to the definitions, knowing that values can refer to each other
34
- exports [ key ] = replaceAll ( exports , value )
38
+ icssExports [ key ] = replaceValueSymbols ( value , icssExports )
35
39
atRule . remove ( )
36
40
}
37
41
}
@@ -41,7 +45,7 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
41
45
if ( matches ) {
42
46
let [ , aliasesString , path ] = matches
43
47
// We can use constants for path names
44
- if ( exports [ path ] ) path = exports [ path ]
48
+ if ( icssExports [ path ] ) path = icssExports [ path ]
45
49
let aliases = aliasesString
46
50
. replace ( / ^ \( \s * ( [ \s \S ] + ) \s * \) $ / , '$1' )
47
51
. split ( / \s * , \s * / )
@@ -50,7 +54,7 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
50
54
if ( tokens ) {
51
55
let [ , theirName , myName = theirName ] = tokens
52
56
let importedName = createImportedName ( path , myName )
53
- exports [ myName ] = importedName
57
+ icssExports [ myName ] = importedName
54
58
return { theirName, importedName }
55
59
} else {
56
60
throw new Error ( `@import statement "${ alias } " is invalid!` )
@@ -60,7 +64,7 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
60
64
acc [ importedName ] = theirName
61
65
return acc
62
66
} , { } )
63
- imports [ path ] = Object . assign ( { } , imports [ path ] , aliases )
67
+ icssImports [ path ] = Object . assign ( { } , icssImports [ path ] , aliases )
64
68
atRule . remove ( )
65
69
}
66
70
}
@@ -79,10 +83,10 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
79
83
} )
80
84
81
85
/* If we have no definitions, don't continue */
82
- if ( Object . keys ( exports ) . length === 0 ) return
86
+ if ( Object . keys ( icssExports ) . length === 0 ) return
83
87
84
88
/* Perform replacements */
85
- replaceSymbols ( css , exports )
89
+ replaceSymbols ( css , icssExports )
86
90
87
- css . prepend ( createICSSRules ( imports , exports ) )
91
+ css . prepend ( createICSSRules ( icssImports , icssExports ) )
88
92
} )
0 commit comments