9
9
getRemainingRequest ,
10
10
getCurrentRequest ,
11
11
stringifyRequest ,
12
- isUrlRequest ,
13
- urlToRequest ,
14
12
} from 'loader-utils' ;
15
13
import postcss from 'postcss' ;
16
14
import postcssPkg from 'postcss/package.json' ;
@@ -22,21 +20,6 @@ import icssPlugin from './plugins/icss';
22
20
import Warning from './Warning' ;
23
21
import SyntaxError from './SyntaxError' ;
24
22
25
- const runtimeApi = require . resolve ( './runtime/api' ) ;
26
- const runtimeEscape = require . resolve ( './runtime/escape' ) ;
27
-
28
- function getImportPrefix ( loaderContext , importLoaders ) {
29
- const loadersRequest = loaderContext . loaders
30
- . slice (
31
- loaderContext . loaderIndex ,
32
- loaderContext . loaderIndex + 1 + importLoaders
33
- )
34
- . map ( ( x ) => x . request )
35
- . join ( '!' ) ;
36
-
37
- return `-!${ loadersRequest } !` ;
38
- }
39
-
40
23
export default function loader ( content , map , meta ) {
41
24
const options = getOptions ( this ) || { } ;
42
25
@@ -61,7 +44,7 @@ export default function loader(content, map, meta) {
61
44
}
62
45
63
46
if ( importOpt ) {
64
- plugins . push ( importPlugin ( ) ) ;
47
+ plugins . push ( importPlugin ( { importLoaders } ) ) ;
65
48
}
66
49
67
50
plugins . push ( icssPlugin ( ) ) ;
@@ -136,147 +119,39 @@ export default function loader(content, map, meta) {
136
119
newMap = JSON . stringify ( newMap ) ;
137
120
}
138
121
139
- let hasURLEscapeRuntime = false ;
140
- let moduleCode = JSON . stringify ( result . css ) ;
141
- const imports = [ ] ;
142
- const exports = [ ] ;
122
+ let moduleObj = {
123
+ runtime : `module.exports = exports = require(${ stringifyRequest (
124
+ this ,
125
+ require . resolve ( './runtime/api' )
126
+ ) } )(${ ! ! sourceMap } );`,
127
+ imports : [ ] ,
128
+ module : `exports.push([module.id, ${ JSON . stringify ( result . css ) } , ""${
129
+ newMap ? `,${ newMap } ` : ''
130
+ } ]);`,
131
+ exports : [ ] ,
132
+ } ;
143
133
144
134
if ( result . messages && result . messages . length > 0 ) {
145
- let exportedTokens = { } ;
146
-
147
135
result . messages
148
- . filter ( ( message ) => ( message . type === 'export ' ? message : false ) )
136
+ . filter ( ( message ) => ( message . type === 'module ' ? message : false ) )
149
137
. forEach ( ( message ) => {
150
- exportedTokens = Object . assign (
151
- { } ,
152
- exportedTokens ,
153
- message . tokens || { }
154
- ) ;
155
- } ) ;
156
-
157
- let importedTokens = { } ;
158
-
159
- result . messages
160
- . filter ( ( message ) => ( message . type === 'import' ? message : false ) )
161
- . forEach ( ( message ) => {
162
- importedTokens = Object . assign (
163
- { } ,
164
- importedTokens ,
165
- message . tokens || { }
166
- ) ;
167
- } ) ;
168
-
169
- let exportsCode =
170
- Object . keys ( exportedTokens ) . length > 0
171
- ? JSON . stringify ( exportedTokens )
172
- : '' ;
173
-
174
- Object . keys ( importedTokens ) . forEach ( ( token ) => {
175
- const value = importedTokens [ token ] ;
176
- const isUrlToken =
177
- Object . keys ( value ) . length === 1 &&
178
- value [ Object . keys ( value ) [ 0 ] ] === 'default' ;
179
- const splittedToken = token . split ( / ( \? ) ? # / ) ;
180
- const [ normalizedToken ] = splittedToken ;
181
-
182
- if ( isUrlToken ) {
183
- // URLs in `url` function
184
- hasURLEscapeRuntime = true ;
185
-
186
- const [ placeholder ] = Object . keys ( value ) ;
187
-
188
- imports . push (
189
- `var ${ placeholder } = escape(require(${ stringifyRequest (
190
- this ,
191
- urlToRequest ( normalizedToken )
192
- ) } ));`
193
- ) ;
194
- } else {
195
- const media = value [ '{media}' ] || '' ;
196
-
197
- if ( isUrlRequest ( token ) ) {
198
- // Requestable url in `@import` at-rule (`@import './style.css`)
199
- imports . push (
200
- `exports.i(require(${ stringifyRequest (
201
- this ,
202
- getImportPrefix ( this , importLoaders ) +
203
- urlToRequest ( normalizedToken )
204
- ) } ), ${ JSON . stringify ( media ) } );`
205
- ) ;
206
- } else {
207
- // Absolute url in `@import` at-rule (`@import 'https://example.com/style.css`)
208
- imports . push (
209
- `exports.push([module.id, ${ JSON . stringify (
210
- `@import url(${ normalizedToken } );`
211
- ) } , ${ JSON . stringify ( media ) } ]);`
212
- ) ;
213
- }
214
- }
215
-
216
- Object . keys ( value ) . forEach ( ( replacedToken ) => {
217
- if ( [ '{media}' , '{type}' ] . includes ( replacedToken ) ) {
218
- return ;
138
+ try {
139
+ moduleObj = message . modify ( moduleObj , this ) ;
140
+ } catch ( err ) {
141
+ this . emitError ( err ) ;
219
142
}
220
-
221
- let replacedCode = null ;
222
-
223
- if ( isUrlToken ) {
224
- // Code for `url` tokens
225
- replacedCode = `" + ${ replacedToken } + "${
226
- splittedToken [ 1 ] ? splittedToken [ 1 ] : ''
227
- } ${ splittedToken [ 2 ] ? `#${ splittedToken [ 2 ] } ` : '' } `;
228
- } else {
229
- // Code for `local` tokens
230
- replacedCode = `" + require(${ stringifyRequest (
231
- this ,
232
- getImportPrefix ( this , importLoaders ) +
233
- urlToRequest ( normalizedToken )
234
- ) } ).locals[${ JSON . stringify ( value [ replacedToken ] ) } ] +"`;
235
- }
236
-
237
- moduleCode = moduleCode . replace (
238
- new RegExp ( replacedToken , 'g' ) ,
239
- replacedCode
240
- ) ;
241
- exportsCode = exportsCode . replace (
242
- new RegExp ( replacedToken , 'g' ) ,
243
- replacedCode
244
- ) ;
245
143
} ) ;
246
- } ) ;
247
-
248
- if ( exportsCode ) {
249
- exports . push ( `exports.locals = ${ exportsCode } ` ) ;
250
- }
251
144
}
252
145
253
- cb (
146
+ const { runtime, imports, module, exports } = moduleObj ;
147
+
148
+ return cb (
254
149
null ,
255
150
[
256
- ...( hasURLEscapeRuntime
257
- ? [
258
- '// CSS runtime escape' ,
259
- `var escape = require(${ stringifyRequest (
260
- this ,
261
- runtimeEscape
262
- ) } );`,
263
- '' ,
264
- ]
265
- : [ ] ) ,
266
- '// CSS runtime' ,
267
- `module.exports = exports = require(${ stringifyRequest (
268
- this ,
269
- runtimeApi
270
- ) } )(${ ! ! sourceMap } );`,
271
- '' ,
272
- ...( imports . length > 0
273
- ? [ '// CSS imports' , imports . join ( '\n' ) , '' ]
274
- : [ ] ) ,
275
- '// CSS module' ,
276
- `exports.push([module.id, ${ moduleCode } , ""${
277
- newMap ? `,${ newMap } ` : ''
278
- } ]);\n`,
279
- ...( exports . length > 0 ? [ '// CSS exports' , exports . join ( '\n' ) ] : [ ] ) ,
151
+ runtime ? `// CSS runtime\n${ runtime } ` : '' ,
152
+ imports . length > 0 ? `\n// CSS imports\n${ imports . join ( '\n' ) } ` : '' ,
153
+ module ? `\n// CSS module\n${ module } ` : '' ,
154
+ exports . length > 0 ? `// CSS exports\n${ exports . join ( '\n' ) } ` : '' ,
280
155
] . join ( '\n' )
281
156
) ;
282
157
} )
0 commit comments