@@ -19,66 +19,78 @@ module.exports = () => {
19
19
prepare ( result ) {
20
20
const importAliases = [ ] ;
21
21
const definitions = { } ;
22
- const addDefinition = ( atRule ) => {
23
- let matches ;
24
-
25
- while ( ( matches = matchValueDefinition . exec ( atRule . params ) ) ) {
26
- let [ , /*match*/ key , value ] = matches ;
27
-
28
- // Add to the definitions, knowing that values can refer to each other
29
- definitions [ key ] = ICSSUtils . replaceValueSymbols ( value , definitions ) ;
30
- atRule . remove ( ) ;
31
- }
32
- } ;
33
- const addImport = ( atRule ) => {
34
- const matches = matchImports . exec ( atRule . params ) ;
35
-
36
- if ( matches ) {
37
- let [ , /*match*/ aliases , path ] = matches ;
38
-
39
- // We can use constants for path names
40
- if ( definitions [ path ] ) {
41
- path = definitions [ path ] ;
42
- }
43
-
44
- const imports = aliases
45
- . replace ( / ^ \( \s * ( [ \s \S ] + ) \s * \) $ / , "$1" )
46
- . split ( / \s * , \s * / )
47
- . map ( ( alias ) => {
48
- const tokens = matchImport . exec ( alias ) ;
49
-
50
- if ( tokens ) {
51
- const [ , /*match*/ theirName , myName = theirName ] = tokens ;
52
- const importedName = createImportedName ( myName ) ;
53
- definitions [ myName ] = importedName ;
54
- return { theirName, importedName } ;
55
- } else {
56
- throw new Error ( `@import statement "${ alias } " is invalid!` ) ;
57
- }
58
- } ) ;
59
-
60
- importAliases . push ( { path, imports } ) ;
61
-
62
- atRule . remove ( ) ;
63
- }
64
- } ;
65
22
66
23
return {
67
24
/* Look at all the @value statements and treat them as locals or as imports */
68
25
AtRule : {
69
26
value ( atRule ) {
70
27
if ( matchImports . exec ( atRule . params ) ) {
71
- addImport ( atRule ) ;
28
+ const matches = matchImports . exec ( atRule . params ) ;
29
+
30
+ if ( matches ) {
31
+ let [ , /*match*/ aliases , path ] = matches ;
32
+
33
+ // We can use constants for path names
34
+ if ( definitions [ path ] ) {
35
+ path = definitions [ path ] ;
36
+ }
37
+
38
+ const imports = aliases
39
+ . replace ( / ^ \( \s * ( [ \s \S ] + ) \s * \) $ / , "$1" )
40
+ . split ( / \s * , \s * / )
41
+ . map ( ( alias ) => {
42
+ const tokens = matchImport . exec ( alias ) ;
43
+
44
+ if ( tokens ) {
45
+ const [
46
+ ,
47
+ /*match*/ theirName ,
48
+ myName = theirName ,
49
+ ] = tokens ;
50
+ const importedName = createImportedName ( myName ) ;
51
+ definitions [ myName ] = importedName ;
52
+ return { theirName, importedName } ;
53
+ } else {
54
+ throw new Error (
55
+ `@import statement "${ alias } " is invalid!`
56
+ ) ;
57
+ }
58
+ } ) ;
59
+
60
+ importAliases . push ( { path, imports } ) ;
61
+
62
+ atRule . remove ( ) ;
63
+ }
72
64
} else {
73
65
if ( atRule . params . indexOf ( "@value" ) !== - 1 ) {
74
66
result . warn ( "Invalid value definition: " + atRule . params ) ;
75
67
}
76
68
77
- addDefinition ( atRule ) ;
69
+ let matches ;
70
+
71
+ while ( ( matches = matchValueDefinition . exec ( atRule . params ) ) ) {
72
+ let [ , /*match*/ key , value ] = matches ;
73
+
74
+ // Add to the definitions, knowing that values can refer to each other
75
+ definitions [ key ] = ICSSUtils . replaceValueSymbols (
76
+ value ,
77
+ definitions
78
+ ) ;
79
+
80
+ atRule . remove ( ) ;
81
+ }
78
82
}
79
83
} ,
80
84
} ,
81
- RootExit ( root , postcss ) {
85
+ OnceExit ( root , postcss ) {
86
+ /* If we have no definitions, don't continue */
87
+ if ( ! Object . keys ( definitions ) . length ) {
88
+ return ;
89
+ }
90
+
91
+ /* Perform replacements */
92
+ ICSSUtils . replaceSymbols ( root , definitions ) ;
93
+
82
94
/* We want to export anything defined by now, but don't add it to the CSS yet or it well get picked up by the replacement stuff */
83
95
const exportDeclarations = Object . keys ( definitions ) . map ( ( key ) =>
84
96
postcss . decl ( {
@@ -88,14 +100,6 @@ module.exports = () => {
88
100
} )
89
101
) ;
90
102
91
- /* If we have no definitions, don't continue */
92
- if ( ! Object . keys ( definitions ) . length ) {
93
- return ;
94
- }
95
-
96
- /* Perform replacements */
97
- ICSSUtils . replaceSymbols ( root , definitions ) ;
98
-
99
103
/* Add export rules if any */
100
104
if ( exportDeclarations . length > 0 ) {
101
105
const exportRule = postcss . rule ( {
0 commit comments