@docusaurus/core 0.0.0-5860 → 0.0.0-5863

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.
@@ -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 type { LoadedPlugin, PluginContentLoadedActions, RouteConfig } from '@docusaurus/types';
8
+ type PluginActionUtils = {
9
+ getRoutes: () => RouteConfig[];
10
+ getGlobalData: () => unknown;
11
+ getActions: () => PluginContentLoadedActions;
12
+ };
13
+ export declare function createPluginActionsUtils({ plugin, generatedFilesDir, baseUrl, trailingSlash, }: {
14
+ plugin: LoadedPlugin;
15
+ generatedFilesDir: string;
16
+ baseUrl: string;
17
+ trailingSlash: boolean | undefined;
18
+ }): Promise<PluginActionUtils>;
19
+ export {};
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.createPluginActionsUtils = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const path_1 = tslib_1.__importDefault(require("path"));
12
+ const utils_1 = require("@docusaurus/utils");
13
+ const routeConfig_1 = require("./routeConfig");
14
+ // TODO refactor historical action system and make this side-effect-free
15
+ // If the function were pure, we could more easily compare previous/next values
16
+ // on site reloads, and bail-out of the reload process earlier
17
+ // Particularly, createData() modules should rather be declarative
18
+ async function createPluginActionsUtils({ plugin, generatedFilesDir, baseUrl, trailingSlash, }) {
19
+ const pluginId = plugin.options.id;
20
+ // Plugins data files are namespaced by pluginName/pluginId
21
+ const dataDir = path_1.default.join(generatedFilesDir, plugin.name, pluginId);
22
+ const pluginRouteContext = {
23
+ name: plugin.name,
24
+ id: pluginId,
25
+ };
26
+ const pluginRouteContextModulePath = path_1.default.join(dataDir, `${(0, utils_1.docuHash)('pluginRouteContextModule')}.json`);
27
+ await (0, utils_1.generate)('/', pluginRouteContextModulePath, JSON.stringify(pluginRouteContext, null, 2));
28
+ const routes = [];
29
+ let globalData;
30
+ const actions = {
31
+ addRoute(initialRouteConfig) {
32
+ // Trailing slash behavior is handled generically for all plugins
33
+ const finalRouteConfig = (0, routeConfig_1.applyRouteTrailingSlash)(initialRouteConfig, {
34
+ baseUrl,
35
+ trailingSlash,
36
+ });
37
+ routes.push({
38
+ ...finalRouteConfig,
39
+ context: {
40
+ ...(finalRouteConfig.context && { data: finalRouteConfig.context }),
41
+ plugin: pluginRouteContextModulePath,
42
+ },
43
+ });
44
+ },
45
+ async createData(name, data) {
46
+ const modulePath = path_1.default.join(dataDir, name);
47
+ await (0, utils_1.generate)(dataDir, name, data);
48
+ return modulePath;
49
+ },
50
+ setGlobalData(data) {
51
+ globalData = data;
52
+ },
53
+ };
54
+ return {
55
+ // Some variables are mutable, so we expose a getter instead of the value
56
+ getRoutes: () => routes,
57
+ getGlobalData: () => globalData,
58
+ getActions: () => actions,
59
+ };
60
+ }
61
+ exports.createPluginActionsUtils = createPluginActionsUtils;
@@ -11,18 +11,16 @@ export type LoadPluginsResult = {
11
11
  routes: RouteConfig[];
12
12
  globalData: GlobalData;
13
13
  };
14
+ export declare function mergeGlobalData(...globalDataList: GlobalData[]): GlobalData;
14
15
  /**
15
- * Initializes the plugins, runs `loadContent`, `translateContent`,
16
- * `contentLoaded`, and `translateThemeConfig`. Because `contentLoaded` is
17
- * side-effect-ful (it generates temp files), so is this function. This function
18
- * would also mutate `context.siteConfig.themeConfig` to translate it.
16
+ * Initializes the plugins and run their lifecycle functions.
19
17
  */
20
18
  export declare function loadPlugins(context: LoadContext): Promise<LoadPluginsResult>;
21
19
  export declare function getPluginByIdentifier({ plugins, pluginIdentifier, }: {
22
20
  pluginIdentifier: PluginIdentifier;
23
21
  plugins: LoadedPlugin[];
24
22
  }): LoadedPlugin;
25
- export declare function reloadPlugin({ pluginIdentifier, plugins, context, }: {
23
+ export declare function reloadPlugin({ pluginIdentifier, plugins: previousPlugins, context, }: {
26
24
  pluginIdentifier: PluginIdentifier;
27
25
  plugins: LoadedPlugin[];
28
26
  context: LoadContext;
@@ -6,17 +6,16 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.reloadPlugin = exports.getPluginByIdentifier = exports.loadPlugins = void 0;
9
+ exports.reloadPlugin = exports.getPluginByIdentifier = exports.loadPlugins = exports.mergeGlobalData = void 0;
10
10
  const tslib_1 = require("tslib");
11
- const path_1 = tslib_1.__importDefault(require("path"));
12
11
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
13
- const utils_1 = require("@docusaurus/utils");
14
12
  const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
15
13
  const init_1 = require("./init");
16
14
  const synthetic_1 = require("./synthetic");
17
15
  const translations_1 = require("../translations/translations");
18
16
  const routeConfig_1 = require("./routeConfig");
19
- const utils_2 = require("../../utils");
17
+ const utils_1 = require("../../utils");
18
+ const actions_1 = require("./actions");
20
19
  async function translatePlugin({ plugin, context, }) {
21
20
  const { content } = plugin;
22
21
  const rawTranslationFiles = (await plugin.getTranslationFiles?.({ content: plugin.content })) ?? [];
@@ -38,14 +37,14 @@ async function translatePlugin({ plugin, context, }) {
38
37
  return { ...plugin, content: translatedContent };
39
38
  }
40
39
  async function executePluginLoadContent({ plugin, context, }) {
41
- return utils_2.PerfLogger.async(`Plugin - loadContent - ${plugin.name}@${plugin.options.id}`, async () => {
40
+ return utils_1.PerfLogger.async(`Plugin - loadContent - ${plugin.name}@${plugin.options.id}`, async () => {
42
41
  const content = await plugin.loadContent?.();
43
42
  const loadedPlugin = { ...plugin, content };
44
43
  return translatePlugin({ plugin: loadedPlugin, context });
45
44
  });
46
45
  }
47
46
  async function executePluginsLoadContent({ plugins, context, }) {
48
- return utils_2.PerfLogger.async(`Plugins - loadContent`, () => Promise.all(plugins.map((plugin) => executePluginLoadContent({ plugin, context }))));
47
+ return utils_1.PerfLogger.async(`Plugins - loadContent`, () => Promise.all(plugins.map((plugin) => executePluginLoadContent({ plugin, context }))));
49
48
  }
50
49
  function aggregateAllContent(loadedPlugins) {
51
50
  return lodash_1.default.chain(loadedPlugins)
@@ -56,58 +55,50 @@ function aggregateAllContent(loadedPlugins) {
56
55
  .value())
57
56
  .value();
58
57
  }
59
- // TODO refactor and make this side-effect-free
60
- // If the function was pure, we could more easily compare previous/next values
61
- // on site reloads, and bail-out of the reload process earlier
62
- // createData() modules should rather be declarative
63
- async function executePluginContentLoaded({ plugin, context, allContent, }) {
64
- return utils_2.PerfLogger.async(`Plugins - contentLoaded - ${plugin.name}@${plugin.options.id}`, async () => {
58
+ async function executePluginContentLoaded({ plugin, context, }) {
59
+ return utils_1.PerfLogger.async(`Plugins - contentLoaded - ${plugin.name}@${plugin.options.id}`, async () => {
65
60
  if (!plugin.contentLoaded) {
66
61
  return { routes: [], globalData: undefined };
67
62
  }
68
- const pluginId = plugin.options.id;
69
- // Plugins data files are namespaced by pluginName/pluginId
70
- const dataDir = path_1.default.join(context.generatedFilesDir, plugin.name, pluginId);
71
- const pluginRouteContextModulePath = path_1.default.join(dataDir, `${(0, utils_1.docuHash)('pluginRouteContextModule')}.json`);
72
- const pluginRouteContext = {
73
- name: plugin.name,
74
- id: pluginId,
75
- };
76
- await (0, utils_1.generate)('/', pluginRouteContextModulePath, JSON.stringify(pluginRouteContext, null, 2));
77
- const routes = [];
78
- let globalData;
79
- const actions = {
80
- addRoute(initialRouteConfig) {
81
- // Trailing slash behavior is handled generically for all plugins
82
- const finalRouteConfig = (0, routeConfig_1.applyRouteTrailingSlash)(initialRouteConfig, context.siteConfig);
83
- routes.push({
84
- ...finalRouteConfig,
85
- context: {
86
- ...(finalRouteConfig.context && { data: finalRouteConfig.context }),
87
- plugin: pluginRouteContextModulePath,
88
- },
89
- });
90
- },
91
- async createData(name, data) {
92
- const modulePath = path_1.default.join(dataDir, name);
93
- await (0, utils_1.generate)(dataDir, name, data);
94
- return modulePath;
95
- },
96
- setGlobalData(data) {
97
- globalData = data;
98
- },
99
- };
63
+ const pluginActionsUtils = await (0, actions_1.createPluginActionsUtils)({
64
+ plugin,
65
+ generatedFilesDir: context.generatedFilesDir,
66
+ baseUrl: context.siteConfig.baseUrl,
67
+ trailingSlash: context.siteConfig.trailingSlash,
68
+ });
100
69
  await plugin.contentLoaded({
101
70
  content: plugin.content,
102
- actions,
71
+ actions: pluginActionsUtils.getActions(),
72
+ });
73
+ return {
74
+ routes: pluginActionsUtils.getRoutes(),
75
+ globalData: pluginActionsUtils.getGlobalData(),
76
+ };
77
+ });
78
+ }
79
+ async function executePluginAllContentLoaded({ plugin, context, allContent, }) {
80
+ return utils_1.PerfLogger.async(`Plugins - allContentLoaded - ${plugin.name}@${plugin.options.id}`, async () => {
81
+ if (!plugin.allContentLoaded) {
82
+ return { routes: [], globalData: undefined };
83
+ }
84
+ const pluginActionsUtils = await (0, actions_1.createPluginActionsUtils)({
85
+ plugin,
86
+ generatedFilesDir: context.generatedFilesDir,
87
+ baseUrl: context.siteConfig.baseUrl,
88
+ trailingSlash: context.siteConfig.trailingSlash,
89
+ });
90
+ await plugin.allContentLoaded({
103
91
  allContent,
92
+ actions: pluginActionsUtils.getActions(),
104
93
  });
105
- return { routes, globalData };
94
+ return {
95
+ routes: pluginActionsUtils.getRoutes(),
96
+ globalData: pluginActionsUtils.getGlobalData(),
97
+ };
106
98
  });
107
99
  }
108
100
  async function executePluginsContentLoaded({ plugins, context, }) {
109
- return utils_2.PerfLogger.async(`Plugins - contentLoaded`, async () => {
110
- const allContent = aggregateAllContent(plugins);
101
+ return utils_1.PerfLogger.async(`Plugins - contentLoaded`, async () => {
111
102
  const routes = [];
112
103
  const globalData = {};
113
104
  await Promise.all(plugins.map(async (plugin) => {
@@ -115,6 +106,29 @@ async function executePluginsContentLoaded({ plugins, context, }) {
115
106
  const { routes: pluginRoutes, globalData: pluginGlobalData } = await executePluginContentLoaded({
116
107
  plugin,
117
108
  context,
109
+ });
110
+ routes.push(...pluginRoutes);
111
+ if (pluginGlobalData !== undefined) {
112
+ globalData[_a = plugin.name] ?? (globalData[_a] = {});
113
+ globalData[plugin.name][plugin.options.id] = pluginGlobalData;
114
+ }
115
+ }));
116
+ // Sort the route config.
117
+ // This ensures that route with sub routes are always placed last.
118
+ (0, routeConfig_1.sortRoutes)(routes, context.siteConfig.baseUrl);
119
+ return { routes, globalData };
120
+ });
121
+ }
122
+ async function executePluginsAllContentLoaded({ plugins, context, }) {
123
+ return utils_1.PerfLogger.async(`Plugins - allContentLoaded`, async () => {
124
+ const allContent = aggregateAllContent(plugins);
125
+ const routes = [];
126
+ const globalData = {};
127
+ await Promise.all(plugins.map(async (plugin) => {
128
+ var _a;
129
+ const { routes: pluginRoutes, globalData: pluginGlobalData } = await executePluginAllContentLoaded({
130
+ plugin,
131
+ context,
118
132
  allContent,
119
133
  });
120
134
  routes.push(...pluginRoutes);
@@ -129,25 +143,53 @@ async function executePluginsContentLoaded({ plugins, context, }) {
129
143
  return { routes, globalData };
130
144
  });
131
145
  }
146
+ function mergeGlobalData(...globalDataList) {
147
+ const result = {};
148
+ const allPluginIdentifiers = globalDataList.flatMap((gd) => Object.keys(gd).flatMap((name) => Object.keys(gd[name]).map((id) => ({ name, id }))));
149
+ allPluginIdentifiers.forEach(({ name, id }) => {
150
+ const allData = globalDataList
151
+ .map((gd) => gd?.[name]?.[id])
152
+ .filter((d) => typeof d !== 'undefined');
153
+ const mergedData = allData.length === 1 ? allData[0] : Object.assign({}, ...allData);
154
+ result[name] ?? (result[name] = {});
155
+ result[name][id] = mergedData;
156
+ });
157
+ return result;
158
+ }
159
+ exports.mergeGlobalData = mergeGlobalData;
160
+ function mergeResults({ contentLoadedResult, allContentLoadedResult, }) {
161
+ const routes = [
162
+ ...contentLoadedResult.routes,
163
+ ...allContentLoadedResult.routes,
164
+ ];
165
+ (0, routeConfig_1.sortRoutes)(routes);
166
+ const globalData = mergeGlobalData(contentLoadedResult.globalData, allContentLoadedResult.globalData);
167
+ return { routes, globalData };
168
+ }
132
169
  /**
133
- * Initializes the plugins, runs `loadContent`, `translateContent`,
134
- * `contentLoaded`, and `translateThemeConfig`. Because `contentLoaded` is
135
- * side-effect-ful (it generates temp files), so is this function. This function
136
- * would also mutate `context.siteConfig.themeConfig` to translate it.
170
+ * Initializes the plugins and run their lifecycle functions.
137
171
  */
138
172
  async function loadPlugins(context) {
139
- return utils_2.PerfLogger.async('Plugins - loadPlugins', async () => {
140
- // 1. Plugin Lifecycle - Initialization/Constructor.
141
- const plugins = await utils_2.PerfLogger.async('Plugins - initPlugins', () => (0, init_1.initPlugins)(context));
142
- plugins.push((0, synthetic_1.createBootstrapPlugin)(context), (0, synthetic_1.createMDXFallbackPlugin)(context));
143
- // 2. Plugin Lifecycle - loadContent.
144
- const loadedPlugins = await executePluginsLoadContent({ plugins, context });
145
- // 3. Plugin Lifecycle - contentLoaded.
146
- const { routes, globalData } = await executePluginsContentLoaded({
147
- plugins: loadedPlugins,
173
+ return utils_1.PerfLogger.async('Plugins - loadPlugins', async () => {
174
+ const initializedPlugins = await utils_1.PerfLogger.async('Plugins - initPlugins', () => (0, init_1.initPlugins)(context));
175
+ initializedPlugins.push((0, synthetic_1.createBootstrapPlugin)(context), (0, synthetic_1.createMDXFallbackPlugin)(context));
176
+ const plugins = await executePluginsLoadContent({
177
+ plugins: initializedPlugins,
178
+ context,
179
+ });
180
+ const contentLoadedResult = await executePluginsContentLoaded({
181
+ plugins,
182
+ context,
183
+ });
184
+ const allContentLoadedResult = await executePluginsAllContentLoaded({
185
+ plugins,
148
186
  context,
149
187
  });
150
- return { plugins: loadedPlugins, routes, globalData };
188
+ const { routes, globalData } = mergeResults({
189
+ contentLoadedResult,
190
+ allContentLoadedResult,
191
+ });
192
+ return { plugins, routes, globalData };
151
193
  });
152
194
  }
153
195
  exports.loadPlugins = loadPlugins;
@@ -159,18 +201,28 @@ function getPluginByIdentifier({ plugins, pluginIdentifier, }) {
159
201
  return plugin;
160
202
  }
161
203
  exports.getPluginByIdentifier = getPluginByIdentifier;
162
- async function reloadPlugin({ pluginIdentifier, plugins, context, }) {
163
- return utils_2.PerfLogger.async('Plugins - reloadPlugin', async () => {
164
- const plugin = getPluginByIdentifier({ plugins, pluginIdentifier });
204
+ async function reloadPlugin({ pluginIdentifier, plugins: previousPlugins, context, }) {
205
+ return utils_1.PerfLogger.async('Plugins - reloadPlugin', async () => {
206
+ const plugin = getPluginByIdentifier({
207
+ plugins: previousPlugins,
208
+ pluginIdentifier,
209
+ });
165
210
  const reloadedPlugin = await executePluginLoadContent({ plugin, context });
166
- const newPlugins = plugins.with(plugins.indexOf(plugin), reloadedPlugin);
167
- // Unfortunately, due to the "AllContent" data we have to re-execute this
168
- // for all plugins, not just the one to reload...
169
- const { routes, globalData } = await executePluginsContentLoaded({
170
- plugins: newPlugins,
211
+ const plugins = previousPlugins.with(previousPlugins.indexOf(plugin), reloadedPlugin);
212
+ // TODO optimize this, we shouldn't need to re-run this lifecycle
213
+ const contentLoadedResult = await executePluginsContentLoaded({
214
+ plugins,
171
215
  context,
172
216
  });
173
- return { plugins: newPlugins, routes, globalData };
217
+ const allContentLoadedResult = await executePluginsAllContentLoaded({
218
+ plugins,
219
+ context,
220
+ });
221
+ const { routes, globalData } = mergeResults({
222
+ contentLoadedResult,
223
+ allContentLoadedResult,
224
+ });
225
+ return { plugins, routes, globalData };
174
226
  });
175
227
  }
176
228
  exports.reloadPlugin = reloadPlugin;
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-5860",
4
+ "version": "0.0.0-5863",
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-5860",
47
- "@docusaurus/logger": "0.0.0-5860",
48
- "@docusaurus/mdx-loader": "0.0.0-5860",
46
+ "@docusaurus/cssnano-preset": "0.0.0-5863",
47
+ "@docusaurus/logger": "0.0.0-5863",
48
+ "@docusaurus/mdx-loader": "0.0.0-5863",
49
49
  "@docusaurus/react-loadable": "5.5.2",
50
- "@docusaurus/utils": "0.0.0-5860",
51
- "@docusaurus/utils-common": "0.0.0-5860",
52
- "@docusaurus/utils-validation": "0.0.0-5860",
50
+ "@docusaurus/utils": "0.0.0-5863",
51
+ "@docusaurus/utils-common": "0.0.0-5863",
52
+ "@docusaurus/utils-validation": "0.0.0-5863",
53
53
  "@svgr/webpack": "^6.5.1",
54
54
  "autoprefixer": "^10.4.14",
55
55
  "babel-loader": "^9.1.3",
@@ -105,8 +105,9 @@
105
105
  "webpackbar": "^5.0.2"
106
106
  },
107
107
  "devDependencies": {
108
- "@docusaurus/module-type-aliases": "0.0.0-5860",
109
- "@docusaurus/types": "0.0.0-5860",
108
+ "@docusaurus/module-type-aliases": "0.0.0-5863",
109
+ "@docusaurus/types": "0.0.0-5863",
110
+ "@total-typescript/shoehorn": "^0.1.2",
110
111
  "@types/detect-port": "^1.3.3",
111
112
  "@types/react-dom": "^18.2.7",
112
113
  "@types/react-router-config": "^5.0.7",
@@ -125,5 +126,5 @@
125
126
  "engines": {
126
127
  "node": ">=18.0"
127
128
  },
128
- "gitHead": "dc91f3f6c8cf54d51409a24d823db17eee03a01a"
129
+ "gitHead": "8a2e5a97b3258f37311ef8d7bb890770e8027867"
129
130
  }