@docusaurus/core 0.0.0-4733 → 0.0.0-4734

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.
@@ -10,12 +10,20 @@ 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
+ import { RouteContextProvider } from '../routeContext';
13
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}`;
@@ -71,7 +79,14 @@ export default function ComponentCreator(path, hash) {
71
79
  });
72
80
  const Component = loadedModules.component;
73
81
  delete loadedModules.component;
74
- return <Component {...loadedModules} {...props}/>;
82
+ /* eslint-disable no-underscore-dangle */
83
+ const routeContextModule = loadedModules.__routeContextModule;
84
+ delete loadedModules.__routeContextModule;
85
+ /* eslint-enable no-underscore-dangle */
86
+ // Is there any way to put this RouteContextProvider upper in the tree?
87
+ return (<RouteContextProvider value={routeContextModule}>
88
+ <Component {...loadedModules} {...props}/>;
89
+ </RouteContextProvider>);
75
90
  },
76
91
  });
77
92
  }
@@ -0,0 +1,8 @@
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 type { PluginRouteContext } from '@docusaurus/types';
8
+ export default function useRouteContext(): PluginRouteContext;
@@ -0,0 +1,15 @@
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 { Context } from '../routeContext';
9
+ export default function useRouteContext() {
10
+ const context = React.useContext(Context);
11
+ if (!context) {
12
+ throw new Error('Unexpected: no Docusaurus parent/current route context found');
13
+ }
14
+ return context;
15
+ }
@@ -0,0 +1,13 @@
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 { PluginRouteContext } from '@docusaurus/types';
9
+ export declare const Context: React.Context<PluginRouteContext | null>;
10
+ export declare function RouteContextProvider({ children, value, }: {
11
+ children: ReactNode;
12
+ value: PluginRouteContext | null;
13
+ }): JSX.Element;
@@ -0,0 +1,31 @@
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, { useMemo } from 'react';
8
+ export const Context = React.createContext(null);
9
+ function mergeContexts({ parent, value, }) {
10
+ if (!parent) {
11
+ if (!value) {
12
+ throw new Error('Unexpected: no Docusaurus parent/current route context found');
13
+ }
14
+ else if (!('plugin' in value)) {
15
+ throw new Error('Unexpected: Docusaurus parent route context has no plugin attribute');
16
+ }
17
+ return value;
18
+ }
19
+ // TODO deep merge this
20
+ const data = { ...parent.data, ...value?.data };
21
+ return {
22
+ // nested routes are not supposed to override plugin attribute
23
+ plugin: parent.plugin,
24
+ data,
25
+ };
26
+ }
27
+ export function RouteContextProvider({ children, value, }) {
28
+ const parent = React.useContext(Context);
29
+ const mergedValue = useMemo(() => mergeContexts({ parent, value }), [parent, value]);
30
+ return <Context.Provider value={mergedValue}>{children}</Context.Provider>;
31
+ }
@@ -96,19 +96,32 @@ async function loadPlugins({ pluginConfigs, context, }) {
96
96
  // plugins data files are namespaced by pluginName/pluginId
97
97
  const dataDirRoot = path_1.default.join(context.generatedFilesDir, plugin.name);
98
98
  const dataDir = path_1.default.join(dataDirRoot, pluginId);
99
+ const createData = async (name, data) => {
100
+ const modulePath = path_1.default.join(dataDir, name);
101
+ await fs_extra_1.default.ensureDir(path_1.default.dirname(modulePath));
102
+ await (0, utils_1.generate)(dataDir, name, data);
103
+ return modulePath;
104
+ };
105
+ // TODO this would be better to do all that in the codegen phase
106
+ // TODO handle context for nested routes
107
+ const pluginRouteContext = {
108
+ plugin: { name: plugin.name, id: pluginId },
109
+ data: undefined, // TODO allow plugins to provide context data
110
+ };
111
+ const pluginRouteContextModulePath = await createData(`${(0, utils_1.docuHash)('pluginRouteContextModule')}.json`, JSON.stringify(pluginRouteContext, null, 2));
99
112
  const addRoute = (initialRouteConfig) => {
100
113
  // Trailing slash behavior is handled in a generic way for all plugins
101
114
  const finalRouteConfig = (0, applyRouteTrailingSlash_1.default)(initialRouteConfig, {
102
115
  trailingSlash: context.siteConfig.trailingSlash,
103
116
  baseUrl: context.siteConfig.baseUrl,
104
117
  });
105
- pluginsRouteConfigs.push(finalRouteConfig);
106
- };
107
- const createData = async (name, data) => {
108
- const modulePath = path_1.default.join(dataDir, name);
109
- await fs_extra_1.default.ensureDir(path_1.default.dirname(modulePath));
110
- await (0, utils_1.generate)(dataDir, name, data);
111
- return modulePath;
118
+ pluginsRouteConfigs.push({
119
+ ...finalRouteConfig,
120
+ modules: {
121
+ ...finalRouteConfig.modules,
122
+ __routeContextModule: pluginRouteContextModulePath,
123
+ },
124
+ });
112
125
  };
113
126
  // the plugins global data are namespaced to avoid data conflicts:
114
127
  // - by plugin name
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@docusaurus/core",
3
3
  "description": "Easy to Maintain Open Source Documentation Websites",
4
- "version": "0.0.0-4733",
4
+ "version": "0.0.0-4734",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -41,13 +41,13 @@
41
41
  "@babel/runtime": "^7.17.7",
42
42
  "@babel/runtime-corejs3": "^7.17.7",
43
43
  "@babel/traverse": "^7.17.3",
44
- "@docusaurus/cssnano-preset": "0.0.0-4733",
45
- "@docusaurus/logger": "0.0.0-4733",
46
- "@docusaurus/mdx-loader": "0.0.0-4733",
44
+ "@docusaurus/cssnano-preset": "0.0.0-4734",
45
+ "@docusaurus/logger": "0.0.0-4734",
46
+ "@docusaurus/mdx-loader": "0.0.0-4734",
47
47
  "@docusaurus/react-loadable": "5.5.2",
48
- "@docusaurus/utils": "0.0.0-4733",
49
- "@docusaurus/utils-common": "0.0.0-4733",
50
- "@docusaurus/utils-validation": "0.0.0-4733",
48
+ "@docusaurus/utils": "0.0.0-4734",
49
+ "@docusaurus/utils-common": "0.0.0-4734",
50
+ "@docusaurus/utils-validation": "0.0.0-4734",
51
51
  "@slorber/static-site-generator-webpack-plugin": "^4.0.1",
52
52
  "@svgr/webpack": "^6.2.1",
53
53
  "autoprefixer": "^10.4.2",
@@ -106,8 +106,8 @@
106
106
  "webpackbar": "^5.0.2"
107
107
  },
108
108
  "devDependencies": {
109
- "@docusaurus/module-type-aliases": "0.0.0-4733",
110
- "@docusaurus/types": "0.0.0-4733",
109
+ "@docusaurus/module-type-aliases": "0.0.0-4734",
110
+ "@docusaurus/types": "0.0.0-4734",
111
111
  "@types/detect-port": "^1.3.2",
112
112
  "@types/nprogress": "^0.2.0",
113
113
  "@types/react-dom": "^17.0.13",
@@ -128,5 +128,5 @@
128
128
  "engines": {
129
129
  "node": ">=14"
130
130
  },
131
- "gitHead": "b3f9b5a5da00e86eaaba2220c340d4ed81c3baea"
131
+ "gitHead": "0ca49e19e1c96a7d4a0f2bb72ccfca4b7a3eb9bb"
132
132
  }