@@ -63,10 +63,9 @@ module.exports = (options = {}) => {
63
63
64
64
return {
65
65
postcssPlugin : "postcss-modules-extract-imports" ,
66
- OnceExit ( root , postcss ) {
66
+ prepare ( result ) {
67
67
const graph = { } ;
68
68
const visited = { } ;
69
-
70
69
const existingImports = { } ;
71
70
const importDecls = { } ;
72
71
const imports = { } ;
@@ -79,114 +78,119 @@ module.exports = (options = {}) => {
79
78
`i__imported_${ importName . replace ( / \W / g, "_" ) } _${ importIndex ++ } `
80
79
: options . createImportedName ;
81
80
82
- // Check the existing imports order and save refs
83
- root . walkRules ( ( rule ) => {
84
- const matches = icssImport . exec ( rule . selector ) ;
85
-
86
- if ( matches ) {
87
- const [ , /*match*/ doubleQuotePath , singleQuotePath ] = matches ;
88
- const importPath = doubleQuotePath || singleQuotePath ;
89
-
90
- addImportToGraph ( importPath , "root" , graph , visited ) ;
91
-
92
- existingImports [ importPath ] = rule ;
93
- }
94
- } ) ;
95
-
96
- // Find any declaration that supports imports
97
- root . walkDecls ( declFilter , ( decl ) => {
98
- let matches = decl . value . match ( matchImports ) ;
99
- let tmpSymbols ;
100
-
101
- if ( matches ) {
102
- let [
103
- ,
104
- /*match*/ symbols ,
105
- doubleQuotePath ,
106
- singleQuotePath ,
107
- global ,
108
- ] = matches ;
109
-
110
- if ( global ) {
111
- // Composing globals simply means changing these classes to wrap them in global(name)
112
- tmpSymbols = symbols . split ( / \s + / ) . map ( ( s ) => `global(${ s } )` ) ;
113
- } else {
81
+ return {
82
+ // Check the existing imports order and save refs
83
+ Rule ( rule ) {
84
+ const matches = icssImport . exec ( rule . selector ) ;
85
+
86
+ if ( matches ) {
87
+ const [ , /*match*/ doubleQuotePath , singleQuotePath ] = matches ;
114
88
const importPath = doubleQuotePath || singleQuotePath ;
115
- const parentRule = createParentName ( decl . parent , root ) ;
116
89
117
- addImportToGraph ( importPath , parentRule , graph , visited ) ;
90
+ addImportToGraph ( importPath , "root" , graph , visited ) ;
91
+
92
+ existingImports [ importPath ] = rule ;
93
+ }
94
+ } ,
95
+ Declaration ( decl ) {
96
+ if ( ! declFilter . test ( decl . prop ) ) {
97
+ return ;
98
+ }
118
99
119
- importDecls [ importPath ] = decl ;
120
- imports [ importPath ] = imports [ importPath ] || { } ;
100
+ let matches = decl . value . match ( matchImports ) ;
101
+ let tmpSymbols ;
121
102
122
- tmpSymbols = symbols . split ( / \s + / ) . map ( ( s ) => {
123
- if ( ! imports [ importPath ] [ s ] ) {
124
- imports [ importPath ] [ s ] = createImportedName ( s , importPath ) ;
125
- }
103
+ if ( matches ) {
104
+ let [
105
+ ,
106
+ /*match*/ symbols ,
107
+ doubleQuotePath ,
108
+ singleQuotePath ,
109
+ global ,
110
+ ] = matches ;
126
111
127
- return imports [ importPath ] [ s ] ;
128
- } ) ;
129
- }
112
+ if ( global ) {
113
+ // Composing globals simply means changing these classes to wrap them in global(name)
114
+ tmpSymbols = symbols . split ( / \s + / ) . map ( ( s ) => `global(${ s } )` ) ;
115
+ } else {
116
+ const importPath = doubleQuotePath || singleQuotePath ;
117
+ const parentRule = createParentName ( decl . parent , result . root ) ;
130
118
131
- decl . value = tmpSymbols . join ( " " ) ;
132
- }
133
- } ) ;
119
+ addImportToGraph ( importPath , parentRule , graph , visited ) ;
134
120
135
- const importsOrder = topologicalSort ( graph , failOnWrongOrder ) ;
121
+ importDecls [ importPath ] = decl ;
122
+ imports [ importPath ] = imports [ importPath ] || { } ;
136
123
137
- if ( importsOrder instanceof Error ) {
138
- const importPath = importsOrder . nodes . find ( ( importPath ) =>
139
- // eslint-disable-next-line no-prototype-builtins
140
- importDecls . hasOwnProperty ( importPath )
141
- ) ;
142
- const decl = importDecls [ importPath ] ;
124
+ tmpSymbols = symbols . split ( / \s + / ) . map ( ( s ) => {
125
+ if ( ! imports [ importPath ] [ s ] ) {
126
+ imports [ importPath ] [ s ] = createImportedName ( s , importPath ) ;
127
+ }
143
128
144
- const errMsg =
145
- "Failed to resolve order of composed modules " +
146
- serializeImports ( importsOrder . nodes ) +
147
- "." ;
129
+ return imports [ importPath ] [ s ] ;
130
+ } ) ;
131
+ }
148
132
149
- throw decl . error ( errMsg , {
150
- plugin : "postcss-modules-extract-imports" ,
151
- word : "composes" ,
152
- } ) ;
153
- }
133
+ decl . value = tmpSymbols . join ( " " ) ;
134
+ }
135
+ } ,
136
+ OnceExit ( root , postcss ) {
137
+ const importsOrder = topologicalSort ( graph , failOnWrongOrder ) ;
138
+
139
+ if ( importsOrder instanceof Error ) {
140
+ const importPath = importsOrder . nodes . find ( ( importPath ) =>
141
+ // eslint-disable-next-line no-prototype-builtins
142
+ importDecls . hasOwnProperty ( importPath )
143
+ ) ;
144
+ const decl = importDecls [ importPath ] ;
145
+
146
+ const errMsg =
147
+ "Failed to resolve order of composed modules " +
148
+ serializeImports ( importsOrder . nodes ) +
149
+ "." ;
150
+
151
+ throw decl . error ( errMsg , {
152
+ plugin : "postcss-modules-extract-imports" ,
153
+ word : "composes" ,
154
+ } ) ;
155
+ }
154
156
155
- let lastImportRule ;
157
+ let lastImportRule ;
156
158
157
- importsOrder . forEach ( ( path ) => {
158
- const importedSymbols = imports [ path ] ;
159
- let rule = existingImports [ path ] ;
159
+ importsOrder . forEach ( ( path ) => {
160
+ const importedSymbols = imports [ path ] ;
161
+ let rule = existingImports [ path ] ;
160
162
161
- if ( ! rule && importedSymbols ) {
162
- rule = postcss . rule ( {
163
- selector : `:import("${ path } ")` ,
164
- raws : { after : "\n" } ,
165
- } ) ;
163
+ if ( ! rule && importedSymbols ) {
164
+ rule = postcss . rule ( {
165
+ selector : `:import("${ path } ")` ,
166
+ raws : { after : "\n" } ,
167
+ } ) ;
166
168
167
- if ( lastImportRule ) {
168
- root . insertAfter ( lastImportRule , rule ) ;
169
- } else {
170
- root . prepend ( rule ) ;
171
- }
172
- }
173
-
174
- lastImportRule = rule ;
175
-
176
- if ( ! importedSymbols ) {
177
- return ;
178
- }
179
-
180
- Object . keys ( importedSymbols ) . forEach ( ( importedSymbol ) => {
181
- rule . append (
182
- postcss . decl ( {
183
- value : importedSymbol ,
184
- prop : importedSymbols [ importedSymbol ] ,
185
- raws : { before : "\n " } ,
186
- } )
187
- ) ;
188
- } ) ;
189
- } ) ;
169
+ if ( lastImportRule ) {
170
+ root . insertAfter ( lastImportRule , rule ) ;
171
+ } else {
172
+ root . prepend ( rule ) ;
173
+ }
174
+ }
175
+
176
+ lastImportRule = rule ;
177
+
178
+ if ( ! importedSymbols ) {
179
+ return ;
180
+ }
181
+
182
+ Object . keys ( importedSymbols ) . forEach ( ( importedSymbol ) => {
183
+ rule . append (
184
+ postcss . decl ( {
185
+ value : importedSymbol ,
186
+ prop : importedSymbols [ importedSymbol ] ,
187
+ raws : { before : "\n " } ,
188
+ } )
189
+ ) ;
190
+ } ) ;
191
+ } ) ;
192
+ } ,
193
+ } ;
190
194
} ,
191
195
} ;
192
196
} ;
0 commit comments