get-sourcemap-asset-name.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. const sourceMapURL = require('source-map-url');
  9. const upath = require('upath');
  10. /**
  11. * If our bundled swDest file contains a sourcemap, we would invalidate that
  12. * mapping if we just replaced injectionPoint with the stringified manifest.
  13. * Instead, we need to update the swDest contents as well as the sourcemap
  14. * at the same time.
  15. *
  16. * See https://github.com/GoogleChrome/workbox/issues/2235
  17. *
  18. * @param {Object} compilation The current webpack compilation.
  19. * @param {string} swContents The contents of the swSrc file, which may or
  20. * may not include a valid sourcemap comment.
  21. * @param {string} swDest The configured swDest value.
  22. * @return {string|undefined} If the swContents contains a valid soucemap
  23. * comment pointing to an asset present in the compilation, this will return the
  24. * name of that asset. Otherwise, it will return undefined.
  25. *
  26. * @private
  27. */
  28. module.exports = (compilation, swContents, swDest) => {
  29. const url = sourceMapURL.getFrom(swContents);
  30. if (url) {
  31. // Translate the relative URL to what the presumed name for the webpack
  32. // asset should be.
  33. // This *might* not be a valid asset if the sourcemap URL that was found
  34. // was added by another module incidentally.
  35. // See https://github.com/GoogleChrome/workbox/issues/2250
  36. const swAssetDirname = upath.dirname(swDest);
  37. const sourcemapURLAssetName = upath.normalize(upath.join(swAssetDirname, url));
  38. if (sourcemapURLAssetName in compilation.assets) {
  39. return sourcemapURLAssetName;
  40. }
  41. }
  42. };