@docusaurus/core 2.0.0-beta.2 → 2.0.0-beta.22

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 (223) hide show
  1. package/README.md +1 -0
  2. package/bin/beforeCli.mjs +140 -0
  3. package/bin/docusaurus.mjs +221 -0
  4. package/lib/babel/preset.d.ts +8 -3
  5. package/lib/babel/preset.js +10 -8
  6. package/lib/client/App.d.ts +2 -3
  7. package/lib/client/App.js +31 -28
  8. package/lib/client/BaseUrlIssueBanner/index.d.ts +27 -0
  9. package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.js → BaseUrlIssueBanner/index.js} +25 -14
  10. package/lib/client/{baseUrlIssueBanner → BaseUrlIssueBanner}/styles.module.css +0 -0
  11. package/lib/client/ClientLifecyclesDispatcher.d.ts +16 -0
  12. package/lib/client/ClientLifecyclesDispatcher.js +39 -0
  13. package/lib/client/LinksCollector.d.ts +3 -3
  14. package/lib/client/LinksCollector.js +8 -13
  15. package/lib/client/PendingNavigation.d.ts +17 -3
  16. package/lib/client/PendingNavigation.js +47 -72
  17. package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.d.ts → SiteMetadataDefaults.d.ts} +1 -2
  18. package/lib/client/SiteMetadataDefaults.js +21 -0
  19. package/lib/{choosePort.d.ts → client/browserContext.d.ts} +5 -5
  20. package/lib/client/browserContext.js +22 -0
  21. package/lib/client/clientEntry.js +14 -9
  22. package/lib/client/docusaurus.d.ts +12 -6
  23. package/lib/client/docusaurus.js +30 -43
  24. package/lib/client/docusaurusContext.d.ts +12 -0
  25. package/lib/client/docusaurusContext.js +25 -0
  26. package/lib/client/exports/BrowserOnly.d.ts +3 -5
  27. package/lib/client/exports/BrowserOnly.js +14 -8
  28. package/lib/client/exports/ComponentCreator.d.ts +6 -2
  29. package/lib/client/exports/ComponentCreator.js +75 -42
  30. package/lib/client/exports/ErrorBoundary.d.ts +18 -0
  31. package/lib/client/exports/ErrorBoundary.js +37 -0
  32. package/lib/client/exports/Head.d.ts +2 -3
  33. package/lib/client/exports/Head.js +3 -4
  34. package/lib/client/exports/Interpolate.d.ts +2 -2
  35. package/lib/client/exports/Interpolate.js +20 -49
  36. package/lib/client/exports/Link.d.ts +4 -10
  37. package/lib/client/exports/Link.js +40 -40
  38. package/lib/client/exports/Translate.d.ts +1 -1
  39. package/lib/client/exports/Translate.js +14 -9
  40. package/lib/client/exports/constants.js +1 -11
  41. package/lib/client/exports/isInternalUrl.js +1 -1
  42. package/lib/client/exports/renderRoutes.d.ts +1 -2
  43. package/lib/client/exports/renderRoutes.js +1 -2
  44. package/lib/client/exports/router.d.ts +1 -1
  45. package/lib/client/exports/router.js +1 -1
  46. package/lib/client/exports/useBaseUrl.js +11 -14
  47. package/lib/client/exports/useDocusaurusContext.d.ts +2 -3
  48. package/lib/client/exports/useDocusaurusContext.js +3 -9
  49. package/lib/client/exports/useGlobalData.d.ts +4 -3
  50. package/lib/client/exports/useGlobalData.js +6 -13
  51. package/lib/{server/versions/__tests/index.test.d.ts → client/exports/useIsBrowser.d.ts} +1 -1
  52. package/lib/client/exports/useIsBrowser.js +11 -0
  53. package/lib/client/exports/useRouteContext.d.ts +8 -0
  54. package/lib/client/exports/useRouteContext.js +15 -0
  55. package/lib/client/flat.d.ts +12 -2
  56. package/lib/client/flat.js +19 -15
  57. package/lib/client/normalizeLocation.d.ts +2 -5
  58. package/lib/client/normalizeLocation.js +14 -10
  59. package/lib/client/prefetch.d.ts +1 -2
  60. package/lib/client/prefetch.js +12 -32
  61. package/lib/client/preload.d.ts +3 -4
  62. package/lib/client/preload.js +5 -12
  63. package/lib/client/routeContext.d.ts +13 -0
  64. package/lib/client/routeContext.js +31 -0
  65. package/lib/client/serverEntry.d.ts +10 -0
  66. package/lib/client/serverEntry.js +108 -146
  67. package/lib/client/theme-fallback/Error/index.d.ts +10 -0
  68. package/lib/client/theme-fallback/Error/index.js +45 -0
  69. package/lib/client/theme-fallback/Layout/index.d.ts +10 -0
  70. package/lib/client/theme-fallback/Layout/index.js +2 -26
  71. package/lib/client/theme-fallback/Loading/index.d.ts +9 -0
  72. package/lib/client/theme-fallback/Loading/index.js +50 -116
  73. package/lib/client/{exports/context.js → theme-fallback/NotFound/index.d.ts} +2 -2
  74. package/lib/client/theme-fallback/NotFound/index.js +19 -18
  75. package/lib/client/theme-fallback/Root/index.d.ts +10 -0
  76. package/lib/client/theme-fallback/Root/index.js +2 -6
  77. package/lib/{server/html-tags/htmlTags.d.ts → client/theme-fallback/SiteMetadata/index.d.ts} +2 -1
  78. package/lib/client/theme-fallback/SiteMetadata/index.js +10 -0
  79. package/lib/commands/build.d.ts +6 -2
  80. package/lib/commands/build.js +81 -64
  81. package/lib/commands/clear.d.ts +7 -1
  82. package/lib/commands/clear.js +34 -21
  83. package/lib/commands/deploy.d.ts +5 -2
  84. package/lib/commands/deploy.js +104 -81
  85. package/lib/commands/external.d.ts +2 -2
  86. package/lib/commands/external.js +9 -11
  87. package/lib/commands/serve.d.ts +8 -2
  88. package/lib/commands/serve.js +29 -28
  89. package/lib/commands/start.d.ts +9 -2
  90. package/lib/commands/start.js +109 -101
  91. package/lib/commands/swizzle/actions.d.ts +23 -0
  92. package/lib/commands/swizzle/actions.js +101 -0
  93. package/lib/commands/swizzle/common.d.ts +33 -0
  94. package/lib/commands/swizzle/common.js +56 -0
  95. package/lib/commands/swizzle/components.d.ts +29 -0
  96. package/lib/commands/swizzle/components.js +200 -0
  97. package/lib/commands/swizzle/config.d.ts +10 -0
  98. package/lib/commands/swizzle/config.js +84 -0
  99. package/lib/{server/client-modules/index.d.ts → commands/swizzle/context.d.ts} +2 -2
  100. package/lib/commands/swizzle/context.js +24 -0
  101. package/lib/commands/swizzle/index.d.ts +8 -0
  102. package/lib/commands/swizzle/index.js +118 -0
  103. package/lib/commands/swizzle/prompts.d.ts +12 -0
  104. package/lib/commands/swizzle/prompts.js +110 -0
  105. package/lib/{client/exports/context.d.ts → commands/swizzle/tables.d.ts} +3 -4
  106. package/lib/commands/swizzle/tables.js +113 -0
  107. package/lib/commands/swizzle/themes.d.ts +20 -0
  108. package/lib/commands/swizzle/themes.js +106 -0
  109. package/lib/commands/writeHeadingIds.d.ts +2 -6
  110. package/lib/commands/writeHeadingIds.js +22 -81
  111. package/lib/commands/writeTranslations.d.ts +4 -5
  112. package/lib/commands/writeTranslations.js +46 -25
  113. package/lib/index.d.ts +9 -9
  114. package/lib/index.js +14 -14
  115. package/lib/server/brokenLinks.d.ts +4 -17
  116. package/lib/server/brokenLinks.js +67 -56
  117. package/lib/server/clientModules.d.ts +12 -0
  118. package/lib/server/clientModules.js +20 -0
  119. package/lib/server/config.d.ts +5 -2
  120. package/lib/server/config.js +29 -6
  121. package/lib/server/configValidation.d.ts +4 -4
  122. package/lib/server/configValidation.js +86 -41
  123. package/lib/server/getHostPort.d.ts +14 -0
  124. package/lib/server/getHostPort.js +79 -0
  125. package/lib/server/htmlTags.d.ts +12 -0
  126. package/lib/server/htmlTags.js +62 -0
  127. package/lib/server/i18n.d.ts +3 -13
  128. package/lib/server/i18n.js +21 -55
  129. package/lib/server/index.d.ts +28 -13
  130. package/lib/server/index.js +83 -165
  131. package/lib/server/plugins/configs.d.ts +51 -0
  132. package/lib/server/plugins/configs.js +101 -0
  133. package/lib/server/plugins/index.d.ts +9 -8
  134. package/lib/server/plugins/index.js +73 -134
  135. package/lib/server/plugins/init.d.ts +6 -5
  136. package/lib/server/plugins/init.js +44 -109
  137. package/lib/server/plugins/moduleShorthand.d.ts +9 -0
  138. package/lib/server/plugins/moduleShorthand.js +46 -0
  139. package/lib/server/plugins/pluginIds.d.ts +5 -1
  140. package/lib/server/plugins/pluginIds.js +12 -7
  141. package/lib/server/plugins/presets.d.ts +12 -0
  142. package/lib/server/plugins/presets.js +49 -0
  143. package/lib/server/plugins/routeConfig.d.ts +11 -0
  144. package/lib/server/plugins/routeConfig.js +54 -0
  145. package/lib/server/plugins/synthetic.d.ts +20 -0
  146. package/lib/server/plugins/synthetic.js +111 -0
  147. package/lib/server/routes.d.ts +39 -7
  148. package/lib/server/routes.js +182 -95
  149. package/lib/server/siteMetadata.d.ts +12 -0
  150. package/lib/server/siteMetadata.js +81 -0
  151. package/lib/server/translations/translations.d.ts +14 -19
  152. package/lib/server/translations/translations.js +40 -72
  153. package/lib/server/translations/translationsExtractor.d.ts +10 -4
  154. package/lib/server/translations/translationsExtractor.js +158 -122
  155. package/lib/server/utils.d.ts +9 -3
  156. package/lib/server/utils.js +7 -9
  157. package/lib/webpack/aliases/index.d.ts +34 -0
  158. package/lib/webpack/aliases/index.js +106 -0
  159. package/lib/webpack/base.d.ts +3 -4
  160. package/lib/webpack/base.js +45 -57
  161. package/lib/webpack/client.d.ts +3 -3
  162. package/lib/webpack/client.js +12 -19
  163. package/lib/webpack/plugins/ChunkAssetPlugin.d.ts +13 -3
  164. package/lib/webpack/plugins/ChunkAssetPlugin.js +24 -17
  165. package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +6 -25
  166. package/lib/webpack/plugins/CleanWebpackPlugin.js +33 -17
  167. package/lib/webpack/plugins/LogPlugin.d.ts +1 -1
  168. package/lib/webpack/plugins/LogPlugin.js +4 -5
  169. package/lib/webpack/plugins/WaitPlugin.d.ts +3 -3
  170. package/lib/webpack/plugins/WaitPlugin.js +1 -1
  171. package/lib/webpack/server.d.ts +5 -5
  172. package/lib/webpack/server.js +22 -9
  173. package/lib/{client → webpack}/templates/index.html.template.ejs +0 -0
  174. package/lib/webpack/templates/ssr.html.template.d.ts +8 -0
  175. package/lib/{client → webpack}/templates/ssr.html.template.js +3 -2
  176. package/lib/webpack/utils.d.ts +15 -31
  177. package/lib/webpack/utils.js +61 -182
  178. package/package.json +81 -77
  179. package/bin/docusaurus.js +0 -325
  180. package/lib/.tsbuildinfo +0 -1
  181. package/lib/choosePort.js +0 -105
  182. package/lib/client/.eslintrc.js +0 -29
  183. package/lib/client/.tsbuildinfo +0 -1
  184. package/lib/client/client-lifecycles-dispatcher.d.ts +0 -12
  185. package/lib/client/client-lifecycles-dispatcher.js +0 -27
  186. package/lib/client/nprogress.css +0 -36
  187. package/lib/commands/buildRemoteBranchUrl.d.ts +0 -7
  188. package/lib/commands/buildRemoteBranchUrl.js +0 -27
  189. package/lib/commands/commandUtils.d.ts +0 -3
  190. package/lib/commands/commandUtils.js +0 -21
  191. package/lib/commands/swizzle.d.ts +0 -9
  192. package/lib/commands/swizzle.js +0 -245
  193. package/lib/constants.d.ts +0 -18
  194. package/lib/constants.js +0 -23
  195. package/lib/server/client-modules/index.js +0 -14
  196. package/lib/server/duplicateRoutes.d.ts +0 -10
  197. package/lib/server/duplicateRoutes.js +0 -38
  198. package/lib/server/html-tags/htmlTags.js +0 -40
  199. package/lib/server/html-tags/index.d.ts +0 -9
  200. package/lib/server/html-tags/index.js +0 -43
  201. package/lib/server/loadSetup.d.ts +0 -9
  202. package/lib/server/loadSetup.js +0 -25
  203. package/lib/server/plugins/applyRouteTrailingSlash.d.ts +0 -8
  204. package/lib/server/plugins/applyRouteTrailingSlash.js +0 -19
  205. package/lib/server/presets/index.d.ts +0 -11
  206. package/lib/server/presets/index.js +0 -48
  207. package/lib/server/themes/alias.d.ts +0 -8
  208. package/lib/server/themes/alias.js +0 -40
  209. package/lib/server/themes/index.d.ts +0 -12
  210. package/lib/server/themes/index.js +0 -47
  211. package/lib/server/versions/__fixtures__/dummy-plugin.js +0 -0
  212. package/lib/server/versions/__fixtures__/package.json +0 -3
  213. package/lib/server/versions/__tests/index.test.js +0 -25
  214. package/lib/server/versions/index.d.ts +0 -10
  215. package/lib/server/versions/index.js +0 -50
  216. package/lib/webpack/react-dev-utils-webpack5/README.md +0 -11
  217. package/lib/webpack/react-dev-utils-webpack5/evalSourceMapMiddleware.js +0 -57
  218. package/lib/webpack/react-dev-utils-webpack5/formatWebpackMessages.js +0 -138
  219. package/lib/webpack/react-dev-utils-webpack5/webpackHotDevClient.js +0 -285
  220. package/lib/webpack/sharedModuleAliases.d.ts +0 -10
  221. package/lib/webpack/sharedModuleAliases.js +0 -18
  222. package/tsconfig.client.json +0 -13
  223. package/tsconfig.json +0 -13
@@ -4,7 +4,7 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import React, { useContext, createContext } from 'react';
7
+ import React, { useContext } from 'react';
8
8
  export const createStatefulLinksCollector = () => {
9
9
  // Set to dedup, as it's not useful to collect multiple times the same link
10
10
  const allLinks = new Set();
@@ -12,20 +12,15 @@ export const createStatefulLinksCollector = () => {
12
12
  collectLink: (link) => {
13
13
  allLinks.add(link);
14
14
  },
15
- getCollectedLinks: () => {
16
- return [...allLinks];
17
- },
15
+ getCollectedLinks: () => [...allLinks],
18
16
  };
19
17
  };
20
- const Context = createContext({
18
+ const Context = React.createContext({
21
19
  collectLink: () => {
22
- // noop by default for client
23
- // we only use the broken links checker server-side
20
+ // No-op for client. We only use the broken links checker server-side.
24
21
  },
25
22
  });
26
- export const useLinksCollector = () => {
27
- return useContext(Context);
28
- };
29
- export const ProvideLinksCollector = ({ children, linksCollector, }) => {
30
- return React.createElement(Context.Provider, { value: linksCollector }, children);
31
- };
23
+ export const useLinksCollector = () => useContext(Context);
24
+ export function LinksCollectorProvider({ children, linksCollector, }) {
25
+ return <Context.Provider value={linksCollector}>{children}</Context.Provider>;
26
+ }
@@ -4,6 +4,20 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import './nprogress.css';
8
- declare const _default: any;
9
- export default _default;
7
+ import React from 'react';
8
+ import type { Location } from 'history';
9
+ declare type Props = {
10
+ readonly location: Location;
11
+ readonly children: JSX.Element;
12
+ };
13
+ declare type State = {
14
+ nextRouteHasLoaded: boolean;
15
+ };
16
+ declare class PendingNavigation extends React.Component<Props, State> {
17
+ private previousLocation;
18
+ private routeUpdateCleanupCb;
19
+ constructor(props: Props);
20
+ shouldComponentUpdate(nextProps: Props, nextState: State): boolean;
21
+ render(): JSX.Element;
22
+ }
23
+ export default PendingNavigation;
@@ -5,19 +5,23 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import React from 'react';
8
- import { Route, withRouter } from 'react-router-dom';
9
- import nprogress from 'nprogress';
10
- import clientLifecyclesDispatcher from './client-lifecycles-dispatcher';
8
+ import { Route } from 'react-router-dom';
9
+ import ClientLifecyclesDispatcher, { dispatchLifecycleAction, } from './ClientLifecyclesDispatcher';
10
+ import ExecutionEnvironment from './exports/ExecutionEnvironment';
11
11
  import preload from './preload';
12
- import normalizeLocation from './normalizeLocation';
13
- import './nprogress.css';
14
- nprogress.configure({ showSpinner: false });
15
12
  class PendingNavigation extends React.Component {
13
+ previousLocation;
14
+ routeUpdateCleanupCb;
16
15
  constructor(props) {
17
16
  super(props);
18
17
  // previousLocation doesn't affect rendering, hence not stored in state.
19
18
  this.previousLocation = null;
20
- this.progressBarTimeout = null;
19
+ this.routeUpdateCleanupCb = ExecutionEnvironment.canUseDOM
20
+ ? dispatchLifecycleAction('onRouteUpdate', {
21
+ previousLocation: null,
22
+ location: this.props.location,
23
+ })
24
+ : () => { };
21
25
  this.state = {
22
26
  nextRouteHasLoaded: true,
23
27
  };
@@ -25,74 +29,45 @@ class PendingNavigation extends React.Component {
25
29
  // Intercept location update and still show current route until next route
26
30
  // is done loading.
27
31
  shouldComponentUpdate(nextProps, nextState) {
28
- const routeDidChange = nextProps.location !== this.props.location;
29
- const { routes, delay = 1000 } = this.props;
30
- // If `routeDidChange` is true, means the router is trying to navigate to a new
31
- // route. We will preload the new route.
32
- if (routeDidChange) {
33
- const nextLocation = normalizeLocation(nextProps.location);
34
- this.startProgressBar(delay);
35
- // Save the location first.
36
- this.previousLocation = normalizeLocation(this.props.location);
37
- this.setState({
38
- nextRouteHasLoaded: false,
39
- });
40
- // Load data while the old screen remains.
41
- preload(routes, nextLocation.pathname)
42
- .then(() => {
43
- clientLifecyclesDispatcher.onRouteUpdate({
44
- previousLocation: this.previousLocation,
45
- location: nextLocation,
46
- });
47
- // Route has loaded, we can reset previousLocation.
48
- this.previousLocation = null;
49
- this.setState({
50
- nextRouteHasLoaded: true,
51
- }, this.stopProgressBar);
52
- const { hash } = nextLocation;
53
- if (!hash) {
54
- window.scrollTo(0, 0);
55
- }
56
- else {
57
- const id = decodeURIComponent(hash.substring(1));
58
- const element = document.getElementById(id);
59
- if (element) {
60
- element.scrollIntoView();
61
- }
62
- }
63
- })
64
- .catch((e) => console.warn(e));
65
- return false;
32
+ if (nextProps.location === this.props.location) {
33
+ // `nextRouteHasLoaded` is false means there's a pending route transition.
34
+ // Don't update until it's done.
35
+ return nextState.nextRouteHasLoaded;
66
36
  }
67
- // There's a pending route transition. Don't update until it's done.
68
- if (!nextState.nextRouteHasLoaded) {
69
- return false;
70
- }
71
- // Route has loaded, we can update now.
72
- return true;
73
- }
74
- clearProgressBarTimeout() {
75
- if (this.progressBarTimeout) {
76
- clearTimeout(this.progressBarTimeout);
77
- this.progressBarTimeout = null;
78
- }
79
- }
80
- startProgressBar(delay) {
81
- this.clearProgressBarTimeout();
82
- this.progressBarTimeout = setTimeout(() => {
83
- clientLifecyclesDispatcher.onRouteUpdateDelayed({
84
- location: normalizeLocation(this.props.location),
85
- });
86
- nprogress.start();
87
- }, delay);
88
- }
89
- stopProgressBar() {
90
- this.clearProgressBarTimeout();
91
- nprogress.done();
37
+ // props.location being different means the router is trying to navigate to
38
+ // a new route. We will preload the new route.
39
+ const nextLocation = nextProps.location;
40
+ // Save the location first.
41
+ this.previousLocation = this.props.location;
42
+ this.setState({ nextRouteHasLoaded: false });
43
+ this.routeUpdateCleanupCb = dispatchLifecycleAction('onRouteUpdate', {
44
+ previousLocation: this.previousLocation,
45
+ location: nextLocation,
46
+ });
47
+ // Load data while the old screen remains. Force preload instead of using
48
+ // `window.docusaurus`, because we want to avoid loading screen even when
49
+ // user is on saveData
50
+ preload(nextLocation.pathname)
51
+ .then(() => {
52
+ this.routeUpdateCleanupCb();
53
+ this.setState({ nextRouteHasLoaded: true });
54
+ })
55
+ .catch((e) => {
56
+ console.warn(e);
57
+ // If chunk loading failed, it could be because the path to a chunk
58
+ // no longer exists due to a new deployment. Force refresh the page
59
+ // instead of just not navigating.
60
+ window.location.reload();
61
+ });
62
+ return false;
92
63
  }
93
64
  render() {
94
65
  const { children, location } = this.props;
95
- return (React.createElement(Route, { location: normalizeLocation(location), render: () => children }));
66
+ // Use a controlled <Route> to trick all descendants into rendering the old
67
+ // location.
68
+ return (<ClientLifecyclesDispatcher previousLocation={this.previousLocation} location={location}>
69
+ <Route location={location} render={() => children}/>
70
+ </ClientLifecyclesDispatcher>);
96
71
  }
97
72
  }
98
- export default withRouter(PendingNavigation);
73
+ export default PendingNavigation;
@@ -5,5 +5,4 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  /// <reference types="react" />
8
- import './styles.module.css';
9
- export default function BaseUrlIssueBanner(): JSX.Element | null;
8
+ export default function SiteMetadataDefaults(): JSX.Element;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import React from 'react';
8
+ import Head from '@docusaurus/Head';
9
+ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
10
+ import useBaseUrl from '@docusaurus/useBaseUrl';
11
+ export default function SiteMetadataDefaults() {
12
+ const { siteConfig: { favicon, title }, i18n: { currentLocale, localeConfigs }, } = useDocusaurusContext();
13
+ const faviconUrl = useBaseUrl(favicon);
14
+ const { htmlLang, direction: htmlDir } = localeConfigs[currentLocale];
15
+ return (<Head>
16
+ <html lang={htmlLang} dir={htmlDir}/>
17
+ <title>{title}</title>
18
+ <meta property="og:title" content={title}/>
19
+ {favicon && <link rel="icon" href={faviconUrl}/>}
20
+ </Head>);
21
+ }
@@ -4,8 +4,8 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- /**
8
- * Detects if program is running on port and prompts user
9
- * to choose another if port is already being used
10
- */
11
- export default function choosePort(host: string, defaultPort: number): Promise<number | null>;
7
+ import React, { type ReactNode } from 'react';
8
+ export declare const Context: React.Context<boolean>;
9
+ export declare function BrowserContextProvider({ children, }: {
10
+ children: ReactNode;
11
+ }): JSX.Element;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import React, { useEffect, useState } from 'react';
8
+ // Encapsulate the logic to avoid React hydration problems
9
+ // See https://www.joshwcomeau.com/react/the-perils-of-rehydration/
10
+ // On first client-side render, we need to render exactly as the server rendered
11
+ // isBrowser is set to true only after a successful hydration
12
+ // Note, isBrowser is not part of useDocusaurusContext() for perf reasons
13
+ // Using useDocusaurusContext() (much more common need) should not trigger
14
+ // re-rendering after a successful hydration
15
+ export const Context = React.createContext(false);
16
+ export function BrowserContextProvider({ children, }) {
17
+ const [isBrowser, setIsBrowser] = useState(false);
18
+ useEffect(() => {
19
+ setIsBrowser(true);
20
+ }, []);
21
+ return <Context.Provider value={isBrowser}>{children}</Context.Provider>;
22
+ }
@@ -5,23 +5,28 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import React from 'react';
8
- import { hydrate, render } from 'react-dom';
8
+ import ReactDOM from 'react-dom';
9
9
  import { BrowserRouter } from 'react-router-dom';
10
- import routes from '@generated/routes';
10
+ import { HelmetProvider } from 'react-helmet-async';
11
11
  import ExecutionEnvironment from './exports/ExecutionEnvironment';
12
12
  import App from './App';
13
13
  import preload from './preload';
14
14
  import docusaurus from './docusaurus';
15
- // Client-side render (e.g: running in browser) to become single-page application (SPA).
15
+ // Client-side render (e.g: running in browser) to become single-page
16
+ // application (SPA).
16
17
  if (ExecutionEnvironment.canUseDOM) {
17
18
  window.docusaurus = docusaurus;
18
- // For production, attempt to hydrate existing markup for performant first-load experience.
19
+ // For production, attempt to hydrate existing markup for performant
20
+ // first-load experience.
19
21
  // For development, there is no existing markup so we had to render it.
20
- // Note that we also preload async component to avoid first-load loading screen.
21
- const renderMethod = process.env.NODE_ENV === 'production' ? hydrate : render;
22
- preload(routes, window.location.pathname).then(() => {
23
- renderMethod(React.createElement(BrowserRouter, null,
24
- React.createElement(App, null)), document.getElementById('__docusaurus'));
22
+ // We also preload async component to avoid first-load loading screen.
23
+ const renderMethod = process.env.NODE_ENV === 'production' ? ReactDOM.hydrate : ReactDOM.render;
24
+ preload(window.location.pathname).then(() => {
25
+ renderMethod(<HelmetProvider>
26
+ <BrowserRouter>
27
+ <App />
28
+ </BrowserRouter>
29
+ </HelmetProvider>, document.getElementById('__docusaurus'));
25
30
  });
26
31
  // Webpack Hot Module Replacement API
27
32
  if (module.hot) {
@@ -1,16 +1,22 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
1
7
  declare global {
2
8
  const __webpack_require__: {
3
9
  gca: (name: string) => string;
4
10
  };
5
11
  interface Navigator {
6
- connection: {
12
+ connection?: {
7
13
  effectiveType: string;
8
14
  saveData: boolean;
9
15
  };
10
16
  }
11
17
  }
12
- declare const docusaurus: {
13
- prefetch: (routePath: string) => boolean;
14
- preload: (routePath: string) => boolean;
15
- };
16
- export default docusaurus;
18
+ declare const _default: Readonly<{
19
+ prefetch(routePath: string): false | Promise<void[]>;
20
+ preload(routePath: string): false | Promise<void[]>;
21
+ }>;
22
+ export default _default;
@@ -10,64 +10,51 @@ import routes from '@generated/routes';
10
10
  import prefetchHelper from './prefetch';
11
11
  import preloadHelper from './preload';
12
12
  import flat from './flat';
13
- const fetched = {};
14
- const loaded = {};
15
- const isSlowConnection = () => {
16
- // If user is on slow or constrained connection.
17
- if (`connection` in navigator) {
18
- if ((navigator.connection.effectiveType || ``).indexOf(`2g`) !== -1 &&
19
- navigator.connection.saveData) {
20
- return true;
21
- }
22
- }
23
- return false;
24
- };
25
- const canPrefetch = (routePath) => !isSlowConnection() && !loaded[routePath] && !fetched[routePath];
26
- const canPreload = (routePath) => !isSlowConnection() && !loaded[routePath];
27
- const flatten = (arrays) => Array.prototype.concat.apply([], arrays);
13
+ const fetched = new Set();
14
+ const loaded = new Set();
15
+ // If user is on slow or constrained connection.
16
+ const isSlowConnection = () => navigator.connection?.effectiveType.includes('2g') ||
17
+ navigator.connection?.saveData;
18
+ const canPrefetch = (routePath) => !isSlowConnection() && !loaded.has(routePath) && !fetched.has(routePath);
19
+ const canPreload = (routePath) => !isSlowConnection() && !loaded.has(routePath);
20
+ const getChunkNamesToLoad = (path) => Object.entries(routesChunkNames)
21
+ .filter(
28
22
  // Remove the last part containing the route hash
29
23
  // input: /blog/2018/12/14/Happy-First-Birthday-Slash-fe9
30
24
  // output: /blog/2018/12/14/Happy-First-Birthday-Slash
31
- const removeRouteNameHash = (str) => str.replace(/(-[^-]+)$/, '');
32
- const getChunkNamesToLoad = (path) => {
33
- return flatten(Object.entries(routesChunkNames)
34
- .filter(([routeNameWithHash]) => removeRouteNameHash(routeNameWithHash) === path)
35
- .map(([, routeChunks]) => {
36
- // flat() is useful for nested chunk names, it's not like array.flat()
37
- return Object.values(flat(routeChunks));
38
- }));
39
- };
25
+ ([routeNameWithHash]) => routeNameWithHash.replace(/-[^-]+$/, '') === path)
26
+ .flatMap(([, routeChunks]) => Object.values(flat(routeChunks)));
40
27
  const docusaurus = {
41
- prefetch: (routePath) => {
28
+ prefetch(routePath) {
42
29
  if (!canPrefetch(routePath)) {
43
30
  return false;
44
31
  }
45
- // Prevent future duplicate prefetch of routePath.
46
- fetched[routePath] = true;
32
+ fetched.add(routePath);
47
33
  // Find all webpack chunk names needed.
48
34
  const matches = matchRoutes(routes, routePath);
49
- const chunkNamesNeeded = flatten(matches.map((match) => getChunkNamesToLoad(match.route.path)));
35
+ const chunkNamesNeeded = matches.flatMap((match) => getChunkNamesToLoad(match.route.path));
50
36
  // Prefetch all webpack chunk assets file needed.
51
- chunkNamesNeeded.forEach((chunkName) => {
52
- // "__webpack_require__.gca" is a custom function provided by ChunkAssetPlugin.
53
- // Pass it the chunkName or chunkId you want to load and it will return the URL for that chunk.
54
- // eslint-disable-next-line no-undef
37
+ return Promise.all(chunkNamesNeeded.map((chunkName) => {
38
+ // "__webpack_require__.gca" is injected by ChunkAssetPlugin. Pass it
39
+ // the name of the chunk you want to load and it will return its URL.
40
+ // eslint-disable-next-line camelcase
55
41
  const chunkAsset = __webpack_require__.gca(chunkName);
56
- // In some cases, webpack might decide to optimize further & hence the chunk assets are merged to another chunk/previous chunk.
57
- // Hence, we can safely filter it out/don't need to load it.
58
- if (chunkAsset && !/undefined/.test(chunkAsset)) {
59
- prefetchHelper(chunkAsset);
42
+ // In some cases, webpack might decide to optimize further, leading to
43
+ // the chunk assets being merged to another chunk. In this case, we can
44
+ // safely filter it out and don't need to load it.
45
+ if (chunkAsset && !chunkAsset.includes('undefined')) {
46
+ return prefetchHelper(chunkAsset);
60
47
  }
61
- });
62
- return true;
48
+ return Promise.resolve();
49
+ }));
63
50
  },
64
- preload: (routePath) => {
51
+ preload(routePath) {
65
52
  if (!canPreload(routePath)) {
66
53
  return false;
67
54
  }
68
- loaded[routePath] = true;
69
- preloadHelper(routes, routePath);
70
- return true;
55
+ loaded.add(routePath);
56
+ return preloadHelper(routePath);
71
57
  },
72
58
  };
73
- export default docusaurus;
59
+ // This object is directly mounted onto window, better freeze it
60
+ export default Object.freeze(docusaurus);
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import React, { type ReactNode } from 'react';
8
+ import type { DocusaurusContext } from '@docusaurus/types';
9
+ export declare const Context: React.Context<DocusaurusContext>;
10
+ export declare function DocusaurusContextProvider({ children, }: {
11
+ children: ReactNode;
12
+ }): JSX.Element;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import React from 'react';
8
+ import siteConfig from '@generated/docusaurus.config';
9
+ import globalData from '@generated/globalData';
10
+ import i18n from '@generated/i18n';
11
+ import codeTranslations from '@generated/codeTranslations';
12
+ import siteMetadata from '@generated/site-metadata';
13
+ // Static value on purpose: don't make it dynamic!
14
+ // Using context is still useful for testability reasons.
15
+ const contextValue = {
16
+ siteConfig,
17
+ siteMetadata,
18
+ globalData,
19
+ i18n,
20
+ codeTranslations,
21
+ };
22
+ export const Context = React.createContext(contextValue);
23
+ export function DocusaurusContextProvider({ children, }) {
24
+ return <Context.Provider value={contextValue}>{children}</Context.Provider>;
25
+ }
@@ -4,9 +4,7 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
+ /// <reference types="@docusaurus/module-type-aliases" />
7
8
  /// <reference types="react" />
8
- declare function BrowserOnly({ children, fallback, }: {
9
- children?: () => JSX.Element;
10
- fallback?: JSX.Element;
11
- }): JSX.Element | null;
12
- export default BrowserOnly;
9
+ import type { Props } from '@docusaurus/BrowserOnly';
10
+ export default function BrowserOnly({ children, fallback, }: Props): JSX.Element | null;
@@ -4,13 +4,19 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import React from 'react';
8
- import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
9
- function BrowserOnly({ children, fallback, }) {
10
- const { isClient } = useDocusaurusContext();
11
- if (isClient && children != null) {
12
- return React.createElement(React.Fragment, null, children());
7
+ import React, { isValidElement } from 'react';
8
+ import useIsBrowser from '@docusaurus/useIsBrowser';
9
+ // Similar comp to the one described here:
10
+ // https://www.joshwcomeau.com/react/the-perils-of-rehydration/#abstractions
11
+ export default function BrowserOnly({ children, fallback, }) {
12
+ const isBrowser = useIsBrowser();
13
+ if (isBrowser) {
14
+ if (typeof children !== 'function' &&
15
+ process.env.NODE_ENV === 'development') {
16
+ throw new Error(`Docusaurus error: The children of <BrowserOnly> must be a "render function", e.g. <BrowserOnly>{() => <span>{window.location.href}</span>}</BrowserOnly>.
17
+ Current type: ${isValidElement(children) ? 'React element' : typeof children}`);
18
+ }
19
+ return <>{children?.()}</>;
13
20
  }
14
- return fallback || null;
21
+ return fallback ?? null;
15
22
  }
16
- export default BrowserOnly;
@@ -5,5 +5,9 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import Loadable from 'react-loadable';
8
- declare function ComponentCreator(path: string, hash: string): ReturnType<typeof Loadable>;
9
- export default ComponentCreator;
8
+ declare global {
9
+ interface NodeRequire {
10
+ resolveWeak: (name: string) => number;
11
+ }
12
+ }
13
+ export default function ComponentCreator(path: string, hash: string): ReturnType<typeof Loadable>;