dynamic.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "client";
  2. "use strict";
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = dynamic;
  7. exports.noSSR = noSSR;
  8. var _extends = require("@swc/helpers/lib/_extends.js").default;
  9. var _interop_require_default = require("@swc/helpers/lib/_interop_require_default.js").default;
  10. var _react = _interop_require_default(require("react"));
  11. var _loadable = _interop_require_default(require("./loadable"));
  12. function dynamic(dynamicOptions, options) {
  13. let loadableFn = _loadable.default;
  14. let loadableOptions = (options == null ? void 0 : options.suspense) ? {} : {
  15. // A loading component is not required, so we default it
  16. loading: ({ error , isLoading , pastDelay })=>{
  17. if (!pastDelay) return null;
  18. if (process.env.NODE_ENV === 'development') {
  19. if (isLoading) {
  20. return null;
  21. }
  22. if (error) {
  23. return /*#__PURE__*/ _react.default.createElement("p", null, error.message, /*#__PURE__*/ _react.default.createElement("br", null), error.stack);
  24. }
  25. }
  26. return null;
  27. }
  28. };
  29. // Support for direct import(), eg: dynamic(import('../hello-world'))
  30. // Note that this is only kept for the edge case where someone is passing in a promise as first argument
  31. // The react-loadable babel plugin will turn dynamic(import('../hello-world')) into dynamic(() => import('../hello-world'))
  32. // To make sure we don't execute the import without rendering first
  33. if (dynamicOptions instanceof Promise) {
  34. loadableOptions.loader = ()=>dynamicOptions;
  35. // Support for having import as a function, eg: dynamic(() => import('../hello-world'))
  36. } else if (typeof dynamicOptions === 'function') {
  37. loadableOptions.loader = dynamicOptions;
  38. // Support for having first argument being options, eg: dynamic({loader: import('../hello-world')})
  39. } else if (typeof dynamicOptions === 'object') {
  40. loadableOptions = _extends({}, loadableOptions, dynamicOptions);
  41. }
  42. // Support for passing options, eg: dynamic(import('../hello-world'), {loading: () => <p>Loading something</p>})
  43. loadableOptions = _extends({}, loadableOptions, options);
  44. // Error if Fizz rendering is not enabled and `suspense` option is set to true
  45. if (!process.env.__NEXT_REACT_ROOT && loadableOptions.suspense) {
  46. throw new Error(`Invalid suspense option usage in next/dynamic. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense`);
  47. }
  48. if (loadableOptions.suspense) {
  49. if (process.env.NODE_ENV !== 'production') {
  50. /**
  51. * TODO: Currently, next/dynamic will opt-in to React.lazy if { suspense: true } is used
  52. * React 18 will always resolve the Suspense boundary on the server-side, effectively ignoring the ssr option
  53. *
  54. * In the future, when React Suspense with third-party libraries is stable, we can implement a custom version of
  55. * React.lazy that can suspense on the server-side while only loading the component on the client-side
  56. */ if (loadableOptions.ssr === false) {
  57. console.warn(`"ssr: false" is ignored by next/dynamic because you can not enable "suspense" while disabling "ssr" at the same time. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense`);
  58. }
  59. if (loadableOptions.loading != null) {
  60. console.warn(`"loading" is ignored by next/dynamic because you have enabled "suspense". Place your loading element in your suspense boundary's "fallback" prop instead. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense`);
  61. }
  62. }
  63. delete loadableOptions.ssr;
  64. delete loadableOptions.loading;
  65. }
  66. // coming from build/babel/plugins/react-loadable-plugin.js
  67. if (loadableOptions.loadableGenerated) {
  68. loadableOptions = _extends({}, loadableOptions, loadableOptions.loadableGenerated);
  69. delete loadableOptions.loadableGenerated;
  70. }
  71. // support for disabling server side rendering, eg: dynamic(import('../hello-world'), {ssr: false}).
  72. // skip `ssr` for suspense mode and opt-in React.lazy directly
  73. if (typeof loadableOptions.ssr === 'boolean' && !loadableOptions.suspense) {
  74. if (!loadableOptions.ssr) {
  75. delete loadableOptions.ssr;
  76. return noSSR(loadableFn, loadableOptions);
  77. }
  78. delete loadableOptions.ssr;
  79. }
  80. return loadableFn(loadableOptions);
  81. }
  82. 'client';
  83. const isServerSide = typeof window === 'undefined';
  84. function noSSR(LoadableInitializer, loadableOptions) {
  85. // Removing webpack and modules means react-loadable won't try preloading
  86. delete loadableOptions.webpack;
  87. delete loadableOptions.modules;
  88. // This check is necessary to prevent react-loadable from initializing on the server
  89. if (!isServerSide) {
  90. return LoadableInitializer(loadableOptions);
  91. }
  92. const Loading = loadableOptions.loading;
  93. // This will only be rendered on the server side
  94. return ()=>/*#__PURE__*/ _react.default.createElement(Loading, {
  95. error: null,
  96. isLoading: true,
  97. pastDelay: false,
  98. timedOut: false
  99. });
  100. }
  101. if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
  102. Object.defineProperty(exports.default, '__esModule', { value: true });
  103. Object.assign(exports.default, exports);
  104. module.exports = exports.default;
  105. }
  106. //# sourceMappingURL=dynamic.js.map