Skip to content

Commit e42f046

Browse files
refactor: improve sources handling in source maps (#1176)
1 parent 4ce556a commit e42f046

File tree

3 files changed

+215
-33
lines changed

3 files changed

+215
-33
lines changed

src/utils.js

+41-21
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,25 @@ function getModulesPlugins(options, loaderContext) {
298298
return plugins;
299299
}
300300

301+
const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i;
302+
const ABSOLUTE_SCHEME = /^[a-z0-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+
301320
function normalizeSourceMap(map, resourcePath) {
302321
let newMap = map;
303322

@@ -307,36 +326,34 @@ function normalizeSourceMap(map, resourcePath) {
307326
newMap = JSON.parse(newMap);
308327
}
309328

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;
316330

317331
const { sourceRoot } = newMap;
318332

319-
if (newMap.sourceRoot) {
320-
delete newMap.sourceRoot;
321-
}
333+
delete newMap.sourceRoot;
322334

323335
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
324338
newMap.sources = newMap.sources.map((source) => {
339+
// Non-standard syntax from `postcss`
325340
if (source.indexOf('<') === 0) {
326341
return source;
327342
}
328343

329-
if (/^\w+:\/\//.test(source)) {
330-
return source;
331-
}
344+
const sourceType = getURLType(source);
332345

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);
336352

337-
const resourceDirname = path.dirname(resourcePath);
353+
return path.relative(path.dirname(resourcePath), absoluteSource);
354+
}
338355

339-
return normalizePath(path.relative(resourceDirname, absoluteSource));
356+
return source;
340357
});
341358
}
342359

@@ -395,16 +412,19 @@ function normalizeSourceMapForRuntime(map, loaderContext) {
395412
const resultMap = map ? map.toJSON() : null;
396413

397414
if (resultMap) {
398-
if (typeof resultMap.file !== 'undefined') {
399-
delete resultMap.file;
400-
}
415+
delete resultMap.file;
416+
417+
resultMap.sourceRoot = '';
401418

402419
resultMap.sources = resultMap.sources.map((source) => {
420+
// Non-standard syntax from `postcss`
403421
if (source.indexOf('<') === 0) {
404422
return source;
405423
}
406424

407-
if (/^\w+:\/\//.test(source)) {
425+
const sourceType = getURLType(source);
426+
427+
if (sourceType !== 'path-relative') {
408428
return source;
409429
}
410430

0 commit comments

Comments
 (0)