diff --git a/packages/workbox-build/package.json b/packages/workbox-build/package.json index 6a0517624..b5ee1d925 100644 --- a/packages/workbox-build/package.json +++ b/packages/workbox-build/package.json @@ -38,7 +38,6 @@ "rollup": "^2.43.1", "rollup-plugin-terser": "^7.0.0", "source-map": "^0.8.0-beta.0", - "source-map-url": "^0.4.0", "stringify-object": "^3.3.0", "strip-comments": "^2.0.1", "tempy": "^0.6.0", diff --git a/packages/workbox-build/src/inject-manifest.ts b/packages/workbox-build/src/inject-manifest.ts index 3a88e1b85..8c1a14a81 100644 --- a/packages/workbox-build/src/inject-manifest.ts +++ b/packages/workbox-build/src/inject-manifest.ts @@ -9,7 +9,6 @@ import {RawSourceMap} from 'source-map'; import assert from 'assert'; import fse from 'fs-extra'; -import sourceMapURL from 'source-map-url'; import stringify from 'fast-json-stable-stringify'; import upath from 'upath'; @@ -17,6 +16,7 @@ import {BuildResult} from './types'; import {errors} from './lib/errors'; import {escapeRegExp} from './lib/escape-regexp'; import {getFileManifestEntries} from './lib/get-file-manifest-entries'; +import {getSourceMapURL} from './lib/get-source-map-url'; import {rebasePath} from './lib/rebase-path'; import {replaceAndUpdateSourceMap} from './lib/replace-and-update-source-map'; import {translateURLToSourcemapPaths} from './lib/translate-url-to-sourcemap-paths'; @@ -164,7 +164,7 @@ export async function injectManifest(config: unknown): Promise { const manifestString = stringify(manifestEntries); const filesToWrite: {[key: string]: string} = {}; - const url = sourceMapURL.getFrom(swFileContents) as string; // eslint-disable-line + const url = getSourceMapURL(swFileContents); // See https://github.com/GoogleChrome/workbox/issues/2957 const {destPath, srcPath, warning} = translateURLToSourcemapPaths( url, diff --git a/packages/workbox-build/src/lib/get-source-map-url.ts b/packages/workbox-build/src/lib/get-source-map-url.ts new file mode 100644 index 000000000..6b0aee97b --- /dev/null +++ b/packages/workbox-build/src/lib/get-source-map-url.ts @@ -0,0 +1,32 @@ +/* + Copyright 2022 Google LLC + + Use of this source code is governed by an MIT-style + license that can be found in the LICENSE file or at + https://opensource.org/licenses/MIT. +*/ + +// Adapted from https://github.com/lydell/source-map-url/blob/master/source-map-url.js +// See https://github.com/GoogleChrome/workbox/issues/3019 +const innerRegex = /[#@] sourceMappingURL=([^\s'"]*)/; +const regex = RegExp( + '(?:' + + '/\\*' + + '(?:\\s*\r?\n(?://)?)?' + + '(?:' + + innerRegex.source + + ')' + + '\\s*' + + '\\*/' + + '|' + + '//(?:' + + innerRegex.source + + ')' + + ')' + + '\\s*', +); + +export function getSourceMapURL(srcContents: string): string | null { + const match = srcContents.match(regex); + return match ? match[1] || match[2] || '' : null; +} diff --git a/packages/workbox-build/src/lib/translate-url-to-sourcemap-paths.ts b/packages/workbox-build/src/lib/translate-url-to-sourcemap-paths.ts index ec5d37945..072eac40d 100644 --- a/packages/workbox-build/src/lib/translate-url-to-sourcemap-paths.ts +++ b/packages/workbox-build/src/lib/translate-url-to-sourcemap-paths.ts @@ -12,7 +12,7 @@ import upath from 'upath'; import {errors} from './errors'; export function translateURLToSourcemapPaths( - url: string, + url: string | null, swSrc: string, swDest: string, ): { diff --git a/packages/workbox-build/src/source-map-url.d.ts b/packages/workbox-build/src/source-map-url.d.ts deleted file mode 100644 index 8958463c8..000000000 --- a/packages/workbox-build/src/source-map-url.d.ts +++ /dev/null @@ -1 +0,0 @@ -declare module 'source-map-url'; diff --git a/packages/workbox-webpack-plugin/package.json b/packages/workbox-webpack-plugin/package.json index 3cc307770..b5a1f0fe9 100644 --- a/packages/workbox-webpack-plugin/package.json +++ b/packages/workbox-webpack-plugin/package.json @@ -23,7 +23,6 @@ "dependencies": { "fast-json-stable-stringify": "^2.1.0", "pretty-bytes": "^5.4.1", - "source-map-url": "^0.4.0", "upath": "^1.2.0", "webpack-sources": "^1.4.3", "workbox-build": "6.4.2" diff --git a/packages/workbox-webpack-plugin/src/lib/get-sourcemap-asset-name.ts b/packages/workbox-webpack-plugin/src/lib/get-sourcemap-asset-name.ts index 2c59d2aff..2a5e9006d 100644 --- a/packages/workbox-webpack-plugin/src/lib/get-sourcemap-asset-name.ts +++ b/packages/workbox-webpack-plugin/src/lib/get-sourcemap-asset-name.ts @@ -6,7 +6,7 @@ https://opensource.org/licenses/MIT. */ -import sourceMapURL from 'source-map-url'; +import {getSourceMapURL} from 'workbox-build/build/lib/get-source-map-url'; import upath from 'upath'; import type {Compilation} from 'webpack'; @@ -33,8 +33,7 @@ export function getSourcemapAssetName( swContents: string, swDest: string, ): string | undefined { - // eslint-disable-next-line - const url = sourceMapURL.getFrom(swContents); + const url = getSourceMapURL(swContents); if (url) { // Translate the relative URL to what the presumed name for the webpack // asset should be. diff --git a/packages/workbox-webpack-plugin/src/source-map-url.d.ts b/packages/workbox-webpack-plugin/src/source-map-url.d.ts deleted file mode 100644 index 8958463c8..000000000 --- a/packages/workbox-webpack-plugin/src/source-map-url.d.ts +++ /dev/null @@ -1 +0,0 @@ -declare module 'source-map-url';