8000 fix: logic when sourceRoot contains absolute URL · rgischk/source-map-loader@e724a1f · GitHub
[go: up one dir, main page]

Skip to content

Commit e724a1f

Browse files
fix: logic when sourceRoot contains absolute URL
1 parent 3094e6a commit e724a1f

File tree

6 files changed

+58
-4
lines changed
  • src
  • test
  • 6 files changed

    +58
    -4
    lines changed

    src/index.js

    Lines changed: 7 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -5,7 +5,12 @@
    55
    import path from "path";
    66

    77
    import schema from "./options.json";
    8-
    import { getSourceMappingURL, fetchFromURL, flattenSourceMap } from "./utils";
    8+
    import {
    9+
    getSourceMappingURL,
    10+
    fetchFromURL,
    11+
    flattenSourceMap,
    12+
    isURL,
    13+
    } from "./utils";
    914

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

    123128
    if (skipReading) {
    124129
    sourceContent = originalSourceContent;
    125-
    } else if (!errored && sourceURL) {
    130+
    } else if (!errored && sourceURL && !isURL(sourceURL)) {
    126131
    this.addDependency(sourceURL);
    127132
    }
    128133

    src/utils.js

    Lines changed: 14 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -96,6 +96,10 @@ function getSourceMappingURL(code) {
    9696
    }
    9797

    9898
    function getAbsolutePath(context, request, sourceRoot) {
    99+
    if (isURL(sourceRoot)) {
    100+
    return new URL(request, sourceRoot).toString();
    101+
    }
    102+
    99103
    if (sourceRoot) {
    100104
    if (path.isAbsolute(sourceRoot)) {
    101105
    return path.join(sourceRoot, request);
    @@ -125,6 +129,10 @@ function fetchFromDataURL(loaderContext, sourceURL) {
    125129
    async function fetchFromFilesystem(loaderContext, sourceURL) {
    126130
    let buffer;
    127131

    132+
    if (isURL(sourceURL)) {
    133+
    return { path: sourceURL };
    134+
    }
    135+
    128136
    try {
    129137
    buffer = await new Promise((resolve, reject) => {
    130138
    loaderContext.fs.readFile(sourceURL, (error, data) => {
    @@ -179,6 +187,10 @@ async function fetchPathsFromFilesystem(
    179187
    return result;
    180188
    }
    181189

    190+
    function isURL(value) {
    191+
    return /^[a-z][a-z0-9+.-]*:/i.test(value) && !path.win32.isAbsolute(value);
    192+
    }
    193+
    182194
    async function fetchFromURL(
    183195
    loaderContext,
    184196
    context,
    @@ -187,7 +199,7 @@ async function fetchFromURL(
    187199
    skipReading = false
    188200
    ) {
    189201
    // 1. It's an absolute url and it is not `windows` path like `C:\dir\file`
    190-
    if (/^[a-z][a-z0-9+.-]*:/i.test(url) && !path.win32.isAbsolute(url)) {
    202+
    if (isURL(url)) {
    191203
    const { protocol } = urlUtils.parse(url);
    192204

    193205
    if (protocol === "data:") {
    @@ -268,4 +280,4 @@ async function fetchFromURL(
    268280
    return { sourceURL, sourceContent };
    269281
    }
    270282

    271-
    export { getSourceMappingURL, fetchFromURL, flattenSourceMap };
    283+
    export { getSourceMappingURL, fetchFromURL, flattenSourceMap, isURL };

    test/__snapshots__/loader.test.js.snap

    Lines changed: 21 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -113,6 +113,27 @@ exports[`source-map-loader should leave normal files with fake source-map untouc
    113113

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

    116+
    exports[`source-map-loader should process absolute URL in sourceRoot: code 1`] = `
    117+
    "const foo = "test";
    118+
    "
    119+
    `;
    120+
    121+
    exports[`source-map-loader should process absolute URL in sourceRoot: errors 1`] = `[]`;
    122+
    123+
    exports[`source-map-loader should process absolute URL in sourceRoot: map 1`] = `
    124+
    {
    125+
    "file": "index.d.ts",
    126+
    "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",
    127+
    "names": [],
    128+
    "sources": [
    129+
    "https://raw.githubusercontent.com/fb55/htmlparser2/4763205746cd80120b5d2b69041197a394d24ba9/src/index.ts",
    130+
    ],
    131+
    "version": 3,
    132+
    }
    133+
    `;
    134+
    135+
    exports[`source-map-loader should process absolute URL in sourceRoot: warnings 1`] = `[]`;
    136+
    116137
    exports[`source-map-loader should process css sourceMap: code 1`] = `
    117138
    "* {
    118139
    box-sizing: border-box; }

    test/fixtures/absolute-source-root.js

    Lines changed: 2 additions & 0 deletions
    Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

    test/fixtures/absolute-source-root.js.map

    Lines changed: 1 addition & 0 deletions
    Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

    test/loader.test.js

    Lines changed: 13 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -752,4 +752,17 @@ describe("source-map-loader", () => {
    752752
    expect(getWarnings(stats)).toMatchSnapshot("warnings");
    753753
    expect(getErrors(stats)).toMatchSnapshot("errors");
    754754
    });
    755+
    756+
    it("should process absolute URL in sourceRoot", async () => {
    757+
    const testId = "absolute-source-root.js";
    758+
    const compiler = getCompiler(testId);
    759+
    const stats = await compile(compiler);
    760+
    const codeFromBundle = getCodeFromBundle(stats, compiler);
    761+
    762+
    expect(codeFromBundle.map).toBeDefined();
    763+
    expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot("map");
    764+
    expect(codeFromBundle.code).toMatchSnapshot("code");
    765+
    expect(getWarnings(stats)).toMatchSnapshot("warnings");
    766+
    expect(getErrors(stats)).toMatchSnapshot("errors");
    767+
    });
    755768
    });

    0 commit comments

    Comments
     (0)
    0