@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
@@ -0,0 +1,46 @@
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.resolveModuleName = exports.getNamePatterns = void 0;
10
+ function getNamePatterns(moduleName, moduleType) {
11
+ if (moduleName.startsWith('@')) {
12
+ // Pure scope: `@scope` => `@scope/docusaurus-plugin`
13
+ if (!moduleName.includes('/')) {
14
+ return [`${moduleName}/docusaurus-${moduleType}`];
15
+ }
16
+ const [scope, packageName] = moduleName.split(/\/(?<rest>.*)/);
17
+ return [
18
+ `${scope}/${packageName}`,
19
+ `${scope}/docusaurus-${moduleType}-${packageName}`,
20
+ ];
21
+ }
22
+ return [
23
+ moduleName,
24
+ `@docusaurus/${moduleType}-${moduleName}`,
25
+ `docusaurus-${moduleType}-${moduleName}`,
26
+ ];
27
+ }
28
+ exports.getNamePatterns = getNamePatterns;
29
+ function resolveModuleName(moduleName, moduleRequire, moduleType) {
30
+ const modulePatterns = getNamePatterns(moduleName, moduleType);
31
+ const module = modulePatterns.find((m) => {
32
+ try {
33
+ moduleRequire.resolve(m);
34
+ return true;
35
+ }
36
+ catch {
37
+ return false;
38
+ }
39
+ });
40
+ if (!module) {
41
+ throw new Error(`Docusaurus was unable to resolve the "${moduleName}" ${moduleType}. Make sure one of the following packages are installed:
42
+ ${modulePatterns.map((m) => `- ${m}`).join('\n')}`);
43
+ }
44
+ return module;
45
+ }
46
+ exports.resolveModuleName = resolveModuleName;
@@ -0,0 +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
+ */
7
+ import type { RouteConfig } from '@docusaurus/types';
8
+ import { type ApplyTrailingSlashParams } from '@docusaurus/utils-common';
9
+ export default function applyRouteTrailingSlash(route: RouteConfig, params: ApplyTrailingSlashParams): RouteConfig;
@@ -0,0 +1,19 @@
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
+ const utils_common_1 = require("@docusaurus/utils-common");
10
+ function applyRouteTrailingSlash(route, params) {
11
+ return {
12
+ ...route,
13
+ path: (0, utils_common_1.applyTrailingSlash)(route.path, params),
14
+ ...(route.routes && {
15
+ routes: route.routes.map((subroute) => applyRouteTrailingSlash(subroute, params)),
16
+ }),
17
+ };
18
+ }
19
+ exports.default = applyRouteTrailingSlash;
@@ -4,15 +4,14 @@
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 { LoadContext, PluginConfig, RouteConfig, ThemeConfig } from '@docusaurus/types';
8
- import { InitPlugin } from './init';
9
- export declare function sortConfig(routeConfigs: RouteConfig[]): void;
7
+ import type { LoadContext, PluginConfig, RouteConfig, GlobalData, ThemeConfig, LoadedPlugin } from '@docusaurus/types';
8
+ export declare function sortConfig(routeConfigs: RouteConfig[], baseUrl?: string): void;
10
9
  export declare function loadPlugins({ pluginConfigs, context, }: {
11
10
  pluginConfigs: PluginConfig[];
12
11
  context: LoadContext;
13
12
  }): Promise<{
14
- plugins: InitPlugin[];
13
+ plugins: LoadedPlugin[];
15
14
  pluginsRouteConfigs: RouteConfig[];
16
- globalData: unknown;
15
+ globalData: GlobalData;
17
16
  themeConfigTranslated: ThemeConfig;
18
17
  }>;
@@ -12,14 +12,21 @@ const utils_1 = require("@docusaurus/utils");
12
12
  const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
13
13
  const path_1 = tslib_1.__importDefault(require("path"));
14
14
  const init_1 = tslib_1.__importDefault(require("./init"));
15
- const chalk_1 = tslib_1.__importDefault(require("chalk"));
16
- const constants_1 = require("../../constants");
17
- const lodash_1 = require("lodash");
15
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
16
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
18
17
  const translations_1 = require("../translations/translations");
19
- function sortConfig(routeConfigs) {
18
+ const applyRouteTrailingSlash_1 = tslib_1.__importDefault(require("./applyRouteTrailingSlash"));
19
+ function sortConfig(routeConfigs, baseUrl = '/') {
20
20
  // Sort the route config. This ensures that route with nested
21
21
  // routes is always placed last.
22
22
  routeConfigs.sort((a, b) => {
23
+ // Root route should get placed last.
24
+ if (a.path === baseUrl && b.path !== baseUrl) {
25
+ return 1;
26
+ }
27
+ if (a.path !== baseUrl && b.path === baseUrl) {
28
+ return -1;
29
+ }
23
30
  if (a.routes && !b.routes) {
24
31
  return 1;
25
32
  }
@@ -38,71 +45,89 @@ function sortConfig(routeConfigs) {
38
45
  return a.path.localeCompare(b.path);
39
46
  });
40
47
  routeConfigs.forEach((routeConfig) => {
41
- var _a;
42
- (_a = routeConfig.routes) === null || _a === void 0 ? void 0 : _a.sort((a, b) => a.path.localeCompare(b.path));
48
+ routeConfig.routes?.sort((a, b) => a.path.localeCompare(b.path));
43
49
  });
44
50
  }
45
51
  exports.sortConfig = sortConfig;
46
52
  async function loadPlugins({ pluginConfigs, context, }) {
47
53
  // 1. Plugin Lifecycle - Initialization/Constructor.
48
- const plugins = init_1.default({
54
+ const plugins = await (0, init_1.default)({
49
55
  pluginConfigs,
50
56
  context,
51
57
  });
52
- const contentLoadedPlugins = await Promise.all(plugins.map(async (plugin) => {
53
- const content = plugin.loadContent ? await plugin.loadContent() : null;
54
- return { plugin, content };
58
+ // 2. Plugin Lifecycle - loadContent.
59
+ // Currently plugins run lifecycle methods in parallel and are not
60
+ // order-dependent. We could change this in future if there are plugins which
61
+ // need to run in certain order or depend on others for data.
62
+ const loadedPlugins = await Promise.all(plugins.map(async (plugin) => {
63
+ const content = await plugin.loadContent?.();
64
+ return { ...plugin, content };
55
65
  }));
56
- const contentLoadedTranslatedPlugins = await Promise.all(contentLoadedPlugins.map(async (contentLoadedPlugin) => {
57
- var _a, _b, _c;
58
- const translationFiles = (_c = (await ((_b = (_a = contentLoadedPlugin.plugin) === null || _a === void 0 ? void 0 : _a.getTranslationFiles) === null || _b === void 0 ? void 0 : _b.call(_a, {
66
+ const contentLoadedTranslatedPlugins = await Promise.all(loadedPlugins.map(async (contentLoadedPlugin) => {
67
+ const translationFiles = (await contentLoadedPlugin?.getTranslationFiles?.({
59
68
  content: contentLoadedPlugin.content,
60
- })))) !== null && _c !== void 0 ? _c : [];
61
- const localizedTranslationFiles = await Promise.all(translationFiles.map((translationFile) => translations_1.localizePluginTranslationFile({
69
+ })) ?? [];
70
+ const localizedTranslationFiles = await Promise.all(translationFiles.map((translationFile) => (0, translations_1.localizePluginTranslationFile)({
62
71
  locale: context.i18n.currentLocale,
63
72
  siteDir: context.siteDir,
64
73
  translationFile,
65
- plugin: contentLoadedPlugin.plugin,
74
+ plugin: contentLoadedPlugin,
66
75
  })));
67
76
  return {
68
77
  ...contentLoadedPlugin,
69
78
  translationFiles: localizedTranslationFiles,
70
79
  };
71
80
  }));
72
- const allContent = lodash_1.chain(contentLoadedPlugins)
73
- .groupBy((item) => item.plugin.name)
74
- .mapValues((nameItems) => {
75
- return lodash_1.chain(nameItems)
76
- .groupBy((item) => { var _a; return (_a = item.plugin.options.id) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_PLUGIN_ID; })
77
- .mapValues((idItems) => idItems[0].content)
78
- .value();
79
- })
81
+ const allContent = lodash_1.default.chain(loadedPlugins)
82
+ .groupBy((item) => item.name)
83
+ .mapValues((nameItems) => lodash_1.default.chain(nameItems)
84
+ .groupBy((item) => item.options.id)
85
+ .mapValues((idItems) => idItems[0].content)
86
+ .value())
80
87
  .value();
81
88
  // 3. Plugin Lifecycle - contentLoaded.
82
89
  const pluginsRouteConfigs = [];
83
90
  const globalData = {};
84
- await Promise.all(contentLoadedTranslatedPlugins.map(async ({ plugin, content, translationFiles }) => {
85
- var _a, _b, _c;
91
+ await Promise.all(contentLoadedTranslatedPlugins.map(async ({ content, translationFiles, ...plugin }) => {
86
92
  if (!plugin.contentLoaded) {
87
93
  return;
88
94
  }
89
- const pluginId = (_a = plugin.options.id) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_PLUGIN_ID;
95
+ const pluginId = plugin.options.id;
90
96
  // plugins data files are namespaced by pluginName/pluginId
91
97
  const dataDirRoot = path_1.default.join(context.generatedFilesDir, plugin.name);
92
98
  const dataDir = path_1.default.join(dataDirRoot, pluginId);
93
- const addRoute = (config) => pluginsRouteConfigs.push(config);
94
99
  const createData = async (name, data) => {
95
100
  const modulePath = path_1.default.join(dataDir, name);
96
101
  await fs_extra_1.default.ensureDir(path_1.default.dirname(modulePath));
97
- await utils_1.generate(dataDir, name, data);
102
+ await (0, utils_1.generate)(dataDir, name, data);
98
103
  return modulePath;
99
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));
112
+ const addRoute = (initialRouteConfig) => {
113
+ // Trailing slash behavior is handled in a generic way for all plugins
114
+ const finalRouteConfig = (0, applyRouteTrailingSlash_1.default)(initialRouteConfig, {
115
+ trailingSlash: context.siteConfig.trailingSlash,
116
+ baseUrl: context.siteConfig.baseUrl,
117
+ });
118
+ pluginsRouteConfigs.push({
119
+ ...finalRouteConfig,
120
+ modules: {
121
+ ...finalRouteConfig.modules,
122
+ __routeContextModule: pluginRouteContextModulePath,
123
+ },
124
+ });
125
+ };
100
126
  // the plugins global data are namespaced to avoid data conflicts:
101
127
  // - by plugin name
102
128
  // - by plugin id (allow using multiple instances of the same plugin)
103
129
  const setGlobalData = (data) => {
104
- var _a;
105
- globalData[plugin.name] = (_a = globalData[plugin.name]) !== null && _a !== void 0 ? _a : {};
130
+ globalData[plugin.name] = globalData[plugin.name] ?? {};
106
131
  globalData[plugin.name][pluginId] = data;
107
132
  };
108
133
  const actions = {
@@ -110,7 +135,7 @@ async function loadPlugins({ pluginConfigs, context, }) {
110
135
  createData,
111
136
  setGlobalData,
112
137
  };
113
- const translatedContent = (_c = (_b = plugin.translateContent) === null || _b === void 0 ? void 0 : _b.call(plugin, { content, translationFiles })) !== null && _c !== void 0 ? _c : content;
138
+ const translatedContent = plugin.translateContent?.({ content, translationFiles }) ?? content;
114
139
  await plugin.contentLoaded({
115
140
  content: translatedContent,
116
141
  actions,
@@ -118,29 +143,28 @@ async function loadPlugins({ pluginConfigs, context, }) {
118
143
  });
119
144
  }));
120
145
  // 4. Plugin Lifecycle - routesLoaded.
121
- // Currently plugins run lifecycle methods in parallel and are not order-dependent.
122
- // We could change this in future if there are plugins which need to
123
- // run in certain order or depend on others for data.
124
- await Promise.all(contentLoadedTranslatedPlugins.map(async ({ plugin }) => {
146
+ // Currently plugins run lifecycle methods in parallel and are not
147
+ // order-dependent. We could change this in future if there are plugins which
148
+ // need to run in certain order or depend on others for data.
149
+ await Promise.all(contentLoadedTranslatedPlugins.map(async (plugin) => {
125
150
  if (!plugin.routesLoaded) {
126
- return null;
151
+ return;
127
152
  }
128
153
  // TODO remove this deprecated lifecycle soon
129
154
  // deprecated since alpha-60
130
155
  // TODO, 1 user reported usage of this lifecycle! https://github.com/facebook/docusaurus/issues/3918
131
- console.error(chalk_1.default.red('plugin routesLoaded lifecycle is deprecated. If you think we should keep this lifecycle, please report here: https://github.com/facebook/docusaurus/issues/3918'));
132
- return plugin.routesLoaded(pluginsRouteConfigs);
156
+ logger_1.default.error `Plugin code=${'routesLoaded'} lifecycle is deprecated. If you think we should keep this lifecycle, please report here: path=${'https://github.com/facebook/docusaurus/issues/3918'}`;
157
+ await plugin.routesLoaded(pluginsRouteConfigs);
133
158
  }));
134
159
  // Sort the route config. This ensures that route with nested
135
160
  // routes are always placed last.
136
- sortConfig(pluginsRouteConfigs);
161
+ sortConfig(pluginsRouteConfigs, context.siteConfig.baseUrl);
137
162
  // Apply each plugin one after the other to translate the theme config
138
163
  function translateThemeConfig(untranslatedThemeConfig) {
139
- return contentLoadedTranslatedPlugins.reduce((currentThemeConfig, { plugin, translationFiles }) => {
140
- var _a;
141
- const translatedThemeConfigSlice = (_a = plugin.translateThemeConfig) === null || _a === void 0 ? void 0 : _a.call(plugin, {
164
+ return contentLoadedTranslatedPlugins.reduce((currentThemeConfig, plugin) => {
165
+ const translatedThemeConfigSlice = plugin.translateThemeConfig?.({
142
166
  themeConfig: currentThemeConfig,
143
- translationFiles,
167
+ translationFiles: plugin.translationFiles,
144
168
  });
145
169
  return {
146
170
  ...currentThemeConfig,
@@ -149,7 +173,7 @@ async function loadPlugins({ pluginConfigs, context, }) {
149
173
  }, untranslatedThemeConfig);
150
174
  }
151
175
  return {
152
- plugins,
176
+ plugins: loadedPlugins,
153
177
  pluginsRouteConfigs,
154
178
  globalData,
155
179
  themeConfigTranslated: translateThemeConfig(context.siteConfig.themeConfig),
@@ -4,12 +4,22 @@
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 { DocusaurusPluginVersionInformation, LoadContext, Plugin, PluginConfig, PluginOptions } from '@docusaurus/types';
8
- export declare type InitPlugin = Plugin<unknown> & {
9
- readonly options: PluginOptions;
10
- readonly version: DocusaurusPluginVersionInformation;
7
+ import type { ImportedPluginModule, LoadContext, PluginModule, PluginConfig, PluginOptions, InitializedPlugin } from '@docusaurus/types';
8
+ export declare type NormalizedPluginConfig = {
9
+ plugin: PluginModule;
10
+ options: PluginOptions;
11
+ pluginModule?: {
12
+ path: string;
13
+ module: ImportedPluginModule;
14
+ };
15
+ /**
16
+ * Different from pluginModule.path, this one is always an absolute path used
17
+ * to resolve relative paths returned from lifecycles
18
+ */
19
+ entryPath: string;
11
20
  };
21
+ export declare function normalizePluginConfigs(pluginConfigs: PluginConfig[], configPath: string): Promise<NormalizedPluginConfig[]>;
12
22
  export default function initPlugins({ pluginConfigs, context, }: {
13
23
  pluginConfigs: PluginConfig[];
14
24
  context: LoadContext;
15
- }): InitPlugin[];
25
+ }): Promise<InitializedPlugin[]>;
@@ -6,27 +6,30 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.normalizePluginConfigs = void 0;
9
10
  const tslib_1 = require("tslib");
10
- const module_1 = tslib_1.__importDefault(require("module"));
11
+ const module_1 = require("module");
12
+ const path_1 = tslib_1.__importDefault(require("path"));
11
13
  const import_fresh_1 = tslib_1.__importDefault(require("import-fresh"));
12
- const constants_1 = require("../../constants");
14
+ const utils_1 = require("@docusaurus/utils");
13
15
  const versions_1 = require("../versions");
14
16
  const pluginIds_1 = require("./pluginIds");
15
17
  const utils_validation_1 = require("@docusaurus/utils-validation");
16
- function normalizePluginConfig(pluginConfig, pluginRequire) {
17
- var _a, _b, _c, _d;
18
+ async function normalizePluginConfig(pluginConfig, configPath) {
19
+ const pluginRequire = (0, module_1.createRequire)(configPath);
18
20
  // plugins: ['./plugin']
19
21
  if (typeof pluginConfig === 'string') {
20
22
  const pluginModuleImport = pluginConfig;
21
23
  const pluginPath = pluginRequire.resolve(pluginModuleImport);
22
- const pluginModule = import_fresh_1.default(pluginPath);
24
+ const pluginModule = (0, import_fresh_1.default)(pluginPath);
23
25
  return {
24
- plugin: (_a = pluginModule === null || pluginModule === void 0 ? void 0 : pluginModule.default) !== null && _a !== void 0 ? _a : pluginModule,
26
+ plugin: pluginModule?.default ?? pluginModule,
25
27
  options: {},
26
28
  pluginModule: {
27
29
  path: pluginModuleImport,
28
30
  module: pluginModule,
29
31
  },
32
+ entryPath: pluginPath,
30
33
  };
31
34
  }
32
35
  // plugins: [function plugin() { }]
@@ -34,74 +37,67 @@ function normalizePluginConfig(pluginConfig, pluginRequire) {
34
37
  return {
35
38
  plugin: pluginConfig,
36
39
  options: {},
40
+ entryPath: configPath,
37
41
  };
38
42
  }
39
- if (Array.isArray(pluginConfig)) {
40
- // plugins: [
41
- // ['./plugin',options],
42
- // ]
43
- if (typeof pluginConfig[0] === 'string') {
44
- const pluginModuleImport = pluginConfig[0];
45
- const pluginPath = pluginRequire.resolve(pluginModuleImport);
46
- const pluginModule = import_fresh_1.default(pluginPath);
47
- return {
48
- plugin: (_b = pluginModule === null || pluginModule === void 0 ? void 0 : pluginModule.default) !== null && _b !== void 0 ? _b : pluginModule,
49
- options: (_c = pluginConfig[1]) !== null && _c !== void 0 ? _c : {},
50
- pluginModule: {
51
- path: pluginModuleImport,
52
- module: pluginModule,
53
- },
54
- };
55
- }
56
- // plugins: [
57
- // [function plugin() { },options],
58
- // ]
59
- if (typeof pluginConfig[0] === 'function') {
60
- return {
61
- plugin: pluginConfig[0],
62
- options: (_d = pluginConfig[1]) !== null && _d !== void 0 ? _d : {},
63
- };
64
- }
43
+ // plugins: [
44
+ // ['./plugin',options],
45
+ // ]
46
+ if (typeof pluginConfig[0] === 'string') {
47
+ const pluginModuleImport = pluginConfig[0];
48
+ const pluginPath = pluginRequire.resolve(pluginModuleImport);
49
+ const pluginModule = (0, import_fresh_1.default)(pluginPath);
50
+ return {
51
+ plugin: pluginModule?.default ?? pluginModule,
52
+ options: pluginConfig[1],
53
+ pluginModule: {
54
+ path: pluginModuleImport,
55
+ module: pluginModule,
56
+ },
57
+ entryPath: pluginPath,
58
+ };
65
59
  }
66
- throw new Error(`Unexpected: cant load plugin for plugin config = ${JSON.stringify(pluginConfig)}`);
60
+ // plugins: [
61
+ // [function plugin() { },options],
62
+ // ]
63
+ return {
64
+ plugin: pluginConfig[0],
65
+ options: pluginConfig[1],
66
+ entryPath: configPath,
67
+ };
68
+ }
69
+ async function normalizePluginConfigs(pluginConfigs, configPath) {
70
+ return Promise.all(pluginConfigs.map((pluginConfig) => normalizePluginConfig(pluginConfig, configPath)));
67
71
  }
72
+ exports.normalizePluginConfigs = normalizePluginConfigs;
68
73
  function getOptionValidationFunction(normalizedPluginConfig) {
69
- var _a, _b, _c, _d;
70
74
  if (normalizedPluginConfig.pluginModule) {
71
75
  // support both commonjs and ES modules
72
- return ((_c = (_b = (_a = normalizedPluginConfig.pluginModule.module) === null || _a === void 0 ? void 0 : _a.default) === null || _b === void 0 ? void 0 : _b.validateOptions) !== null && _c !== void 0 ? _c : (_d = normalizedPluginConfig.pluginModule.module) === null || _d === void 0 ? void 0 : _d.validateOptions);
73
- }
74
- else {
75
- return normalizedPluginConfig.plugin.validateOptions;
76
+ return (normalizedPluginConfig.pluginModule.module?.default?.validateOptions ??
77
+ normalizedPluginConfig.pluginModule.module?.validateOptions);
76
78
  }
79
+ return normalizedPluginConfig.plugin.validateOptions;
77
80
  }
78
81
  function getThemeValidationFunction(normalizedPluginConfig) {
79
- var _a, _b;
80
82
  if (normalizedPluginConfig.pluginModule) {
81
83
  // support both commonjs and ES modules
82
- return ((_b = (_a = normalizedPluginConfig.pluginModule.module.default) === null || _a === void 0 ? void 0 : _a.validateThemeConfig) !== null && _b !== void 0 ? _b : normalizedPluginConfig.pluginModule.module.validateThemeConfig);
83
- }
84
- else {
85
- return normalizedPluginConfig.plugin.validateThemeConfig;
84
+ return (normalizedPluginConfig.pluginModule.module.default?.validateThemeConfig ??
85
+ normalizedPluginConfig.pluginModule.module.validateThemeConfig);
86
86
  }
87
+ return normalizedPluginConfig.plugin.validateThemeConfig;
87
88
  }
88
- function initPlugins({ pluginConfigs, context, }) {
89
- // We need to resolve plugins from the perspective of the siteDir, since the siteDir's package.json
90
- // declares the dependency on these plugins.
91
- // We need to fallback to createRequireFromPath since createRequire is only available in node v12.
92
- // See: https://nodejs.org/api/modules.html#modules_module_createrequire_filename
93
- const createRequire = module_1.default.createRequire || module_1.default.createRequireFromPath;
94
- const pluginRequire = createRequire(context.siteConfigPath);
95
- function doGetPluginVersion(normalizedPluginConfig) {
96
- var _a, _b;
89
+ async function initPlugins({ pluginConfigs, context, }) {
90
+ // We need to resolve plugins from the perspective of the siteDir, since the
91
+ // siteDir's package.json declares the dependency on these plugins.
92
+ const pluginRequire = (0, module_1.createRequire)(context.siteConfigPath);
93
+ const pluginConfigsNormalized = await normalizePluginConfigs(pluginConfigs, context.siteConfigPath);
94
+ async function doGetPluginVersion(normalizedPluginConfig) {
97
95
  // get plugin version
98
- if ((_a = normalizedPluginConfig.pluginModule) === null || _a === void 0 ? void 0 : _a.path) {
99
- const pluginPath = pluginRequire.resolve((_b = normalizedPluginConfig.pluginModule) === null || _b === void 0 ? void 0 : _b.path);
100
- return versions_1.getPluginVersion(pluginPath, context.siteDir);
101
- }
102
- else {
103
- return { type: 'local' };
96
+ if (normalizedPluginConfig.pluginModule?.path) {
97
+ const pluginPath = pluginRequire.resolve(normalizedPluginConfig.pluginModule?.path);
98
+ return (0, versions_1.getPluginVersion)(pluginPath, context.siteDir);
104
99
  }
100
+ return { type: 'local' };
105
101
  }
106
102
  function doValidateThemeConfig(normalizedPluginConfig) {
107
103
  const validateThemeConfig = getThemeValidationFunction(normalizedPluginConfig);
@@ -111,12 +107,9 @@ function initPlugins({ pluginConfigs, context, }) {
111
107
  themeConfig: context.siteConfig.themeConfig,
112
108
  });
113
109
  }
114
- else {
115
- return context.siteConfig.themeConfig;
116
- }
110
+ return context.siteConfig.themeConfig;
117
111
  }
118
112
  function doValidatePluginOptions(normalizedPluginConfig) {
119
- var _a;
120
113
  const validateOptions = getOptionValidationFunction(normalizedPluginConfig);
121
114
  if (validateOptions) {
122
115
  return validateOptions({
@@ -124,37 +117,31 @@ function initPlugins({ pluginConfigs, context, }) {
124
117
  options: normalizedPluginConfig.options,
125
118
  });
126
119
  }
127
- else {
128
- // Important to ensure all plugins have an id
129
- // as we don't go through the Joi schema that adds it
130
- return {
131
- ...normalizedPluginConfig.options,
132
- id: (_a = normalizedPluginConfig.options.id) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_PLUGIN_ID,
133
- };
134
- }
120
+ // Important to ensure all plugins have an id
121
+ // as we don't go through the Joi schema that adds it
122
+ return {
123
+ ...normalizedPluginConfig.options,
124
+ id: normalizedPluginConfig.options.id ?? utils_1.DEFAULT_PLUGIN_ID,
125
+ };
135
126
  }
136
- const plugins = pluginConfigs
137
- .map((pluginConfig) => {
138
- if (!pluginConfig) {
139
- return null;
140
- }
141
- const normalizedPluginConfig = normalizePluginConfig(pluginConfig, pluginRequire);
142
- const pluginVersion = doGetPluginVersion(normalizedPluginConfig);
127
+ async function initializePlugin(normalizedPluginConfig) {
128
+ const pluginVersion = await doGetPluginVersion(normalizedPluginConfig);
143
129
  const pluginOptions = doValidatePluginOptions(normalizedPluginConfig);
144
130
  // Side-effect: merge the normalized theme config in the original one
145
131
  context.siteConfig.themeConfig = {
146
132
  ...context.siteConfig.themeConfig,
147
133
  ...doValidateThemeConfig(normalizedPluginConfig),
148
134
  };
149
- const pluginInstance = normalizedPluginConfig.plugin(context, pluginOptions);
135
+ const pluginInstance = await normalizedPluginConfig.plugin(context, pluginOptions);
150
136
  return {
151
137
  ...pluginInstance,
152
138
  options: pluginOptions,
153
139
  version: pluginVersion,
140
+ path: path_1.default.dirname(normalizedPluginConfig.entryPath),
154
141
  };
155
- })
156
- .filter((item) => Boolean(item));
157
- pluginIds_1.ensureUniquePluginInstanceIds(plugins);
142
+ }
143
+ const plugins = await Promise.all(pluginConfigsNormalized.map(initializePlugin));
144
+ (0, pluginIds_1.ensureUniquePluginInstanceIds)(plugins);
158
145
  return plugins;
159
146
  }
160
147
  exports.default = initPlugins;
@@ -4,5 +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
- import { InitPlugin } from './init';
8
- export declare function ensureUniquePluginInstanceIds(plugins: InitPlugin[]): void;
7
+ import type { InitializedPlugin } from '@docusaurus/types';
8
+ export declare function ensureUniquePluginInstanceIds(plugins: InitializedPlugin[]): void;
@@ -7,17 +7,20 @@
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.ensureUniquePluginInstanceIds = void 0;
10
- const lodash_1 = require("lodash");
11
- const constants_1 = require("../../constants");
10
+ const tslib_1 = require("tslib");
11
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
12
+ const utils_1 = require("@docusaurus/utils");
12
13
  // It is forbidden to have 2 plugins of the same name sharing the same id
13
14
  // this is required to support multi-instance plugins without conflict
14
15
  function ensureUniquePluginInstanceIds(plugins) {
15
- const pluginsByName = lodash_1.groupBy(plugins, (p) => p.name);
16
+ const pluginsByName = lodash_1.default.groupBy(plugins, (p) => p.name);
16
17
  Object.entries(pluginsByName).forEach(([pluginName, pluginInstances]) => {
17
- const pluginInstancesById = lodash_1.groupBy(pluginInstances, (p) => { var _a; return (_a = p.options.id) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_PLUGIN_ID; });
18
+ const pluginInstancesById = lodash_1.default.groupBy(pluginInstances, (p) => p.options.id ?? utils_1.DEFAULT_PLUGIN_ID);
18
19
  Object.entries(pluginInstancesById).forEach(([pluginId, pluginInstancesWithId]) => {
19
20
  if (pluginInstancesWithId.length !== 1) {
20
- throw new Error(`Plugin ${pluginName} is used ${pluginInstancesWithId.length} times with id=${pluginId}.\nTo use the same plugin multiple times on a Docusaurus site, you need to assign a unique id to each plugin instance.`);
21
+ throw new Error(`Plugin "${pluginName}" is used ${pluginInstancesWithId.length} times with ID "${pluginId}".\nTo use the same plugin multiple times on a Docusaurus site, you need to assign a unique ID to each plugin instance.${pluginId === utils_1.DEFAULT_PLUGIN_ID
22
+ ? `\n\nThe plugin ID is "${utils_1.DEFAULT_PLUGIN_ID}" by default. It's possible that the preset you are using already includes a plugin instance, in which case you either want to disable the plugin in the preset (to use a single instance), or assign another ID to your extra plugin instance (to use multiple instances).`
23
+ : ''}`);
21
24
  }
22
25
  });
23
26
  });
@@ -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
- import { LoadContext, PluginConfig } from '@docusaurus/types';
8
- export default function loadPresets(context: LoadContext): {
7
+ import type { LoadContext, PluginConfig } from '@docusaurus/types';
8
+ export default function loadPresets(context: LoadContext): Promise<{
9
9
  plugins: PluginConfig[];
10
10
  themes: PluginConfig[];
11
- };
11
+ }>;