Skip to content

Commit

Permalink
Migrate off of source-map-url (#3031)
Browse files Browse the repository at this point in the history
* WIP

* getSourceMapURL in webpack plugin
  • Loading branch information
jeffposnick committed Feb 23, 2022
1 parent 55e8144 commit c57f076
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 10 deletions.
1 change: 0 additions & 1 deletion packages/workbox-build/package.json
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/workbox-build/src/inject-manifest.ts
Expand Up @@ -9,14 +9,14 @@
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';

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';
Expand Down Expand Up @@ -164,7 +164,7 @@ export async function injectManifest(config: unknown): Promise<BuildResult> {
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,
Expand Down
32 changes: 32 additions & 0 deletions 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;
}
Expand Up @@ -12,7 +12,7 @@ import upath from 'upath';
import {errors} from './errors';

export function translateURLToSourcemapPaths(
url: string,
url: string | null,
swSrc: string,
swDest: string,
): {
Expand Down
1 change: 0 additions & 1 deletion packages/workbox-build/src/source-map-url.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/workbox-webpack-plugin/package.json
Expand Up @@ -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"
Expand Down
Expand Up @@ -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';

Expand All @@ -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.
Expand Down
1 change: 0 additions & 1 deletion packages/workbox-webpack-plugin/src/source-map-url.d.ts

This file was deleted.

0 comments on commit c57f076

Please sign in to comment.