@@ -19,66 +19,78 @@ module.exports = () => {
1919 prepare ( result ) {
2020 const importAliases = [ ] ;
2121 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- } ;
6522
6623 return {
6724 /* Look at all the @value statements and treat them as locals or as imports */
6825 AtRule : {
6926 value ( atRule ) {
7027 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+ }
7264 } else {
7365 if ( atRule . params . indexOf ( "@value" ) !== - 1 ) {
7466 result . warn ( "Invalid value definition: " + atRule . params ) ;
7567 }
7668
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+ }
7882 }
7983 } ,
8084 } ,
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+
8294 /* 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 */
8395 const exportDeclarations = Object . keys ( definitions ) . map ( ( key ) =>
8496 postcss . decl ( {
@@ -88,14 +100,6 @@ module.exports = () => {
88100 } )
89101 ) ;
90102
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-
99103 /* Add export rules if any */
100104 if ( exportDeclarations . length > 0 ) {
101105 const exportRule = postcss . rule ( {
0 commit comments