@@ -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
}
@@ -141,6 +151,14 @@ export default function loader(content, map, meta) {
141
151
. warnings ( )
142
152
. forEach ( ( warning ) => this . emitWarning ( new Warning ( warning ) ) ) ;
143
153
154
+ if ( result . map ) {
155
+ const newMap = result . map . toJSON ( ) ;
156
+
157
+ console . log ( newMap . file ) ;
158
+ console . log ( newMap . sourceRoot ) ;
159
+ console . log ( newMap . sources ) ;
160
+ }
161
+
144
162
const messages = result . messages || [ ] ;
145
163
146
164
// Run other loader (`postcss-loader`, `sass-loader` and etc) for importing CSS
@@ -301,39 +319,14 @@ export default function loader(content, map, meta) {
301
319
) ;
302
320
} ) ;
303
321
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
322
const runtimeCode = `exports = module.exports = require(${ stringifyRequest (
330
323
this ,
331
324
require . resolve ( './runtime/api' )
332
325
) } )(${ ! ! sourceMap } );\n`;
333
326
const importCode =
334
327
imports . length > 0 ? `// Imports\n${ imports . join ( '\n' ) } \n\n` : '' ;
335
328
const moduleCode = `// Module\nexports.push([module.id, ${ cssAsString } , ""${
336
- newMap ? `,${ newMap } ` : ''
329
+ result . map ? `,${ result . map } ` : ''
337
330
} ]);\n\n`;
338
331
const exportsCode =
339
332
exports . length > 0
0 commit comments