@@ -6,7 +6,6 @@ import { fileURLToPath } from 'url';
6
6
import path from 'path' ;
7
7
8
8
import { urlToRequest , interpolateName } from 'loader-utils' ;
9
- import normalizePath from 'normalize-path' ;
10
9
import cssesc from 'cssesc' ;
11
10
import modulesValues from 'postcss-modules-values' ;
12
11
import localByDefault from 'postcss-modules-local-by-default' ;
@@ -41,6 +40,10 @@ function unescape(str) {
41
40
} ) ;
42
41
}
43
42
43
+ function normalizePath ( file ) {
44
+ return path . sep === '\\' ? file . replace ( / \\ / g, '/' ) : file ;
45
+ }
46
+
44
47
// eslint-disable-next-line no-control-regex
45
48
const filenameReservedRegex = / [ < > : " / \\ | ? * ] / g;
46
49
// eslint-disable-next-line no-control-regex
@@ -295,7 +298,7 @@ function getModulesPlugins(options, loaderContext) {
295
298
return plugins ;
296
299
}
297
300
298
- function normalizeSourceMap ( map ) {
301
+ function normalizeSourceMap ( map , resourcePath ) {
299
302
let newMap = map ;
300
303
301
304
// Some loader emit source map as string
@@ -308,15 +311,33 @@ function normalizeSourceMap(map) {
308
311
// We should normalize path because previous loaders like `sass-loader` using backslash when generate source map
309
312
310
313
if ( newMap . file ) {
311
- newMap . file = normalizePath ( newMap . file ) ;
314
+ delete newMap . file ;
312
315
}
313
316
317
+ const { sourceRoot } = newMap ;
318
+
314
319
if ( newMap . sourceRoot ) {
315
- newMap . sourceRoot = normalizePath ( newMap . sourceRoot ) ;
320
+ delete newMap . sourceRoot ;
316
321
}
317
322
318
323
if ( newMap . sources ) {
319
- newMap . sources = newMap . sources . map ( ( source ) => normalizePath ( source ) ) ;
324
+ newMap . sources = newMap . sources . map ( ( source ) => {
325
+ if ( source . indexOf ( '<' ) === 0 ) {
326
+ return source ;
327
+ }
328
+
329
+ if ( / ^ \w + : \/ \/ / . test ( source ) ) {
330
+ return source ;
331
+ }
332
+
333
+ const absoluteSource = ! sourceRoot
334
+ ? source
335
+ : path . resolve ( sourceRoot , source ) ;
336
+
337
+ const resourceDirname = path . dirname ( resourcePath ) ;
338
+
339
+ return normalizePath ( path . relative ( resourceDirname , absoluteSource ) ) ;
340
+ } ) ;
320
341
}
321
342
322
343
return newMap ;
@@ -370,14 +391,46 @@ function getImportCode(imports, options) {
370
391
return code ? `// Imports\n${ code } ` : '' ;
371
392
}
372
393
373
- function getModuleCode ( result , api , replacements , options ) {
394
+ function normalizeSourceMapForRuntime ( map , loaderContext ) {
395
+ const resultMap = map ? map . toJSON ( ) : null ;
396
+
397
+ if ( resultMap ) {
398
+ if ( typeof resultMap . file !== 'undefined' ) {
399
+ delete resultMap . file ;
400
+ }
401
+
402
+ resultMap . sources = resultMap . sources . map ( ( source ) => {
403
+ if ( source . indexOf ( '<' ) === 0 ) {
404
+ return source ;
405
+ }
406
+
407
+ if ( / ^ \w + : \/ \/ / . test ( source ) ) {
408
+ return source ;
409
+ }
410
+
411
+ const resourceDirname = path . dirname ( loaderContext . resourcePath ) ;
412
+ const absoluteSource = path . resolve ( resourceDirname , source ) ;
413
+ const contextifyPath = normalizePath (
414
+ path . relative ( loaderContext . rootContext , absoluteSource )
415
+ ) ;
416
+
417
+ return `webpack:///${ contextifyPath } ` ;
418
+ } ) ;
419
+ }
420
+
421
+ return JSON . stringify ( resultMap ) ;
422
+ }
423
+
424
+ function getModuleCode ( result , api , replacements , options , loaderContext ) {
374
425
if ( options . modules . exportOnlyLocals === true ) {
375
426
return '' ;
376
427
}
377
428
378
- const { css, map } = result ;
379
- const sourceMapValue = options . sourceMap && map ? `,${ map } ` : '' ;
380
- let code = JSON . stringify ( css ) ;
429
+ const sourceMapValue = options . sourceMap
430
+ ? `,${ normalizeSourceMapForRuntime ( result . map , loaderContext ) } `
431
+ : '' ;
432
+
433
+ let code = JSON . stringify ( result . css ) ;
381
434
let beforeCode = `var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(${ options . sourceMap } );\n` ;
382
435
383
436
for ( const item of api ) {
0 commit comments