get-workbox-sw-imports.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
  4. /*
  5. Copyright 2018 Google LLC
  6. Use of this source code is governed by an MIT-style
  7. license that can be found in the LICENSE file or at
  8. https://opensource.org/licenses/MIT.
  9. */
  10. const path = require('path');
  11. const _require = require('workbox-build'),
  12. copyWorkboxLibraries = _require.copyWorkboxLibraries,
  13. getModuleURL = _require.getModuleURL;
  14. /**
  15. * @param {Object} compilation The webpack compilation.
  16. * @param {Object} config The options passed to the plugin constructor.
  17. * - config.excludeChunks may be modified by this function if
  18. * config.importWorkboxFrom is set to a chunk name.
  19. * - config.modulePathPrefix may be modified by this function if
  20. * config.importWorkboxFrom is set to 'local'.
  21. * @return {Array<string>|null} A list of URLs to use to import the Workbox
  22. * runtime code, or null if importWorkboxFrom is 'disabled'.
  23. * @private
  24. */
  25. function getWorkboxSWImport(_x, _x2) {
  26. return _getWorkboxSWImport.apply(this, arguments);
  27. }
  28. function _getWorkboxSWImport() {
  29. _getWorkboxSWImport = (0, _asyncToGenerator2.default)(function* (compilation, config) {
  30. switch (config.importWorkboxFrom) {
  31. case 'cdn':
  32. {
  33. return [getModuleURL('workbox-sw')];
  34. }
  35. case 'local':
  36. {
  37. // This will create a local copy of the Workbox runtime libraries in
  38. // the output directory, independent of the webpack build pipeline.
  39. // In general, this should work, but one thing to keep in mind is that
  40. // when using the webpack-dev-server, the output will be created on
  41. // disk, rather than in the in-memory filesystem. (webpack-dev-server will
  42. // still be able to serve the runtime libraries from disk.)
  43. const wbDir = yield copyWorkboxLibraries(path.join(compilation.options.output.path, config.importsDirectory)); // We need to set this extra option in the config to ensure that the
  44. // workbox library loader knows where to get the local libraries from.
  45. config.modulePathPrefix = (compilation.options.output.publicPath || '') + path.join(config.importsDirectory, wbDir).split(path.sep).join('/');
  46. const workboxSWImport = config.modulePathPrefix + '/workbox-sw.js';
  47. return [workboxSWImport];
  48. }
  49. case 'disabled':
  50. {
  51. return null;
  52. }
  53. default:
  54. {
  55. // If importWorkboxFrom is anything else, then treat it as the name of
  56. // a webpack chunk that corresponds to the custom compilation of the
  57. // Workbox code.
  58. var _iteratorNormalCompletion = true;
  59. var _didIteratorError = false;
  60. var _iteratorError = undefined;
  61. try {
  62. for (var _iterator = compilation.chunks[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  63. const chunk = _step.value;
  64. // Make sure that we actually have a chunk with the appropriate name.
  65. if (chunk.name === config.importWorkboxFrom) {
  66. config.excludeChunks.push(chunk.name);
  67. return chunk.files.map(file => {
  68. return (compilation.options.output.publicPath || '') + file;
  69. });
  70. }
  71. } // If there's no chunk with the right name, treat it as a fatal error.
  72. } catch (err) {
  73. _didIteratorError = true;
  74. _iteratorError = err;
  75. } finally {
  76. try {
  77. if (!_iteratorNormalCompletion && _iterator.return != null) {
  78. _iterator.return();
  79. }
  80. } finally {
  81. if (_didIteratorError) {
  82. throw _iteratorError;
  83. }
  84. }
  85. }
  86. throw Error(`importWorkboxFrom was set to ` + `'${config.importWorkboxFrom}', which is not an existing chunk name.`);
  87. }
  88. }
  89. });
  90. return _getWorkboxSWImport.apply(this, arguments);
  91. }
  92. module.exports = getWorkboxSWImport;