@@ -298,6 +298,25 @@ function getModulesPlugins(options, loaderContext) {
298
298
return plugins ;
299
299
}
300
300
301
+ const IS_NATIVE_WIN32_PATH = / ^ [ a - z ] : [ / \\ ] | ^ \\ \\ / i;
302
+ const ABSOLUTE_SCHEME = / ^ [ a - z 0 - 9 + \- . ] + : / i;
303
+
304
+ function getURLType ( source ) {
305
+ if ( source [ 0 ] === '/' ) {
306
+ if ( source [ 1 ] === '/' ) {
307
+ return 'scheme-relative' ;
308
+ }
309
+
310
+ return 'path-absolute' ;
311
+ }
312
+
313
+ if ( IS_NATIVE_WIN32_PATH . test ( source ) ) {
314
+ return 'path-absolute' ;
315
+ }
316
+
317
+ return ABSOLUTE_SCHEME . test ( source ) ? 'absolute' : 'path-relative' ;
318
+ }
319
+
301
320
function normalizeSourceMap ( map , resourcePath ) {
302
321
let newMap = map ;
303
322
@@ -307,36 +326,34 @@ function normalizeSourceMap(map, resourcePath) {
307
326
newMap = JSON . parse ( newMap ) ;
308
327
}
309
328
310
- // Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
311
- // We should normalize path because previous loaders like `sass-loader` using backslash when generate source map
312
-
313
- if ( newMap . file ) {
314
- delete newMap . file ;
315
- }
329
+ delete newMap . file ;
316
330
317
331
const { sourceRoot } = newMap ;
318
332
319
- if ( newMap . sourceRoot ) {
320
- delete newMap . sourceRoot ;
321
- }
333
+ delete newMap . sourceRoot ;
322
334
323
335
if ( newMap . sources ) {
336
+ // Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
337
+ // We should normalize path because previous loaders like `sass-loader` using backslash when generate source map
324
338
newMap . sources = newMap . sources . map ( ( source ) => {
339
+ // Non-standard syntax from `postcss`
325
340
if ( source . indexOf ( '<' ) === 0 ) {
326
341
return source ;
327
342
}
328
343
329
- if ( / ^ \w + : \/ \/ / . test ( source ) ) {
330
- return source ;
331
- }
344
+ const sourceType = getURLType ( source ) ;
332
345
333
- const absoluteSource = ! sourceRoot
334
- ? source
335
- : path . resolve ( sourceRoot , source ) ;
346
+ // Do no touch `scheme-relative` and `absolute` URLs
347
+ if ( sourceType === 'path-relative' || sourceType === 'path-absolute' ) {
348
+ const absoluteSource =
349
+ sourceType === 'path-relative' && sourceRoot
350
+ ? path . resolve ( sourceRoot , normalizePath ( source ) )
351
+ : normalizePath ( source ) ;
336
352
337
- const resourceDirname = path . dirname ( resourcePath ) ;
353
+ return path . relative ( path . dirname ( resourcePath ) , absoluteSource ) ;
354
+ }
338
355
339
- return normalizePath ( path . relative ( resourceDirname , absoluteSource ) ) ;
356
+ return source ;
340
357
} ) ;
341
358
}
342
359
@@ -395,16 +412,19 @@ function normalizeSourceMapForRuntime(map, loaderContext) {
395
412
const resultMap = map ? map . toJSON ( ) : null ;
396
413
397
414
if ( resultMap ) {
398
- if ( typeof resultMap . file !== 'undefined' ) {
399
- delete resultMap . file ;
400
- }
415
+ delete resultMap . file ;
416
+
417
+ resultMap . sourceRoot = '' ;
401
418
402
419
resultMap . sources = resultMap . sources . map ( ( source ) => {
420
+ // Non-standard syntax from `postcss`
403
421
if ( source . indexOf ( '<' ) === 0 ) {
404
422
return source ;
405
423
}
406
424
407
- if ( / ^ \w + : \/ \/ / . test ( source ) ) {
425
+ const sourceType = getURLType ( source ) ;
426
+
427
+ if ( sourceType !== 'path-relative' ) {
408
428
return source ;
409
429
}
410
430
0 commit comments