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

Commit eef2757

Browse files
fix: remove webpack sourceURL (#289)
1 parent 0d4624c commit eef2757

File tree

6 files changed

+109
-10
lines changed

6 files changed

+109
-10
lines changed

src/supportWebpack4.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { workerGenerator, sourceMappingURLRegex } from './utils';
1+
import {
2+
workerGenerator,
3+
sourceMappingURLRegex,
4+
sourceURLWebpackRegex,
5+
} from './utils';
26

37
export default function runAsChild(
48
loaderContext,
@@ -30,6 +34,9 @@ export default function runAsChild(
3034

3135
// Remove `/* sourceMappingURL=url */` comment
3236
workerSource = workerSource.replace(sourceMappingURLRegex, '');
37+
38+
// Remove `//# sourceURL=webpack-internal` comment
39+
workerSource = workerSource.replace(sourceURLWebpackRegex, '');
3340
}
3441

3542
const workerCode = workerGenerator(

src/supportWebpack5.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { workerGenerator, sourceMappingURLRegex } from './utils';
1+
import {
2+
workerGenerator,
3+
sourceMappingURLRegex,
4+
sourceURLWebpackRegex,
5+
} from './utils';
26

37
export default function runAsChild(
48
loaderContext,
@@ -45,6 +49,9 @@ export default function runAsChild(
4549
if (options.inline === 'no-fallback') {
4650
// Remove `/* sourceMappingURL=url */` comment
4751
workerSource = workerSource.replace(sourceMappingURLRegex, '');
52+
53+
// Remove `//# sourceURL=webpack-internal` comment
54+
workerSource = workerSource.replace(sourceURLWebpackRegex, '');
4855
}
4956

5057
const workerCode = workerGenerator(

src/utils.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ ${
8787
}
8888

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

9292
/* eslint-disable prefer-template */
9393
const sourceMappingURLRegex = RegExp(
@@ -106,6 +106,10 @@ const sourceMappingURLRegex = RegExp(
106106
')' +
107107
'\\s*'
108108
);
109+
110+
const sourceURLWebpackRegex = RegExp(
111+
'\\/\\/#\\ssourceURL=webpack-internal:\\/\\/\\/(.*?)\\\\n'
112+
);
109113
/* eslint-enable prefer-template */
110114

111115
export {
@@ -114,4 +118,5 @@ export {
114118
getExternalsType,
115119
workerGenerator,
116120
sourceMappingURLRegex,
121+
sourceURLWebpackRegex,
117122
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`source-map-loader should work with "
4+
with SourceMap
5+
6+
// #sourceMappingURL = /sample-source-map.map
7+
// comment
8+
" url: result 1`] = `
9+
"
10+
with SourceMap
11+
12+
REPLASED// comment
13+
"
14+
`;
15+
16+
exports[`source-map-loader should work with " // # sourceMappingURL = absolute-sourceRoot-source-map.map " url: result 1`] = `" REPLASED"`;
17+
18+
exports[`source-map-loader should work with " // #sourceMappingURL=absolute-sourceRoot-source-map.map" url: result 1`] = `" REPLASED"`;
19+
20+
exports[`source-map-loader should work with "/* #sourceMappingURL=absolute-sourceRoot-source-map.map */" url: result 1`] = `"REPLASED"`;
21+
22+
exports[`source-map-loader should work with "/*#sourceMappingURL=absolute-sourceRoot-source-map.map*/" url: result 1`] = `"REPLASED"`;
23+
24+
exports[`source-map-loader should work with "// #sourceMappingURL = //sampledomain.com/external-source-map2.map" url: result 1`] = `"REPLASED"`;
25+
26+
exports[`source-map-loader should work with "// #sourceMappingURL = http://sampledomain.com/external-source-map2.map" url: result 1`] = `"REPLASED"`;
27+
28+
exports[`source-map-loader should work with "// @sourceMappingURL=data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ==" url: result 1`] = `"REPLASED"`;
29+
30+
exports[`source-map-loader should work with "//#sourceMappingURL=absolute-sourceRoot-source-map.map" url: result 1`] = `"REPLASED"`;
31+
32+
exports[`source-map-loader should work with "//@sourceMappingURL=absolute-sourceRoot-source-map.map" url: result 1`] = `"REPLASED"`;
33+
34+
exports[`source-map-loader should work with "onmessage = function(event) {
35+
const workerResult = event.data;
36+
37+
workerResult.onmessage = true;
38+
39+
postMessage(workerResult);
40+
};
41+
//# sourceURL=[module]
42+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vLi9iYXNpYy93b3JrZXIuanM/OGFiZiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBOztBQUVBOztBQUVBO0FBQ0EiLCJmaWxlIjoiLi9iYXNpYy93b3JrZXIuanMuanMiLCJzb3VyY2VzQ29udGVudCI6WyJvbm1lc3NhZ2UgPSBmdW5jdGlvbihldmVudCkge1xuICBjb25zdCB3b3JrZXJSZXN1bHQgPSBldmVudC5kYXRhO1xuXG4gIHdvcmtlclJlc3VsdC5vbm1lc3NhZ2UgPSB0cnVlO1xuXG4gIHBvc3RNZXNzYWdlKHdvcmtlclJlc3VsdCk7XG59O1xuIl0sInNvdXJjZVJvb3QiOiIifQ==
43+
//# sourceURL=webpack-internal:///./basic/worker.js
44+
" url: result 1`] = `
45+
"onmessage = function(event) {
46+
const workerResult = event.data;
47+
48+
workerResult.onmessage = true;
49+
50+
postMessage(workerResult);
51+
};
52+
//# sourceURL=[module]
53+
REPLASED//# sourceURL=webpack-internal:///./basic/worker.js
54+
"
55+
`;

test/inline-option.test.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,7 @@ describe('"inline" option', () => {
8484
'sourceMappingURL=data:application/json;charset=utf-8;base64,'
8585
) === -1
8686
).toBe(true);
87-
expect(sourceUrlInternalIndex >= 0).toBe(true);
88-
expect(
89-
moduleSource.lastIndexOf('//', sourceUrlInternalIndex) >
90-
moduleSource.lastIndexOf('\\n', sourceUrlInternalIndex) ||
91-
moduleSource.lastIndexOf('/*', sourceUrlInternalIndex) >
92-
moduleSource.lastIndexOf('*/', sourceUrlInternalIndex)
93-
).toBe(true);
87+
expect(sourceUrlInternalIndex === -1).toBe(true);
9488
expect(stats.compilation.assets['test.worker.js']).toBeUndefined();
9589
expect(stats.compilation.assets['test.worker.js.map']).toBeUndefined();
9690
expect(result).toMatchSnapshot('result');

test/sourceMapperRegexp.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { sourceMappingURLRegex } from '../src/utils';
2+
3+
describe('source-map-loader', () => {
4+
const cases = [
5+
'/*#sourceMappingURL=absolute-sourceRoot-source-map.map*/',
6+
'/* #sourceMappingURL=absolute-sourceRoot-source-map.map */',
7+
'//#sourceMappingURL=absolute-sourceRoot-source-map.map',
8+
'//@sourceMappingURL=absolute-sourceRoot-source-map.map',
9+
' // #sourceMappingURL=absolute-sourceRoot-source-map.map',
10+
' // # sourceMappingURL = absolute-sourceRoot-source-map.map ',
11+
'// #sourceMappingURL = http://sampledomain.com/external-source-map2.map',
12+
'// #sourceMappingURL = //sampledomain.com/external-source-map2.map',
13+
'// @sourceMappingURL=data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ==',
14+
`
15+
with SourceMap
16+
17+
// #sourceMappingURL = /sample-source-map.map
18+
// comment
19+
`,
20+
'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',
21+
];
22+
23+
cases.forEach((item) => {
24+
it(`should work with "${item}" url`, async () => {
25+
const result = item.replace(sourceMappingURLRegex, 'REPLASED');
26+
27+
expect(result.indexOf('REPLASED') !== -1).toBe(true);
28+
expect(result).toMatchSnapshot('result');
29+
});
30+
});
31+
});

0 commit comments

Comments
 (0)