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

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