check-for-deprecated-options.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  4. /*
  5. Copyright 2019 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 ol = require('common-tags').oneLine;
  11. const checkURLCasing = options => {
  12. const oldToNewOptionNames = {
  13. dontCacheBustUrlsMatching: 'dontCacheBustURLsMatching',
  14. ignoreUrlParametersMatching: 'ignoreURLParametersMatching',
  15. modifyUrlPrefix: 'modifyURLPrefix',
  16. templatedUrls: 'templatedURLs'
  17. };
  18. const warnings = [];
  19. var _arr = Object.entries(oldToNewOptionNames);
  20. for (var _i = 0; _i < _arr.length; _i++) {
  21. const _arr$_i = (0, _slicedToArray2.default)(_arr[_i], 2),
  22. oldOption = _arr$_i[0],
  23. newOption = _arr$_i[1];
  24. if (oldOption in options) {
  25. warnings.push(ol`The '${oldOption}' option has been renamed to
  26. '${newOption}'. Please update your config. '${oldOption}' is now
  27. deprecated and will be removed in a future release of Workbox.`); // Rename the option so the config will be valid.
  28. options[newOption] = options[oldOption];
  29. delete options[oldOption];
  30. }
  31. }
  32. return warnings;
  33. };
  34. const checkStrategyClasses = options => {
  35. const oldToNewOptionValues = {
  36. cacheFirst: 'CacheFirst',
  37. cacheOnly: 'CacheOnly',
  38. networkFirst: 'NetworkFirst',
  39. networkOnly: 'NetworkOnly',
  40. staleWhileRevalidate: 'StaleWhileRevalidate'
  41. };
  42. const warnings = [];
  43. if (options.runtimeCaching) {
  44. var _iteratorNormalCompletion = true;
  45. var _didIteratorError = false;
  46. var _iteratorError = undefined;
  47. try {
  48. for (var _iterator = options.runtimeCaching[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  49. const entry = _step.value;
  50. if (typeof entry.handler === 'string' && oldToNewOptionValues.hasOwnProperty(entry.handler)) {
  51. const oldValue = entry.handler;
  52. const newValue = oldToNewOptionValues[entry.handler];
  53. warnings.push(ol`Specifying '${oldValue}'' in a
  54. 'runtimeCaching[].handler' option is deprecated. Please update your
  55. config to use '${newValue}' instead. In v4 Workbox strategies are now
  56. classes instead of functions.`); // Set the new value so the config will be valid.
  57. entry.handler = newValue;
  58. }
  59. }
  60. } catch (err) {
  61. _didIteratorError = true;
  62. _iteratorError = err;
  63. } finally {
  64. try {
  65. if (!_iteratorNormalCompletion && _iterator.return != null) {
  66. _iterator.return();
  67. }
  68. } finally {
  69. if (_didIteratorError) {
  70. throw _iteratorError;
  71. }
  72. }
  73. }
  74. }
  75. return warnings;
  76. };
  77. module.exports = options => {
  78. return [...checkURLCasing(options), ...checkStrategyClasses(options)];
  79. };