Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Commit

Permalink
fix: remove webpack sourceURL (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Sep 22, 2020
1 parent 0d4624c commit eef2757
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/supportWebpack4.js
@@ -1,4 +1,8 @@
import { workerGenerator, sourceMappingURLRegex } from './utils';
import {
workerGenerator,
sourceMappingURLRegex,
sourceURLWebpackRegex,
} from './utils';

export default function runAsChild(
loaderContext,
Expand Down Expand Up @@ -30,6 +34,9 @@ export default function runAsChild(

// Remove `/* sourceMappingURL=url */` comment
workerSource = workerSource.replace(sourceMappingURLRegex, '');

// Remove `//# sourceURL=webpack-internal` comment
workerSource = workerSource.replace(sourceURLWebpackRegex, '');
}

const workerCode = workerGenerator(
Expand Down
9 changes: 8 additions & 1 deletion src/supportWebpack5.js
@@ -1,4 +1,8 @@
import { workerGenerator, sourceMappingURLRegex } from './utils';
import {
workerGenerator,
sourceMappingURLRegex,
sourceURLWebpackRegex,
} from './utils';

export default function runAsChild(
loaderContext,
Expand Down Expand Up @@ -45,6 +49,9 @@ export default function runAsChild(
if (options.inline === 'no-fallback') {
// Remove `/* sourceMappingURL=url */` comment
workerSource = workerSource.replace(sourceMappingURLRegex, '');

// Remove `//# sourceURL=webpack-internal` comment
workerSource = workerSource.replace(sourceURLWebpackRegex, '');
}

const workerCode = workerGenerator(
Expand Down
7 changes: 6 additions & 1 deletion src/utils.js
Expand Up @@ -87,7 +87,7 @@ ${
}

// Matches only the last occurrence of sourceMappingURL
const innerRegex = /\s*[#@]\s*sourceMappingURL\s*=\s*(.*?(?=[\s'"]|\\n|$)(?:\\n)?)\s*/;
const innerRegex = /\s*[#@]\s*sourceMappingURL\s*=\s*(.*?(?=[\s'"]|\\n|\*\/|$)(?:\\n)?)\s*/;

/* eslint-disable prefer-template */
const sourceMappingURLRegex = RegExp(
Expand All @@ -106,6 +106,10 @@ const sourceMappingURLRegex = RegExp(
')' +
'\\s*'
);

const sourceURLWebpackRegex = RegExp(
'\\/\\/#\\ssourceURL=webpack-internal:\\/\\/\\/(.*?)\\\\n'
);
/* eslint-enable prefer-template */

export {
Expand All @@ -114,4 +118,5 @@ export {
getExternalsType,
workerGenerator,
sourceMappingURLRegex,
sourceURLWebpackRegex,
};
55 changes: 55 additions & 0 deletions test/__snapshots__/sourceMapperRegexp.test.js.snap
@@ -0,0 +1,55 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`source-map-loader should work with "
with SourceMap
// #sourceMappingURL = /sample-source-map.map
// comment
" url: result 1`] = `
"
with SourceMap
REPLASED// comment
"
`;

exports[`source-map-loader should work with " // # sourceMappingURL = absolute-sourceRoot-source-map.map " url: result 1`] = `" REPLASED"`;

exports[`source-map-loader should work with " // #sourceMappingURL=absolute-sourceRoot-source-map.map" url: result 1`] = `" REPLASED"`;

exports[`source-map-loader should work with "/* #sourceMappingURL=absolute-sourceRoot-source-map.map */" url: result 1`] = `"REPLASED"`;

exports[`source-map-loader should work with "/*#sourceMappingURL=absolute-sourceRoot-source-map.map*/" url: result 1`] = `"REPLASED"`;

exports[`source-map-loader should work with "// #sourceMappingURL = //sampledomain.com/external-source-map2.map" url: result 1`] = `"REPLASED"`;

exports[`source-map-loader should work with "// #sourceMappingURL = http://sampledomain.com/external-source-map2.map" url: result 1`] = `"REPLASED"`;

exports[`source-map-loader should work with "// @sourceMappingURL=data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ==" url: result 1`] = `"REPLASED"`;

exports[`source-map-loader should work with "//#sourceMappingURL=absolute-sourceRoot-source-map.map" url: result 1`] = `"REPLASED"`;

exports[`source-map-loader should work with "//@sourceMappingURL=absolute-sourceRoot-source-map.map" url: result 1`] = `"REPLASED"`;

exports[`source-map-loader should work with "onmessage = function(event) {
const workerResult = event.data;
workerResult.onmessage = true;
postMessage(workerResult);
};
//# sourceURL=[module]
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vLi9iYXNpYy93b3JrZXIuanM/OGFiZiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBOztBQUVBOztBQUVBO0FBQ0EiLCJmaWxlIjoiLi9iYXNpYy93b3JrZXIuanMuanMiLCJzb3VyY2VzQ29udGVudCI6WyJvbm1lc3NhZ2UgPSBmdW5jdGlvbihldmVudCkge1xuICBjb25zdCB3b3JrZXJSZXN1bHQgPSBldmVudC5kYXRhO1xuXG4gIHdvcmtlclJlc3VsdC5vbm1lc3NhZ2UgPSB0cnVlO1xuXG4gIHBvc3RNZXNzYWdlKHdvcmtlclJlc3VsdCk7XG59O1xuIl0sInNvdXJjZVJvb3QiOiIifQ==
//# sourceURL=webpack-internal:///./basic/worker.js
" url: result 1`] = `
"onmessage = function(event) {
const workerResult = event.data;
workerResult.onmessage = true;
postMessage(workerResult);
};
//# sourceURL=[module]
REPLASED//# sourceURL=webpack-internal:///./basic/worker.js
"
`;
8 changes: 1 addition & 7 deletions test/inline-option.test.js
Expand Up @@ -84,13 +84,7 @@ describe('"inline" option', () => {
'sourceMappingURL=data:application/json;charset=utf-8;base64,'
) === -1
).toBe(true);
expect(sourceUrlInternalIndex >= 0).toBe(true);
expect(
moduleSource.lastIndexOf('//', sourceUrlInternalIndex) >
moduleSource.lastIndexOf('\\n', sourceUrlInternalIndex) ||
moduleSource.lastIndexOf('/*', sourceUrlInternalIndex) >
moduleSource.lastIndexOf('*/', sourceUrlInternalIndex)
).toBe(true);
expect(sourceUrlInternalIndex === -1).toBe(true);
expect(stats.compilation.assets['test.worker.js']).toBeUndefined();
expect(stats.compilation.assets['test.worker.js.map']).toBeUndefined();
expect(result).toMatchSnapshot('result');
Expand Down
31 changes: 31 additions & 0 deletions test/sourceMapperRegexp.test.js
@@ -0,0 +1,31 @@
import { sourceMappingURLRegex } from '../src/utils';

describe('source-map-loader', () => {
const cases = [
'/*#sourceMappingURL=absolute-sourceRoot-source-map.map*/',
'/* #sourceMappingURL=absolute-sourceRoot-source-map.map */',
'//#sourceMappingURL=absolute-sourceRoot-source-map.map',
'//@sourceMappingURL=absolute-sourceRoot-source-map.map',
' // #sourceMappingURL=absolute-sourceRoot-source-map.map',
' // # sourceMappingURL = absolute-sourceRoot-source-map.map ',
'// #sourceMappingURL = http://sampledomain.com/external-source-map2.map',
'// #sourceMappingURL = //sampledomain.com/external-source-map2.map',
'// @sourceMappingURL=data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ==',
`
with SourceMap
// #sourceMappingURL = /sample-source-map.map
// comment
`,
'onmessage = function(event) {\n const workerResult = event.data;\n\n workerResult.onmessage = true;\n\n postMessage(workerResult);\n};\n//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vLi9iYXNpYy93b3JrZXIuanM/OGFiZiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBOztBQUVBOztBQUVBO0FBQ0EiLCJmaWxlIjoiLi9iYXNpYy93b3JrZXIuanMuanMiLCJzb3VyY2VzQ29udGVudCI6WyJvbm1lc3NhZ2UgPSBmdW5jdGlvbihldmVudCkge1xuICBjb25zdCB3b3JrZXJSZXN1bHQgPSBldmVudC5kYXRhO1xuXG4gIHdvcmtlclJlc3VsdC5vbm1lc3NhZ2UgPSB0cnVlO1xuXG4gIHBvc3RNZXNzYWdlKHdvcmtlclJlc3VsdCk7XG59O1xuIl0sInNvdXJjZVJvb3QiOiIifQ==\n//# sourceURL=webpack-internal:///./basic/worker.js\n',
];

cases.forEach((item) => {
it(`should work with "${item}" url`, async () => {
const result = item.replace(sourceMappingURLRegex, 'REPLASED');

expect(result.indexOf('REPLASED') !== -1).toBe(true);
expect(result).toMatchSnapshot('result');
});
});
});

0 comments on commit eef2757

Please sign in to comment.