Skip to content

Commit

Permalink
fix: logic when sourceRoot contains absolute URL (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgischk committed Oct 21, 2022
1 parent 060adfc commit 625cc81
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/index.js
Expand Up @@ -5,7 +5,12 @@
import path from "path";

import schema from "./options.json";
import { getSourceMappingURL, fetchFromURL, flattenSourceMap } from "./utils";
import {
getSourceMappingURL,
fetchFromURL,
flattenSourceMap,
isURL,
} from "./utils";

export default async function loader(input, inputMap) {
const options = this.getOptions(schema);
Expand Down Expand Up @@ -122,7 +127,7 @@ export default async function loader(input, inputMap) {

if (skipReading) {
sourceContent = originalSourceContent;
} else if (!errored && sourceURL) {
} else if (!errored && sourceURL && !isURL(sourceURL)) {
this.addDependency(sourceURL);
}

Expand Down
16 changes: 14 additions & 2 deletions src/utils.js
Expand Up @@ -96,6 +96,10 @@ function getSourceMappingURL(code) {
}

function getAbsolutePath(context, request, sourceRoot) {
if (isURL(sourceRoot)) {
return new URL(request, sourceRoot).toString();
}

if (sourceRoot) {
if (path.isAbsolute(sourceRoot)) {
return path.join(sourceRoot, request);
Expand Down Expand Up @@ -125,6 +129,10 @@ function fetchFromDataURL(loaderContext, sourceURL) {
async function fetchFromFilesystem(loaderContext, sourceURL) {
let buffer;

if (isURL(sourceURL)) {
return { path: sourceURL };
}

try {
buffer = await new Promise((resolve, reject) => {
loaderContext.fs.readFile(sourceURL, (error, data) => {
Expand Down Expand Up @@ -179,6 +187,10 @@ async function fetchPathsFromFilesystem(
return result;
}

function isURL(value) {
return /^[a-z][a-z0-9+.-]*:/i.test(value) && !path.win32.isAbsolute(value);
}

async function fetchFromURL(
loaderContext,
context,
Expand All @@ -187,7 +199,7 @@ async function fetchFromURL(
skipReading = false
) {
// 1. It's an absolute url and it is not `windows` path like `C:\dir\file`
if (/^[a-z][a-z0-9+.-]*:/i.test(url) && !path.win32.isAbsolute(url)) {
if (isURL(url)) {
const { protocol } = urlUtils.parse(url);

if (protocol === "data:") {
Expand Down Expand Up @@ -268,4 +280,4 @@ async function fetchFromURL(
return { sourceURL, sourceContent };
}

export { getSourceMappingURL, fetchFromURL, flattenSourceMap };
export { getSourceMappingURL, fetchFromURL, flattenSourceMap, isURL };
21 changes: 21 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -113,6 +113,27 @@ exports[`source-map-loader should leave normal files with fake source-map untouc

exports[`source-map-loader should leave normal files with fake source-map untouched: warnings 1`] = `Array []`;

exports[`source-map-loader should process absolute URL in sourceRoot: code 1`] = `
"const foo = "test";
"
`;

exports[`source-map-loader should process absolute URL in sourceRoot: errors 1`] = `[]`;

exports[`source-map-loader should process absolute URL in sourceRoot: map 1`] = `
{
"file": "index.d.ts",
"mappings": "AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,KAAK,aAAa,EAAE,CAAC;AAEtC,OAAO,EACH,UAAU,EACV,iBAAiB,EACjB,SAAS,EACT,OAAO,EACP,QAAQ,EACX,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,UAAU,EAAE,KAAK,iBAAiB,EAAE,CAAC;AAE9C,aAAK,OAAO,GAAG,aAAa,GAAG,iBAAiB,CAAC;AAIjD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,QAAQ,CAIvE;AACD;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,EAAE,CAErE;AACD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC3B,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,IAAI,EACnD,OAAO,CAAC,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,GACvC,MAAM,CAGR;AAED,OAAO,EACH,OAAO,IAAI,SAAS,EACpB,KAAK,SAAS,IAAI,kBAAkB,GACvC,MAAM,gBAAgB,CAAC;AAMxB,OAAO,KAAK,WAAW,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,CAAC;AAEvB,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAEzC,OAAO,EAAE,OAAO,EAAE,CAAC;AAEnB;;;;;GAKG;AACH,wBAAgB,SAAS,CACrB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,aAAa,GAAG,iBAAqC,GAC/D,IAAI,GAAG,IAAI,CAEb;AAED,OAAO,KAAK,QAAQ,MAAM,UAAU,CAAC;AAGrC,OAAO,EAAE,UAAU,IAAI,cAAc,EAAE,CAAC",
"names": [],
"sources": [
"https://raw.githubusercontent.com/fb55/htmlparser2/4763205746cd80120b5d2b69041197a394d24ba9/src/index.ts",
],
"version": 3,
}
`;

exports[`source-map-loader should process absolute URL in sourceRoot: warnings 1`] = `[]`;

exports[`source-map-loader should process css sourceMap: code 1`] = `
"* {
box-sizing: border-box; }
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/absolute-source-root.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/absolute-source-root.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions test/loader.test.js
Expand Up @@ -752,4 +752,17 @@ describe("source-map-loader", () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should process absolute URL in sourceRoot", async () => {
const testId = "absolute-source-root.js";
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);

expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot("map");
expect(codeFromBundle.code).toMatchSnapshot("code");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
});

0 comments on commit 625cc81

Please sign in to comment.