| [9af201e] | 1 | /*
|
|---|
| 2 | Copyright 2019 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 | import {getSourceMapURL} from 'workbox-build/build/lib/get-source-map-url';
|
|---|
| 10 | import upath from 'upath';
|
|---|
| 11 | import type {Compilation} from 'webpack';
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * If our bundled swDest file contains a sourcemap, we would invalidate that
|
|---|
| 15 | * mapping if we just replaced injectionPoint with the stringified manifest.
|
|---|
| 16 | * Instead, we need to update the swDest contents as well as the sourcemap
|
|---|
| 17 | * at the same time.
|
|---|
| 18 | *
|
|---|
| 19 | * See https://github.com/GoogleChrome/workbox/issues/2235
|
|---|
| 20 | *
|
|---|
| 21 | * @param {Object} compilation The current webpack compilation.
|
|---|
| 22 | * @param {string} swContents The contents of the swSrc file, which may or
|
|---|
| 23 | * may not include a valid sourcemap comment.
|
|---|
| 24 | * @param {string} swDest The configured swDest value.
|
|---|
| 25 | * @return {string|undefined} If the swContents contains a valid sourcemap
|
|---|
| 26 | * comment pointing to an asset present in the compilation, this will return the
|
|---|
| 27 | * name of that asset. Otherwise, it will return undefined.
|
|---|
| 28 | *
|
|---|
| 29 | * @private
|
|---|
| 30 | */
|
|---|
| 31 | export function getSourcemapAssetName(
|
|---|
| 32 | compilation: Compilation,
|
|---|
| 33 | swContents: string,
|
|---|
| 34 | swDest: string,
|
|---|
| 35 | ): string | undefined {
|
|---|
| 36 | const url = getSourceMapURL(swContents);
|
|---|
| 37 | if (url) {
|
|---|
| 38 | // Translate the relative URL to what the presumed name for the webpack
|
|---|
| 39 | // asset should be.
|
|---|
| 40 | // This *might* not be a valid asset if the sourcemap URL that was found
|
|---|
| 41 | // was added by another module incidentally.
|
|---|
| 42 | // See https://github.com/GoogleChrome/workbox/issues/2250
|
|---|
| 43 | const swAssetDirname = upath.dirname(swDest);
|
|---|
| 44 | const sourcemapURLAssetName = upath.normalize(
|
|---|
| 45 | upath.join(swAssetDirname, url),
|
|---|
| 46 | );
|
|---|
| 47 |
|
|---|
| 48 | // Not sure if there's a better way to check for asset existence?
|
|---|
| 49 | if (compilation.getAsset(sourcemapURLAssetName)) {
|
|---|
| 50 | return sourcemapURLAssetName;
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 | return undefined;
|
|---|
| 54 | }
|
|---|