@dr.pogodin/react-utils 1.42.0 → 1.42.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.
- package/babel.config.js +3 -0
- package/bin/build.js +5 -8
- package/bin/setup.js +2 -1
- package/build/development/client/getInj.js +7 -2
- package/build/development/client/getInj.js.map +1 -1
- package/build/development/client/index.js +1 -2
- package/build/development/client/index.js.map +1 -1
- package/build/development/client/init.js +16 -13
- package/build/development/client/init.js.map +1 -1
- package/build/development/index.js +4 -1
- package/build/development/index.js.map +1 -1
- package/build/development/server/Cache.js +5 -9
- package/build/development/server/Cache.js.map +1 -1
- package/build/development/server/index.js +14 -11
- package/build/development/server/index.js.map +1 -1
- package/build/development/server/renderer.js +36 -40
- package/build/development/server/renderer.js.map +1 -1
- package/build/development/server/server.js +19 -12
- package/build/development/server/server.js.map +1 -1
- package/build/development/server/utils/errors.js.map +1 -1
- package/build/development/shared/components/Button/index.js +2 -3
- package/build/development/shared/components/Button/index.js.map +1 -1
- package/build/development/shared/components/Checkbox/index.js +3 -1
- package/build/development/shared/components/Checkbox/index.js.map +1 -1
- package/build/development/shared/components/GenericLink/index.js +13 -6
- package/build/development/shared/components/GenericLink/index.js.map +1 -1
- package/build/development/shared/components/Input/index.js +5 -1
- package/build/development/shared/components/Input/index.js.map +1 -1
- package/build/development/shared/components/Link.js +5 -6
- package/build/development/shared/components/Link.js.map +1 -1
- package/build/development/shared/components/MetaTags.js +31 -24
- package/build/development/shared/components/MetaTags.js.map +1 -1
- package/build/development/shared/components/Modal/index.js +13 -6
- package/build/development/shared/components/Modal/index.js.map +1 -1
- package/build/development/shared/components/NavLink.js +6 -6
- package/build/development/shared/components/NavLink.js.map +1 -1
- package/build/development/shared/components/TextArea/index.js +6 -3
- package/build/development/shared/components/TextArea/index.js.map +1 -1
- package/build/development/shared/components/WithTooltip/Tooltip.js +35 -39
- package/build/development/shared/components/WithTooltip/Tooltip.js.map +1 -1
- package/build/development/shared/components/WithTooltip/index.js +27 -21
- package/build/development/shared/components/WithTooltip/index.js.map +1 -1
- package/build/development/shared/components/YouTubeVideo/index.js +4 -3
- package/build/development/shared/components/YouTubeVideo/index.js.map +1 -1
- package/build/development/shared/components/selectors/CustomDropdown/Options/index.js +4 -5
- package/build/development/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
- package/build/development/shared/components/selectors/CustomDropdown/index.js +7 -9
- package/build/development/shared/components/selectors/CustomDropdown/index.js.map +1 -1
- package/build/development/shared/components/selectors/NativeDropdown/index.js +3 -5
- package/build/development/shared/components/selectors/NativeDropdown/index.js.map +1 -1
- package/build/development/shared/components/selectors/Switch/index.js +21 -20
- package/build/development/shared/components/selectors/Switch/index.js.map +1 -1
- package/build/development/shared/utils/config.js +6 -3
- package/build/development/shared/utils/config.js.map +1 -1
- package/build/development/shared/utils/globalState.js +6 -0
- package/build/development/shared/utils/globalState.js.map +1 -1
- package/build/development/shared/utils/isomorphy/buildInfo.js.map +1 -1
- package/build/development/shared/utils/isomorphy/environment-check.js +6 -1
- package/build/development/shared/utils/isomorphy/environment-check.js.map +1 -1
- package/build/development/shared/utils/isomorphy/index.js +1 -3
- package/build/development/shared/utils/isomorphy/index.js.map +1 -1
- package/build/development/shared/utils/jest/E2eSsrEnv.js +26 -17
- package/build/development/shared/utils/jest/E2eSsrEnv.js.map +1 -1
- package/build/development/shared/utils/jest/global.js +0 -7
- package/build/development/shared/utils/jest/global.js.map +1 -1
- package/build/development/shared/utils/jest/index.js +15 -4
- package/build/development/shared/utils/jest/index.js.map +1 -1
- package/build/development/shared/utils/splitComponent.js +37 -19
- package/build/development/shared/utils/splitComponent.js.map +1 -1
- package/build/development/shared/utils/time.js +3 -3
- package/build/development/shared/utils/time.js.map +1 -1
- package/build/development/shared/utils/webpack.js +11 -11
- package/build/development/shared/utils/webpack.js.map +1 -1
- package/build/development/web.bundle.js +30 -30
- package/build/production/client/getInj.js +4 -2
- package/build/production/client/getInj.js.map +1 -1
- package/build/production/client/index.js +2 -2
- package/build/production/client/index.js.map +1 -1
- package/build/production/client/init.js +4 -2
- package/build/production/client/init.js.map +1 -1
- package/build/production/index.js +2 -1
- package/build/production/index.js.map +1 -1
- package/build/production/server/Cache.js +1 -2
- package/build/production/server/Cache.js.map +1 -1
- package/build/production/server/index.js +9 -5
- package/build/production/server/index.js.map +1 -1
- package/build/production/server/renderer.js +24 -21
- package/build/production/server/renderer.js.map +1 -1
- package/build/production/server/server.js +13 -8
- package/build/production/server/server.js.map +1 -1
- package/build/production/server/utils/errors.js.map +1 -1
- package/build/production/shared/components/Button/index.js +1 -1
- package/build/production/shared/components/Button/index.js.map +1 -1
- package/build/production/shared/components/Checkbox/index.js +1 -1
- package/build/production/shared/components/Checkbox/index.js.map +1 -1
- package/build/production/shared/components/GenericLink/index.js +6 -4
- package/build/production/shared/components/GenericLink/index.js.map +1 -1
- package/build/production/shared/components/Input/index.js +3 -1
- package/build/production/shared/components/Input/index.js.map +1 -1
- package/build/production/shared/components/Link.js +3 -1
- package/build/production/shared/components/Link.js.map +1 -1
- package/build/production/shared/components/MetaTags.js +5 -2
- package/build/production/shared/components/MetaTags.js.map +1 -1
- package/build/production/shared/components/Modal/index.js +6 -2
- package/build/production/shared/components/Modal/index.js.map +1 -1
- package/build/production/shared/components/NavLink.js +4 -1
- package/build/production/shared/components/NavLink.js.map +1 -1
- package/build/production/shared/components/TextArea/index.js +4 -4
- package/build/production/shared/components/TextArea/index.js.map +1 -1
- package/build/production/shared/components/WithTooltip/Tooltip.js +37 -38
- package/build/production/shared/components/WithTooltip/Tooltip.js.map +1 -1
- package/build/production/shared/components/WithTooltip/index.js +4 -4
- package/build/production/shared/components/WithTooltip/index.js.map +1 -1
- package/build/production/shared/components/YouTubeVideo/index.js +3 -2
- package/build/production/shared/components/YouTubeVideo/index.js.map +1 -1
- package/build/production/shared/components/selectors/CustomDropdown/Options/index.js +2 -2
- package/build/production/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
- package/build/production/shared/components/selectors/CustomDropdown/index.js +2 -2
- package/build/production/shared/components/selectors/CustomDropdown/index.js.map +1 -1
- package/build/production/shared/components/selectors/NativeDropdown/index.js +2 -2
- package/build/production/shared/components/selectors/NativeDropdown/index.js.map +1 -1
- package/build/production/shared/components/selectors/Switch/index.js +1 -1
- package/build/production/shared/components/selectors/Switch/index.js.map +1 -1
- package/build/production/shared/utils/config.js +6 -4
- package/build/production/shared/utils/config.js.map +1 -1
- package/build/production/shared/utils/globalState.js +4 -1
- package/build/production/shared/utils/globalState.js.map +1 -1
- package/build/production/shared/utils/isomorphy/buildInfo.js.map +1 -1
- package/build/production/shared/utils/isomorphy/environment-check.js +5 -1
- package/build/production/shared/utils/isomorphy/environment-check.js.map +1 -1
- package/build/production/shared/utils/isomorphy/index.js +1 -3
- package/build/production/shared/utils/isomorphy/index.js.map +1 -1
- package/build/production/shared/utils/jest/E2eSsrEnv.js +15 -8
- package/build/production/shared/utils/jest/E2eSsrEnv.js.map +1 -1
- package/build/production/shared/utils/jest/global.js +1 -1
- package/build/production/shared/utils/jest/global.js.map +1 -1
- package/build/production/shared/utils/jest/index.js +13 -5
- package/build/production/shared/utils/jest/index.js.map +1 -1
- package/build/production/shared/utils/splitComponent.js +16 -8
- package/build/production/shared/utils/splitComponent.js.map +1 -1
- package/build/production/shared/utils/time.js +2 -2
- package/build/production/shared/utils/time.js.map +1 -1
- package/build/production/shared/utils/webpack.js +5 -2
- package/build/production/shared/utils/webpack.js.map +1 -1
- package/build/production/web.bundle.js +1 -1
- package/build/production/web.bundle.js.map +1 -1
- package/build/types-code/client/getInj.d.ts +1 -1
- package/build/types-code/client/index.d.ts +2 -2
- package/build/types-code/client/init.d.ts +1 -1
- package/build/types-code/index.d.ts +6 -5
- package/build/types-code/server/Cache.d.ts +1 -2
- package/build/types-code/server/index.d.ts +4 -5
- package/build/types-code/server/renderer.d.ts +8 -10
- package/build/types-code/server/server.d.ts +3 -5
- package/build/types-code/server/utils/errors.d.ts +1 -1
- package/build/types-code/shared/components/Button/index.d.ts +2 -2
- package/build/types-code/shared/components/TextArea/index.d.ts +3 -3
- package/build/types-code/shared/components/WithTooltip/Tooltip.d.ts +7 -1
- package/build/types-code/shared/components/index.d.ts +12 -12
- package/build/types-code/shared/utils/config.d.ts +1 -1
- package/build/types-code/shared/utils/globalState.d.ts +3 -6
- package/build/types-code/shared/utils/isomorphy/index.d.ts +1 -3
- package/build/types-code/shared/utils/jest/E2eSsrEnv.d.ts +1 -1
- package/build/types-code/shared/utils/jest/global.d.ts +4 -4
- package/build/types-code/shared/utils/jest/index.d.ts +2 -2
- package/build/types-code/shared/utils/webpack.d.ts +1 -1
- package/config/babel/node-ssr.d.ts +3 -2
- package/config/babel/node-ssr.js +5 -7
- package/config/babel/webpack.d.ts +3 -11
- package/config/babel/webpack.js +15 -15
- package/config/eslint/default.mjs +32 -0
- package/config/jest/default.js +10 -6
- package/config/jest/resolver.js +2 -0
- package/config/jest/setup.js +2 -2
- package/config/stylelint/default.js +3 -0
- package/config/webpack/app-base.d.ts +0 -6
- package/config/webpack/app-base.js +64 -70
- package/config/webpack/app-development.d.ts +2 -2
- package/config/webpack/app-development.js +8 -12
- package/config/webpack/app-production.js +1 -0
- package/config/webpack/lib-base.js +20 -18
- package/config/webpack/lib-development.js +1 -0
- package/config/webpack/lib-production.js +1 -0
- package/config/workbox/default.js +2 -5
- package/dev-styles.js +1 -0
- package/eslint.config.mjs +13 -0
- package/node-entry.js +7 -2
- package/null.js +1 -0
- package/package.json +17 -25
- package/prod-styles.js +1 -0
- package/src/client/getInj.ts +8 -3
- package/src/client/index.tsx +4 -4
- package/src/client/init.ts +18 -15
- package/src/index.ts +8 -3
- package/src/server/Cache.ts +7 -15
- package/src/server/index.ts +30 -21
- package/src/server/renderer.tsx +76 -66
- package/src/server/server.ts +38 -20
- package/src/server/utils/errors.ts +6 -3
- package/src/shared/components/Button/index.tsx +4 -5
- package/src/shared/components/Checkbox/index.tsx +10 -7
- package/src/shared/components/GenericLink/index.tsx +14 -7
- package/src/shared/components/Input/index.tsx +4 -1
- package/src/shared/components/Link.tsx +9 -5
- package/src/shared/components/MetaTags.tsx +21 -15
- package/src/shared/components/Modal/index.tsx +12 -11
- package/src/shared/components/NavLink.tsx +10 -5
- package/src/shared/components/TextArea/index.tsx +17 -9
- package/src/shared/components/WithTooltip/{Tooltip.tsx → Tooltip.ts} +35 -39
- package/src/shared/components/WithTooltip/index.tsx +36 -30
- package/src/shared/components/YouTubeVideo/index.tsx +10 -6
- package/src/shared/components/selectors/CustomDropdown/Options/index.tsx +5 -6
- package/src/shared/components/selectors/CustomDropdown/index.tsx +10 -14
- package/src/shared/components/selectors/NativeDropdown/index.tsx +5 -7
- package/src/shared/components/selectors/Switch/index.tsx +19 -17
- package/src/shared/utils/config.ts +12 -5
- package/src/shared/utils/globalState.ts +8 -5
- package/src/shared/utils/isomorphy/buildInfo.ts +1 -1
- package/src/shared/utils/isomorphy/environment-check.ts +5 -1
- package/src/shared/utils/isomorphy/index.ts +4 -6
- package/src/shared/utils/jest/E2eSsrEnv.ts +64 -39
- package/src/shared/utils/jest/global.ts +6 -8
- package/src/shared/utils/jest/{index.tsx → index.ts} +25 -12
- package/src/shared/utils/splitComponent.tsx +44 -25
- package/src/shared/utils/time.ts +16 -9
- package/src/shared/utils/webpack.ts +19 -14
- package/webpack.config.ts +36 -10
- package/config/eslint/default.json +0 -30
- package/config/eslint/jest.json +0 -19
- package/config/eslint/typescript.js +0 -46
|
@@ -11,7 +11,6 @@ var _jsUtils = require("@dr.pogodin/js-utils");
|
|
|
11
11
|
var _globalState = require("./globalState");
|
|
12
12
|
var _isomorphy = require("./isomorphy");
|
|
13
13
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
14
|
-
/* eslint-disable react/jsx-props-no-spreading */
|
|
15
14
|
/* global document */
|
|
16
15
|
|
|
17
16
|
// Note: At the client side we can get chunk groups immediately when loading
|
|
@@ -20,8 +19,16 @@ var _jsxRuntime = require("react/jsx-runtime");
|
|
|
20
19
|
// needed on the server side, we'll fetch it differently.
|
|
21
20
|
let clientChunkGroups;
|
|
22
21
|
if (_isomorphy.IS_CLIENT_SIDE) {
|
|
23
|
-
//
|
|
24
|
-
|
|
22
|
+
// TODO: Rewrite to avoid these overrides of ESLint rules.
|
|
23
|
+
/* eslint-disable @typescript-eslint/no-unsafe-assignment,
|
|
24
|
+
@typescript-eslint/no-require-imports,
|
|
25
|
+
@typescript-eslint/no-unsafe-call,
|
|
26
|
+
@typescript-eslint/no-unsafe-member-access */
|
|
27
|
+
clientChunkGroups = require("../../client/getInj").default().CHUNK_GROUPS ?? {};
|
|
28
|
+
/* eslint-enable @typescript-eslint/no-unsafe-assignment,
|
|
29
|
+
@typescript-eslint/no-require-imports,
|
|
30
|
+
@typescript-eslint/no-unsafe-call,
|
|
31
|
+
@typescript-eslint/no-unsafe-member-access */
|
|
25
32
|
}
|
|
26
33
|
const refCounts = {};
|
|
27
34
|
function getPublicPath() {
|
|
@@ -50,11 +57,17 @@ function bookStyleSheet(name, loadedSheets, refCount) {
|
|
|
50
57
|
document.head.appendChild(link);
|
|
51
58
|
}
|
|
52
59
|
res = new _jsUtils.Barrier();
|
|
53
|
-
link.addEventListener('load', () =>
|
|
54
|
-
|
|
60
|
+
link.addEventListener('load', () => {
|
|
61
|
+
if (!res) throw Error('Internal error');
|
|
62
|
+
void res.resolve();
|
|
63
|
+
});
|
|
64
|
+
link.addEventListener('error', () => {
|
|
65
|
+
if (!res) throw Error('Internal error');
|
|
66
|
+
void res.resolve();
|
|
67
|
+
});
|
|
55
68
|
}
|
|
56
69
|
if (refCount) {
|
|
57
|
-
const current = refCounts[path]
|
|
70
|
+
const current = refCounts[path] ?? 0;
|
|
58
71
|
refCounts[path] = 1 + current;
|
|
59
72
|
}
|
|
60
73
|
return res;
|
|
@@ -69,8 +82,9 @@ function getLoadedStyleSheets() {
|
|
|
69
82
|
const {
|
|
70
83
|
styleSheets
|
|
71
84
|
} = document;
|
|
72
|
-
for (
|
|
73
|
-
|
|
85
|
+
for (const {
|
|
86
|
+
href
|
|
87
|
+
} of styleSheets) {
|
|
74
88
|
if (href) res.add(href);
|
|
75
89
|
}
|
|
76
90
|
return res;
|
|
@@ -89,14 +103,13 @@ function assertChunkName(chunkName, chunkGroups) {
|
|
|
89
103
|
* @return Resolves once all pending stylesheets, necessary for
|
|
90
104
|
* the chunk, are either loaded, or failed to load.
|
|
91
105
|
*/
|
|
92
|
-
function bookStyleSheets(chunkName, chunkGroups, refCount) {
|
|
106
|
+
async function bookStyleSheets(chunkName, chunkGroups, refCount) {
|
|
93
107
|
const promises = [];
|
|
94
108
|
const assets = chunkGroups[chunkName];
|
|
95
109
|
if (!assets) return Promise.resolve();
|
|
96
110
|
const loadedSheets = getLoadedStyleSheets();
|
|
97
|
-
for (
|
|
98
|
-
|
|
99
|
-
if (asset?.endsWith('.css')) {
|
|
111
|
+
for (const asset of assets) {
|
|
112
|
+
if (asset.endsWith('.css')) {
|
|
100
113
|
const promise = bookStyleSheet(asset, loadedSheets, refCount);
|
|
101
114
|
if (promise) promises.push(promise);
|
|
102
115
|
}
|
|
@@ -114,9 +127,8 @@ function bookStyleSheets(chunkName, chunkGroups, refCount) {
|
|
|
114
127
|
function freeStyleSheets(chunkName, chunkGroups) {
|
|
115
128
|
const assets = chunkGroups[chunkName];
|
|
116
129
|
if (!assets) return;
|
|
117
|
-
for (
|
|
118
|
-
|
|
119
|
-
if (asset?.endsWith('.css')) {
|
|
130
|
+
for (const asset of assets) {
|
|
131
|
+
if (asset.endsWith('.css')) {
|
|
120
132
|
const path = `${getPublicPath()}/${asset}`;
|
|
121
133
|
const pathRefCount = refCounts[path];
|
|
122
134
|
if (pathRefCount) {
|
|
@@ -182,10 +194,14 @@ function splitComponent({
|
|
|
182
194
|
// This takes care about stylesheets management every time an instance of
|
|
183
195
|
// this component is mounted / unmounted.
|
|
184
196
|
(0, _react.useInsertionEffect)(() => {
|
|
185
|
-
bookStyleSheets(chunkName, clientChunkGroups, true);
|
|
186
|
-
return () =>
|
|
197
|
+
void bookStyleSheets(chunkName, clientChunkGroups, true);
|
|
198
|
+
return () => {
|
|
199
|
+
freeStyleSheets(chunkName, clientChunkGroups);
|
|
200
|
+
};
|
|
187
201
|
}, []);
|
|
188
|
-
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Component
|
|
202
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Component
|
|
203
|
+
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
204
|
+
, {
|
|
189
205
|
...rest,
|
|
190
206
|
ref: ref,
|
|
191
207
|
children: children
|
|
@@ -200,7 +216,9 @@ function splitComponent({
|
|
|
200
216
|
...rest
|
|
201
217
|
}) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.Suspense, {
|
|
202
218
|
fallback: placeholder,
|
|
203
|
-
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(LazyComponent
|
|
219
|
+
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(LazyComponent
|
|
220
|
+
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
221
|
+
, {
|
|
204
222
|
...rest,
|
|
205
223
|
children: children
|
|
206
224
|
})
|
|
@@ -1 +1 @@
|
|
|
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","children","ref","rest","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 FunctionComponent,\n type ReactNode,\n type RefObject,\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; ref?: RefObject<unknown> },\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: FunctionComponent<ComponentPropsT> = ({\n children,\n ref,\n ...rest\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 {...(rest as unknown as ComponentPropsT)} ref={ref}>\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;AAUA,IAAAC,QAAA,GAAAD,OAAA;AAEA,IAAAE,YAAA,GAAAF,OAAA;AAEA,IAAAG,UAAA,GAAAH,OAAA;AAIqB,IAAAI,WAAA,GAAAJ,OAAA;AArBrB;AACA;;AAsBA;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,QAAQ,CAACM,aAAa,CAAC,MAAM,CAAC;MACrCF,I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;QAAO,CAAC,GAAG,IAAAC,0BAAa,EAAC,CAAE;QAChD/B,eAAe,CAACC,SAAS,EAAEC,WAAW,CAAC;QACvC,IAAI,CAAC4B,MAAM,CAACE,QAAQ,CAAC/B,SAAS,CAAC,EAAE6B,MAAM,CAACnB,IAAI,CAACV,SAAS,CAAC;MACzD;;MAEA;MACA;MACA,IAAAgC,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,EAACV,SAAS;QAAA,GAAMI,IAAI;QAAiCD,GAAG,EAAEA,GAAI;QAAAD,QAAA,EAC3DA;MAAQ,CACA,CAAC;IAEhB,CAAC;IAED,OAAO;MAAE5D,OAAO,EAAE2D;IAAQ,CAAC;EAC7B,CAAC,CAAC;EAEF,MAAMU,SAAmD,GAAGA,CAAC;IAC3DT,QAAQ;IACR,GAAGE;EACY,CAAC,kBAChB,IAAAjE,WAAA,CAAAuE,GAAA,EAAC5E,MAAA,CAAA8E,QAAQ;IAACC,QAAQ,EAAEjB,WAAY;IAAAM,QAAA,eAC9B,IAAA/D,WAAA,CAAAuE,GAAA,EAACb,aAAa;MAAA,GAAKO,IAAI;MAAAF,QAAA,EACpBA;IAAQ,CACI;EAAC,CACR,CACX;EAED,OAAOS,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","Error","resolve","current","getLoadedStyleSheets","Set","styleSheets","href","add","assertChunkName","chunkName","chunkGroups","bookStyleSheets","promises","assets","Promise","asset","endsWith","promise","push","length","allSettled","then","freeStyleSheets","pathRefCount","remove","usedChunkNames","splitComponent","getComponent","placeholder","LazyComponent","lazy","resolved","Component","Wrapper","children","ref","rest","IS_SERVER_SIDE","chunks","getSsrContext","includes","useInsertionEffect","jsx","CodeSplit","Suspense","fallback"],"sources":["../../../../src/shared/utils/splitComponent.tsx"],"sourcesContent":["/* global document */\n\nimport {\n type ComponentType,\n type FunctionComponent,\n type ReactNode,\n type RefObject,\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 // TODO: Rewrite to avoid these overrides of ESLint rules.\n /* eslint-disable @typescript-eslint/no-unsafe-assignment,\n @typescript-eslint/no-require-imports,\n @typescript-eslint/no-unsafe-call,\n @typescript-eslint/no-unsafe-member-access */\n clientChunkGroups = require('client/getInj').default().CHUNK_GROUPS ?? {};\n /* eslint-enable @typescript-eslint/no-unsafe-assignment,\n @typescript-eslint/no-require-imports,\n @typescript-eslint/no-unsafe-call,\n @typescript-eslint/no-unsafe-member-access */\n}\n\nconst refCounts: Record<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', () => {\n if (!res) throw Error('Internal error');\n void res.resolve();\n });\n link.addEventListener('error', () => {\n if (!res) throw Error('Internal error');\n void res.resolve();\n });\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 (const { href } of styleSheets) {\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 async 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 (const asset of assets) {\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): void {\n const assets = chunkGroups[chunkName];\n if (!assets) return;\n\n for (const asset of assets) {\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; ref?: RefObject<unknown> },\n>({\n chunkName,\n getComponent,\n placeholder,\n}: {\n chunkName: string;\n getComponent: () => Promise<ComponentOrModule<ComponentPropsT>>;\n placeholder?: ReactNode;\n}): FunctionComponent<ComponentPropsT> {\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: FunctionComponent<ComponentPropsT> = ({\n children,\n ref,\n ...rest\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 void bookStyleSheets(chunkName, clientChunkGroups, true);\n return () => {\n freeStyleSheets(chunkName, clientChunkGroups);\n };\n }, []);\n\n return (\n <Component\n // eslint-disable-next-line react/jsx-props-no-spreading\n {...(rest as unknown as ComponentPropsT)}\n ref={ref}\n >\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\n // eslint-disable-next-line react/jsx-props-no-spreading\n {...rest as Parameters<typeof LazyComponent>[0]}\n >\n {children}\n </LazyComponent>\n </Suspense>\n );\n\n return CodeSplit;\n}\n"],"mappings":";;;;;;;;AAEA,IAAAA,MAAA,GAAAC,OAAA;AAUA,IAAAC,QAAA,GAAAD,OAAA;AAEA,IAAAE,YAAA,GAAAF,OAAA;AAEA,IAAAG,UAAA,GAAAH,OAAA;AAIqB,IAAAI,WAAA,GAAAJ,OAAA;AApBrB;;AAsBA;AACA;AACA;AACA;AACA,IAAIK,iBAA+B;AAEnC,IAAIC,yBAAc,EAAE;EAClB;EACA;AACF;AACA;AACA;EACED,iBAAiB,GAAGL,OAAO,sBAAgB,CAAC,CAACO,OAAO,CAAC,CAAC,CAACC,YAAY,IAAI,CAAC,CAAC;EACzE;AACF;AACA;AACA;AACA;AAEA,MAAMC,SAAiC,GAAG,CAAC,CAAC;AAE5C,SAASC,aAAaA,CAAA,EAAG;EACvB,OAAO,IAAAC,uBAAY,EAAC,CAAC,CAAC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,WAAA,CAAAsE,GAAA,EAACb;IACC;IAAA;MAAA,GACIO,IAAI;MAAAF,QAAA,EAEPA;IAAQ,CACI;EAAC,CACR,CACX;EAED,OAAOS,SAAS;AAClB","ignoreList":[]}
|
|
@@ -8,8 +8,8 @@ exports.default = void 0;
|
|
|
8
8
|
exports.useCurrent = useCurrent;
|
|
9
9
|
exports.useTimezoneOffset = useTimezoneOffset;
|
|
10
10
|
var _cookie = require("cookie");
|
|
11
|
-
var _dayjs = _interopRequireDefault(require("dayjs"));
|
|
12
11
|
var _react = require("react");
|
|
12
|
+
var _dayjs = _interopRequireDefault(require("dayjs"));
|
|
13
13
|
var _jsUtils = require("@dr.pogodin/js-utils");
|
|
14
14
|
var _reactGlobalState = require("@dr.pogodin/react-global-state");
|
|
15
15
|
var _globalState = require("./globalState");
|
|
@@ -63,8 +63,8 @@ function useTimezoneOffset({
|
|
|
63
63
|
} = {}) {
|
|
64
64
|
const ssrContext = (0, _globalState.getSsrContext)(false);
|
|
65
65
|
const [offset, setOffset] = (0, _reactGlobalState.useGlobalState)(globalStatePath, () => {
|
|
66
|
-
const value = cookieName && ssrContext?.req
|
|
67
|
-
return value ? parseInt(value
|
|
66
|
+
const value = cookieName && ssrContext?.req.cookies[cookieName];
|
|
67
|
+
return value ? parseInt(value) : 0;
|
|
68
68
|
});
|
|
69
69
|
(0, _react.useEffect)(() => {
|
|
70
70
|
const date = new Date();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.js","names":["_cookie","require","
|
|
1
|
+
{"version":3,"file":"time.js","names":["_cookie","require","_react","_dayjs","_interopRequireDefault","_jsUtils","_reactGlobalState","_globalState","useCurrent","autorefresh","globalStatePath","precision","SEC_MS","now","setter","useGlobalState","Date","useEffect","timerId","update","old","neu","Math","abs","setTimeout","clearTimeout","useTimezoneOffset","cookieName","ssrContext","getSsrContext","offset","setOffset","value","req","cookies","parseInt","date","getTimezoneOffset","document","cookie","serialize","toString","path","time","DAY_MS","HOUR_MS","MIN_MS","YEAR_MS","timer","_default","exports","default","Object","assign","dayjs"],"sources":["../../../../src/shared/utils/time.ts"],"sourcesContent":["import { serialize } from 'cookie';\nimport { useEffect } from 'react';\nimport dayjs from 'dayjs';\n\nimport {\n DAY_MS,\n HOUR_MS,\n MIN_MS,\n SEC_MS,\n YEAR_MS,\n timer,\n} from '@dr.pogodin/js-utils';\n\nimport { type ForceT, useGlobalState } from '@dr.pogodin/react-global-state';\n\nimport { getSsrContext } from './globalState';\n\n/**\n * This react hook wraps Date.now() function in a SSR friendly way,\n * ensuring that all calls to useCurrent() within the same render return\n * exactly the same time (which is retrieved from Date.now() first, and\n * then stored in the global state to be reused in all other calls), which\n * is also passed and used in the first client side render, and then updated\n * with a finite precision to avoid infinite re-rendering loops.\n */\n// TODO: Should we request the state type as generic parameter, to be able\n// to verify the give globalStatePath is correct?\nexport function useCurrent({\n autorefresh = false,\n globalStatePath = 'currentTime',\n precision = 5 * SEC_MS,\n} = {}): number {\n const [now, setter] = useGlobalState<ForceT, number>(\n globalStatePath,\n Date.now,\n );\n useEffect(() => {\n let timerId: NodeJS.Timeout | undefined;\n const update = (): void => {\n setter((old) => {\n const neu = Date.now();\n return Math.abs(neu - old) > precision ? neu : old;\n });\n if (autorefresh) timerId = setTimeout(update, precision);\n };\n update();\n return (): void => {\n if (timerId) clearTimeout(timerId);\n };\n }, [autorefresh, precision, setter]);\n return now;\n}\n\n/**\n * Wraps the standard Date.getTimezoneOffset() method in a SSR-friendly way.\n * This hook retrieves the offset value at the client side and uses a cookie\n * to pass it to the server in subsequent requests from that user. At the server\n * side the value from cookie is used in renders and passed back to the client\n * via the global state. Prior to the value being known (in the very first\n * request from the user, when the cookie is still missing), zero value is used\n * as the default value.\n */\n// TODO: Should we request the state type as generic parameter, to be able\n// to verify the give globalStatePath is correct?\nexport function useTimezoneOffset({\n cookieName = 'timezoneOffset',\n globalStatePath = 'timezoneOffset',\n} = {}): number {\n const ssrContext = getSsrContext(false);\n const [offset, setOffset] = useGlobalState<ForceT, number>(\n globalStatePath,\n () => {\n const value = cookieName\n && ssrContext?.req.cookies[cookieName] as string;\n return value ? parseInt(value) : 0;\n },\n );\n useEffect(() => {\n const date = new Date();\n const value = date.getTimezoneOffset();\n setOffset(value);\n if (cookieName) {\n document.cookie = serialize(cookieName, value.toString(), { path: '/' });\n }\n }, [cookieName, setOffset]);\n return offset;\n}\n\nconst time = {\n DAY_MS,\n HOUR_MS,\n MIN_MS,\n SEC_MS,\n YEAR_MS,\n now: Date.now,\n timer,\n useCurrent,\n useTimezoneOffset,\n};\n\nexport default Object.assign(dayjs, time);\n"],"mappings":";;;;;;;;;AAAA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAC,sBAAA,CAAAH,OAAA;AAEA,IAAAI,QAAA,GAAAJ,OAAA;AASA,IAAAK,iBAAA,GAAAL,OAAA;AAEA,IAAAM,YAAA,GAAAN,OAAA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASO,UAAUA,CAAC;EACzBC,WAAW,GAAG,KAAK;EACnBC,eAAe,GAAG,aAAa;EAC/BC,SAAS,GAAG,CAAC,GAAGC;AAClB,CAAC,GAAG,CAAC,CAAC,EAAU;EACd,MAAM,CAACC,GAAG,EAAEC,MAAM,CAAC,GAAG,IAAAC,gCAAc,EAClCL,eAAe,EACfM,IAAI,CAACH,GACP,CAAC;EACD,IAAAI,gBAAS,EAAC,MAAM;IACd,IAAIC,OAAmC;IACvC,MAAMC,MAAM,GAAGA,CAAA,KAAY;MACzBL,MAAM,CAAEM,GAAG,IAAK;QACd,MAAMC,GAAG,GAAGL,IAAI,CAACH,GAAG,CAAC,CAAC;QACtB,OAAOS,IAAI,CAACC,GAAG,CAACF,GAAG,GAAGD,GAAG,CAAC,GAAGT,SAAS,GAAGU,GAAG,GAAGD,GAAG;MACpD,CAAC,CAAC;MACF,IAAIX,WAAW,EAAES,OAAO,GAA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AO;EACPC,MAAM,EAANA,eAAM;EACNlC,MAAM,EAANA,eAAM;EACNmC,OAAO,EAAPA,gBAAO;EACPlC,GAAG,EAAEG,IAAI,CAACH,GAAG;EACbmC,KAAK,EAALA,cAAK;EACLxC,UAAU;EACVkB;AACF,CAAC;AAAC,IAAAuB,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEaC,MAAM,CAACC,MAAM,CAACC,cAAK,EAAEX,IAAI,CAAC","ignoreList":[]}
|
|
@@ -18,18 +18,20 @@ function requireWeak(modulePath, basePath) {
|
|
|
18
18
|
|
|
19
19
|
// TODO: On one hand, this try/catch wrap silencing errors is bad, as it may
|
|
20
20
|
// hide legit errors, in a way difficult to notice and understand; but on the
|
|
21
|
-
// other hand it fails for some (unclear, but legit?) reasons in some
|
|
21
|
+
// other hand it fails for some (unclear, but legit?) reasons in some
|
|
22
|
+
// environments,
|
|
22
23
|
// like during the static code generation for docs. Perhaps, something should
|
|
23
24
|
// be implemented differently here.
|
|
24
25
|
try {
|
|
25
|
-
|
|
26
|
+
// eslint-disable-next-line no-eval
|
|
27
|
+
const req = eval('require');
|
|
28
|
+
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
26
30
|
const {
|
|
27
31
|
resolve
|
|
28
|
-
} =
|
|
32
|
+
} = req('path');
|
|
29
33
|
const path = basePath ? resolve(basePath, modulePath) : modulePath;
|
|
30
|
-
const module =
|
|
31
|
-
/* eslint-enable no-eval */
|
|
32
|
-
|
|
34
|
+
const module = req(path);
|
|
33
35
|
if (!('default' in module) || !module.default) return module;
|
|
34
36
|
const {
|
|
35
37
|
default: def,
|
|
@@ -38,11 +40,9 @@ function requireWeak(modulePath, basePath) {
|
|
|
38
40
|
const res = def;
|
|
39
41
|
Object.entries(named).forEach(([name, value]) => {
|
|
40
42
|
const assigned = res[name];
|
|
41
|
-
if (assigned !==
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
} else res[name] = value;
|
|
43
|
+
if (assigned) res[name] = value;else if (assigned !== value) {
|
|
44
|
+
throw Error('Conflict between default and named exports');
|
|
45
|
+
}
|
|
46
46
|
});
|
|
47
47
|
return res;
|
|
48
48
|
} catch {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webpack.js","names":["_isomorphy","require","requireWeak","modulePath","basePath","IS_CLIENT_SIDE","
|
|
1
|
+
{"version":3,"file":"webpack.js","names":["_isomorphy","require","requireWeak","modulePath","basePath","IS_CLIENT_SIDE","req","eval","resolve","path","module","default","def","named","res","Object","entries","forEach","name","value","assigned","Error","resolveWeak"],"sources":["../../../../src/shared/utils/webpack.ts"],"sourcesContent":["import type PathT from 'path';\n\nimport { IS_CLIENT_SIDE } from './isomorphy';\n\n/**\n * Requires the specified module without including it into the bundle during\n * Webpack build.\n * @param modulePath\n * @param [basePath]\n * @return Required module.\n */\nexport function requireWeak<T extends object>(\n modulePath: string,\n basePath?: string,\n): T | null {\n if (IS_CLIENT_SIDE) return null;\n\n // TODO: On one hand, this try/catch wrap silencing errors is bad, as it may\n // hide legit errors, in a way difficult to notice and understand; but on the\n // other hand it fails for some (unclear, but legit?) reasons in some\n // environments,\n // like during the static code generation for docs. Perhaps, something should\n // be implemented differently here.\n try {\n // eslint-disable-next-line no-eval\n const req = eval('require') as (path: string) => unknown;\n\n // eslint-disable-next-line @typescript-eslint/unbound-method\n const { resolve } = req('path') as typeof PathT;\n\n const path = basePath ? resolve(basePath, modulePath) : modulePath;\n const module = req(path) as T;\n\n if (!('default' in module) || !module.default) return module;\n\n const { default: def, ...named } = module;\n\n const res = def as T;\n\n Object.entries(named).forEach(([name, value]) => {\n const assigned = res[name as keyof T];\n if (assigned) (res[name as keyof T] as unknown) = value;\n else if (assigned !== value) {\n throw Error('Conflict between default and named exports');\n }\n });\n return res;\n } catch {\n return null;\n }\n}\n\n/**\n * Resolves specified module path with help of Babel's module resolver.\n * Yes, the function itself just returns its argument to the caller, but Babel\n * is configured to resolve the first argument of resolveWeak(..) function, thus\n * the result will be the resolved path.\n * @param {string} modulePath\n * @return {string} Absolute or relative path to the module.\n */\nexport function resolveWeak(modulePath: string): string {\n return modulePath;\n}\n"],"mappings":";;;;;;;AAEA,IAAAA,UAAA,GAAAC,OAAA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,WAAWA,CACzBC,UAAkB,EAClBC,QAAiB,EACP;EACV,IAAIC,yBAAc,EAAE,OAAO,IAAI;;EAE/B;EACA;EACA;EACA;EACA;EACA;EACA,IAAI;IACF;IACA,MAAMC,GAAG,GAAGC,IAAI,CAAC,SAAS,CAA8B;;IAExD;IACA,MAAM;MAAEC;IAAQ,CAAC,GAAGF,GAAG,CAAC,MAAM,CAAiB;IAE/C,MAAMG,IAAI,GAAGL,QAAQ,GAAGI,OAAO,CAACJ,QAAQ,EAAED,UAAU,CAAC,GAAGA,UAAU;IAClE,MAAMO,MAAM,GAAGJ,GAAG,CAACG,IAAI,CAAM;IAE7B,IAAI,EAAE,SAAS,IAAIC,MAAM,CAAC,IAAI,CAACA,MAAM,CAACC,OAAO,EAAE,OAAOD,MAAM;IAE5D,MAAM;MAAEC,OAAO,EAAEC,GAAG;MAAE,GAAGC;IAAM,CAAC,GAAGH,MAAM;IAEzC,MAAMI,GAAG,GAAGF,GAAQ;IAEpBG,MAAM,CAACC,OAAO,CAACH,KAAK,CAAC,CAACI,OAAO,CAAC,CAAC,CAACC,IAAI,EAAEC,KAAK,CAAC,KAAK;MAC/C,MAAMC,QAAQ,GAAGN,GAAG,CAACI,IAAI,CAAY;MACrC,IAAIE,QAAQ,EAAGN,GAAG,CAACI,IAAI,CAAY,GAAeC,KAAK,CAAC,KACnD,IAAIC,QAAQ,KAAKD,KAAK,EAAE;QAC3B,MAAME,KAAK,CAAC,4CAA4C,CAAC;MAC3D;IACF,CAAC,CAAC;IACF,OAAOP,GAAG;EACZ,CAAC,CAAC,MAAM;IACN,OAAO,IAAI;EACb;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AAC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