sanitize-config.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. /*
  3. Copyright 2018 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. /**
  9. * Helper method that sanitizes the config based on what
  10. * workbox-build.getManifest() expects.
  11. *
  12. * @param {Object} originalConfig
  13. * @return {Object} Sanitized config.
  14. * @private
  15. */
  16. function forGetManifest(originalConfig) {
  17. const propertiesToRemove = ['chunks', 'exclude', 'excludeChunks', 'importScripts', 'importWorkboxFrom', 'importsDirectory', 'include', 'precacheManifestFilename', 'swDest', 'swSrc', 'test'];
  18. return sanitizeConfig(originalConfig, propertiesToRemove);
  19. }
  20. /**
  21. * Helper method that sanitizes the config based on what
  22. * workbox-build.generateSWString() expects.
  23. *
  24. * @param {Object} originalConfig
  25. * @return {Object} Sanitized config.
  26. * @private
  27. */
  28. function forGenerateSWString(originalConfig) {
  29. const propertiesToRemove = ['chunks', 'exclude', 'excludeChunks', 'importWorkboxFrom', 'importsDirectory', 'include', 'precacheManifestFilename', 'swDest', 'test'];
  30. return sanitizeConfig(originalConfig, propertiesToRemove);
  31. }
  32. /**
  33. * Given a config object, make a shallow copy via Object.assign(), and remove
  34. * the properties from the copy that we know are webpack-plugin
  35. * specific, so that the remaining properties can be passed through to the
  36. * appropriate workbox-build method.
  37. *
  38. * @param {Object} originalConfig
  39. * @param {Array<string>} propertiesToRemove
  40. * @return {Object} A copy of config, sanitized.
  41. *
  42. * @private
  43. */
  44. function sanitizeConfig(originalConfig, propertiesToRemove) {
  45. const config = Object.assign({}, originalConfig);
  46. var _iteratorNormalCompletion = true;
  47. var _didIteratorError = false;
  48. var _iteratorError = undefined;
  49. try {
  50. for (var _iterator = propertiesToRemove[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  51. const property = _step.value;
  52. delete config[property];
  53. }
  54. } catch (err) {
  55. _didIteratorError = true;
  56. _iteratorError = err;
  57. } finally {
  58. try {
  59. if (!_iteratorNormalCompletion && _iterator.return != null) {
  60. _iterator.return();
  61. }
  62. } finally {
  63. if (_didIteratorError) {
  64. throw _iteratorError;
  65. }
  66. }
  67. }
  68. return config;
  69. }
  70. module.exports = {
  71. forGetManifest,
  72. forGenerateSWString
  73. };