@docusaurus/core 0.0.0-5898 → 0.0.0-5900

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.
@@ -100,10 +100,12 @@ export default function ComponentCreator(path, hash) {
100
100
  delete loadedModules.__comp;
101
101
  const routeContext = loadedModules.__context;
102
102
  delete loadedModules.__context;
103
+ const routeProps = loadedModules.__props;
104
+ delete loadedModules.__props;
103
105
  /* eslint-enable no-underscore-dangle */
104
106
  // Is there any way to put this RouteContextProvider upper in the tree?
105
107
  return (<RouteContextProvider value={routeContext}>
106
- <Component {...loadedModules} {...props}/>
108
+ <Component {...loadedModules} {...routeProps} {...props}/>
107
109
  </RouteContextProvider>);
108
110
  },
109
111
  });
@@ -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 type { CodeTranslations, DocusaurusConfig, GlobalData, I18n, RouteConfig, SiteMetadata } from '@docusaurus/types';
7
+ import type { CodeTranslations, DocusaurusConfig, GlobalData, I18n, PluginRouteConfig, SiteMetadata } from '@docusaurus/types';
8
8
  type CodegenParams = {
9
9
  generatedFilesDir: string;
10
10
  siteConfig: DocusaurusConfig;
@@ -14,7 +14,7 @@ type CodegenParams = {
14
14
  i18n: I18n;
15
15
  codeTranslations: CodeTranslations;
16
16
  siteMetadata: SiteMetadata;
17
- routes: RouteConfig[];
17
+ routes: PluginRouteConfig[];
18
18
  };
19
19
  export declare function generateSiteFiles(params: CodegenParams): Promise<void>;
20
20
  export {};
@@ -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 type { RouteConfig, RouteChunkNames } from '@docusaurus/types';
7
+ import type { RouteConfig, RouteChunkNames, PluginRouteConfig } from '@docusaurus/types';
8
8
  type RoutesCode = {
9
9
  /** Serialized routes config that can be directly emitted into temp file. */
10
10
  routesConfig: string;
@@ -42,8 +42,8 @@ export declare function genChunkName(modulePath: string, prefix?: string, prefer
42
42
  export declare function generateRoutesCode(routeConfigs: RouteConfig[]): RoutesCode;
43
43
  type GenerateRouteFilesParams = {
44
44
  generatedFilesDir: string;
45
- routes: RouteConfig[];
45
+ routes: PluginRouteConfig[];
46
46
  baseUrl: string;
47
47
  };
48
- export declare function generateRouteFiles({ generatedFilesDir, routes, }: GenerateRouteFilesParams): Promise<void>;
48
+ export declare function generateRouteFiles({ generatedFilesDir, routes: initialRoutes, }: GenerateRouteFilesParams): Promise<void>;
49
49
  export {};
@@ -9,6 +9,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.generateRouteFiles = exports.generateRoutesCode = exports.genChunkName = void 0;
10
10
  const tslib_1 = require("tslib");
11
11
  const querystring_1 = tslib_1.__importDefault(require("querystring"));
12
+ const path_1 = tslib_1.__importDefault(require("path"));
12
13
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
13
14
  const utils_1 = require("@docusaurus/utils");
14
15
  /** Indents every line of `str` by one level. */
@@ -58,7 +59,7 @@ exports.genChunkName = genChunkName;
58
59
  * is the same as react-router's `RouteConfig`. Formatting is similar to
59
60
  * `JSON.stringify` but without all the quotes.
60
61
  */
61
- function serializeRouteConfig({ routePath, routeHash, exact, subroutesCodeStrings, props, }) {
62
+ function serializeRouteConfig({ routePath, routeHash, exact, subroutesCodeStrings, attributes, }) {
62
63
  const parts = [
63
64
  `path: '${routePath}'`,
64
65
  `component: ComponentCreator('${routePath}', '${routeHash}')`,
@@ -71,10 +72,10 @@ function serializeRouteConfig({ routePath, routeHash, exact, subroutesCodeString
71
72
  ${indent(subroutesCodeStrings.join(',\n'))}
72
73
  ]`);
73
74
  }
74
- Object.entries(props).forEach(([propName, propValue]) => {
75
- const isIdentifier = /^[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u.test(propName);
76
- const key = isIdentifier ? propName : JSON.stringify(propName);
77
- parts.push(`${key}: ${JSON.stringify(propValue)}`);
75
+ Object.entries(attributes).forEach(([attrName, attrValue]) => {
76
+ const isIdentifier = /^[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u.test(attrName);
77
+ const key = isIdentifier ? attrName : JSON.stringify(attrName);
78
+ parts.push(`${key}: ${JSON.stringify(attrValue)}`);
78
79
  });
79
80
  return `{
80
81
  ${indent(parts.join(',\n'))}
@@ -114,7 +115,7 @@ function genChunkNames(routeModule, prefix, name, res) {
114
115
  * `routesPaths`, and `routesChunkNames` accordingly.
115
116
  */
116
117
  function genRouteCode(routeConfig, res) {
117
- const { path: routePath, component, modules = {}, context, routes: subroutes, priority, exact, metadata, ...props } = routeConfig;
118
+ const { path: routePath, component, modules = {}, context, routes: subroutes, priority, exact, metadata, props, plugin, ...attributes } = routeConfig;
118
119
  if (typeof routePath !== 'string' || !component) {
119
120
  throw new Error(`Invalid route config: path must be a string and component is required.
120
121
  ${JSON.stringify(routeConfig)}`);
@@ -132,7 +133,7 @@ ${JSON.stringify(routeConfig)}`);
132
133
  routeHash,
133
134
  subroutesCodeStrings: subroutes?.map((r) => genRouteCode(r, res)),
134
135
  exact,
135
- props,
136
+ attributes,
136
137
  });
137
138
  }
138
139
  /**
@@ -179,7 +180,75 @@ ${Object.entries(registry)
179
180
  `);
180
181
  const genRoutesChunkNames = ({ generatedFilesDir, routesChunkNames, }) => (0, utils_1.generate)(generatedFilesDir, 'routesChunkNames.json', JSON.stringify(routesChunkNames, null, 2));
181
182
  const genRoutes = ({ generatedFilesDir, routesConfig, }) => (0, utils_1.generate)(generatedFilesDir, 'routes.js', routesConfig);
182
- async function generateRouteFiles({ generatedFilesDir, routes, }) {
183
+ async function generateRoutePropModule({ generatedFilesDir, route, plugin, }) {
184
+ ensureNoPropsConflict(route);
185
+ const moduleContent = JSON.stringify(route.props);
186
+ // TODO we should aim to reduce this path length
187
+ // This adds bytes to the global module registry
188
+ const relativePath = path_1.default.posix.join(plugin.name, plugin.id, 'p', `${(0, utils_1.docuHash)(route.path)}.json`);
189
+ const modulePath = path_1.default.posix.join(generatedFilesDir, relativePath);
190
+ const aliasedPath = path_1.default.posix.join('@generated', relativePath);
191
+ await (0, utils_1.generate)(generatedFilesDir, modulePath, moduleContent);
192
+ return aliasedPath;
193
+ }
194
+ function ensureNoPropsConflict(route) {
195
+ if (!route.props && !route.modules) {
196
+ return;
197
+ }
198
+ const conflictingPropNames = lodash_1.default.intersection(Object.keys(route.props ?? {}), Object.keys(route.modules ?? {}));
199
+ if (conflictingPropNames.length > 0) {
200
+ throw new Error(`Route ${route.path} has conflicting props declared using both route.modules and route.props APIs for keys: ${conflictingPropNames.join(', ')}\nThis is not permitted, otherwise one prop would override the over.`);
201
+ }
202
+ }
203
+ async function preprocessRouteProps({ generatedFilesDir, route, plugin, }) {
204
+ const propsModulePathPromise = route.props
205
+ ? generateRoutePropModule({
206
+ generatedFilesDir,
207
+ route,
208
+ plugin,
209
+ })
210
+ : undefined;
211
+ const subRoutesPromise = route.routes
212
+ ? Promise.all(route.routes.map((subRoute) => {
213
+ return preprocessRouteProps({
214
+ generatedFilesDir,
215
+ route: subRoute,
216
+ plugin,
217
+ });
218
+ }))
219
+ : undefined;
220
+ const [propsModulePath, subRoutes] = await Promise.all([
221
+ propsModulePathPromise,
222
+ subRoutesPromise,
223
+ ]);
224
+ const newRoute = {
225
+ ...route,
226
+ modules: {
227
+ ...route.modules,
228
+ ...(propsModulePath && { __props: propsModulePath }),
229
+ },
230
+ routes: subRoutes,
231
+ props: undefined,
232
+ };
233
+ return newRoute;
234
+ }
235
+ // For convenience, it's possible to pass a "route.props" object
236
+ // This method converts the props object to a regular module
237
+ // and assigns it to route.modules.__props attribute
238
+ async function preprocessAllPluginsRoutesProps({ generatedFilesDir, routes, }) {
239
+ return Promise.all(routes.map((route) => {
240
+ return preprocessRouteProps({
241
+ generatedFilesDir,
242
+ route,
243
+ plugin: route.plugin,
244
+ });
245
+ }));
246
+ }
247
+ async function generateRouteFiles({ generatedFilesDir, routes: initialRoutes, }) {
248
+ const routes = await preprocessAllPluginsRoutesProps({
249
+ generatedFilesDir,
250
+ routes: initialRoutes,
251
+ });
183
252
  const { registry, routesChunkNames, routesConfig } = generateRoutesCode(routes);
184
253
  await Promise.all([
185
254
  genRegistry({ generatedFilesDir, registry }),
@@ -18,13 +18,20 @@ const routeConfig_1 = require("./routeConfig");
18
18
  async function createPluginActionsUtils({ plugin, generatedFilesDir, baseUrl, trailingSlash, }) {
19
19
  const pluginId = plugin.options.id;
20
20
  // Plugins data files are namespaced by pluginName/pluginId
21
+ // TODO Docusaurus v4 breaking change
22
+ // module aliasing should be automatic
23
+ // we should never find local absolute FS paths in the codegen registry
24
+ const aliasedSource = (source) => `@generated/${(0, utils_1.posixPath)(path_1.default.relative(generatedFilesDir, source))}`;
25
+ // TODO use @generated data dir here!
26
+ // The module registry should not contain absolute paths
21
27
  const dataDir = path_1.default.join(generatedFilesDir, plugin.name, pluginId);
22
28
  const pluginRouteContext = {
23
29
  name: plugin.name,
24
30
  id: pluginId,
25
31
  };
26
- const pluginRouteContextModulePath = path_1.default.join(dataDir, `${(0, utils_1.docuHash)('pluginRouteContextModule')}.json`);
32
+ const pluginRouteContextModulePath = path_1.default.join(dataDir, `__plugin.json`);
27
33
  // TODO not ideal place to generate that file
34
+ // move to codegen step instead!
28
35
  await (0, utils_1.generate)('/', pluginRouteContextModulePath, JSON.stringify(pluginRouteContext, null, 2));
29
36
  const routes = [];
30
37
  let globalData;
@@ -39,13 +46,14 @@ async function createPluginActionsUtils({ plugin, generatedFilesDir, baseUrl, tr
39
46
  ...finalRouteConfig,
40
47
  context: {
41
48
  ...(finalRouteConfig.context && { data: finalRouteConfig.context }),
42
- plugin: pluginRouteContextModulePath,
49
+ plugin: aliasedSource(pluginRouteContextModulePath),
43
50
  },
44
51
  });
45
52
  },
46
53
  async createData(name, data) {
47
54
  const modulePath = path_1.default.join(dataDir, name);
48
- await (0, utils_1.generate)(dataDir, name, data);
55
+ const dataString = typeof data === 'string' ? data : JSON.stringify(data, null, 2);
56
+ await (0, utils_1.generate)(dataDir, name, dataString);
49
57
  return modulePath;
50
58
  },
51
59
  setGlobalData(data) {
@@ -4,10 +4,10 @@
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 type { LoadContext, RouteConfig, GlobalData, PluginIdentifier, LoadedPlugin } from '@docusaurus/types';
7
+ import type { LoadContext, GlobalData, PluginIdentifier, LoadedPlugin, PluginRouteConfig } from '@docusaurus/types';
8
8
  export type LoadPluginsResult = {
9
9
  plugins: LoadedPlugin[];
10
- routes: RouteConfig[];
10
+ routes: PluginRouteConfig[];
11
11
  globalData: GlobalData;
12
12
  };
13
13
  /**
@@ -101,22 +101,23 @@ async function executePluginAllContentLoaded({ plugin, context, allContent, }) {
101
101
  async function executeAllPluginsAllContentLoaded({ plugins, context, }) {
102
102
  return utils_1.PerfLogger.async(`allContentLoaded()`, async () => {
103
103
  const allContent = (0, pluginsUtils_1.aggregateAllContent)(plugins);
104
- const routes = [];
105
- const globalData = {};
104
+ const allRoutes = [];
105
+ const allGlobalData = {};
106
106
  await Promise.all(plugins.map(async (plugin) => {
107
107
  var _a;
108
- const { routes: pluginRoutes, globalData: pluginGlobalData } = await executePluginAllContentLoaded({
108
+ const { routes, globalData: pluginGlobalData } = await executePluginAllContentLoaded({
109
109
  plugin,
110
110
  context,
111
111
  allContent,
112
112
  });
113
- routes.push(...pluginRoutes);
113
+ const pluginRoutes = routes.map((route) => (0, pluginsUtils_1.toPluginRoute)({ plugin, route }));
114
+ allRoutes.push(...pluginRoutes);
114
115
  if (pluginGlobalData !== undefined) {
115
- globalData[_a = plugin.name] ?? (globalData[_a] = {});
116
- globalData[plugin.name][plugin.options.id] = pluginGlobalData;
116
+ allGlobalData[_a = plugin.name] ?? (allGlobalData[_a] = {});
117
+ allGlobalData[plugin.name][plugin.options.id] = pluginGlobalData;
117
118
  }
118
119
  }));
119
- return { routes, globalData };
120
+ return { routes: allRoutes, globalData: allGlobalData };
120
121
  });
121
122
  }
122
123
  // This merges plugins routes and global data created from both lifecycles:
@@ -4,13 +4,17 @@
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 type { AllContent, GlobalData, InitializedPlugin, LoadedPlugin, PluginIdentifier, RouteConfig } from '@docusaurus/types';
7
+ import type { AllContent, GlobalData, InitializedPlugin, LoadedPlugin, PluginIdentifier, PluginRouteConfig, RouteConfig } from '@docusaurus/types';
8
8
  export declare function getPluginByIdentifier<P extends InitializedPlugin>({ plugins, pluginIdentifier, }: {
9
9
  pluginIdentifier: PluginIdentifier;
10
10
  plugins: P[];
11
11
  }): P;
12
12
  export declare function aggregateAllContent(loadedPlugins: LoadedPlugin[]): AllContent;
13
- export declare function aggregateRoutes(loadedPlugins: LoadedPlugin[]): RouteConfig[];
13
+ export declare function toPluginRoute({ plugin, route, }: {
14
+ plugin: LoadedPlugin;
15
+ route: RouteConfig;
16
+ }): PluginRouteConfig;
17
+ export declare function aggregateRoutes(loadedPlugins: LoadedPlugin[]): PluginRouteConfig[];
14
18
  export declare function aggregateGlobalData(loadedPlugins: LoadedPlugin[]): GlobalData;
15
19
  export declare function mergeGlobalData(...globalDataList: GlobalData[]): GlobalData;
16
20
  export declare function formatPluginName(plugin: InitializedPlugin | PluginIdentifier): string;
@@ -6,7 +6,7 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.formatPluginName = exports.mergeGlobalData = exports.aggregateGlobalData = exports.aggregateRoutes = exports.aggregateAllContent = exports.getPluginByIdentifier = void 0;
9
+ exports.formatPluginName = exports.mergeGlobalData = exports.aggregateGlobalData = exports.aggregateRoutes = exports.toPluginRoute = exports.aggregateAllContent = exports.getPluginByIdentifier = void 0;
10
10
  const tslib_1 = require("tslib");
11
11
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
12
12
  const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
@@ -28,8 +28,12 @@ function aggregateAllContent(loadedPlugins) {
28
28
  .value();
29
29
  }
30
30
  exports.aggregateAllContent = aggregateAllContent;
31
+ function toPluginRoute({ plugin, route, }) {
32
+ return { plugin: { name: plugin.name, id: plugin.options.id }, ...route };
33
+ }
34
+ exports.toPluginRoute = toPluginRoute;
31
35
  function aggregateRoutes(loadedPlugins) {
32
- return loadedPlugins.flatMap((p) => p.routes);
36
+ return loadedPlugins.flatMap((plugin) => plugin.routes.map((route) => toPluginRoute({ plugin, route })));
33
37
  }
34
38
  exports.aggregateRoutes = aggregateRoutes;
35
39
  function aggregateGlobalData(loadedPlugins) {
@@ -7,5 +7,5 @@
7
7
  import { type ApplyTrailingSlashParams } from '@docusaurus/utils-common';
8
8
  import type { RouteConfig } from '@docusaurus/types';
9
9
  /** Recursively applies trailing slash config to all nested routes. */
10
- export declare function applyRouteTrailingSlash(route: RouteConfig, params: ApplyTrailingSlashParams): RouteConfig;
10
+ export declare function applyRouteTrailingSlash<Route extends RouteConfig>(route: Route, params: ApplyTrailingSlashParams): Route;
11
11
  export declare function sortRoutes(routeConfigs: RouteConfig[], baseUrl?: string): void;
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-5898",
4
+ "version": "0.0.0-5900",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -43,13 +43,13 @@
43
43
  "@babel/runtime": "^7.22.6",
44
44
  "@babel/runtime-corejs3": "^7.22.6",
45
45
  "@babel/traverse": "^7.22.8",
46
- "@docusaurus/cssnano-preset": "0.0.0-5898",
47
- "@docusaurus/logger": "0.0.0-5898",
48
- "@docusaurus/mdx-loader": "0.0.0-5898",
46
+ "@docusaurus/cssnano-preset": "0.0.0-5900",
47
+ "@docusaurus/logger": "0.0.0-5900",
48
+ "@docusaurus/mdx-loader": "0.0.0-5900",
49
49
  "@docusaurus/react-loadable": "5.5.2",
50
- "@docusaurus/utils": "0.0.0-5898",
51
- "@docusaurus/utils-common": "0.0.0-5898",
52
- "@docusaurus/utils-validation": "0.0.0-5898",
50
+ "@docusaurus/utils": "0.0.0-5900",
51
+ "@docusaurus/utils-common": "0.0.0-5900",
52
+ "@docusaurus/utils-validation": "0.0.0-5900",
53
53
  "@svgr/webpack": "^6.5.1",
54
54
  "autoprefixer": "^10.4.14",
55
55
  "babel-loader": "^9.1.3",
@@ -105,8 +105,8 @@
105
105
  "webpackbar": "^5.0.2"
106
106
  },
107
107
  "devDependencies": {
108
- "@docusaurus/module-type-aliases": "0.0.0-5898",
109
- "@docusaurus/types": "0.0.0-5898",
108
+ "@docusaurus/module-type-aliases": "0.0.0-5900",
109
+ "@docusaurus/types": "0.0.0-5900",
110
110
  "@total-typescript/shoehorn": "^0.1.2",
111
111
  "@types/detect-port": "^1.3.3",
112
112
  "@types/react-dom": "^18.2.7",
@@ -126,5 +126,5 @@
126
126
  "engines": {
127
127
  "node": ">=18.0"
128
128
  },
129
- "gitHead": "9ae3e7f8841dc5ae7c07b084c48c1e80d4d355fe"
129
+ "gitHead": "8c799b8d6990ec2aea5708a732db09f4dd9e66cc"
130
130
  }