normalizeOptions.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Sets a constant default value for the property when it is undefined.
  3. * @template T
  4. * @template {keyof T} Property
  5. * @param {T} object An object.
  6. * @param {Property} property A property of the provided object.
  7. * @param {T[Property]} defaultValue The default value to set for the property.
  8. * @returns {T[Property]} The defaulted property value.
  9. */
  10. const d = (object, property, defaultValue) => {
  11. if (typeof object[property] === 'undefined' && typeof defaultValue !== 'undefined') {
  12. object[property] = defaultValue;
  13. }
  14. return object[property];
  15. };
  16. /**
  17. * Resolves the value for a nested object option.
  18. * @template T
  19. * @template {keyof T} Property
  20. * @template Result
  21. * @param {T} object An object.
  22. * @param {Property} property A property of the provided object.
  23. * @param {function(T | undefined): Result} fn The handler to resolve the property's value.
  24. * @returns {Result} The resolved option value.
  25. */
  26. const nestedOption = (object, property, fn) => {
  27. object[property] = fn(object[property]);
  28. return object[property];
  29. };
  30. /**
  31. * Normalizes the options for the plugin.
  32. * @param {import('../types').ReactRefreshPluginOptions} options Non-normalized plugin options.
  33. * @returns {import('../types').NormalizedPluginOptions} Normalized plugin options.
  34. */
  35. const normalizeOptions = (options) => {
  36. // Show deprecation notice for the `disableRefreshCheck` option and remove it
  37. if (typeof options.disableRefreshCheck !== 'undefined') {
  38. delete options.disableRefreshCheck;
  39. console.warn(
  40. [
  41. 'The "disableRefreshCheck" option has been deprecated and will not have any effect on how the plugin parses files.',
  42. 'Please remove it from your configuration.',
  43. ].join(' ')
  44. );
  45. }
  46. d(options, 'exclude', /node_modules/i);
  47. d(options, 'include', /\.([jt]sx?|flow)$/i);
  48. d(options, 'forceEnable');
  49. nestedOption(options, 'overlay', (overlay) => {
  50. /** @type {import('../types').NormalizedErrorOverlayOptions} */
  51. const defaults = {
  52. entry: require.resolve('../../client/ErrorOverlayEntry'),
  53. module: require.resolve('../../overlay'),
  54. sockIntegration: 'wds',
  55. };
  56. if (overlay === false) {
  57. return false;
  58. }
  59. if (typeof overlay === 'undefined' || overlay === true) {
  60. return defaults;
  61. }
  62. d(overlay, 'entry', defaults.entry);
  63. d(overlay, 'module', defaults.module);
  64. d(overlay, 'sockIntegration', defaults.sockIntegration);
  65. d(overlay, 'sockHost');
  66. d(overlay, 'sockPath');
  67. d(overlay, 'sockPort');
  68. d(options, 'useLegacyWDSSockets');
  69. return overlay;
  70. });
  71. return options;
  72. };
  73. module.exports = normalizeOptions;