From 27a985114ce24cd5b2956e34f5ca46ad976feec9 Mon Sep 17 00:00:00 2001 From: cap-Bernardito Date: Tue, 22 Sep 2020 15:53:31 +0300 Subject: [PATCH] fix: remove webpack sourceURL --- src/supportWebpack4.js | 9 ++- src/supportWebpack5.js | 9 ++- src/utils.js | 7 ++- .../sourceMapperRegexp.test.js.snap | 55 +++++++++++++++++++ test/inline-option.test.js | 8 +-- test/sourceMapperRegexp.test.js | 31 +++++++++++ 6 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 test/__snapshots__/sourceMapperRegexp.test.js.snap create mode 100644 test/sourceMapperRegexp.test.js diff --git a/src/supportWebpack4.js b/src/supportWebpack4.js index fec90d4..508beeb 100644 --- a/src/supportWebpack4.js +++ b/src/supportWebpack4.js @@ -1,4 +1,8 @@ -import { workerGenerator, sourceMappingURLRegex } from './utils'; +import { + workerGenerator, + sourceMappingURLRegex, + sourceURLWebpackRegex, +} from './utils'; export default function runAsChild( loaderContext, @@ -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( diff --git a/src/supportWebpack5.js b/src/supportWebpack5.js index 6c6f910..c2a6725 100644 --- a/src/supportWebpack5.js +++ b/src/supportWebpack5.js @@ -1,4 +1,8 @@ -import { workerGenerator, sourceMappingURLRegex } from './utils'; +import { + workerGenerator, + sourceMappingURLRegex, + sourceURLWebpackRegex, +} from './utils'; export default function runAsChild( loaderContext, @@ -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( diff --git a/src/utils.js b/src/utils.js index 8db6af8..7a7a034 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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( @@ -106,6 +106,10 @@ const sourceMappingURLRegex = RegExp( ')' + '\\s*' ); + +const sourceURLWebpackRegex = RegExp( + '\\/\\/#\\ssourceURL=webpack-internal:\\/\\/\\/(.*?)\\\\n' +); /* eslint-enable prefer-template */ export { @@ -114,4 +118,5 @@ export { getExternalsType, workerGenerator, sourceMappingURLRegex, + sourceURLWebpackRegex, }; diff --git a/test/__snapshots__/sourceMapperRegexp.test.js.snap b/test/__snapshots__/sourceMapperRegexp.test.js.snap new file mode 100644 index 0000000..2d301e2 --- /dev/null +++ b/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 +" +`; diff --git a/test/inline-option.test.js b/test/inline-option.test.js index 3b3b436..1c5a0f0 100644 --- a/test/inline-option.test.js +++ b/test/inline-option.test.js @@ -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'); diff --git a/test/sourceMapperRegexp.test.js b/test/sourceMapperRegexp.test.js new file mode 100644 index 0000000..ed3188a --- /dev/null +++ b/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'); + }); + }); +});