get-script-files-for-chunks.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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.getScriptFilesForChunks = void 0;
  13. const upath_1 = __importDefault(require("upath"));
  14. const resolve_webpack_url_1 = require("./resolve-webpack-url");
  15. function getScriptFilesForChunks(compilation, chunkNames) {
  16. var _a;
  17. const { chunks } = compilation.getStats().toJson({ chunks: true });
  18. const { publicPath } = compilation.options.output;
  19. const scriptFiles = new Set();
  20. for (const chunkName of chunkNames) {
  21. const chunk = chunks.find((chunk) => { var _a; return (_a = chunk.names) === null || _a === void 0 ? void 0 : _a.includes(chunkName); });
  22. if (chunk) {
  23. for (const file of (_a = chunk === null || chunk === void 0 ? void 0 : chunk.files) !== null && _a !== void 0 ? _a : []) {
  24. // See https://github.com/GoogleChrome/workbox/issues/2161
  25. if (upath_1.default.extname(file) === '.js') {
  26. scriptFiles.add((0, resolve_webpack_url_1.resolveWebpackURL)(publicPath, file));
  27. }
  28. }
  29. }
  30. else {
  31. compilation.warnings.push(new Error(`${chunkName} was provided to ` +
  32. `importScriptsViaChunks, but didn't match any named chunks.`));
  33. }
  34. }
  35. if (scriptFiles.size === 0) {
  36. compilation.warnings.push(new Error(`There were no assets matching ` +
  37. `importScriptsViaChunks: [${chunkNames.join(' ')}].`));
  38. }
  39. return Array.from(scriptFiles);
  40. }
  41. exports.getScriptFilesForChunks = getScriptFilesForChunks;