@docusaurus/core 2.0.0-beta.15d451942 → 2.0.0-beta.18

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 (200) hide show
  1. package/bin/beforeCli.mjs +133 -0
  2. package/bin/docusaurus.mjs +279 -0
  3. package/lib/babel/preset.d.ts +8 -3
  4. package/lib/babel/preset.js +8 -7
  5. package/lib/choosePort.js +31 -43
  6. package/lib/client/.eslintrc.js +0 -1
  7. package/lib/client/App.d.ts +1 -2
  8. package/lib/client/App.js +23 -24
  9. package/lib/client/LinksCollector.d.ts +3 -3
  10. package/lib/client/LinksCollector.js +7 -11
  11. package/lib/client/PendingNavigation.d.ts +24 -1
  12. package/lib/client/PendingNavigation.js +5 -7
  13. package/lib/{webpack/sharedModuleAliases.d.ts → client/SiteMetadataDefaults.d.ts} +2 -4
  14. package/lib/client/SiteMetadataDefaults.js +19 -0
  15. package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.d.ts +13 -0
  16. package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.js +15 -10
  17. package/lib/client/browserContext.d.ts +11 -0
  18. package/lib/client/browserContext.js +22 -0
  19. package/lib/client/client-lifecycles-dispatcher.d.ts +2 -5
  20. package/lib/client/client-lifecycles-dispatcher.js +6 -10
  21. package/lib/client/clientEntry.js +13 -7
  22. package/lib/client/docusaurus.d.ts +6 -0
  23. package/lib/client/docusaurus.js +16 -25
  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 +2 -3
  27. package/lib/client/exports/BrowserOnly.js +14 -7
  28. package/lib/client/exports/ComponentCreator.d.ts +1 -2
  29. package/lib/client/exports/ComponentCreator.js +40 -19
  30. package/lib/client/exports/ErrorBoundary.d.ts +18 -0
  31. package/lib/client/exports/ErrorBoundary.js +35 -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 -26
  36. package/lib/client/exports/Link.d.ts +11 -5
  37. package/lib/client/exports/Link.js +31 -20
  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 +8 -5
  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 +4 -8
  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/{server/loadSetup.d.ts → client/exports/useRouteContext.d.ts} +2 -3
  54. package/lib/client/exports/useRouteContext.js +15 -0
  55. package/lib/client/flat.d.ts +4 -2
  56. package/lib/client/flat.js +9 -13
  57. package/lib/client/normalizeLocation.d.ts +2 -5
  58. package/lib/client/normalizeLocation.js +2 -7
  59. package/lib/client/nprogress.css +2 -2
  60. package/lib/client/prefetch.d.ts +1 -2
  61. package/lib/client/prefetch.js +4 -6
  62. package/lib/client/preload.d.ts +3 -2
  63. package/lib/client/preload.js +5 -10
  64. package/lib/client/routeContext.d.ts +13 -0
  65. package/lib/client/routeContext.js +31 -0
  66. package/lib/client/serverEntry.d.ts +10 -0
  67. package/lib/client/serverEntry.js +90 -142
  68. package/lib/client/theme-fallback/Error/index.d.ts +10 -0
  69. package/lib/client/theme-fallback/Error/index.js +43 -0
  70. package/lib/client/theme-fallback/Layout/index.d.ts +10 -0
  71. package/lib/client/theme-fallback/Layout/index.js +2 -26
  72. package/lib/client/theme-fallback/Loading/index.d.ts +9 -0
  73. package/lib/client/theme-fallback/Loading/index.js +47 -115
  74. package/lib/client/{exports/context.js → theme-fallback/NotFound/index.d.ts} +2 -2
  75. package/lib/client/theme-fallback/NotFound/index.js +17 -18
  76. package/lib/client/theme-fallback/Root/index.d.ts +10 -0
  77. package/lib/client/theme-fallback/Root/index.js +2 -6
  78. package/lib/client/theme-fallback/SiteMetadata/index.d.ts +8 -0
  79. package/lib/client/theme-fallback/SiteMetadata/index.js +10 -0
  80. package/lib/commands/build.d.ts +1 -1
  81. package/lib/commands/build.js +50 -54
  82. package/lib/commands/clear.d.ts +6 -0
  83. package/lib/commands/clear.js +30 -19
  84. package/lib/commands/commandUtils.d.ts +7 -1
  85. package/lib/commands/commandUtils.js +7 -7
  86. package/lib/commands/deploy.d.ts +1 -1
  87. package/lib/commands/deploy.js +83 -66
  88. package/lib/commands/external.d.ts +2 -2
  89. package/lib/commands/external.js +4 -8
  90. package/lib/commands/serve.d.ts +1 -1
  91. package/lib/commands/serve.js +25 -12
  92. package/lib/commands/start.d.ts +1 -1
  93. package/lib/commands/start.js +96 -87
  94. package/lib/commands/swizzle/actions.d.ts +23 -0
  95. package/lib/commands/swizzle/actions.js +101 -0
  96. package/lib/commands/swizzle/common.d.ts +33 -0
  97. package/lib/commands/swizzle/common.js +56 -0
  98. package/lib/commands/swizzle/components.d.ts +29 -0
  99. package/lib/commands/swizzle/components.js +162 -0
  100. package/lib/commands/swizzle/config.d.ts +10 -0
  101. package/lib/commands/swizzle/config.js +80 -0
  102. package/lib/commands/swizzle/context.d.ts +8 -0
  103. package/lib/commands/swizzle/context.js +28 -0
  104. package/lib/commands/swizzle/index.d.ts +8 -0
  105. package/lib/commands/swizzle/index.js +115 -0
  106. package/lib/commands/swizzle/prompts.d.ts +12 -0
  107. package/lib/commands/swizzle/prompts.js +110 -0
  108. package/lib/{client/exports/context.d.ts → commands/swizzle/tables.d.ts} +3 -4
  109. package/lib/commands/swizzle/tables.js +113 -0
  110. package/lib/commands/swizzle/themes.d.ts +20 -0
  111. package/lib/commands/swizzle/themes.js +106 -0
  112. package/lib/commands/writeHeadingIds.d.ts +2 -6
  113. package/lib/commands/writeHeadingIds.js +19 -76
  114. package/lib/commands/writeTranslations.d.ts +2 -2
  115. package/lib/commands/writeTranslations.js +37 -17
  116. package/lib/index.d.ts +10 -9
  117. package/lib/index.js +20 -19
  118. package/lib/server/brokenLinks.d.ts +4 -17
  119. package/lib/server/brokenLinks.js +64 -52
  120. package/lib/server/client-modules/index.d.ts +2 -2
  121. package/lib/server/client-modules/index.js +4 -3
  122. package/lib/server/config.d.ts +2 -2
  123. package/lib/server/config.js +8 -5
  124. package/lib/server/configValidation.d.ts +4 -2
  125. package/lib/server/configValidation.js +93 -37
  126. package/lib/server/duplicateRoutes.d.ts +1 -3
  127. package/lib/server/duplicateRoutes.js +17 -13
  128. package/lib/server/html-tags/htmlTags.js +7 -8
  129. package/lib/server/html-tags/index.d.ts +2 -3
  130. package/lib/server/html-tags/index.js +3 -4
  131. package/lib/server/i18n.d.ts +2 -3
  132. package/lib/server/i18n.js +27 -48
  133. package/lib/server/index.d.ts +9 -2
  134. package/lib/server/index.js +175 -96
  135. package/lib/server/moduleShorthand.d.ts +9 -0
  136. package/lib/server/moduleShorthand.js +46 -0
  137. package/lib/server/plugins/applyRouteTrailingSlash.d.ts +9 -0
  138. package/lib/server/plugins/applyRouteTrailingSlash.js +19 -0
  139. package/lib/server/plugins/index.d.ts +4 -5
  140. package/lib/server/plugins/index.js +69 -45
  141. package/lib/server/plugins/init.d.ts +15 -5
  142. package/lib/server/plugins/init.js +68 -81
  143. package/lib/server/plugins/pluginIds.d.ts +2 -2
  144. package/lib/server/plugins/pluginIds.js +8 -5
  145. package/lib/server/presets/index.d.ts +3 -3
  146. package/lib/server/presets/index.js +16 -23
  147. package/lib/server/routes.d.ts +3 -5
  148. package/lib/server/routes.js +68 -36
  149. package/lib/server/themes/alias.d.ts +3 -2
  150. package/lib/server/themes/alias.js +24 -13
  151. package/lib/server/themes/index.d.ts +6 -2
  152. package/lib/server/themes/index.js +36 -24
  153. package/lib/server/translations/translations.d.ts +21 -17
  154. package/lib/server/translations/translations.js +33 -54
  155. package/lib/server/translations/translationsExtractor.d.ts +12 -5
  156. package/lib/server/translations/translationsExtractor.js +159 -121
  157. package/lib/server/utils.d.ts +9 -3
  158. package/lib/server/utils.js +7 -9
  159. package/lib/server/versions/index.d.ts +3 -4
  160. package/lib/server/versions/index.js +22 -24
  161. package/lib/webpack/base.d.ts +6 -4
  162. package/lib/webpack/base.js +51 -36
  163. package/lib/webpack/client.d.ts +3 -3
  164. package/lib/webpack/client.js +12 -19
  165. package/lib/webpack/plugins/ChunkAssetPlugin.d.ts +13 -3
  166. package/lib/webpack/plugins/ChunkAssetPlugin.js +24 -17
  167. package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +6 -6
  168. package/lib/webpack/plugins/CleanWebpackPlugin.js +10 -16
  169. package/lib/webpack/plugins/LogPlugin.d.ts +1 -1
  170. package/lib/webpack/plugins/LogPlugin.js +4 -5
  171. package/lib/webpack/plugins/WaitPlugin.d.ts +1 -1
  172. package/lib/webpack/plugins/WaitPlugin.js +1 -1
  173. package/lib/webpack/server.d.ts +3 -3
  174. package/lib/webpack/server.js +14 -7
  175. package/lib/{client → webpack}/templates/index.html.template.ejs +0 -0
  176. package/lib/webpack/templates/ssr.html.template.d.ts +8 -0
  177. package/lib/{client → webpack}/templates/ssr.html.template.js +5 -4
  178. package/lib/webpack/utils.d.ts +15 -30
  179. package/lib/webpack/utils.js +57 -175
  180. package/package.json +77 -70
  181. package/bin/docusaurus.js +0 -326
  182. package/lib/.tsbuildinfo +0 -5732
  183. package/lib/client/.tsbuildinfo +0 -4171
  184. package/lib/commands/buildRemoteBranchUrl.d.ts +0 -7
  185. package/lib/commands/buildRemoteBranchUrl.js +0 -27
  186. package/lib/commands/swizzle.d.ts +0 -9
  187. package/lib/commands/swizzle.js +0 -245
  188. package/lib/constants.d.ts +0 -18
  189. package/lib/constants.js +0 -23
  190. package/lib/server/loadSetup.js +0 -25
  191. package/lib/server/versions/__fixtures__/dummy-plugin.js +0 -0
  192. package/lib/server/versions/__fixtures__/package.json +0 -3
  193. package/lib/server/versions/__tests/index.test.js +0 -25
  194. package/lib/webpack/react-dev-utils-webpack5/README.md +0 -11
  195. package/lib/webpack/react-dev-utils-webpack5/evalSourceMapMiddleware.js +0 -57
  196. package/lib/webpack/react-dev-utils-webpack5/formatWebpackMessages.js +0 -138
  197. package/lib/webpack/react-dev-utils-webpack5/webpackHotDevClient.js +0 -285
  198. package/lib/webpack/sharedModuleAliases.js +0 -18
  199. package/tsconfig.client.json +0 -13
  200. 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,16 @@ 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
20
  // noop by default for client
23
21
  // we only use the broken links checker server-side
24
22
  },
25
23
  });
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
- };
24
+ export const useLinksCollector = () => useContext(Context);
25
+ export function LinksCollectorProvider({ children, linksCollector, }) {
26
+ return <Context.Provider value={linksCollector}>{children}</Context.Provider>;
27
+ }
@@ -4,6 +4,29 @@
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="node" />
8
+ import React from 'react';
9
+ import { type RouteComponentProps } from 'react-router-dom';
10
+ import type { RouteConfig } from 'react-router-config';
11
+ import type { Location } from 'history';
7
12
  import './nprogress.css';
8
- declare const _default: any;
13
+ interface Props extends RouteComponentProps {
14
+ readonly routes: RouteConfig[];
15
+ readonly delay: number;
16
+ readonly location: Location;
17
+ }
18
+ interface State {
19
+ nextRouteHasLoaded: boolean;
20
+ }
21
+ declare class PendingNavigation extends React.Component<Props, State> {
22
+ previousLocation: Location | null;
23
+ progressBarTimeout: NodeJS.Timeout | null;
24
+ constructor(props: Props);
25
+ shouldComponentUpdate(nextProps: Props, nextState: State): boolean;
26
+ private clearProgressBarTimeout;
27
+ private startProgressBar;
28
+ private stopProgressBar;
29
+ render(): JSX.Element;
30
+ }
31
+ declare const _default: React.ComponentClass<Pick<Props, "routes" | "delay">, any> & import("react-router").WithRouterStatics<typeof PendingNavigation>;
9
32
  export default _default;
@@ -26,9 +26,9 @@ class PendingNavigation extends React.Component {
26
26
  // is done loading.
27
27
  shouldComponentUpdate(nextProps, nextState) {
28
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.
29
+ const { routes, delay } = this.props;
30
+ // If `routeDidChange` is true, means the router is trying to navigate to a
31
+ // new route. We will preload the new route.
32
32
  if (routeDidChange) {
33
33
  const nextLocation = normalizeLocation(nextProps.location);
34
34
  this.startProgressBar(delay);
@@ -46,9 +46,7 @@ class PendingNavigation extends React.Component {
46
46
  });
47
47
  // Route has loaded, we can reset previousLocation.
48
48
  this.previousLocation = null;
49
- this.setState({
50
- nextRouteHasLoaded: true,
51
- }, this.stopProgressBar);
49
+ this.setState({ nextRouteHasLoaded: true }, this.stopProgressBar);
52
50
  const { hash } = nextLocation;
53
51
  if (!hash) {
54
52
  window.scrollTo(0, 0);
@@ -92,7 +90,7 @@ class PendingNavigation extends React.Component {
92
90
  }
93
91
  render() {
94
92
  const { children, location } = this.props;
95
- return (React.createElement(Route, { location: normalizeLocation(location), render: () => children }));
93
+ return (<Route location={normalizeLocation(location)} render={() => children}/>);
96
94
  }
97
95
  }
98
96
  export default withRouter(PendingNavigation);
@@ -4,7 +4,5 @@
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
- declare const SharedModuleAliases: {
8
- 'react-loadable': string;
9
- };
10
- export default SharedModuleAliases;
7
+ /// <reference types="react" />
8
+ export default function SiteMetadataDefaults(): JSX.Element;
@@ -0,0 +1,19 @@
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, tagline, title }, i18n: { currentLocale, localeConfigs }, } = useDocusaurusContext();
13
+ const faviconUrl = useBaseUrl(favicon);
14
+ const { htmlLang, direction: htmlDir } = localeConfigs[currentLocale];
15
+ return (<Head defaultTitle={`${title}${tagline ? ` · ${tagline}` : ''}`}>
16
+ <html lang={htmlLang} dir={htmlDir}/>
17
+ {favicon && <link rel="icon" href={faviconUrl}/>}
18
+ </Head>);
19
+ }
@@ -6,4 +6,17 @@
6
6
  */
7
7
  /// <reference types="react" />
8
8
  import './styles.module.css';
9
+ declare global {
10
+ interface Window {
11
+ __DOCUSAURUS_INSERT_BASEURL_BANNER: boolean;
12
+ }
13
+ }
14
+ /**
15
+ * We want to help the users with a bad baseUrl configuration (very common
16
+ * error) Help message is inlined, and hidden if JS or CSS is able to load
17
+ * Note: it might create false positives (ie network failures): not a big deal
18
+ * Note: we only inline this for the homepage to avoid polluting all the site's
19
+ * pages
20
+ * @see https://github.com/facebook/docusaurus/pull/3621
21
+ */
9
22
  export default function BaseUrlIssueBanner(): JSX.Element | null;
@@ -67,21 +67,26 @@ function BaseUrlIssueBannerEnabled() {
67
67
  useLayoutEffect(() => {
68
68
  window[InsertBannerWindowAttribute] = false;
69
69
  }, []);
70
- return (React.createElement(React.Fragment, null,
71
- !ExecutionEnvironment.canUseDOM && (React.createElement(Head, null,
72
- React.createElement("script", null, createInlineScript(baseUrl)))),
73
- React.createElement("div", { id: BannerContainerId })));
70
+ return (<>
71
+ {!ExecutionEnvironment.canUseDOM && (<Head>
72
+ <script>{createInlineScript(baseUrl)}</script>
73
+ </Head>)}
74
+ <div id={BannerContainerId}/>
75
+ </>);
74
76
  }
75
- // We want to help the users with a bad baseUrl configuration (very common error)
76
- // Help message is inlined, and hidden if JS or CSS is able to load
77
- // Note: it might create false positives (ie network failures): not a big deal
78
- // Note: we only inline this for the homepage to avoid polluting all the site's pages
79
- // See https://github.com/facebook/docusaurus/pull/3621
77
+ /**
78
+ * We want to help the users with a bad baseUrl configuration (very common
79
+ * error) Help message is inlined, and hidden if JS or CSS is able to load
80
+ * Note: it might create false positives (ie network failures): not a big deal
81
+ * Note: we only inline this for the homepage to avoid polluting all the site's
82
+ * pages
83
+ * @see https://github.com/facebook/docusaurus/pull/3621
84
+ */
80
85
  export default function BaseUrlIssueBanner() {
81
86
  const { siteConfig: { baseUrl, baseUrlIssueBanner }, } = useDocusaurusContext();
82
87
  const { pathname } = useLocation();
83
88
  // returns true for the homepage during SRR
84
89
  const isHomePage = pathname === baseUrl;
85
90
  const enabled = baseUrlIssueBanner && isHomePage;
86
- return enabled ? React.createElement(BaseUrlIssueBannerEnabled, null) : null;
91
+ return enabled ? <BaseUrlIssueBannerEnabled /> : null;
87
92
  }
@@ -0,0 +1,11 @@
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
+ 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
+ }
@@ -4,9 +4,6 @@
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
- interface Dispatchers {
8
- onRouteUpdate: (...args: any) => void;
9
- onRouteUpdateDelayed: (...args: any) => void;
10
- }
11
- declare const clientLifecyclesDispatchers: Dispatchers;
7
+ import type { ClientModule } from '@docusaurus/types';
8
+ declare const clientLifecyclesDispatchers: Required<ClientModule>;
12
9
  export default clientLifecyclesDispatchers;
@@ -4,24 +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
- // too dynamic
8
- /* eslint-disable @typescript-eslint/no-explicit-any */
9
7
  import clientModules from '@generated/client-modules';
10
- function dispatchLifecycleAction(lifecycleAction, ...args) {
8
+ function dispatchLifecycleAction(lifecycleAction, args) {
11
9
  clientModules.forEach((clientModule) => {
12
- var _a, _b;
13
- const lifecycleFunction = (_b = (_a = clientModule === null || clientModule === void 0 ? void 0 : clientModule.default) === null || _a === void 0 ? void 0 : _a[lifecycleAction]) !== null && _b !== void 0 ? _b : clientModule[lifecycleAction];
14
- if (lifecycleFunction) {
15
- lifecycleFunction(...args);
16
- }
10
+ const lifecycleFunction = (clientModule?.default?.[lifecycleAction] ??
11
+ clientModule[lifecycleAction]);
12
+ lifecycleFunction?.(...args);
17
13
  });
18
14
  }
19
15
  const clientLifecyclesDispatchers = {
20
16
  onRouteUpdate(...args) {
21
- dispatchLifecycleAction('onRouteUpdate', ...args);
17
+ dispatchLifecycleAction('onRouteUpdate', args);
22
18
  },
23
19
  onRouteUpdateDelayed(...args) {
24
- dispatchLifecycleAction('onRouteUpdateDelayed', ...args);
20
+ dispatchLifecycleAction('onRouteUpdateDelayed', args);
25
21
  },
26
22
  };
27
23
  export default clientLifecyclesDispatchers;
@@ -5,23 +5,29 @@
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 { HelmetProvider } from 'react-helmet-async';
10
11
  import routes from '@generated/routes';
11
12
  import ExecutionEnvironment from './exports/ExecutionEnvironment';
12
13
  import App from './App';
13
14
  import preload from './preload';
14
15
  import docusaurus from './docusaurus';
15
- // Client-side render (e.g: running in browser) to become single-page application (SPA).
16
+ // Client-side render (e.g: running in browser) to become single-page
17
+ // application (SPA).
16
18
  if (ExecutionEnvironment.canUseDOM) {
17
19
  window.docusaurus = docusaurus;
18
- // For production, attempt to hydrate existing markup for performant first-load experience.
20
+ // For production, attempt to hydrate existing markup for performant
21
+ // first-load experience.
19
22
  // 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;
23
+ // We also preload async component to avoid first-load loading screen.
24
+ const renderMethod = process.env.NODE_ENV === 'production' ? ReactDOM.hydrate : ReactDOM.render;
22
25
  preload(routes, window.location.pathname).then(() => {
23
- renderMethod(React.createElement(BrowserRouter, null,
24
- React.createElement(App, null)), document.getElementById('__docusaurus'));
26
+ renderMethod(<HelmetProvider>
27
+ <BrowserRouter>
28
+ <App />
29
+ </BrowserRouter>
30
+ </HelmetProvider>, document.getElementById('__docusaurus'));
25
31
  });
26
32
  // Webpack Hot Module Replacement API
27
33
  if (module.hot) {
@@ -1,3 +1,9 @@
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;
@@ -12,31 +12,20 @@ import preloadHelper from './preload';
12
12
  import flat from './flat';
13
13
  const fetched = {};
14
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
- };
15
+ // If user is on slow or constrained connection.
16
+ const isSlowConnection = () => navigator.connection?.effectiveType.includes('2g') &&
17
+ navigator.connection?.saveData;
25
18
  const canPrefetch = (routePath) => !isSlowConnection() && !loaded[routePath] && !fetched[routePath];
26
19
  const canPreload = (routePath) => !isSlowConnection() && !loaded[routePath];
27
- const flatten = (arrays) => Array.prototype.concat.apply([], arrays);
28
20
  // Remove the last part containing the route hash
29
21
  // input: /blog/2018/12/14/Happy-First-Birthday-Slash-fe9
30
22
  // 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
- };
23
+ const removeRouteNameHash = (str) => str.replace(/-[^-]+$/, '');
24
+ const getChunkNamesToLoad = (path) => Object.entries(routesChunkNames)
25
+ .filter(([routeNameWithHash]) => removeRouteNameHash(routeNameWithHash) === path)
26
+ .flatMap(([, routeChunks]) =>
27
+ // flat() is useful for nested chunk names, it's not like array.flat()
28
+ Object.values(flat(routeChunks)));
40
29
  const docusaurus = {
41
30
  prefetch: (routePath) => {
42
31
  if (!canPrefetch(routePath)) {
@@ -46,14 +35,16 @@ const docusaurus = {
46
35
  fetched[routePath] = true;
47
36
  // Find all webpack chunk names needed.
48
37
  const matches = matchRoutes(routes, routePath);
49
- const chunkNamesNeeded = flatten(matches.map((match) => getChunkNamesToLoad(match.route.path)));
38
+ const chunkNamesNeeded = matches.flatMap((match) => getChunkNamesToLoad(match.route.path));
50
39
  // Prefetch all webpack chunk assets file needed.
51
40
  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
41
+ // "__webpack_require__.gca" is a custom function provided by
42
+ // ChunkAssetPlugin. Pass it the chunkName or chunkId you want to load and
43
+ // it will return the URL for that chunk.
44
+ // eslint-disable-next-line camelcase
55
45
  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.
46
+ // In some cases, webpack might decide to optimize further & hence the
47
+ // chunk assets are merged to another chunk/previous chunk.
57
48
  // Hence, we can safely filter it out/don't need to load it.
58
49
  if (chunkAsset && !/undefined/.test(chunkAsset)) {
59
50
  prefetchHelper(chunkAsset);
@@ -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
+ }
@@ -5,8 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  /// <reference types="react" />
8
- declare function BrowserOnly({ children, fallback, }: {
9
- children?: () => JSX.Element;
8
+ export default function BrowserOnly({ children, fallback, }: {
9
+ children: () => JSX.Element;
10
10
  fallback?: JSX.Element;
11
11
  }): JSX.Element | null;
12
- export default BrowserOnly;
@@ -4,12 +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 ExecutionEnvironment from './ExecutionEnvironment';
9
- function BrowserOnly({ children, fallback, }) {
10
- if (!ExecutionEnvironment.canUseDOM || children == null) {
11
- return fallback || null;
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()}</>;
12
20
  }
13
- return React.createElement(React.Fragment, null, children());
21
+ return fallback ?? null;
14
22
  }
15
- export default BrowserOnly;
@@ -5,5 +5,4 @@
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
+ export default function ComponentCreator(path: string, hash: string): ReturnType<typeof Loadable>;
@@ -10,18 +10,27 @@ import Loading from '@theme/Loading';
10
10
  import routesChunkNames from '@generated/routesChunkNames';
11
11
  import registry from '@generated/registry';
12
12
  import flat from '../flat';
13
- function ComponentCreator(path, hash) {
13
+ import { RouteContextProvider } from '../routeContext';
14
+ export default function ComponentCreator(path, hash) {
14
15
  // 404 page
15
16
  if (path === '*') {
16
17
  return Loadable({
17
18
  loading: Loading,
18
- loader: () => import('@theme/NotFound'),
19
+ loader: async () => {
20
+ const NotFound = (await import('@theme/NotFound')).default;
21
+ return (props) => (
22
+ // Is there a better API for this?
23
+ <RouteContextProvider value={{ plugin: { name: 'native', id: 'default' } }}>
24
+ <NotFound {...props}/>
25
+ </RouteContextProvider>);
26
+ },
19
27
  });
20
28
  }
21
29
  const chunkNamesKey = `${path}-${hash}`;
22
30
  const chunkNames = routesChunkNames[chunkNamesKey];
23
31
  const optsModules = [];
24
32
  const optsWebpack = [];
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
34
  const optsLoader = {};
26
35
  /* Prepare opts data that react-loadable needs
27
36
  https://github.com/jamiebuilds/react-loadable#declaring-which-modules-are-being-loaded
@@ -32,17 +41,19 @@ function ComponentCreator(path, hash) {
32
41
  content.foo: () => import('./doc1.md'),
33
42
  }
34
43
  - optsModules: ['./Pages.js', './doc1.md']
35
- - optsWebpack: [require.resolveWeak('./Pages.js'), require.resolveWeak('./doc1.md')]
44
+ - optsWebpack: [
45
+ require.resolveWeak('./Pages.js'),
46
+ require.resolveWeak('./doc1.md'),
47
+ ]
36
48
  */
37
49
  const flatChunkNames = flat(chunkNames);
38
- Object.keys(flatChunkNames).forEach((key) => {
39
- const chunkRegistry = registry[flatChunkNames[key]];
50
+ Object.entries(flatChunkNames).forEach(([key, chunkName]) => {
51
+ const chunkRegistry = registry[chunkName];
40
52
  if (chunkRegistry) {
41
- /* eslint-disable prefer-destructuring */
53
+ // eslint-disable-next-line prefer-destructuring
42
54
  optsLoader[key] = chunkRegistry[0];
43
55
  optsModules.push(chunkRegistry[1]);
44
56
  optsWebpack.push(chunkRegistry[2]);
45
- /* eslint-enable prefer-destructuring */
46
57
  }
47
58
  });
48
59
  return Loadable.Map({
@@ -54,24 +65,34 @@ function ComponentCreator(path, hash) {
54
65
  // Clone the original object since we don't want to alter the original.
55
66
  const loadedModules = JSON.parse(JSON.stringify(chunkNames));
56
67
  Object.keys(loaded).forEach((key) => {
57
- let val = loadedModules;
58
- const keyPath = key.split('.');
59
- for (let i = 0; i < keyPath.length - 1; i += 1) {
60
- val = val[keyPath[i]];
68
+ const newComp = loaded[key].default;
69
+ if (!newComp) {
70
+ throw new Error(`The page component at ${path} doesn't have a default export. This makes it impossible to render anything. Consider default-exporting a React component.`);
61
71
  }
62
- val[keyPath[keyPath.length - 1]] = loaded[key].default;
63
- const nonDefaultKeys = Object.keys(loaded[key]).filter((k) => k !== 'default');
64
- if (nonDefaultKeys && nonDefaultKeys.length) {
65
- nonDefaultKeys.forEach((nonDefaultKey) => {
66
- val[keyPath[keyPath.length - 1]][nonDefaultKey] =
67
- loaded[key][nonDefaultKey];
72
+ if (typeof newComp === 'object' || typeof newComp === 'function') {
73
+ Object.keys(loaded[key])
74
+ .filter((k) => k !== 'default')
75
+ .forEach((nonDefaultKey) => {
76
+ newComp[nonDefaultKey] = loaded[key][nonDefaultKey];
68
77
  });
69
78
  }
79
+ let val = loadedModules;
80
+ const keyPath = key.split('.');
81
+ keyPath.slice(0, -1).forEach((k) => {
82
+ val = val[k];
83
+ });
84
+ val[keyPath[keyPath.length - 1]] = newComp;
70
85
  });
71
86
  const Component = loadedModules.component;
72
87
  delete loadedModules.component;
73
- return React.createElement(Component, Object.assign({}, loadedModules, props));
88
+ /* eslint-disable no-underscore-dangle */
89
+ const routeContextModule = loadedModules.__routeContextModule;
90
+ delete loadedModules.__routeContextModule;
91
+ /* eslint-enable no-underscore-dangle */
92
+ // Is there any way to put this RouteContextProvider upper in the tree?
93
+ return (<RouteContextProvider value={routeContextModule}>
94
+ <Component {...loadedModules} {...props}/>
95
+ </RouteContextProvider>);
74
96
  },
75
97
  });
76
98
  }
77
- export default ComponentCreator;
@@ -0,0 +1,18 @@
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
+ /// <reference types="@docusaurus/module-type-aliases" />
8
+ import React, { type ReactNode } from 'react';
9
+ import type { Props } from '@docusaurus/ErrorBoundary';
10
+ interface State {
11
+ error: Error | null;
12
+ }
13
+ export default class ErrorBoundary extends React.Component<Props, State> {
14
+ constructor(props: Props);
15
+ componentDidCatch(error: Error): void;
16
+ render(): ReactNode;
17
+ }
18
+ export {};
@@ -0,0 +1,35 @@
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 ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
9
+ import DefaultFallback from '@theme/Error';
10
+ export default class ErrorBoundary extends React.Component {
11
+ constructor(props) {
12
+ super(props);
13
+ this.state = { error: null };
14
+ }
15
+ componentDidCatch(error) {
16
+ // Catch errors in any components below and re-render with error message
17
+ if (ExecutionEnvironment.canUseDOM) {
18
+ this.setState({ error });
19
+ }
20
+ }
21
+ render() {
22
+ const { children } = this.props;
23
+ const { error } = this.state;
24
+ if (error) {
25
+ const fallback = this.props.fallback ?? DefaultFallback;
26
+ return fallback({
27
+ error,
28
+ tryAgain: () => this.setState({ error: null }),
29
+ });
30
+ }
31
+ return (children ??
32
+ // See https://github.com/facebook/docusaurus/issues/6337#issuecomment-1012913647
33
+ null);
34
+ }
35
+ }
@@ -6,6 +6,5 @@
6
6
  */
7
7
  /// <reference types="@docusaurus/module-type-aliases" />
8
8
  /// <reference types="react" />
9
- import type { HeadProps } from '@docusaurus/Head';
10
- declare function Head(props: HeadProps): JSX.Element;
11
- export default Head;
9
+ import type { Props } from '@docusaurus/Head';
10
+ export default function Head(props: Props): JSX.Element;