Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve sources handling in source maps #1176

Merged
merged 3 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,25 @@ function getModulesPlugins(options, loaderContext) {
return plugins;
}

const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i;
const ABSOLUTE_SCHEME = /^[a-z0-9+\-.]+:/i;

function getURLType(source) {
if (source[0] === '/') {
if (source[1] === '/') {
return 'scheme-relative';
}

return 'path-absolute';
}

if (IS_NATIVE_WIN32_PATH.test(source)) {
return 'path-absolute';
}

return ABSOLUTE_SCHEME.test(source) ? 'absolute' : 'path-relative';
}

function normalizeSourceMap(map, resourcePath) {
let newMap = map;

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

// Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
// We should normalize path because previous loaders like `sass-loader` using backslash when generate source map

if (newMap.file) {
delete newMap.file;
}
delete newMap.file;

const { sourceRoot } = newMap;

if (newMap.sourceRoot) {
delete newMap.sourceRoot;
}
delete newMap.sourceRoot;

if (newMap.sources) {
// Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
// We should normalize path because previous loaders like `sass-loader` using backslash when generate source map
newMap.sources = newMap.sources.map((source) => {
// Non-standard syntax from `postcss`
if (source.indexOf('<') === 0) {
return source;
}

if (/^\w+:\/\//.test(source)) {
return source;
}
const sourceType = getURLType(source);

const absoluteSource = !sourceRoot
? source
: path.resolve(sourceRoot, source);
// Do no touch `scheme-relative` and `absolute` URLs
if (sourceType === 'path-relative' || sourceType === 'path-absolute') {
const absoluteSource =
sourceType === 'path-relative' && sourceRoot
? path.resolve(sourceRoot, normalizePath(source))
: normalizePath(source);

const resourceDirname = path.dirname(resourcePath);
return path.relative(path.dirname(resourcePath), absoluteSource);
}

return normalizePath(path.relative(resourceDirname, absoluteSource));
return source;
});
}

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

if (resultMap) {
if (typeof resultMap.file !== 'undefined') {
delete resultMap.file;
}
delete resultMap.file;

resultMap.sourceRoot = '';

resultMap.sources = resultMap.sources.map((source) => {
// Non-standard syntax from `postcss`
if (source.indexOf('<') === 0) {
return source;
}

if (/^\w+:\/\//.test(source)) {
const sourceType = getURLType(source);

if (sourceType !== 'path-relative') {
return source;
}

Expand Down
Loading