@docusaurus/core 2.0.0-beta.16 → 2.0.0-beta.19

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 (171) hide show
  1. package/bin/beforeCli.mjs +14 -12
  2. package/bin/docusaurus.mjs +41 -93
  3. package/lib/babel/preset.js +2 -2
  4. package/lib/client/.eslintrc.js +2 -3
  5. package/lib/client/App.d.ts +1 -1
  6. package/lib/client/App.js +15 -7
  7. package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.d.ts → BaseUrlIssueBanner/index.d.ts} +10 -5
  8. package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.js → BaseUrlIssueBanner/index.js} +14 -9
  9. package/lib/client/{baseUrlIssueBanner → BaseUrlIssueBanner}/styles.module.css +0 -0
  10. package/lib/client/ClientLifecyclesDispatcher.d.ts +16 -0
  11. package/lib/client/ClientLifecyclesDispatcher.js +34 -0
  12. package/lib/client/LinksCollector.d.ts +1 -1
  13. package/lib/client/LinksCollector.js +4 -5
  14. package/lib/client/PendingNavigation.d.ts +8 -17
  15. package/lib/client/PendingNavigation.js +39 -70
  16. package/lib/{server/loadSetup.d.ts → client/SiteMetadataDefaults.d.ts} +2 -2
  17. package/lib/client/SiteMetadataDefaults.js +19 -0
  18. package/lib/client/{exports/browserContext.d.ts → browserContext.d.ts} +0 -0
  19. package/lib/client/{exports/browserContext.js → browserContext.js} +0 -0
  20. package/lib/client/clientEntry.js +3 -4
  21. package/lib/client/docusaurus.d.ts +5 -5
  22. package/lib/client/docusaurus.js +26 -33
  23. package/lib/client/{exports/docusaurusContext.d.ts → docusaurusContext.d.ts} +0 -0
  24. package/lib/client/{exports/docusaurusContext.js → docusaurusContext.js} +0 -0
  25. package/lib/client/exports/BrowserOnly.d.ts +3 -4
  26. package/lib/client/exports/BrowserOnly.js +2 -2
  27. package/lib/client/exports/ComponentCreator.js +65 -40
  28. package/lib/client/exports/ErrorBoundary.d.ts +2 -2
  29. package/lib/client/exports/ErrorBoundary.js +4 -5
  30. package/lib/client/exports/Interpolate.d.ts +1 -1
  31. package/lib/client/exports/Interpolate.js +18 -41
  32. package/lib/client/exports/Link.d.ts +3 -15
  33. package/lib/client/exports/Link.js +24 -30
  34. package/lib/client/exports/Translate.d.ts +2 -2
  35. package/lib/client/exports/Translate.js +3 -3
  36. package/lib/client/exports/useBaseUrl.js +8 -9
  37. package/lib/client/exports/useDocusaurusContext.js +1 -1
  38. package/lib/client/exports/useGlobalData.d.ts +4 -3
  39. package/lib/client/exports/useGlobalData.js +5 -5
  40. package/lib/client/exports/useIsBrowser.js +1 -1
  41. package/lib/{server/client-modules/index.d.ts → client/exports/useRouteContext.d.ts} +2 -2
  42. package/lib/client/exports/useRouteContext.js +15 -0
  43. package/lib/client/flat.d.ts +12 -2
  44. package/lib/client/flat.js +12 -5
  45. package/lib/client/normalizeLocation.js +13 -8
  46. package/lib/client/prefetch.js +10 -28
  47. package/lib/client/preload.d.ts +1 -3
  48. package/lib/client/preload.js +5 -11
  49. package/lib/client/routeContext.d.ts +13 -0
  50. package/lib/client/routeContext.js +31 -0
  51. package/lib/client/serverEntry.js +19 -18
  52. package/lib/client/theme-fallback/Error/index.js +7 -1
  53. package/lib/client/theme-fallback/Layout/index.d.ts +1 -1
  54. package/lib/client/theme-fallback/Layout/index.js +2 -17
  55. package/lib/client/theme-fallback/Loading/index.js +2 -0
  56. package/lib/client/theme-fallback/NotFound/index.js +13 -5
  57. package/lib/client/theme-fallback/Root/index.d.ts +4 -4
  58. package/lib/client/theme-fallback/Root/index.js +2 -1
  59. package/lib/{server/html-tags/htmlTags.d.ts → client/theme-fallback/SiteMetadata/index.d.ts} +2 -1
  60. package/lib/client/theme-fallback/SiteMetadata/index.js +10 -0
  61. package/lib/commands/build.d.ts +6 -2
  62. package/lib/commands/build.js +48 -30
  63. package/lib/commands/clear.d.ts +1 -1
  64. package/lib/commands/clear.js +6 -5
  65. package/lib/commands/deploy.d.ts +5 -5
  66. package/lib/commands/deploy.js +21 -45
  67. package/lib/commands/external.d.ts +1 -1
  68. package/lib/commands/external.js +6 -11
  69. package/lib/commands/serve.d.ts +7 -2
  70. package/lib/commands/serve.js +18 -19
  71. package/lib/commands/start.d.ts +8 -2
  72. package/lib/commands/start.js +33 -30
  73. package/lib/commands/swizzle/actions.d.ts +2 -2
  74. package/lib/commands/swizzle/actions.js +10 -10
  75. package/lib/commands/swizzle/common.d.ts +3 -3
  76. package/lib/commands/swizzle/common.js +8 -9
  77. package/lib/commands/swizzle/components.js +48 -13
  78. package/lib/commands/swizzle/config.js +21 -15
  79. package/lib/commands/swizzle/context.js +6 -12
  80. package/lib/commands/swizzle/index.d.ts +2 -2
  81. package/lib/commands/swizzle/index.js +5 -4
  82. package/lib/commands/swizzle/prompts.js +2 -2
  83. package/lib/commands/swizzle/tables.js +10 -13
  84. package/lib/commands/swizzle/themes.js +9 -8
  85. package/lib/commands/writeHeadingIds.d.ts +2 -9
  86. package/lib/commands/writeHeadingIds.js +11 -69
  87. package/lib/commands/writeTranslations.d.ts +3 -4
  88. package/lib/commands/writeTranslations.js +10 -14
  89. package/lib/index.d.ts +9 -10
  90. package/lib/index.js +18 -19
  91. package/lib/server/brokenLinks.d.ts +3 -16
  92. package/lib/server/brokenLinks.js +37 -31
  93. package/lib/server/clientModules.d.ts +12 -0
  94. package/lib/server/clientModules.js +20 -0
  95. package/lib/server/config.d.ts +5 -2
  96. package/lib/server/config.js +14 -9
  97. package/lib/server/configValidation.d.ts +1 -1
  98. package/lib/server/configValidation.js +39 -13
  99. package/lib/server/getHostPort.d.ts +14 -0
  100. package/lib/{choosePort.js → server/getHostPort.js} +24 -41
  101. package/lib/server/htmlTags.d.ts +12 -0
  102. package/lib/server/htmlTags.js +62 -0
  103. package/lib/server/i18n.d.ts +2 -11
  104. package/lib/server/i18n.js +7 -28
  105. package/lib/server/index.d.ts +28 -13
  106. package/lib/server/index.js +62 -229
  107. package/lib/server/plugins/configs.d.ts +51 -0
  108. package/lib/server/plugins/configs.js +101 -0
  109. package/lib/server/plugins/index.d.ts +9 -8
  110. package/lib/server/plugins/index.js +65 -132
  111. package/lib/server/plugins/init.d.ts +6 -15
  112. package/lib/server/plugins/init.js +25 -83
  113. package/lib/server/{moduleShorthand.d.ts → plugins/moduleShorthand.d.ts} +0 -0
  114. package/lib/server/{moduleShorthand.js → plugins/moduleShorthand.js} +0 -0
  115. package/lib/server/plugins/pluginIds.d.ts +4 -0
  116. package/lib/server/plugins/pluginIds.js +6 -4
  117. package/lib/server/plugins/presets.d.ts +12 -0
  118. package/lib/server/{presets/index.js → plugins/presets.js} +21 -20
  119. package/lib/server/plugins/{applyRouteTrailingSlash.d.ts → routeConfig.d.ts} +3 -1
  120. package/lib/server/plugins/routeConfig.js +54 -0
  121. package/lib/server/plugins/synthetic.d.ts +20 -0
  122. package/lib/server/plugins/synthetic.js +112 -0
  123. package/lib/server/routes.d.ts +39 -7
  124. package/lib/server/routes.js +169 -102
  125. package/lib/server/siteMetadata.d.ts +12 -0
  126. package/lib/server/siteMetadata.js +81 -0
  127. package/lib/server/translations/translations.d.ts +5 -14
  128. package/lib/server/translations/translations.js +23 -39
  129. package/lib/server/translations/translationsExtractor.d.ts +2 -2
  130. package/lib/server/translations/translationsExtractor.js +16 -19
  131. package/lib/server/utils.js +1 -1
  132. package/lib/webpack/aliases/index.d.ts +34 -0
  133. package/lib/webpack/aliases/index.js +106 -0
  134. package/lib/webpack/base.d.ts +0 -1
  135. package/lib/webpack/base.js +12 -30
  136. package/lib/webpack/client.js +7 -8
  137. package/lib/webpack/plugins/ChunkAssetPlugin.js +7 -7
  138. package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +2 -2
  139. package/lib/webpack/plugins/CleanWebpackPlugin.js +2 -2
  140. package/lib/webpack/plugins/LogPlugin.js +2 -2
  141. package/lib/webpack/plugins/WaitPlugin.d.ts +2 -2
  142. package/lib/webpack/plugins/WaitPlugin.js +3 -3
  143. package/lib/webpack/server.d.ts +2 -2
  144. package/lib/webpack/server.js +10 -8
  145. package/lib/webpack/utils.d.ts +9 -3
  146. package/lib/webpack/utils.js +20 -25
  147. package/package.json +38 -41
  148. package/lib/choosePort.d.ts +0 -11
  149. package/lib/client/client-lifecycles-dispatcher.d.ts +0 -9
  150. package/lib/client/client-lifecycles-dispatcher.js +0 -23
  151. package/lib/client/nprogress.css +0 -36
  152. package/lib/commands/commandUtils.d.ts +0 -9
  153. package/lib/commands/commandUtils.js +0 -21
  154. package/lib/server/client-modules/index.js +0 -12
  155. package/lib/server/duplicateRoutes.d.ts +0 -10
  156. package/lib/server/duplicateRoutes.js +0 -42
  157. package/lib/server/html-tags/htmlTags.js +0 -38
  158. package/lib/server/html-tags/index.d.ts +0 -9
  159. package/lib/server/html-tags/index.js +0 -43
  160. package/lib/server/loadSetup.js +0 -25
  161. package/lib/server/plugins/applyRouteTrailingSlash.js +0 -19
  162. package/lib/server/presets/index.d.ts +0 -11
  163. package/lib/server/themes/alias.d.ts +0 -9
  164. package/lib/server/themes/alias.js +0 -48
  165. package/lib/server/themes/index.d.ts +0 -12
  166. package/lib/server/themes/index.js +0 -47
  167. package/lib/server/versions/__fixtures__/dummy-plugin.d.ts +0 -0
  168. package/lib/server/versions/__fixtures__/dummy-plugin.js +0 -0
  169. package/lib/server/versions/__fixtures__/package.json +0 -3
  170. package/lib/server/versions/index.d.ts +0 -9
  171. package/lib/server/versions/index.js +0 -51
@@ -6,169 +6,102 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.loadPlugins = exports.sortConfig = void 0;
9
+ exports.loadPlugins = void 0;
10
10
  const tslib_1 = require("tslib");
11
11
  const utils_1 = require("@docusaurus/utils");
12
- const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
13
- const path_1 = (0, tslib_1.__importDefault)(require("path"));
14
- const init_1 = (0, tslib_1.__importDefault)(require("./init"));
15
- const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
16
- const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
12
+ const path_1 = tslib_1.__importDefault(require("path"));
13
+ const init_1 = require("./init");
14
+ const synthetic_1 = require("./synthetic");
15
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
17
16
  const translations_1 = require("../translations/translations");
18
- const applyRouteTrailingSlash_1 = (0, tslib_1.__importDefault)(require("./applyRouteTrailingSlash"));
19
- function sortConfig(routeConfigs, baseUrl = '/') {
20
- // Sort the route config. This ensures that route with nested
21
- // routes is always placed last.
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
- }
30
- if (a.routes && !b.routes) {
31
- return 1;
32
- }
33
- if (!a.routes && b.routes) {
34
- return -1;
35
- }
36
- // Higher priority get placed first.
37
- if (a.priority || b.priority) {
38
- const priorityA = a.priority || 0;
39
- const priorityB = b.priority || 0;
40
- const score = priorityB - priorityA;
41
- if (score !== 0) {
42
- return score;
43
- }
44
- }
45
- return a.path.localeCompare(b.path);
46
- });
47
- routeConfigs.forEach((routeConfig) => {
48
- var _a;
49
- (_a = routeConfig.routes) === null || _a === void 0 ? void 0 : _a.sort((a, b) => a.path.localeCompare(b.path));
50
- });
51
- }
52
- exports.sortConfig = sortConfig;
53
- async function loadPlugins({ pluginConfigs, context, }) {
17
+ const routeConfig_1 = require("./routeConfig");
18
+ /**
19
+ * Initializes the plugins, runs `loadContent`, `translateContent`,
20
+ * `contentLoaded`, and `translateThemeConfig`. Because `contentLoaded` is
21
+ * side-effect-ful (it generates temp files), so is this function. This function
22
+ * would also mutate `context.siteConfig.themeConfig` to translate it.
23
+ */
24
+ async function loadPlugins(context) {
54
25
  // 1. Plugin Lifecycle - Initialization/Constructor.
55
- const plugins = await (0, init_1.default)({
56
- pluginConfigs,
57
- context,
58
- });
26
+ const plugins = await (0, init_1.initPlugins)(context);
27
+ plugins.push((0, synthetic_1.createBootstrapPlugin)(context), (0, synthetic_1.createMDXFallbackPlugin)(context));
59
28
  // 2. Plugin Lifecycle - loadContent.
60
29
  // Currently plugins run lifecycle methods in parallel and are not
61
30
  // order-dependent. We could change this in future if there are plugins which
62
31
  // need to run in certain order or depend on others for data.
32
+ // This would also translate theme config and content upfront, given the
33
+ // translation files that the plugin declares.
63
34
  const loadedPlugins = await Promise.all(plugins.map(async (plugin) => {
64
- const content = plugin.loadContent ? await plugin.loadContent() : null;
65
- return { ...plugin, content };
66
- }));
67
- const contentLoadedTranslatedPlugins = await Promise.all(loadedPlugins.map(async (contentLoadedPlugin) => {
68
- var _a, _b;
69
- const translationFiles = (_b = (await ((_a = contentLoadedPlugin === null || contentLoadedPlugin === void 0 ? void 0 : contentLoadedPlugin.getTranslationFiles) === null || _a === void 0 ? void 0 : _a.call(contentLoadedPlugin, {
70
- content: contentLoadedPlugin.content,
71
- })))) !== null && _b !== void 0 ? _b : [];
72
- const localizedTranslationFiles = await Promise.all(translationFiles.map((translationFile) => (0, translations_1.localizePluginTranslationFile)({
35
+ const content = await plugin.loadContent?.();
36
+ const rawTranslationFiles = (await plugin?.getTranslationFiles?.({ content })) ?? [];
37
+ const translationFiles = await Promise.all(rawTranslationFiles.map((translationFile) => (0, translations_1.localizePluginTranslationFile)({
73
38
  locale: context.i18n.currentLocale,
74
39
  siteDir: context.siteDir,
75
40
  translationFile,
76
- plugin: contentLoadedPlugin,
41
+ plugin,
77
42
  })));
78
- return {
79
- ...contentLoadedPlugin,
80
- translationFiles: localizedTranslationFiles,
81
- };
43
+ const translatedContent = plugin.translateContent?.({ content, translationFiles }) ?? content;
44
+ const translatedThemeConfigSlice = plugin.translateThemeConfig?.({
45
+ themeConfig: context.siteConfig.themeConfig,
46
+ translationFiles,
47
+ });
48
+ // Side-effect to merge theme config translations. A plugin should only
49
+ // translate its own slice of theme config and should make no assumptions
50
+ // about other plugins' keys, so this is safe to run in parallel.
51
+ Object.assign(context.siteConfig.themeConfig, translatedThemeConfigSlice);
52
+ return { ...plugin, content: translatedContent };
82
53
  }));
83
54
  const allContent = lodash_1.default.chain(loadedPlugins)
84
55
  .groupBy((item) => item.name)
85
56
  .mapValues((nameItems) => lodash_1.default.chain(nameItems)
86
- .groupBy((item) => { var _a; return (_a = item.options.id) !== null && _a !== void 0 ? _a : utils_1.DEFAULT_PLUGIN_ID; })
57
+ .groupBy((item) => item.options.id)
87
58
  .mapValues((idItems) => idItems[0].content)
88
59
  .value())
89
60
  .value();
90
61
  // 3. Plugin Lifecycle - contentLoaded.
91
62
  const pluginsRouteConfigs = [];
92
63
  const globalData = {};
93
- await Promise.all(contentLoadedTranslatedPlugins.map(async ({ content, translationFiles, ...plugin }) => {
94
- var _a, _b, _c;
64
+ await Promise.all(loadedPlugins.map(async ({ content, ...plugin }) => {
95
65
  if (!plugin.contentLoaded) {
96
66
  return;
97
67
  }
98
- const pluginId = (_a = plugin.options.id) !== null && _a !== void 0 ? _a : utils_1.DEFAULT_PLUGIN_ID;
99
- // plugins data files are namespaced by pluginName/pluginId
100
- const dataDirRoot = path_1.default.join(context.generatedFilesDir, plugin.name);
101
- const dataDir = path_1.default.join(dataDirRoot, pluginId);
102
- const addRoute = (initialRouteConfig) => {
103
- // Trailing slash behavior is handled in a generic way for all plugins
104
- const finalRouteConfig = (0, applyRouteTrailingSlash_1.default)(initialRouteConfig, {
105
- trailingSlash: context.siteConfig.trailingSlash,
106
- baseUrl: context.siteConfig.baseUrl,
107
- });
108
- pluginsRouteConfigs.push(finalRouteConfig);
109
- };
110
- const createData = async (name, data) => {
111
- const modulePath = path_1.default.join(dataDir, name);
112
- await fs_extra_1.default.ensureDir(path_1.default.dirname(modulePath));
113
- await (0, utils_1.generate)(dataDir, name, data);
114
- return modulePath;
115
- };
116
- // the plugins global data are namespaced to avoid data conflicts:
117
- // - by plugin name
118
- // - by plugin id (allow using multiple instances of the same plugin)
119
- const setGlobalData = (data) => {
120
- var _a;
121
- globalData[plugin.name] = (_a = globalData[plugin.name]) !== null && _a !== void 0 ? _a : {};
122
- globalData[plugin.name][pluginId] = data;
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,
123
75
  };
76
+ await (0, utils_1.generate)('/', pluginRouteContextModulePath, JSON.stringify(pluginRouteContext, null, 2));
124
77
  const actions = {
125
- addRoute,
126
- createData,
127
- setGlobalData,
78
+ addRoute(initialRouteConfig) {
79
+ // Trailing slash behavior is handled generically for all plugins
80
+ const finalRouteConfig = (0, routeConfig_1.applyRouteTrailingSlash)(initialRouteConfig, context.siteConfig);
81
+ pluginsRouteConfigs.push({
82
+ ...finalRouteConfig,
83
+ context: {
84
+ ...(finalRouteConfig.context && { data: finalRouteConfig.context }),
85
+ plugin: pluginRouteContextModulePath,
86
+ },
87
+ });
88
+ },
89
+ async createData(name, data) {
90
+ const modulePath = path_1.default.join(dataDir, name);
91
+ await (0, utils_1.generate)(dataDir, name, data);
92
+ return modulePath;
93
+ },
94
+ setGlobalData(data) {
95
+ var _a;
96
+ globalData[_a = plugin.name] ?? (globalData[_a] = {});
97
+ globalData[plugin.name][pluginId] = data;
98
+ },
128
99
  };
129
- const translatedContent = (_c = (_b = plugin.translateContent) === null || _b === void 0 ? void 0 : _b.call(plugin, { content, translationFiles })) !== null && _c !== void 0 ? _c : content;
130
- await plugin.contentLoaded({
131
- content: translatedContent,
132
- actions,
133
- allContent,
134
- });
135
- }));
136
- // 4. Plugin Lifecycle - routesLoaded.
137
- // Currently plugins run lifecycle methods in parallel and are not
138
- // order-dependent. We could change this in future if there are plugins which
139
- // need to run in certain order or depend on others for data.
140
- await Promise.all(contentLoadedTranslatedPlugins.map(async (plugin) => {
141
- if (!plugin.routesLoaded) {
142
- return null;
143
- }
144
- // TODO remove this deprecated lifecycle soon
145
- // deprecated since alpha-60
146
- // TODO, 1 user reported usage of this lifecycle! https://github.com/facebook/docusaurus/issues/3918
147
- 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'}`;
148
- return plugin.routesLoaded(pluginsRouteConfigs);
100
+ await plugin.contentLoaded({ content, actions, allContent });
149
101
  }));
150
102
  // Sort the route config. This ensures that route with nested
151
103
  // routes are always placed last.
152
- sortConfig(pluginsRouteConfigs, context.siteConfig.baseUrl);
153
- // Apply each plugin one after the other to translate the theme config
154
- function translateThemeConfig(untranslatedThemeConfig) {
155
- return contentLoadedTranslatedPlugins.reduce((currentThemeConfig, plugin) => {
156
- var _a;
157
- const translatedThemeConfigSlice = (_a = plugin.translateThemeConfig) === null || _a === void 0 ? void 0 : _a.call(plugin, {
158
- themeConfig: currentThemeConfig,
159
- translationFiles: plugin.translationFiles,
160
- });
161
- return {
162
- ...currentThemeConfig,
163
- ...translatedThemeConfigSlice,
164
- };
165
- }, untranslatedThemeConfig);
166
- }
167
- return {
168
- plugins: loadedPlugins,
169
- pluginsRouteConfigs,
170
- globalData,
171
- themeConfigTranslated: translateThemeConfig(context.siteConfig.themeConfig),
172
- };
104
+ (0, routeConfig_1.sortConfig)(pluginsRouteConfigs, context.siteConfig.baseUrl);
105
+ return { plugins: loadedPlugins, pluginsRouteConfigs, globalData };
173
106
  }
174
107
  exports.loadPlugins = loadPlugins;
@@ -4,18 +4,9 @@
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 type { ImportedPluginModule, LoadContext, PluginModule, PluginConfig, PluginOptions, InitializedPlugin } from '@docusaurus/types';
9
- export declare type NormalizedPluginConfig = {
10
- plugin: PluginModule;
11
- options: PluginOptions;
12
- pluginModule?: {
13
- path: string;
14
- module: ImportedPluginModule;
15
- };
16
- };
17
- export declare function normalizePluginConfigs(pluginConfigs: PluginConfig[], pluginRequire: NodeRequire): Promise<NormalizedPluginConfig[]>;
18
- export default function initPlugins({ pluginConfigs, context, }: {
19
- pluginConfigs: PluginConfig[];
20
- context: LoadContext;
21
- }): Promise<InitializedPlugin[]>;
7
+ import type { LoadContext, InitializedPlugin } from '@docusaurus/types';
8
+ /**
9
+ * Runs the plugin constructors and returns their return values. It would load
10
+ * plugin configs from `plugins`, `themes`, and `presets`.
11
+ */
12
+ export declare function initPlugins(context: LoadContext): Promise<InitializedPlugin[]>;
@@ -6,97 +6,44 @@
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
+ exports.initPlugins = void 0;
10
10
  const tslib_1 = require("tslib");
11
11
  const module_1 = require("module");
12
- const import_fresh_1 = (0, tslib_1.__importDefault)(require("import-fresh"));
12
+ const path_1 = tslib_1.__importDefault(require("path"));
13
13
  const utils_1 = require("@docusaurus/utils");
14
- const versions_1 = require("../versions");
14
+ const siteMetadata_1 = require("../siteMetadata");
15
15
  const pluginIds_1 = require("./pluginIds");
16
16
  const utils_validation_1 = require("@docusaurus/utils-validation");
17
- async function normalizePluginConfig(pluginConfig, pluginRequire) {
18
- var _a, _b, _c, _d;
19
- // plugins: ['./plugin']
20
- if (typeof pluginConfig === 'string') {
21
- const pluginModuleImport = pluginConfig;
22
- const pluginPath = pluginRequire.resolve(pluginModuleImport);
23
- const pluginModule = (0, import_fresh_1.default)(pluginPath);
24
- return {
25
- plugin: (_a = pluginModule === null || pluginModule === void 0 ? void 0 : pluginModule.default) !== null && _a !== void 0 ? _a : pluginModule,
26
- options: {},
27
- pluginModule: {
28
- path: pluginModuleImport,
29
- module: pluginModule,
30
- },
31
- };
32
- }
33
- // plugins: [function plugin() { }]
34
- if (typeof pluginConfig === 'function') {
35
- return {
36
- plugin: pluginConfig,
37
- options: {},
38
- };
39
- }
40
- if (Array.isArray(pluginConfig)) {
41
- // plugins: [
42
- // ['./plugin',options],
43
- // ]
44
- if (typeof pluginConfig[0] === 'string') {
45
- const pluginModuleImport = pluginConfig[0];
46
- const pluginPath = pluginRequire.resolve(pluginModuleImport);
47
- const pluginModule = (0, import_fresh_1.default)(pluginPath);
48
- return {
49
- plugin: (_b = pluginModule === null || pluginModule === void 0 ? void 0 : pluginModule.default) !== null && _b !== void 0 ? _b : pluginModule,
50
- options: (_c = pluginConfig[1]) !== null && _c !== void 0 ? _c : {},
51
- pluginModule: {
52
- path: pluginModuleImport,
53
- module: pluginModule,
54
- },
55
- };
56
- }
57
- // plugins: [
58
- // [function plugin() { },options],
59
- // ]
60
- if (typeof pluginConfig[0] === 'function') {
61
- return {
62
- plugin: pluginConfig[0],
63
- options: (_d = pluginConfig[1]) !== null && _d !== void 0 ? _d : {},
64
- };
65
- }
66
- }
67
- throw new Error(`Unexpected: can't load plugin for following plugin config.\n${JSON.stringify(pluginConfig)}`);
68
- }
69
- async function normalizePluginConfigs(pluginConfigs, pluginRequire) {
70
- return Promise.all(pluginConfigs.map((pluginConfig) => normalizePluginConfig(pluginConfig, pluginRequire)));
71
- }
72
- exports.normalizePluginConfigs = normalizePluginConfigs;
17
+ const configs_1 = require("./configs");
73
18
  function getOptionValidationFunction(normalizedPluginConfig) {
74
- var _a, _b, _c, _d;
75
19
  if (normalizedPluginConfig.pluginModule) {
76
- // support both commonjs and ES modules
77
- 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);
20
+ // Support both CommonJS and ES modules
21
+ return (normalizedPluginConfig.pluginModule.module?.default?.validateOptions ??
22
+ normalizedPluginConfig.pluginModule.module?.validateOptions);
78
23
  }
79
24
  return normalizedPluginConfig.plugin.validateOptions;
80
25
  }
81
26
  function getThemeValidationFunction(normalizedPluginConfig) {
82
- var _a, _b;
83
27
  if (normalizedPluginConfig.pluginModule) {
84
- // support both commonjs and ES modules
85
- return ((_b = (_a = normalizedPluginConfig.pluginModule.module.default) === null || _a === void 0 ? void 0 : _a.validateThemeConfig) !== null && _b !== void 0 ? _b : normalizedPluginConfig.pluginModule.module.validateThemeConfig);
28
+ // Support both CommonJS and ES modules
29
+ return (normalizedPluginConfig.pluginModule.module.default?.validateThemeConfig ??
30
+ normalizedPluginConfig.pluginModule.module.validateThemeConfig);
86
31
  }
87
32
  return normalizedPluginConfig.plugin.validateThemeConfig;
88
33
  }
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.
34
+ /**
35
+ * Runs the plugin constructors and returns their return values. It would load
36
+ * plugin configs from `plugins`, `themes`, and `presets`.
37
+ */
38
+ async function initPlugins(context) {
39
+ // We need to resolve plugins from the perspective of the site config, as if
40
+ // we are using `require.resolve` on those module names.
92
41
  const pluginRequire = (0, module_1.createRequire)(context.siteConfigPath);
93
- const pluginConfigsNormalized = await normalizePluginConfigs(pluginConfigs, pluginRequire);
42
+ const pluginConfigs = await (0, configs_1.loadPluginConfigs)(context);
94
43
  async function doGetPluginVersion(normalizedPluginConfig) {
95
- var _a, _b;
96
- // get plugin version
97
- if ((_a = normalizedPluginConfig.pluginModule) === null || _a === void 0 ? void 0 : _a.path) {
98
- const pluginPath = pluginRequire.resolve((_b = normalizedPluginConfig.pluginModule) === null || _b === void 0 ? void 0 : _b.path);
99
- return (0, versions_1.getPluginVersion)(pluginPath, context.siteDir);
44
+ if (normalizedPluginConfig.pluginModule?.path) {
45
+ const pluginPath = pluginRequire.resolve(normalizedPluginConfig.pluginModule?.path);
46
+ return (0, siteMetadata_1.getPluginVersion)(pluginPath, context.siteDir);
100
47
  }
101
48
  return { type: 'local' };
102
49
  }
@@ -111,7 +58,6 @@ async function initPlugins({ pluginConfigs, context, }) {
111
58
  return context.siteConfig.themeConfig;
112
59
  }
113
60
  function doValidatePluginOptions(normalizedPluginConfig) {
114
- var _a;
115
61
  const validateOptions = getOptionValidationFunction(normalizedPluginConfig);
116
62
  if (validateOptions) {
117
63
  return validateOptions({
@@ -123,7 +69,7 @@ async function initPlugins({ pluginConfigs, context, }) {
123
69
  // as we don't go through the Joi schema that adds it
124
70
  return {
125
71
  ...normalizedPluginConfig.options,
126
- id: (_a = normalizedPluginConfig.options.id) !== null && _a !== void 0 ? _a : utils_1.DEFAULT_PLUGIN_ID,
72
+ id: normalizedPluginConfig.options.id ?? utils_1.DEFAULT_PLUGIN_ID,
127
73
  };
128
74
  }
129
75
  async function initializePlugin(normalizedPluginConfig) {
@@ -139,15 +85,11 @@ async function initPlugins({ pluginConfigs, context, }) {
139
85
  ...pluginInstance,
140
86
  options: pluginOptions,
141
87
  version: pluginVersion,
88
+ path: path_1.default.dirname(normalizedPluginConfig.entryPath),
142
89
  };
143
90
  }
144
- const plugins = (await Promise.all(pluginConfigsNormalized.map((pluginConfig) => {
145
- if (!pluginConfig) {
146
- return null;
147
- }
148
- return initializePlugin(pluginConfig);
149
- }))).filter((item) => Boolean(item));
91
+ const plugins = await Promise.all(pluginConfigs.map(initializePlugin));
150
92
  (0, pluginIds_1.ensureUniquePluginInstanceIds)(plugins);
151
93
  return plugins;
152
94
  }
153
- exports.default = initPlugins;
95
+ exports.initPlugins = initPlugins;
@@ -5,4 +5,8 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import type { InitializedPlugin } from '@docusaurus/types';
8
+ /**
9
+ * It is forbidden to have 2 plugins of the same name sharing the same ID.
10
+ * This is required to support multi-instance plugins without conflict.
11
+ */
8
12
  export declare function ensureUniquePluginInstanceIds(plugins: InitializedPlugin[]): void;
@@ -8,14 +8,16 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.ensureUniquePluginInstanceIds = void 0;
10
10
  const tslib_1 = require("tslib");
11
- const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
11
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
12
12
  const utils_1 = require("@docusaurus/utils");
13
- // It is forbidden to have 2 plugins of the same name sharing the same id
14
- // this is required to support multi-instance plugins without conflict
13
+ /**
14
+ * It is forbidden to have 2 plugins of the same name sharing the same ID.
15
+ * This is required to support multi-instance plugins without conflict.
16
+ */
15
17
  function ensureUniquePluginInstanceIds(plugins) {
16
18
  const pluginsByName = lodash_1.default.groupBy(plugins, (p) => p.name);
17
19
  Object.entries(pluginsByName).forEach(([pluginName, pluginInstances]) => {
18
- const pluginInstancesById = lodash_1.default.groupBy(pluginInstances, (p) => { var _a; return (_a = p.options.id) !== null && _a !== void 0 ? _a : utils_1.DEFAULT_PLUGIN_ID; });
20
+ const pluginInstancesById = lodash_1.default.groupBy(pluginInstances, (p) => p.options.id ?? utils_1.DEFAULT_PLUGIN_ID);
19
21
  Object.entries(pluginInstancesById).forEach(([pluginId, pluginInstancesWithId]) => {
20
22
  if (pluginInstancesWithId.length !== 1) {
21
23
  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
@@ -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 type { LoadContext, DocusaurusConfig } from '@docusaurus/types';
8
+ /**
9
+ * Calls preset functions, aggregates each of their return values, and returns
10
+ * the plugin and theme configs.
11
+ */
12
+ export declare function loadPresets(context: LoadContext): Promise<Pick<DocusaurusConfig, 'plugins' | 'themes'>>;
@@ -6,43 +6,44 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.loadPresets = void 0;
9
10
  const tslib_1 = require("tslib");
10
11
  const module_1 = require("module");
11
- const import_fresh_1 = (0, tslib_1.__importDefault)(require("import-fresh"));
12
- const moduleShorthand_1 = require("../moduleShorthand");
12
+ const import_fresh_1 = tslib_1.__importDefault(require("import-fresh"));
13
+ const moduleShorthand_1 = require("./moduleShorthand");
14
+ /**
15
+ * Calls preset functions, aggregates each of their return values, and returns
16
+ * the plugin and theme configs.
17
+ */
13
18
  async function loadPresets(context) {
14
- // We need to resolve presets from the perspective of the siteDir, since the
15
- // siteDir's package.json declares the dependency on these presets.
19
+ // We need to resolve plugins from the perspective of the site config, as if
20
+ // we are using `require.resolve` on those module names.
16
21
  const presetRequire = (0, module_1.createRequire)(context.siteConfigPath);
17
- const presets = context.siteConfig.presets || [];
18
- const unflatPlugins = [];
19
- const unflatThemes = [];
22
+ const { presets } = context.siteConfig;
23
+ const plugins = [];
24
+ const themes = [];
20
25
  presets.forEach((presetItem) => {
21
- var _a;
22
26
  let presetModuleImport;
23
27
  let presetOptions = {};
28
+ if (!presetItem) {
29
+ return;
30
+ }
24
31
  if (typeof presetItem === 'string') {
25
32
  presetModuleImport = presetItem;
26
33
  }
27
- else if (Array.isArray(presetItem)) {
28
- [presetModuleImport, presetOptions = {}] = presetItem;
29
- }
30
34
  else {
31
- throw new Error('Invalid presets format detected in config.');
35
+ [presetModuleImport, presetOptions] = presetItem;
32
36
  }
33
37
  const presetName = (0, moduleShorthand_1.resolveModuleName)(presetModuleImport, presetRequire, 'preset');
34
38
  const presetModule = (0, import_fresh_1.default)(presetRequire.resolve(presetName));
35
- const preset = ((_a = presetModule.default) !== null && _a !== void 0 ? _a : presetModule)(context, presetOptions);
39
+ const preset = (presetModule.default ?? presetModule)(context, presetOptions);
36
40
  if (preset.plugins) {
37
- unflatPlugins.push(preset.plugins);
41
+ plugins.push(...preset.plugins);
38
42
  }
39
43
  if (preset.themes) {
40
- unflatThemes.push(preset.themes);
44
+ themes.push(...preset.themes);
41
45
  }
42
46
  });
43
- return {
44
- plugins: unflatPlugins.flat().filter(Boolean),
45
- themes: unflatThemes.flat().filter(Boolean),
46
- };
47
+ return { plugins, themes };
47
48
  }
48
- exports.default = loadPresets;
49
+ exports.loadPresets = loadPresets;
@@ -6,4 +6,6 @@
6
6
  */
7
7
  import type { RouteConfig } from '@docusaurus/types';
8
8
  import { type ApplyTrailingSlashParams } from '@docusaurus/utils-common';
9
- export default function applyRouteTrailingSlash(route: RouteConfig, params: ApplyTrailingSlashParams): RouteConfig;
9
+ /** Recursively applies trailing slash config to all nested routes. */
10
+ export declare function applyRouteTrailingSlash(route: RouteConfig, params: ApplyTrailingSlashParams): RouteConfig;
11
+ export declare function sortConfig(routeConfigs: RouteConfig[], baseUrl?: string): void;
@@ -0,0 +1,54 @@
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.sortConfig = exports.applyRouteTrailingSlash = void 0;
10
+ const utils_common_1 = require("@docusaurus/utils-common");
11
+ /** Recursively applies trailing slash config to all nested routes. */
12
+ function applyRouteTrailingSlash(route, params) {
13
+ return {
14
+ ...route,
15
+ path: (0, utils_common_1.applyTrailingSlash)(route.path, params),
16
+ ...(route.routes && {
17
+ routes: route.routes.map((subroute) => applyRouteTrailingSlash(subroute, params)),
18
+ }),
19
+ };
20
+ }
21
+ exports.applyRouteTrailingSlash = applyRouteTrailingSlash;
22
+ function sortConfig(routeConfigs, baseUrl = '/') {
23
+ // Sort the route config. This ensures that route with nested
24
+ // routes is always placed last.
25
+ routeConfigs.sort((a, b) => {
26
+ // Root route should get placed last.
27
+ if (a.path === baseUrl && b.path !== baseUrl) {
28
+ return 1;
29
+ }
30
+ if (a.path !== baseUrl && b.path === baseUrl) {
31
+ return -1;
32
+ }
33
+ if (a.routes && !b.routes) {
34
+ return 1;
35
+ }
36
+ if (!a.routes && b.routes) {
37
+ return -1;
38
+ }
39
+ // Higher priority get placed first.
40
+ if (a.priority || b.priority) {
41
+ const priorityA = a.priority || 0;
42
+ const priorityB = b.priority || 0;
43
+ const score = priorityB - priorityA;
44
+ if (score !== 0) {
45
+ return score;
46
+ }
47
+ }
48
+ return a.path.localeCompare(b.path);
49
+ });
50
+ routeConfigs.forEach((routeConfig) => {
51
+ routeConfig.routes?.sort((a, b) => a.path.localeCompare(b.path));
52
+ });
53
+ }
54
+ exports.sortConfig = sortConfig;
@@ -0,0 +1,20 @@
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, LoadContext } from '@docusaurus/types';
8
+ /**
9
+ * Make a synthetic plugin to:
10
+ * - Inject site client modules
11
+ * - Inject scripts/stylesheets
12
+ */
13
+ export declare function createBootstrapPlugin({ siteDir, siteConfig, }: LoadContext): LoadedPlugin;
14
+ /**
15
+ * Configure Webpack fallback mdx loader for md/mdx files out of content-plugin
16
+ * folders. Adds a "fallback" mdx loader for mdx files that are not processed by
17
+ * content plugins. This allows to do things such as importing repo/README.md as
18
+ * a partial from another doc. Not ideal solution, but good enough for now
19
+ */
20
+ export declare function createMDXFallbackPlugin({ siteDir, siteConfig, }: LoadContext): LoadedPlugin;