@dr.pogodin/react-utils 1.35.5 → 1.36.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/LICENSE.md +1 -1
  2. package/build/development/index.js +0 -6
  3. package/build/development/index.js.map +1 -1
  4. package/build/development/server/Cache.js +19 -19
  5. package/build/development/server/Cache.js.map +1 -1
  6. package/build/development/server/renderer.js +7 -4
  7. package/build/development/server/renderer.js.map +1 -1
  8. package/build/development/server/server.js +4 -1
  9. package/build/development/server/server.js.map +1 -1
  10. package/build/development/shared/components/Button/index.js +1 -16
  11. package/build/development/shared/components/Button/index.js.map +1 -1
  12. package/build/development/shared/components/Checkbox/index.js +1 -29
  13. package/build/development/shared/components/Checkbox/index.js.map +1 -1
  14. package/build/development/shared/components/GenericLink/index.js +0 -15
  15. package/build/development/shared/components/GenericLink/index.js.map +1 -1
  16. package/build/development/shared/components/Input/index.js +1 -8
  17. package/build/development/shared/components/Input/index.js.map +1 -1
  18. package/build/development/shared/components/MetaTags.js +0 -12
  19. package/build/development/shared/components/MetaTags.js.map +1 -1
  20. package/build/development/shared/components/Modal/index.js +2 -13
  21. package/build/development/shared/components/Modal/index.js.map +1 -1
  22. package/build/development/shared/components/PageLayout/index.js +1 -10
  23. package/build/development/shared/components/PageLayout/index.js.map +1 -1
  24. package/build/development/shared/components/TextArea/index.js +1 -12
  25. package/build/development/shared/components/TextArea/index.js.map +1 -1
  26. package/build/development/shared/components/Throbber/index.js +1 -6
  27. package/build/development/shared/components/Throbber/index.js.map +1 -1
  28. package/build/development/shared/components/WithTooltip/Tooltip.js +1 -8
  29. package/build/development/shared/components/WithTooltip/Tooltip.js.map +1 -1
  30. package/build/development/shared/components/WithTooltip/index.js +1 -9
  31. package/build/development/shared/components/WithTooltip/index.js.map +1 -1
  32. package/build/development/shared/components/YouTubeVideo/index.js +2 -11
  33. package/build/development/shared/components/YouTubeVideo/index.js.map +1 -1
  34. package/build/development/shared/components/selectors/CustomDropdown/Options/index.js +1 -16
  35. package/build/development/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
  36. package/build/development/shared/components/selectors/CustomDropdown/index.js +2 -12
  37. package/build/development/shared/components/selectors/CustomDropdown/index.js.map +1 -1
  38. package/build/development/shared/components/selectors/NativeDropdown/index.js +2 -12
  39. package/build/development/shared/components/selectors/NativeDropdown/index.js.map +1 -1
  40. package/build/development/shared/components/selectors/Switch/index.js +21 -28
  41. package/build/development/shared/components/selectors/Switch/index.js.map +1 -1
  42. package/build/development/shared/components/selectors/common.js +0 -19
  43. package/build/development/shared/components/selectors/common.js.map +1 -1
  44. package/build/development/shared/components/selectors/index.js +0 -13
  45. package/build/development/shared/components/selectors/index.js.map +1 -1
  46. package/build/development/shared/utils/jest/index.js +4 -2
  47. package/build/development/shared/utils/jest/index.js.map +1 -1
  48. package/build/development/shared/utils/splitComponent.js +11 -12
  49. package/build/development/shared/utils/splitComponent.js.map +1 -1
  50. package/build/development/web.bundle.js +26 -36
  51. package/build/production/index.js +1 -1
  52. package/build/production/index.js.map +1 -1
  53. package/build/production/server/Cache.js +3 -3
  54. package/build/production/server/Cache.js.map +1 -1
  55. package/build/production/server/renderer.js +2 -2
  56. package/build/production/server/renderer.js.map +1 -1
  57. package/build/production/server/server.js +1 -1
  58. package/build/production/server/server.js.map +1 -1
  59. package/build/production/shared/components/Button/index.js +3 -3
  60. package/build/production/shared/components/Button/index.js.map +1 -1
  61. package/build/production/shared/components/Checkbox/index.js +1 -16
  62. package/build/production/shared/components/Checkbox/index.js.map +1 -1
  63. package/build/production/shared/components/GenericLink/index.js +2 -2
  64. package/build/production/shared/components/GenericLink/index.js.map +1 -1
  65. package/build/production/shared/components/Input/index.js +2 -2
  66. package/build/production/shared/components/Input/index.js.map +1 -1
  67. package/build/production/shared/components/MetaTags.js +2 -2
  68. package/build/production/shared/components/MetaTags.js.map +1 -1
  69. package/build/production/shared/components/Modal/index.js +2 -2
  70. package/build/production/shared/components/Modal/index.js.map +1 -1
  71. package/build/production/shared/components/PageLayout/index.js +2 -2
  72. package/build/production/shared/components/PageLayout/index.js.map +1 -1
  73. package/build/production/shared/components/TextArea/index.js +2 -2
  74. package/build/production/shared/components/TextArea/index.js.map +1 -1
  75. package/build/production/shared/components/Throbber/index.js +2 -2
  76. package/build/production/shared/components/Throbber/index.js.map +1 -1
  77. package/build/production/shared/components/WithTooltip/Tooltip.js +3 -3
  78. package/build/production/shared/components/WithTooltip/Tooltip.js.map +1 -1
  79. package/build/production/shared/components/WithTooltip/index.js +2 -2
  80. package/build/production/shared/components/WithTooltip/index.js.map +1 -1
  81. package/build/production/shared/components/YouTubeVideo/index.js +3 -3
  82. package/build/production/shared/components/YouTubeVideo/index.js.map +1 -1
  83. package/build/production/shared/components/selectors/CustomDropdown/Options/index.js +2 -2
  84. package/build/production/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
  85. package/build/production/shared/components/selectors/CustomDropdown/index.js +2 -2
  86. package/build/production/shared/components/selectors/CustomDropdown/index.js.map +1 -1
  87. package/build/production/shared/components/selectors/NativeDropdown/index.js +3 -3
  88. package/build/production/shared/components/selectors/NativeDropdown/index.js.map +1 -1
  89. package/build/production/shared/components/selectors/Switch/index.js +1 -1
  90. package/build/production/shared/components/selectors/Switch/index.js.map +1 -1
  91. package/build/production/shared/components/selectors/common.js +2 -4
  92. package/build/production/shared/components/selectors/common.js.map +1 -1
  93. package/build/production/shared/components/selectors/index.js +1 -1
  94. package/build/production/shared/components/selectors/index.js.map +1 -1
  95. package/build/production/shared/utils/jest/index.js +1 -1
  96. package/build/production/shared/utils/jest/index.js.map +1 -1
  97. package/build/production/shared/utils/splitComponent.js +5 -5
  98. package/build/production/shared/utils/splitComponent.js.map +1 -1
  99. package/build/production/web.bundle.js +1 -1
  100. package/build/production/web.bundle.js.map +1 -1
  101. package/build/types-code/index.d.ts +1 -2
  102. package/build/types-code/server/Cache.d.ts +3 -11
  103. package/build/types-code/server/index.d.ts +0 -2
  104. package/build/types-code/server/renderer.d.ts +0 -1
  105. package/build/types-code/server/server.d.ts +0 -1
  106. package/build/types-code/shared/components/Button/index.d.ts +3 -4
  107. package/build/types-code/shared/components/Checkbox/index.d.ts +3 -12
  108. package/build/types-code/shared/components/Input/index.d.ts +4 -5
  109. package/build/types-code/shared/components/Link.d.ts +0 -1
  110. package/build/types-code/shared/components/Modal/index.d.ts +3 -4
  111. package/build/types-code/shared/components/NavLink.d.ts +0 -1
  112. package/build/types-code/shared/components/PageLayout/index.d.ts +4 -4
  113. package/build/types-code/shared/components/TextArea/index.d.ts +4 -5
  114. package/build/types-code/shared/components/Throbber/index.d.ts +4 -4
  115. package/build/types-code/shared/components/WithTooltip/Tooltip.d.ts +1 -1
  116. package/build/types-code/shared/components/WithTooltip/index.d.ts +2 -3
  117. package/build/types-code/shared/components/YouTubeVideo/index.d.ts +3 -4
  118. package/build/types-code/shared/components/selectors/CustomDropdown/Options/index.d.ts +1 -2
  119. package/build/types-code/shared/components/selectors/CustomDropdown/index.d.ts +2 -3
  120. package/build/types-code/shared/components/selectors/NativeDropdown/index.d.ts +2 -2
  121. package/build/types-code/shared/components/selectors/Switch/index.d.ts +4 -5
  122. package/build/types-code/shared/components/selectors/common.d.ts +4 -10
  123. package/build/types-code/shared/components/selectors/index.d.ts +1 -1
  124. package/build/types-code/shared/utils/globalState.d.ts +1 -2
  125. package/build/types-code/shared/utils/jest/index.d.ts +4 -1
  126. package/build/types-code/shared/utils/splitComponent.d.ts +0 -1
  127. package/build/types-code/shared/utils/webpack.d.ts +0 -1
  128. package/config/babel/node-ssr.js +1 -1
  129. package/config/babel/webpack.js +1 -1
  130. package/config/eslint/default.json +1 -0
  131. package/config/eslint/jest.json +1 -0
  132. package/config/eslint/typescript.js +7 -0
  133. package/config/typescript/base.json +9 -0
  134. package/config/webpack/app-base.d.ts +0 -1
  135. package/config/webpack/app-base.js +1 -1
  136. package/config/webpack/app-development.js +1 -1
  137. package/config/webpack/app-production.js +1 -1
  138. package/config/webpack/lib-base.js +1 -2
  139. package/config/webpack/lib-development.js +1 -1
  140. package/config/webpack/lib-production.js +1 -1
  141. package/package.json +16 -17
  142. package/src/index.ts +0 -1
  143. package/src/server/Cache.ts +31 -30
  144. package/src/server/renderer.tsx +8 -5
  145. package/src/server/server.ts +6 -1
  146. package/src/shared/components/Button/index.tsx +2 -20
  147. package/src/shared/components/Checkbox/index.tsx +2 -37
  148. package/src/shared/components/GenericLink/index.tsx +0 -15
  149. package/src/shared/components/Input/index.tsx +6 -15
  150. package/src/shared/components/MetaTags.tsx +0 -12
  151. package/src/shared/components/Modal/index.tsx +3 -22
  152. package/src/shared/components/PageLayout/index.tsx +8 -24
  153. package/src/shared/components/TextArea/index.tsx +6 -24
  154. package/src/shared/components/Throbber/index.tsx +6 -18
  155. package/src/shared/components/WithTooltip/Tooltip.tsx +6 -14
  156. package/src/shared/components/WithTooltip/index.tsx +3 -24
  157. package/src/shared/components/YouTubeVideo/index.tsx +3 -20
  158. package/src/shared/components/selectors/CustomDropdown/Options/index.tsx +2 -20
  159. package/src/shared/components/selectors/CustomDropdown/index.tsx +3 -27
  160. package/src/shared/components/selectors/NativeDropdown/index.tsx +3 -27
  161. package/src/shared/components/selectors/Switch/index.tsx +34 -50
  162. package/src/shared/components/selectors/common.ts +12 -44
  163. package/src/shared/components/selectors/index.ts +0 -2
  164. package/src/shared/utils/jest/index.tsx +13 -2
  165. package/src/shared/utils/splitComponent.tsx +14 -11
  166. package/tsconfig.configs.json +7 -3
  167. package/tsconfig.json +3 -10
  168. package/tsconfig.types.json +1 -7
  169. package/tstyche.config.json +6 -0
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["_mockdate","_interopRequireDefault","require","_react","_client","_react2","_global","global","IS_REACT_ACT_ENVIRONMENT","originalProcessVersions","process","versions","mockClientSide","Object","defineProperty","value","unmockClientSide","writable","getMockUuid","seed","x","toString","padStart","slice","mockTimer","time","mockdate","set","Date","now","jest","advanceTimersByTime","mount","scene","root","element","document","createElement","body","appendChild","res","destroy","act","unmount","remove","createRoot","render","snapshot","undefined","Error","nodes","asFragment","childNodes","expect","length","toMatchSnapshot"],"sources":["../../../../../src/shared/utils/jest/index.tsx"],"sourcesContent":["/* global jest, document */\n/* eslint-disable import/no-extraneous-dependencies */\n\nimport mockdate from 'mockdate';\nimport { type ReactNode, act } from 'react';\nimport { type Root, createRoot } from 'react-dom/client';\n\nimport { type RenderResult, render } from '@testing-library/react';\n\n/**\n * An alias for [act(..)](https://reactjs.org/docs/test-utils.html#act)\n * from `react`.\n */\nexport { act };\n\nexport { default as getGlobal } from './global';\n\ndeclare global {\n // eslint-disable-next-line no-var, vars-on-top\n var IS_REACT_ACT_ENVIRONMENT: boolean | undefined;\n}\n\nglobal.IS_REACT_ACT_ENVIRONMENT = true;\n\nconst originalProcessVersions = process.versions;\n\n/**\n * Tricks **react-utils** into thinking the test is running within client-side\n * (browser) environment.\n */\nexport function mockClientSide() {\n Object.defineProperty(process, 'versions', { value: null });\n}\n\n/**\n * Reverts the effect of {@link module:JU.mockClientSide mockClientSide(..)}.\n */\nexport function unmockClientSide() {\n Object.defineProperty(process, 'versions', {\n value: originalProcessVersions,\n writable: false,\n });\n}\n\n/**\n * Generates a mock UUID, or better said it determenistically transforms given\n * `seed` number into a UUID-formatted string.\n * @param {number} seed\n * @return {string}\n */\nexport function getMockUuid(seed = 0) {\n const x = seed.toString(16).padStart(32, '0');\n return `${x.slice(0, 8)}-${x.slice(8, 12)}-${x.slice(12, 16)}-${x.slice(16, 20)}-${x.slice(20)}`;\n}\n\n/**\n * Advances mock timers, and mock date by the specified time.\n * @param {number} time Time step [ms].\n * @returns {Promise} Wait for this to \"jump after\" any async code which should\n * be executed because of the mock time movement.\n */\nexport async function mockTimer(time: number) {\n mockdate.set(time + Date.now());\n jest.advanceTimersByTime(time);\n}\n\nexport type MountedSceneT = HTMLElement & {\n destroy: () => void;\n};\n\n/**\n * Mounts `scene` to the DOM, and returns the root scene element.\n * @param scene\n * @return Created container DOM element with destroy() function\n * attached.\n */\nexport function mount(scene: ReactNode): MountedSceneT {\n let root: Root;\n const element = document.createElement('div');\n document.body.appendChild(element);\n\n const res: MountedSceneT = (element as unknown) as MountedSceneT;\n res.destroy = () => {\n // NOTE: As it seems @testing-library may reset this flag to false\n // when it is simulating user events.\n global.IS_REACT_ACT_ENVIRONMENT = true;\n\n act(() => root.unmount());\n res.remove();\n };\n\n // NOTE: As it seems @testing-library may reset this flag to false\n // when it is simulating user events.\n global.IS_REACT_ACT_ENVIRONMENT = true;\n\n act(() => {\n root = createRoot(res);\n root.render(scene);\n });\n return res;\n}\n\nexport function snapshot(element: React.ReactElement) {\n let res: RenderResult | undefined;\n act(() => {\n res = render(element);\n });\n if (res === undefined) throw Error('Render failed');\n\n const nodes = res.asFragment().childNodes;\n expect(nodes.length > 1 ? [...nodes] : nodes[0]).toMatchSnapshot();\n return res;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAGA,IAAAA,SAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,OAAA,GAAAF,OAAA;AAEA,IAAAG,OAAA,GAAAH,OAAA;AAQA,IAAAI,OAAA,GAAAL,sBAAA,CAAAC,OAAA;AAfA;AACA;;AAQA;AACA;AACA;AACA;;AAUAK,MAAM,CAACC,wBAAwB,GAAG,IAAI;AAEtC,MAAMC,uBAAuB,GAAGC,OAAO,CAACC,QAAQ;;AAEhD;AACA;AACA;AACA;AACO,SAASC,cAAcA,CAAA,EAAG;EAC/BC,MAAM,CAACC,cAAc,CAACJ,OAAO,EAAE,UAAU,EAAE;IAAEK,KAAK,EAAE;EAAK,CAAC,CAAC;AAC7D;;AAEA;AACA;AACA;AACO,SAASC,gBAAgBA,CAAA,EAAG;EACjCH,MAAM,CAACC,cAAc,CAACJ,OAAO,EAAE,UAAU,EAAE;IACzCK,KAAK,EAAEN,uBAAuB;IAC9BQ,QAAQ,EAAE;EACZ,CAAC,CAAC;AACJ;;AAE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AW,kBAAU,EAACL,GAAG,CAAC;IACtBN,IAAI,CAACY,MAAM,CAACb,KAAK,CAAC;EACpB,CAAC,CAAC;EACF,OAAOO,GAAG;AACZ;AAEO,SAASO,QAAQA,CAACZ,OAA2B,EAAE;EACpD,IAAIK,GAA6B;EACjC,IAAAE,UAAG,EAAC,MAAM;IACRF,GAAG,GAAG,IAAAM,cAAM,EAACX,OAAO,CAAC;EACvB,CAAC,CAAC;EACF,IAAIK,GAAG,KAAKQ,SAAS,EAAE,MAAMC,KAAK,CAAC,eAAe,CAAC;EAEnD,MAAMC,KAAK,GAAGV,GAAG,CAACW,UAAU,CAAC,CAAC,CAACC,UAAU;EACzCC,MAAM,CAACH,KAAK,CAACI,MAAM,GAAG,CAAC,GAAG,CAAC,GAAGJ,KAAK,CAAC,GAAGA,KAAK,CAAC,CAAC,CAAC,CAAC,CAACK,eAAe,CAAC,CAAC;EAClE,OAAOf,GAAG;AACZ","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":["_mockdate","_interopRequireDefault","require","_react","_client","_react2","_global","global","IS_REACT_ACT_ENVIRONMENT","originalProcessVersions","process","versions","mockClientSide","Object","defineProperty","value","unmockClientSide","writable","getMockUuid","seed","x","toString","padStart","slice","mockTimer","time","mockdate","set","Date","now","jest","advanceTimersByTime","mount","scene","root","element","document","createElement","body","appendChild","res","destroy","act","unmount","remove","createRoot","render","snapshot","options","promise","await","undefined","Error","nodes","asFragment","childNodes","expect","length","toMatchSnapshot"],"sources":["../../../../../src/shared/utils/jest/index.tsx"],"sourcesContent":["/* global jest, document */\n/* eslint-disable import/no-extraneous-dependencies */\n\nimport mockdate from 'mockdate';\nimport { type ReactNode, act } from 'react';\nimport { type Root, createRoot } from 'react-dom/client';\n\nimport { type RenderResult, render } from '@testing-library/react';\n\n/**\n * An alias for [act(..)](https://reactjs.org/docs/test-utils.html#act)\n * from `react`.\n */\nexport { act };\n\nexport { default as getGlobal } from './global';\n\ndeclare global {\n // eslint-disable-next-line no-var, vars-on-top\n var IS_REACT_ACT_ENVIRONMENT: boolean | undefined;\n}\n\nglobal.IS_REACT_ACT_ENVIRONMENT = true;\n\nconst originalProcessVersions = process.versions;\n\n/**\n * Tricks **react-utils** into thinking the test is running within client-side\n * (browser) environment.\n */\nexport function mockClientSide() {\n Object.defineProperty(process, 'versions', { value: null });\n}\n\n/**\n * Reverts the effect of {@link module:JU.mockClientSide mockClientSide(..)}.\n */\nexport function unmockClientSide() {\n Object.defineProperty(process, 'versions', {\n value: originalProcessVersions,\n writable: false,\n });\n}\n\n/**\n * Generates a mock UUID, or better said it determenistically transforms given\n * `seed` number into a UUID-formatted string.\n * @param {number} seed\n * @return {string}\n */\nexport function getMockUuid(seed = 0) {\n const x = seed.toString(16).padStart(32, '0');\n return `${x.slice(0, 8)}-${x.slice(8, 12)}-${x.slice(12, 16)}-${x.slice(16, 20)}-${x.slice(20)}`;\n}\n\n/**\n * Advances mock timers, and mock date by the specified time.\n * @param {number} time Time step [ms].\n * @returns {Promise} Wait for this to \"jump after\" any async code which should\n * be executed because of the mock time movement.\n */\nexport async function mockTimer(time: number) {\n mockdate.set(time + Date.now());\n jest.advanceTimersByTime(time);\n}\n\nexport type MountedSceneT = HTMLElement & {\n destroy: () => void;\n};\n\n/**\n * Mounts `scene` to the DOM, and returns the root scene element.\n * @param scene\n * @return Created container DOM element with destroy() function\n * attached.\n */\nexport function mount(scene: ReactNode): MountedSceneT {\n let root: Root;\n const element = document.createElement('div');\n document.body.appendChild(element);\n\n const res: MountedSceneT = (element as unknown) as MountedSceneT;\n res.destroy = () => {\n // NOTE: As it seems @testing-library may reset this flag to false\n // when it is simulating user events.\n global.IS_REACT_ACT_ENVIRONMENT = true;\n\n act(() => root.unmount());\n res.remove();\n };\n\n // NOTE: As it seems @testing-library may reset this flag to false\n // when it is simulating user events.\n global.IS_REACT_ACT_ENVIRONMENT = true;\n\n act(() => {\n root = createRoot(res);\n root.render(scene);\n });\n return res;\n}\n\ntype SnapshotOptionsT = {\n await?: Promise<void>;\n};\n\nexport async function snapshot(\n element: React.ReactElement,\n options?: SnapshotOptionsT,\n) {\n let res: RenderResult | undefined;\n\n const promise = act(() => {\n res = render(element);\n return options?.await;\n });\n\n if (res === undefined) throw Error('Render failed');\n if (options?.await) await promise;\n\n const nodes = res.asFragment().childNodes;\n expect(nodes.length > 1 ? [...nodes] : nodes[0]).toMatchSnapshot();\n return res;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAGA,IAAAA,SAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,OAAA,GAAAF,OAAA;AAEA,IAAAG,OAAA,GAAAH,OAAA;AAQA,IAAAI,OAAA,GAAAL,sBAAA,CAAAC,OAAA;AAfA;AACA;;AAQA;AACA;AACA;AACA;;AAUAK,MAAM,CAACC,wBAAwB,GAAG,IAAI;AAEtC,MAAMC,uBAAuB,GAAGC,OAAO,CAACC,QAAQ;;AAEhD;AACA;AACA;AACA;AACO,SAASC,cAAcA,CAAA,EAAG;EAC/BC,MAAM,CAACC,cAAc,CAACJ,OAAO,EAAE,UAAU,EAAE;IAAEK,KAAK,EAAE;EAAK,CAAC,CAAC;AAC7D;;AAEA;AACA;AACA;AACO,SAASC,gBAAgBA,CAAA,EAAG;EACjCH,MAAM,CAACC,cAAc,CAACJ,OAAO,EAAE,UAAU,EAAE;IACzCK,KAAK,EAAEN,uBAAuB;IAC9BQ,QAAQ,EAAE;EACZ,CAAC,CAAC;AACJ;;AAEA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,WAAWA,CAACC,IAAI,GAAG,CAAC,EAAE;EACpC,MAAMC,CAAC,GAAGD,IAAI,CAACE,QAAQ,CAAC,EAAE,CAAC,CAACC,QAAQ,CAAC,EAAE,EAAE,GAAG,CAAC;EAC7C,OAAO,GAAGF,CAAC,CAACG,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAIH,CAAC,CAACG,KAAK,CAAC,CAAC,EAAE,EA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;IACrB,OAAOa,OAAO,EAAEE,KAAK;EACvB,CAAC,CAAC;EAEF,IAAIV,GAAG,KAAKW,SAAS,EAAE,MAAMC,KAAK,CAAC,eAAe,CAAC;EACnD,IAAIJ,OAAO,EAAEE,KAAK,EAAE,MAAMD,OAAO;EAEjC,MAAMI,KAAK,GAAGb,GAAG,CAACc,UAAU,CAAC,CAAC,CAACC,UAAU;EACzCC,MAAM,CAACH,KAAK,CAACI,MAAM,GAAG,CAAC,GAAG,CAAC,GAAGJ,KAAK,CAAC,GAAGA,KAAK,CAAC,CAAC,CAAC,CAAC,CAACK,eAAe,CAAC,CAAC;EAClE,OAAOlB,GAAG;AACZ","ignoreList":[]}
@@ -1,13 +1,11 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
3
  Object.defineProperty(exports, "__esModule", {
5
4
  value: true
6
5
  });
7
6
  exports.bookStyleSheets = bookStyleSheets;
8
7
  exports.default = splitComponent;
9
8
  exports.freeStyleSheets = freeStyleSheets;
10
- var _propTypes = _interopRequireDefault(require("prop-types"));
11
9
  var _react = require("react");
12
10
  var _jsUtils = require("@dr.pogodin/js-utils");
13
11
  var _globalState = require("./globalState");
@@ -72,9 +70,7 @@ function getLoadedStyleSheets() {
72
70
  styleSheets
73
71
  } = document;
74
72
  for (let i = 0; i < styleSheets.length; ++i) {
75
- const {
76
- href
77
- } = styleSheets[i];
73
+ const href = styleSheets[i]?.href;
78
74
  if (href) res.add(href);
79
75
  }
80
76
  return res;
@@ -96,10 +92,11 @@ function assertChunkName(chunkName, chunkGroups) {
96
92
  function bookStyleSheets(chunkName, chunkGroups, refCount) {
97
93
  const promises = [];
98
94
  const assets = chunkGroups[chunkName];
95
+ if (!assets) return Promise.resolve();
99
96
  const loadedSheets = getLoadedStyleSheets();
100
97
  for (let i = 0; i < assets.length; ++i) {
101
98
  const asset = assets[i];
102
- if (asset.endsWith('.css')) {
99
+ if (asset?.endsWith('.css')) {
103
100
  const promise = bookStyleSheet(asset, loadedSheets, refCount);
104
101
  if (promise) promises.push(promise);
105
102
  }
@@ -116,12 +113,17 @@ function bookStyleSheets(chunkName, chunkGroups, refCount) {
116
113
  */
117
114
  function freeStyleSheets(chunkName, chunkGroups) {
118
115
  const assets = chunkGroups[chunkName];
116
+ if (!assets) return;
119
117
  for (let i = 0; i < assets.length; ++i) {
120
118
  const asset = assets[i];
121
- if (asset.endsWith('.css')) {
119
+ if (asset?.endsWith('.css')) {
122
120
  const path = `${getPublicPath()}/${asset}`;
123
- if (--refCounts[path] <= 0) {
124
- document.head.querySelector(`link[href="${path}"]`).remove();
121
+ const pathRefCount = refCounts[path];
122
+ if (pathRefCount) {
123
+ if (pathRefCount <= 1) {
124
+ document.head.querySelector(`link[href="${path}"]`).remove();
125
+ delete refCounts[path];
126
+ } else refCounts[path] = pathRefCount - 1;
125
127
  }
126
128
  }
127
129
  }
@@ -202,9 +204,6 @@ function splitComponent({
202
204
  children: children
203
205
  })
204
206
  });
205
- CodeSplit.propTypes = {
206
- children: _propTypes.default.node
207
- };
208
207
  return CodeSplit;
209
208
  }
210
209
  //# sourceMappingURL=splitComponent.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"splitComponent.js","names":["_propTypes","_interopRequireDefault","require","_react","_jsUtils","_globalState","_isomorphy","_jsxRuntime","clientChunkGroups","IS_CLIENT_SIDE","default","CHUNK_GROUPS","refCounts","getPublicPath","getBuildInfo","publicPath","bookStyleSheet","name","loadedSheets","refCount","res","path","fullPath","document","location","origin","has","link","querySelector","createElement","setAttribute","head","appendChild","Barrier","addEventListener","resolve","current","getLoadedStyleSheets","Set","styleSheets","i","length","href","add","assertChunkName","chunkName","chunkGroups","Error","bookStyleSheets","promises","assets","asset","endsWith","promise","push","Promise","allSettled","then","freeStyleSheets","remove","usedChunkNames","splitComponent","getComponent","placeholder","LazyComponent","lazy","resolved","Component","Wrapper","forwardRef","children","rest","ref","IS_SERVER_SIDE","chunks","getSsrContext","includes","useInsertionEffect","jsx","CodeSplit","Suspense","fallback","propTypes","PT","node"],"sources":["../../../../src/shared/utils/splitComponent.tsx"],"sourcesContent":["/* eslint-disable react/jsx-props-no-spreading */\n/* global document */\n\nimport PT from 'prop-types';\n\nimport {\n type ComponentType,\n type ReactNode,\n forwardRef,\n lazy,\n Suspense,\n useInsertionEffect,\n} from 'react';\n\nimport { Barrier } from '@dr.pogodin/js-utils';\n\nimport { type ChunkGroupsT, getSsrContext } from './globalState';\n\nimport {\n IS_CLIENT_SIDE,\n IS_SERVER_SIDE,\n getBuildInfo,\n} from './isomorphy';\n\n// Note: At the client side we can get chunk groups immediately when loading\n// the module; at the server-side we only can get them within React render flow.\n// Thus, we set and use the following variable at the client-side, and then when\n// needed on the server side, we'll fetch it differently.\nlet clientChunkGroups: ChunkGroupsT;\n\nif (IS_CLIENT_SIDE) {\n // eslint-disable-next-line global-require\n clientChunkGroups = require('client/getInj').default().CHUNK_GROUPS || {};\n}\n\nconst refCounts: { [path: string]: number } = {};\n\nfunction getPublicPath() {\n return getBuildInfo().publicPath;\n}\n\n/**\n * Client-side only! Ensures the specified CSS stylesheet is loaded into\n * the document; loads if it is missing; and does simple reference counting\n * to facilitate future clean-up.\n * @param name\n * @param loadedSheets\n * @param refCount\n * @return\n */\nfunction bookStyleSheet(\n name: string,\n loadedSheets: Set<string>,\n refCount: boolean,\n): Promise<void> | undefined {\n let res: Barrier<void> | undefined;\n const path = `${getPublicPath()}/${name}`;\n const fullPath = `${document.location.origin}${path}`;\n\n if (!loadedSheets.has(fullPath)) {\n let link = document.querySelector(`link[href=\"${path}\"]`);\n\n if (!link) {\n link = document.createElement('link');\n link.setAttribute('rel', 'stylesheet');\n link.setAttribute('href', path);\n document.head.appendChild(link);\n }\n\n res = new Barrier<void>();\n link.addEventListener('load', () => res!.resolve());\n link.addEventListener('error', () => res!.resolve());\n }\n\n if (refCount) {\n const current = refCounts[path] || 0;\n refCounts[path] = 1 + current;\n }\n\n return res;\n}\n\n/**\n * Generates the set of URLs for currently loaded, linked stylesheets.\n * @return\n */\nfunction getLoadedStyleSheets(): Set<string> {\n const res = new Set<string>();\n const { styleSheets } = document;\n for (let i = 0; i < styleSheets.length; ++i) {\n const { href } = styleSheets[i];\n if (href) res.add(href);\n }\n return res;\n}\n\nfunction assertChunkName(\n chunkName: string,\n chunkGroups: ChunkGroupsT,\n) {\n if (chunkGroups[chunkName]) return;\n throw Error(`Unknown chunk name \"${chunkName}\"`);\n}\n\n/**\n * Client-side only! Ensures all CSS stylesheets required for the specified\n * code chunk are loaded into the document; loads the missing ones; and does\n * simple reference counting to facilitate future clean-up.\n * @param chunkName Chunk name.\n * @param refCount\n * @return Resolves once all pending stylesheets, necessary for\n * the chunk, are either loaded, or failed to load.\n */\nexport function bookStyleSheets(\n chunkName: string,\n chunkGroups: ChunkGroupsT,\n refCount: boolean,\n): Promise<void> {\n const promises = [];\n const assets = chunkGroups[chunkName];\n const loadedSheets = getLoadedStyleSheets();\n\n for (let i = 0; i < assets.length; ++i) {\n const asset = assets[i];\n if (asset.endsWith('.css')) {\n const promise = bookStyleSheet(asset, loadedSheets, refCount);\n if (promise) promises.push(promise);\n }\n }\n\n return promises.length\n ? Promise.allSettled(promises).then()\n : Promise.resolve();\n}\n\n/**\n * Client-side only! Frees from the document all CSS stylesheets that are\n * required by the specified chunk, and have reference counter equal to one\n * (for chunks with larger reference counter values, it just decrements\n * the reference counter).\n * @param {string} chunkName\n */\nexport function freeStyleSheets(\n chunkName: string,\n chunkGroups: ChunkGroupsT,\n) {\n const assets = chunkGroups[chunkName];\n for (let i = 0; i < assets.length; ++i) {\n const asset = assets[i];\n if (asset.endsWith('.css')) {\n const path = `${getPublicPath()}/${asset}`;\n if (--refCounts[path] <= 0) {\n document.head.querySelector(`link[href=\"${path}\"]`)!.remove();\n }\n }\n }\n}\n\n// Holds the set of chunk names already used for splitComponent() calls.\nconst usedChunkNames = new Set();\n\ntype ComponentOrModule<PropsT> = ComponentType<PropsT> | {\n default: ComponentType<PropsT>,\n};\n\n/**\n * Given an async component retrieval function `getComponent()` it creates\n * a special \"code split\" component, which uses <Suspense> to asynchronously\n * load on demand the code required by `getComponent()`.\n * @param options\n * @param options.chunkName\n * @param {function} options.getComponent\n * @param {React.Element} [options.placeholder]\n * @return {React.ElementType}\n */\nexport default function splitComponent<\n ComponentPropsT extends { children?: ReactNode },\n>({\n chunkName,\n getComponent,\n placeholder,\n}: {\n chunkName: string;\n getComponent: () => Promise<ComponentOrModule<ComponentPropsT>>,\n placeholder?: ReactNode,\n}) {\n // On the client side we can check right away if the chunk name is known.\n if (IS_CLIENT_SIDE) assertChunkName(chunkName, clientChunkGroups);\n\n // The correct usage of splitComponent() assumes a single call per chunk.\n if (usedChunkNames.has(chunkName)) {\n throw Error(`Repeated splitComponent() call for the chunk \"${chunkName}\"`);\n } else usedChunkNames.add(chunkName);\n\n const LazyComponent = lazy(async () => {\n const resolved = await getComponent();\n const Component = 'default' in resolved ? resolved.default : resolved;\n\n // This pre-loads necessary stylesheets prior to the first mount of\n // the component (the lazy load function is executed by React one at\n // the frist mount).\n if (IS_CLIENT_SIDE) {\n await bookStyleSheets(chunkName, clientChunkGroups, false);\n }\n\n const Wrapper = forwardRef((\n { children, ...rest }: ComponentPropsT,\n ref,\n ) => {\n // On the server side we'll assert the chunk name here,\n // and also push it to the SSR chunks array.\n if (IS_SERVER_SIDE) {\n const { chunkGroups, chunks } = getSsrContext()!;\n assertChunkName(chunkName, chunkGroups);\n if (!chunks.includes(chunkName)) chunks.push(chunkName);\n }\n\n // This takes care about stylesheets management every time an instance of\n // this component is mounted / unmounted.\n useInsertionEffect(() => {\n bookStyleSheets(chunkName, clientChunkGroups, true);\n return () => freeStyleSheets(chunkName, clientChunkGroups);\n }, []);\n\n return (\n <Component ref={ref} {...rest as ComponentPropsT}>\n {children}\n </Component>\n );\n });\n\n return { default: Wrapper };\n });\n\n const CodeSplit: React.FunctionComponent<ComponentPropsT> = ({\n children,\n ...rest\n }: ComponentPropsT) => (\n <Suspense fallback={placeholder}>\n <LazyComponent {...rest as Parameters<typeof LazyComponent>[0]}>\n {children}\n </LazyComponent>\n </Suspense>\n );\n\n CodeSplit.propTypes = {\n children: PT.node,\n } as PT.WeakValidationMap<ComponentPropsT>;\n\n return CodeSplit;\n}\n"],"mappings":";;;;;;;;;AAGA,IAAAA,UAAA,GAAAC,sBAAA,CAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAD,OAAA;AASA,IAAAE,QAAA,GAAAF,OAAA;AAEA,IAAAG,YAAA,GAAAH,OAAA;AAEA,IAAAI,UAAA,GAAAJ,OAAA;AAIqB,IAAAK,WAAA,GAAAL,OAAA;AAtBrB;AACA;;AAuBA;AACA;AACA;AACA;AACA,IAAIM,iBAA+B;AAEnC,IAAIC,yBAAc,EAAE;EAClB;EACAD,iBAAiB,GAAGN,OAAO,sBAAgB,CAAC,CAACQ,OAAO,CAAC,CAAC,CAACC,YAAY,IAAI,CAAC,CAAC;AAC3E;AAEA,MAAMC,SAAqC,GAAG,CAAC,CAAC;AAEhD,SAASC,aAAaA,CAAA,EAAG;EACvB,OAAO,IAAAC,uBAAY,EAAC,CAAC,CAACC,UAAU;AAClC;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASC,cAAcA,CACrBC,IAAY,EACZC,YAAyB,EACzBC,QAAiB,EACU;EAC3B,IAAIC,GAA8B;EAClC,MAAMC,IAAI,GAAG,GAAGR,aAAa,CAAC,CAAC,IAAII,IAAI,EAAE;EACzC,MAAMK,QAAQ,GAAG,GAAGC,QAAQ,CAACC,QAAQ,CAACC,MAAM,GAAGJ,IAAI,EAAE;EAErD,IAAI,CAACH,YAAY,CAACQ,GAAG,CAACJ,QAAQ,CAAC,EAAE;IAC/B,IAAIK,IAAI,GAAGJ,QAAQ,CAACK,aAAa,CAAC,cAAcP,IAAI,IAAI,CAAC;IAEzD,IAAI,CAACM,IAAI,EAAE;MACTA,IAAI,GAAGJ,QAAQ,CAACM,aAAa,CAAC,MAAM,CAAC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yBAAkB,EAAC,MAAM;QACvB7B,eAAe,CAACH,SAAS,EAAErC,iBAAiB,EAAE,IAAI,CAAC;QACnD,OAAO,MAAMkD,eAAe,CAACb,SAAS,EAAErC,iBAAiB,CAAC;MAC5D,CAAC,EAAE,EAAE,CAAC;MAEN,oBACE,IAAAD,WAAA,CAAAuE,GAAA,EAACX,SAAS;QAACK,GAAG,EAAEA,GAAI;QAAA,GAAKD,IAAI;QAAAD,QAAA,EAC1BA;MAAQ,CACA,CAAC;IAEhB,CAAC,CAAC;IAEF,OAAO;MAAE5D,OAAO,EAAE0D;IAAQ,CAAC;EAC7B,CAAC,CAAC;EAEF,MAAMW,SAAmD,GAAGA,CAAC;IAC3DT,QAAQ;IACR,GAAGC;EACY,CAAC,kBAChB,IAAAhE,WAAA,CAAAuE,GAAA,EAAC3E,MAAA,CAAA6E,QAAQ;IAACC,QAAQ,EAAElB,WAAY;IAAAO,QAAA,eAC9B,IAAA/D,WAAA,CAAAuE,GAAA,EAACd,aAAa;MAAA,GAAKO,IAAI;MAAAD,QAAA,EACpBA;IAAQ,CACI;EAAC,CACR,CACX;EAEDS,SAAS,CAACG,SAAS,GAAG;IACpBZ,QAAQ,EAAEa,kBAAE,CAACC;EACf,CAA0C;EAE1C,OAAOL,SAAS;AAClB","ignoreList":[]}
1
+ {"version":3,"file":"splitComponent.js","names":["_react","require","_jsUtils","_globalState","_isomorphy","_jsxRuntime","clientChunkGroups","IS_CLIENT_SIDE","default","CHUNK_GROUPS","refCounts","getPublicPath","getBuildInfo","publicPath","bookStyleSheet","name","loadedSheets","refCount","res","path","fullPath","document","location","origin","has","link","querySelector","createElement","setAttribute","head","appendChild","Barrier","addEventListener","resolve","current","getLoadedStyleSheets","Set","styleSheets","i","length","href","add","assertChunkName","chunkName","chunkGroups","Error","bookStyleSheets","promises","assets","Promise","asset","endsWith","promise","push","allSettled","then","freeStyleSheets","pathRefCount","remove","usedChunkNames","splitComponent","getComponent","placeholder","LazyComponent","lazy","resolved","Component","Wrapper","forwardRef","children","rest","ref","IS_SERVER_SIDE","chunks","getSsrContext","includes","useInsertionEffect","jsx","CodeSplit","Suspense","fallback"],"sources":["../../../../src/shared/utils/splitComponent.tsx"],"sourcesContent":["/* eslint-disable react/jsx-props-no-spreading */\n/* global document */\n\nimport {\n type ComponentType,\n type ReactNode,\n forwardRef,\n lazy,\n Suspense,\n useInsertionEffect,\n} from 'react';\n\nimport { Barrier } from '@dr.pogodin/js-utils';\n\nimport { type ChunkGroupsT, getSsrContext } from './globalState';\n\nimport {\n IS_CLIENT_SIDE,\n IS_SERVER_SIDE,\n getBuildInfo,\n} from './isomorphy';\n\n// Note: At the client side we can get chunk groups immediately when loading\n// the module; at the server-side we only can get them within React render flow.\n// Thus, we set and use the following variable at the client-side, and then when\n// needed on the server side, we'll fetch it differently.\nlet clientChunkGroups: ChunkGroupsT;\n\nif (IS_CLIENT_SIDE) {\n // eslint-disable-next-line global-require\n clientChunkGroups = require('client/getInj').default().CHUNK_GROUPS || {};\n}\n\nconst refCounts: { [path: string]: number } = {};\n\nfunction getPublicPath() {\n return getBuildInfo().publicPath;\n}\n\n/**\n * Client-side only! Ensures the specified CSS stylesheet is loaded into\n * the document; loads if it is missing; and does simple reference counting\n * to facilitate future clean-up.\n * @param name\n * @param loadedSheets\n * @param refCount\n * @return\n */\nfunction bookStyleSheet(\n name: string,\n loadedSheets: Set<string>,\n refCount: boolean,\n): Promise<void> | undefined {\n let res: Barrier<void> | undefined;\n const path = `${getPublicPath()}/${name}`;\n const fullPath = `${document.location.origin}${path}`;\n\n if (!loadedSheets.has(fullPath)) {\n let link = document.querySelector(`link[href=\"${path}\"]`);\n\n if (!link) {\n link = document.createElement('link');\n link.setAttribute('rel', 'stylesheet');\n link.setAttribute('href', path);\n document.head.appendChild(link);\n }\n\n res = new Barrier<void>();\n link.addEventListener('load', () => res!.resolve());\n link.addEventListener('error', () => res!.resolve());\n }\n\n if (refCount) {\n const current = refCounts[path] || 0;\n refCounts[path] = 1 + current;\n }\n\n return res;\n}\n\n/**\n * Generates the set of URLs for currently loaded, linked stylesheets.\n * @return\n */\nfunction getLoadedStyleSheets(): Set<string> {\n const res = new Set<string>();\n const { styleSheets } = document;\n for (let i = 0; i < styleSheets.length; ++i) {\n const href = styleSheets[i]?.href;\n if (href) res.add(href);\n }\n return res;\n}\n\nfunction assertChunkName(\n chunkName: string,\n chunkGroups: ChunkGroupsT,\n) {\n if (chunkGroups[chunkName]) return;\n throw Error(`Unknown chunk name \"${chunkName}\"`);\n}\n\n/**\n * Client-side only! Ensures all CSS stylesheets required for the specified\n * code chunk are loaded into the document; loads the missing ones; and does\n * simple reference counting to facilitate future clean-up.\n * @param chunkName Chunk name.\n * @param refCount\n * @return Resolves once all pending stylesheets, necessary for\n * the chunk, are either loaded, or failed to load.\n */\nexport function bookStyleSheets(\n chunkName: string,\n chunkGroups: ChunkGroupsT,\n refCount: boolean,\n): Promise<void> {\n const promises = [];\n const assets = chunkGroups[chunkName];\n if (!assets) return Promise.resolve();\n\n const loadedSheets = getLoadedStyleSheets();\n\n for (let i = 0; i < assets.length; ++i) {\n const asset = assets[i];\n if (asset?.endsWith('.css')) {\n const promise = bookStyleSheet(asset, loadedSheets, refCount);\n if (promise) promises.push(promise);\n }\n }\n\n return promises.length\n ? Promise.allSettled(promises).then()\n : Promise.resolve();\n}\n\n/**\n * Client-side only! Frees from the document all CSS stylesheets that are\n * required by the specified chunk, and have reference counter equal to one\n * (for chunks with larger reference counter values, it just decrements\n * the reference counter).\n * @param {string} chunkName\n */\nexport function freeStyleSheets(\n chunkName: string,\n chunkGroups: ChunkGroupsT,\n) {\n const assets = chunkGroups[chunkName];\n if (!assets) return;\n\n for (let i = 0; i < assets.length; ++i) {\n const asset = assets[i];\n if (asset?.endsWith('.css')) {\n const path = `${getPublicPath()}/${asset}`;\n\n const pathRefCount = refCounts[path];\n if (pathRefCount) {\n if (pathRefCount <= 1) {\n document.head.querySelector(`link[href=\"${path}\"]`)!.remove();\n delete refCounts[path];\n } else refCounts[path] = pathRefCount - 1;\n }\n }\n }\n}\n\n// Holds the set of chunk names already used for splitComponent() calls.\nconst usedChunkNames = new Set();\n\ntype ComponentOrModule<PropsT> = ComponentType<PropsT> | {\n default: ComponentType<PropsT>,\n};\n\n/**\n * Given an async component retrieval function `getComponent()` it creates\n * a special \"code split\" component, which uses <Suspense> to asynchronously\n * load on demand the code required by `getComponent()`.\n * @param options\n * @param options.chunkName\n * @param {function} options.getComponent\n * @param {React.Element} [options.placeholder]\n * @return {React.ElementType}\n */\nexport default function splitComponent<\n ComponentPropsT extends { children?: ReactNode },\n>({\n chunkName,\n getComponent,\n placeholder,\n}: {\n chunkName: string;\n getComponent: () => Promise<ComponentOrModule<ComponentPropsT>>,\n placeholder?: ReactNode,\n}) {\n // On the client side we can check right away if the chunk name is known.\n if (IS_CLIENT_SIDE) assertChunkName(chunkName, clientChunkGroups);\n\n // The correct usage of splitComponent() assumes a single call per chunk.\n if (usedChunkNames.has(chunkName)) {\n throw Error(`Repeated splitComponent() call for the chunk \"${chunkName}\"`);\n } else usedChunkNames.add(chunkName);\n\n const LazyComponent = lazy(async () => {\n const resolved = await getComponent();\n const Component = 'default' in resolved ? resolved.default : resolved;\n\n // This pre-loads necessary stylesheets prior to the first mount of\n // the component (the lazy load function is executed by React one at\n // the frist mount).\n if (IS_CLIENT_SIDE) {\n await bookStyleSheets(chunkName, clientChunkGroups, false);\n }\n\n const Wrapper = forwardRef((\n { children, ...rest }: ComponentPropsT,\n ref,\n ) => {\n // On the server side we'll assert the chunk name here,\n // and also push it to the SSR chunks array.\n if (IS_SERVER_SIDE) {\n const { chunkGroups, chunks } = getSsrContext()!;\n assertChunkName(chunkName, chunkGroups);\n if (!chunks.includes(chunkName)) chunks.push(chunkName);\n }\n\n // This takes care about stylesheets management every time an instance of\n // this component is mounted / unmounted.\n useInsertionEffect(() => {\n bookStyleSheets(chunkName, clientChunkGroups, true);\n return () => freeStyleSheets(chunkName, clientChunkGroups);\n }, []);\n\n return (\n <Component ref={ref} {...rest as ComponentPropsT}>\n {children}\n </Component>\n );\n });\n\n return { default: Wrapper };\n });\n\n const CodeSplit: React.FunctionComponent<ComponentPropsT> = ({\n children,\n ...rest\n }: ComponentPropsT) => (\n <Suspense fallback={placeholder}>\n <LazyComponent {...rest as Parameters<typeof LazyComponent>[0]}>\n {children}\n </LazyComponent>\n </Suspense>\n );\n\n return CodeSplit;\n}\n"],"mappings":";;;;;;;;AAGA,IAAAA,MAAA,GAAAC,OAAA;AASA,IAAAC,QAAA,GAAAD,OAAA;AAEA,IAAAE,YAAA,GAAAF,OAAA;AAEA,IAAAG,UAAA,GAAAH,OAAA;AAIqB,IAAAI,WAAA,GAAAJ,OAAA;AApBrB;AACA;;AAqBA;AACA;AACA;AACA;AACA,IAAIK,iBAA+B;AAEnC,IAAIC,yBAAc,EAAE;EAClB;EACAD,iBAAiB,GAAGL,OAAO,sBAAgB,CAAC,CAACO,OAAO,CAAC,CAAC,CAACC,YAAY,IAAI,CAAC,CAAC;AAC3E;AAEA,MAAMC,SAAqC,GAAG,CAAC,CAAC;AAEhD,SAASC,aAAaA,CAAA,EAAG;EACvB,OAAO,IAAAC,uBAAY,EAAC,CAAC,CAACC,UAAU;AAClC;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASC,cAAcA,CACrBC,IAAY,EACZC,YAAyB,EACzBC,QAAiB,EACU;EAC3B,IAAIC,GAA8B;EAClC,MAAMC,IAAI,GAAG,GAAGR,aAAa,CAAC,CAAC,IAAII,IAAI,EAAE;EACzC,MAAMK,QAAQ,GAAG,GAAGC,QAAQ,CAACC,QAAQ,CAACC,MAAM,GAAGJ,IAAI,EAAE;EAErD,IAAI,CAACH,YAAY,CAACQ,GAAG,CAACJ,QAAQ,CAAC,EAAE;IAC/B,IAAIK,IAAI,GAAGJ,QAAQ,CAACK,aAAa,CAAC,cAAcP,IAAI,IAAI,CAAC;IAEzD,IAAI,CAACM,IAAI,EAAE;MACTA,IAAI,GAAGJ,Q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