get-sourcemap-asset-name.js 2.2 KB

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