@@ -17,6 +17,7 @@ import {
17
17
getCurrentRequest ,
18
18
stringifyRequest ,
19
19
} from 'loader-utils' ;
20
+ import normalizePath from 'normalize-path' ;
20
21
21
22
import schema from './options.json' ;
22
23
import { importParser , icssParser , urlParser } from './plugins' ;
@@ -42,13 +43,24 @@ export default function loader(content, map, meta) {
42
43
/* eslint-disable no-param-reassign */
43
44
if ( sourceMap ) {
44
45
if ( map ) {
46
+ // Some loader emit source map as string
45
47
if ( typeof map === 'string' ) {
46
48
map = JSON . stringify ( map ) ;
47
49
}
48
50
51
+ // Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
52
+ // We should normalize path because previous loaders like `sass-loader` using backslash when generate source map
53
+
54
+ if ( map . file ) {
55
+ map . file = normalizePath ( map . file ) ;
56
+ }
57
+
58
+ if ( map . sourceRoot ) {
59
+ map . sourceRoot = normalizePath ( map . sourceRoot ) ;
60
+ }
61
+
49
62
if ( map . sources ) {
50
- map . sources = map . sources . map ( ( source ) => source . replace ( / \\ / g, '/' ) ) ;
51
- map . sourceRoot = '' ;
63
+ map . sources = map . sources . map ( ( source ) => normalizePath ( source ) ) ;
52
64
}
53
65
}
54
66
} else {
@@ -120,17 +132,15 @@ export default function loader(content, map, meta) {
120
132
121
133
postcss ( plugins )
122
134
. process ( content , {
123
- // we need a prefix to avoid path rewriting of PostCSS
124
- from : `/css-loader!${ getRemainingRequest ( this )
135
+ from : getRemainingRequest ( this )
125
136
. split ( '!' )
126
- . pop ( ) } ` ,
137
+ . pop ( ) ,
127
138
to : getCurrentRequest ( this )
128
139
. split ( '!' )
129
140
. pop ( ) ,
130
141
map : options . sourceMap
131
142
? {
132
143
prev : map ,
133
- sourcesContent : true ,
134
144
inline : false ,
135
145
annotation : false ,
136
146
}
@@ -301,39 +311,14 @@ export default function loader(content, map, meta) {
301
311
) ;
302
312
} ) ;
303
313
304
- let newMap = result . map ;
305
-
306
- if ( sourceMap && newMap ) {
307
- // Add a SourceMap
308
- newMap = newMap . toJSON ( ) ;
309
-
310
- if ( newMap . sources ) {
311
- newMap . sources = newMap . sources . map (
312
- ( source ) =>
313
- source
314
- . split ( '!' )
315
- . pop ( )
316
- . replace ( / \\ / g, '/' ) ,
317
- this
318
- ) ;
319
- newMap . sourceRoot = '' ;
320
- }
321
-
322
- newMap . file = newMap . file
323
- . split ( '!' )
324
- . pop ( )
325
- . replace ( / \\ / g, '/' ) ;
326
- newMap = JSON . stringify ( newMap ) ;
327
- }
328
-
329
314
const runtimeCode = `exports = module.exports = require(${ stringifyRequest (
330
315
this ,
331
316
require . resolve ( './runtime/api' )
332
317
) } )(${ ! ! sourceMap } );\n`;
333
318
const importCode =
334
319
imports . length > 0 ? `// Imports\n${ imports . join ( '\n' ) } \n\n` : '' ;
335
320
const moduleCode = `// Module\nexports.push([module.id, ${ cssAsString } , ""${
336
- newMap ? `,${ newMap } ` : ''
321
+ result . map ? `,${ result . map } ` : ''
337
322
} ]);\n\n`;
338
323
const exportsCode =
339
324
exports . length > 0
0 commit comments