Skip to content

Commit c57f076

Browse files
authored
Migrate off of source-map-url (#3031)
* WIP * getSourceMapURL in webpack plugin
1 parent 55e8144 commit c57f076

File tree

8 files changed

+37
-10
lines changed

8 files changed

+37
-10
lines changed

packages/workbox-build/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"rollup": "^2.43.1",
3939
"rollup-plugin-terser": "^7.0.0",
4040
"source-map": "^0.8.0-beta.0",
41-
"source-map-url": "^0.4.0",
4241
"stringify-object": "^3.3.0",
4342
"strip-comments": "^2.0.1",
4443
"tempy": "^0.6.0",

packages/workbox-build/src/inject-manifest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import {RawSourceMap} from 'source-map';
1010
import assert from 'assert';
1111
import fse from 'fs-extra';
12-
import sourceMapURL from 'source-map-url';
1312
import stringify from 'fast-json-stable-stringify';
1413
import upath from 'upath';
1514

1615
import {BuildResult} from './types';
1716
import {errors} from './lib/errors';
1817
import {escapeRegExp} from './lib/escape-regexp';
1918
import {getFileManifestEntries} from './lib/get-file-manifest-entries';
19+
import {getSourceMapURL} from './lib/get-source-map-url';
2020
import {rebasePath} from './lib/rebase-path';
2121
import {replaceAndUpdateSourceMap} from './lib/replace-and-update-source-map';
2222
import {translateURLToSourcemapPaths} from './lib/translate-url-to-sourcemap-paths';
@@ -164,7 +164,7 @@ export async function injectManifest(config: unknown): Promise<BuildResult> {
164164
const manifestString = stringify(manifestEntries);
165165
const filesToWrite: {[key: string]: string} = {};
166166

167-
const url = sourceMapURL.getFrom(swFileContents) as string; // eslint-disable-line
167+
const url = getSourceMapURL(swFileContents);
168168
// See https://github.com/GoogleChrome/workbox/issues/2957
169169
const {destPath, srcPath, warning} = translateURLToSourcemapPaths(
170170
url,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2022 Google LLC
3+
4+
Use of this source code is governed by an MIT-style
5+
license that can be found in the LICENSE file or at
6+
https://opensource.org/licenses/MIT.
7+
*/
8+
9+
// Adapted from https://github.com/lydell/source-map-url/blob/master/source-map-url.js
10+
// See https://github.com/GoogleChrome/workbox/issues/3019
11+
const innerRegex = /[#@] sourceMappingURL=([^\s'"]*)/;
12+
const regex = RegExp(
13+
'(?:' +
14+
'/\\*' +
15+
'(?:\\s*\r?\n(?://)?)?' +
16+
'(?:' +
17+
innerRegex.source +
18+
')' +
19+
'\\s*' +
20+
'\\*/' +
21+
'|' +
22+
'//(?:' +
23+
innerRegex.source +
24+
')' +
25+
')' +
26+
'\\s*',
27+
);
28+
29+
export function getSourceMapURL(srcContents: string): string | null {
30+
const match = srcContents.match(regex);
31+
return match ? match[1] || match[2] || '' : null;
32+
}

packages/workbox-build/src/lib/translate-url-to-sourcemap-paths.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import upath from 'upath';
1212
import {errors} from './errors';
1313

1414
export function translateURLToSourcemapPaths(
15-
url: string,
15+
url: string | null,
1616
swSrc: string,
1717
swDest: string,
1818
): {

packages/workbox-build/src/source-map-url.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/workbox-webpack-plugin/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"dependencies": {
2424
"fast-json-stable-stringify": "^2.1.0",
2525
"pretty-bytes": "^5.4.1",
26-
"source-map-url": "^0.4.0",
2726
"upath": "^1.2.0",
2827
"webpack-sources": "^1.4.3",
2928
"workbox-build": "6.4.2"

packages/workbox-webpack-plugin/src/lib/get-sourcemap-asset-name.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
https://opensource.org/licenses/MIT.
77
*/
88

9-
import sourceMapURL from 'source-map-url';
9+
import {getSourceMapURL} from 'workbox-build/build/lib/get-source-map-url';
1010
import upath from 'upath';
1111
import type {Compilation} from 'webpack';
1212

@@ -33,8 +33,7 @@ export function getSourcemapAssetName(
3333
swContents: string,
3434
swDest: string,
3535
): string | undefined {
36-
// eslint-disable-next-line
37-
const url = sourceMapURL.getFrom(swContents);
36+
const url = getSourceMapURL(swContents);
3837
if (url) {
3938
// Translate the relative URL to what the presumed name for the webpack
4039
// asset should be.

packages/workbox-webpack-plugin/src/source-map-url.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)