@docusaurus/core 2.0.0-beta.ff31de0ff → 2.0.0-rc.1

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 (222) hide show
  1. package/README.md +1 -0
  2. package/bin/beforeCli.mjs +140 -0
  3. package/bin/docusaurus.mjs +221 -0
  4. package/lib/babel/preset.d.ts +8 -3
  5. package/lib/babel/preset.js +10 -8
  6. package/lib/client/App.d.ts +2 -3
  7. package/lib/client/App.js +31 -28
  8. package/lib/client/BaseUrlIssueBanner/index.d.ts +27 -0
  9. package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.js → BaseUrlIssueBanner/index.js} +25 -14
  10. package/lib/client/{baseUrlIssueBanner → BaseUrlIssueBanner}/styles.module.css +0 -0
  11. package/lib/client/ClientLifecyclesDispatcher.d.ts +16 -0
  12. package/lib/client/ClientLifecyclesDispatcher.js +39 -0
  13. package/lib/client/LinksCollector.d.ts +3 -3
  14. package/lib/client/LinksCollector.js +8 -13
  15. package/lib/client/PendingNavigation.d.ts +17 -3
  16. package/lib/client/PendingNavigation.js +47 -72
  17. package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.d.ts → SiteMetadataDefaults.d.ts} +1 -2
  18. package/lib/client/SiteMetadataDefaults.js +21 -0
  19. package/lib/{choosePort.d.ts → client/browserContext.d.ts} +5 -5
  20. package/lib/client/browserContext.js +22 -0
  21. package/lib/client/clientEntry.js +14 -9
  22. package/lib/client/docusaurus.d.ts +12 -6
  23. package/lib/client/docusaurus.js +30 -43
  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 +3 -5
  27. package/lib/client/exports/BrowserOnly.js +14 -7
  28. package/lib/client/exports/ComponentCreator.d.ts +6 -2
  29. package/lib/client/exports/ComponentCreator.js +75 -42
  30. package/lib/client/exports/ErrorBoundary.d.ts +18 -0
  31. package/lib/client/exports/ErrorBoundary.js +37 -0
  32. package/lib/client/exports/ExecutionEnvironment.js +7 -8
  33. package/lib/client/exports/Head.d.ts +2 -3
  34. package/lib/client/exports/Head.js +3 -4
  35. package/lib/client/exports/Interpolate.d.ts +2 -2
  36. package/lib/client/exports/Interpolate.js +20 -49
  37. package/lib/client/exports/Link.d.ts +4 -10
  38. package/lib/client/exports/Link.js +44 -38
  39. package/lib/client/exports/Translate.d.ts +1 -1
  40. package/lib/client/exports/Translate.js +14 -9
  41. package/lib/client/exports/constants.js +1 -11
  42. package/lib/client/exports/isInternalUrl.js +1 -1
  43. package/lib/client/exports/renderRoutes.d.ts +1 -2
  44. package/lib/client/exports/renderRoutes.js +1 -2
  45. package/lib/client/exports/router.d.ts +1 -1
  46. package/lib/client/exports/router.js +1 -1
  47. package/lib/client/exports/useBaseUrl.js +11 -14
  48. package/lib/client/exports/useDocusaurusContext.d.ts +2 -3
  49. package/lib/client/exports/useDocusaurusContext.js +3 -9
  50. package/lib/client/exports/useGlobalData.d.ts +4 -3
  51. package/lib/client/exports/useGlobalData.js +8 -15
  52. package/lib/{server/versions/__tests/index.test.d.ts → client/exports/useIsBrowser.d.ts} +1 -1
  53. package/lib/client/exports/useIsBrowser.js +11 -0
  54. package/lib/client/exports/useRouteContext.d.ts +8 -0
  55. package/lib/client/exports/useRouteContext.js +15 -0
  56. package/lib/client/flat.d.ts +12 -2
  57. package/lib/client/flat.js +19 -15
  58. package/lib/client/normalizeLocation.d.ts +2 -5
  59. package/lib/client/normalizeLocation.js +14 -10
  60. package/lib/client/prefetch.d.ts +1 -2
  61. package/lib/client/prefetch.js +12 -32
  62. package/lib/client/preload.d.ts +3 -4
  63. package/lib/client/preload.js +5 -12
  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 +109 -136
  68. package/lib/client/theme-fallback/Error/index.d.ts +10 -0
  69. package/lib/client/theme-fallback/Error/index.js +45 -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 +50 -116
  74. package/lib/client/{exports/context.js → theme-fallback/NotFound/index.d.ts} +2 -2
  75. package/lib/client/theme-fallback/NotFound/index.js +19 -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/{server/html-tags/htmlTags.d.ts → client/theme-fallback/SiteMetadata/index.d.ts} +2 -1
  79. package/lib/client/theme-fallback/SiteMetadata/index.js +10 -0
  80. package/lib/commands/build.d.ts +6 -2
  81. package/lib/commands/build.js +81 -64
  82. package/lib/commands/clear.d.ts +7 -1
  83. package/lib/commands/clear.js +34 -21
  84. package/lib/commands/deploy.d.ts +5 -2
  85. package/lib/commands/deploy.js +102 -81
  86. package/lib/commands/external.d.ts +2 -2
  87. package/lib/commands/external.js +9 -11
  88. package/lib/commands/serve.d.ts +8 -2
  89. package/lib/commands/serve.js +40 -20
  90. package/lib/commands/start.d.ts +9 -2
  91. package/lib/commands/start.js +111 -101
  92. package/lib/commands/swizzle/actions.d.ts +23 -0
  93. package/lib/commands/swizzle/actions.js +106 -0
  94. package/lib/commands/swizzle/common.d.ts +33 -0
  95. package/lib/commands/swizzle/common.js +56 -0
  96. package/lib/commands/swizzle/components.d.ts +29 -0
  97. package/lib/commands/swizzle/components.js +200 -0
  98. package/lib/commands/swizzle/config.d.ts +10 -0
  99. package/lib/commands/swizzle/config.js +84 -0
  100. package/lib/{server/client-modules/index.d.ts → commands/swizzle/context.d.ts} +2 -2
  101. package/lib/commands/swizzle/context.js +24 -0
  102. package/lib/commands/swizzle/index.d.ts +8 -0
  103. package/lib/commands/swizzle/index.js +119 -0
  104. package/lib/commands/swizzle/prompts.d.ts +12 -0
  105. package/lib/commands/swizzle/prompts.js +110 -0
  106. package/lib/{client/exports/context.d.ts → commands/swizzle/tables.d.ts} +3 -4
  107. package/lib/commands/swizzle/tables.js +113 -0
  108. package/lib/commands/swizzle/themes.d.ts +20 -0
  109. package/lib/commands/swizzle/themes.js +106 -0
  110. package/lib/commands/writeHeadingIds.d.ts +2 -6
  111. package/lib/commands/writeHeadingIds.js +22 -81
  112. package/lib/commands/writeTranslations.d.ts +4 -5
  113. package/lib/commands/writeTranslations.js +48 -28
  114. package/lib/index.d.ts +9 -9
  115. package/lib/index.js +14 -14
  116. package/lib/server/brokenLinks.d.ts +4 -17
  117. package/lib/server/brokenLinks.js +67 -56
  118. package/lib/server/clientModules.d.ts +12 -0
  119. package/lib/server/clientModules.js +20 -0
  120. package/lib/server/config.d.ts +5 -2
  121. package/lib/server/config.js +29 -6
  122. package/lib/server/configValidation.d.ts +5 -3
  123. package/lib/server/configValidation.js +105 -44
  124. package/lib/server/getHostPort.d.ts +14 -0
  125. package/lib/server/getHostPort.js +79 -0
  126. package/lib/server/htmlTags.d.ts +12 -0
  127. package/lib/server/htmlTags.js +62 -0
  128. package/lib/server/i18n.d.ts +3 -13
  129. package/lib/server/i18n.js +21 -55
  130. package/lib/server/index.d.ts +28 -6
  131. package/lib/server/index.js +86 -173
  132. package/lib/server/plugins/configs.d.ts +51 -0
  133. package/lib/server/plugins/configs.js +101 -0
  134. package/lib/server/plugins/index.d.ts +10 -10
  135. package/lib/server/plugins/index.js +76 -128
  136. package/lib/server/plugins/init.d.ts +6 -9
  137. package/lib/server/plugins/init.js +44 -109
  138. package/lib/server/plugins/moduleShorthand.d.ts +9 -0
  139. package/lib/server/plugins/moduleShorthand.js +46 -0
  140. package/lib/server/plugins/pluginIds.d.ts +6 -2
  141. package/lib/server/plugins/pluginIds.js +12 -7
  142. package/lib/server/plugins/presets.d.ts +12 -0
  143. package/lib/server/plugins/presets.js +49 -0
  144. package/lib/server/plugins/routeConfig.d.ts +11 -0
  145. package/lib/server/plugins/routeConfig.js +54 -0
  146. package/lib/server/plugins/synthetic.d.ts +20 -0
  147. package/lib/server/plugins/synthetic.js +111 -0
  148. package/lib/server/routes.d.ts +39 -7
  149. package/lib/server/routes.js +182 -95
  150. package/lib/server/siteMetadata.d.ts +12 -0
  151. package/lib/server/siteMetadata.js +81 -0
  152. package/lib/server/translations/translations.d.ts +16 -22
  153. package/lib/server/translations/translations.js +41 -73
  154. package/lib/server/translations/translationsExtractor.d.ts +10 -5
  155. package/lib/server/translations/translationsExtractor.js +158 -122
  156. package/lib/server/utils.d.ts +9 -3
  157. package/lib/server/utils.js +7 -9
  158. package/lib/webpack/aliases/index.d.ts +34 -0
  159. package/lib/webpack/aliases/index.js +106 -0
  160. package/lib/webpack/base.d.ts +3 -4
  161. package/lib/webpack/base.js +56 -58
  162. package/lib/webpack/client.d.ts +3 -3
  163. package/lib/webpack/client.js +12 -19
  164. package/lib/webpack/plugins/ChunkAssetPlugin.d.ts +13 -3
  165. package/lib/webpack/plugins/ChunkAssetPlugin.js +24 -17
  166. package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +6 -25
  167. package/lib/webpack/plugins/CleanWebpackPlugin.js +34 -18
  168. package/lib/webpack/plugins/LogPlugin.d.ts +1 -1
  169. package/lib/webpack/plugins/LogPlugin.js +4 -5
  170. package/lib/webpack/plugins/WaitPlugin.d.ts +3 -3
  171. package/lib/webpack/plugins/WaitPlugin.js +1 -1
  172. package/lib/webpack/server.d.ts +5 -5
  173. package/lib/webpack/server.js +23 -8
  174. package/lib/{client → webpack}/templates/index.html.template.ejs +0 -0
  175. package/lib/webpack/templates/ssr.html.template.d.ts +8 -0
  176. package/lib/{client → webpack}/templates/ssr.html.template.js +5 -4
  177. package/lib/webpack/utils.d.ts +16 -31
  178. package/lib/webpack/utils.js +63 -183
  179. package/package.json +83 -77
  180. package/bin/docusaurus.js +0 -326
  181. package/lib/.tsbuildinfo +0 -5682
  182. package/lib/choosePort.js +0 -105
  183. package/lib/client/.eslintrc.js +0 -29
  184. package/lib/client/.tsbuildinfo +0 -4127
  185. package/lib/client/client-lifecycles-dispatcher.d.ts +0 -12
  186. package/lib/client/client-lifecycles-dispatcher.js +0 -27
  187. package/lib/client/nprogress.css +0 -36
  188. package/lib/commands/buildRemoteBranchUrl.d.ts +0 -7
  189. package/lib/commands/buildRemoteBranchUrl.js +0 -27
  190. package/lib/commands/commandUtils.d.ts +0 -3
  191. package/lib/commands/commandUtils.js +0 -21
  192. package/lib/commands/swizzle.d.ts +0 -9
  193. package/lib/commands/swizzle.js +0 -245
  194. package/lib/constants.d.ts +0 -18
  195. package/lib/constants.js +0 -23
  196. package/lib/server/client-modules/index.js +0 -14
  197. package/lib/server/duplicateRoutes.d.ts +0 -10
  198. package/lib/server/duplicateRoutes.js +0 -38
  199. package/lib/server/html-tags/htmlTags.js +0 -39
  200. package/lib/server/html-tags/index.d.ts +0 -9
  201. package/lib/server/html-tags/index.js +0 -43
  202. package/lib/server/loadSetup.d.ts +0 -9
  203. package/lib/server/loadSetup.js +0 -25
  204. package/lib/server/presets/index.d.ts +0 -11
  205. package/lib/server/presets/index.js +0 -48
  206. package/lib/server/themes/alias.d.ts +0 -8
  207. package/lib/server/themes/alias.js +0 -39
  208. package/lib/server/themes/index.d.ts +0 -8
  209. package/lib/server/themes/index.js +0 -35
  210. package/lib/server/versions/__fixtures__/dummy-plugin.js +0 -0
  211. package/lib/server/versions/__fixtures__/package.json +0 -3
  212. package/lib/server/versions/__tests/index.test.js +0 -25
  213. package/lib/server/versions/index.d.ts +0 -10
  214. package/lib/server/versions/index.js +0 -50
  215. package/lib/webpack/react-dev-utils-webpack5/README.md +0 -11
  216. package/lib/webpack/react-dev-utils-webpack5/evalSourceMapMiddleware.js +0 -57
  217. package/lib/webpack/react-dev-utils-webpack5/formatWebpackMessages.js +0 -138
  218. package/lib/webpack/react-dev-utils-webpack5/webpackHotDevClient.js +0 -285
  219. package/lib/webpack/sharedModuleAliases.d.ts +0 -10
  220. package/lib/webpack/sharedModuleAliases.js +0 -18
  221. package/tsconfig.client.json +0 -13
  222. package/tsconfig.json +0 -13
@@ -0,0 +1,51 @@
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, PluginModule, PluginOptions } from '@docusaurus/types';
8
+ declare type ImportedPluginModule = PluginModule & {
9
+ default?: PluginModule;
10
+ };
11
+ export declare type NormalizedPluginConfig = {
12
+ /**
13
+ * The default export of the plugin module, or alternatively, what's provided
14
+ * in the config file as inline plugins. Note that if a file is like:
15
+ *
16
+ * ```ts
17
+ * export default plugin() {...}
18
+ * export validateOptions() {...}
19
+ * ```
20
+ *
21
+ * Then the static methods may not exist here. `pluginModule.module` will
22
+ * always take priority.
23
+ */
24
+ plugin: PluginModule;
25
+ /** Options as they are provided in the config, not validated yet. */
26
+ options: PluginOptions;
27
+ /** Only available when a string is provided in config. */
28
+ pluginModule?: {
29
+ /**
30
+ * Raw module name as provided in the config. Shorthands have been resolved,
31
+ * so at least it's directly `require.resolve`able.
32
+ */
33
+ path: string;
34
+ /** Whatever gets imported with `require`. */
35
+ module: ImportedPluginModule;
36
+ };
37
+ /**
38
+ * Different from `pluginModule.path`, this one is always an absolute path,
39
+ * used to resolve relative paths returned from lifecycles. If it's an inline
40
+ * plugin, it will be path to the config file.
41
+ */
42
+ entryPath: string;
43
+ };
44
+ /**
45
+ * Reads the site config's `presets`, `themes`, and `plugins`, imports them, and
46
+ * normalizes the return value. Plugin configs are ordered, mostly for theme
47
+ * alias shadowing. Site themes have the highest priority, and preset plugins
48
+ * are the lowest.
49
+ */
50
+ export declare function loadPluginConfigs(context: LoadContext): Promise<NormalizedPluginConfig[]>;
51
+ export {};
@@ -0,0 +1,101 @@
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.loadPluginConfigs = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const module_1 = require("module");
12
+ const import_fresh_1 = tslib_1.__importDefault(require("import-fresh"));
13
+ const presets_1 = require("./presets");
14
+ const moduleShorthand_1 = require("./moduleShorthand");
15
+ async function normalizePluginConfig(pluginConfig, configPath, pluginRequire) {
16
+ // plugins: ["./plugin"]
17
+ if (typeof pluginConfig === 'string') {
18
+ const pluginModuleImport = pluginConfig;
19
+ const pluginPath = pluginRequire.resolve(pluginModuleImport);
20
+ const pluginModule = (0, import_fresh_1.default)(pluginPath);
21
+ return {
22
+ plugin: pluginModule.default ?? pluginModule,
23
+ options: {},
24
+ pluginModule: {
25
+ path: pluginModuleImport,
26
+ module: pluginModule,
27
+ },
28
+ entryPath: pluginPath,
29
+ };
30
+ }
31
+ // plugins: [() => {...}]
32
+ if (typeof pluginConfig === 'function') {
33
+ return {
34
+ plugin: pluginConfig,
35
+ options: {},
36
+ entryPath: configPath,
37
+ };
38
+ }
39
+ // plugins: [
40
+ // ["./plugin",options],
41
+ // ]
42
+ if (typeof pluginConfig[0] === 'string') {
43
+ const pluginModuleImport = pluginConfig[0];
44
+ const pluginPath = pluginRequire.resolve(pluginModuleImport);
45
+ const pluginModule = (0, import_fresh_1.default)(pluginPath);
46
+ return {
47
+ plugin: pluginModule.default ?? pluginModule,
48
+ options: pluginConfig[1],
49
+ pluginModule: {
50
+ path: pluginModuleImport,
51
+ module: pluginModule,
52
+ },
53
+ entryPath: pluginPath,
54
+ };
55
+ }
56
+ // plugins: [
57
+ // [() => {...}, options],
58
+ // ]
59
+ return {
60
+ plugin: pluginConfig[0],
61
+ options: pluginConfig[1],
62
+ entryPath: configPath,
63
+ };
64
+ }
65
+ /**
66
+ * Reads the site config's `presets`, `themes`, and `plugins`, imports them, and
67
+ * normalizes the return value. Plugin configs are ordered, mostly for theme
68
+ * alias shadowing. Site themes have the highest priority, and preset plugins
69
+ * are the lowest.
70
+ */
71
+ async function loadPluginConfigs(context) {
72
+ const preset = await (0, presets_1.loadPresets)(context);
73
+ const { siteConfig, siteConfigPath } = context;
74
+ const pluginRequire = (0, module_1.createRequire)(siteConfigPath);
75
+ function normalizeShorthand(pluginConfig, pluginType) {
76
+ if (typeof pluginConfig === 'string') {
77
+ return (0, moduleShorthand_1.resolveModuleName)(pluginConfig, pluginRequire, pluginType);
78
+ }
79
+ else if (Array.isArray(pluginConfig) &&
80
+ typeof pluginConfig[0] === 'string') {
81
+ return [
82
+ (0, moduleShorthand_1.resolveModuleName)(pluginConfig[0], pluginRequire, pluginType),
83
+ pluginConfig[1] ?? {},
84
+ ];
85
+ }
86
+ return pluginConfig;
87
+ }
88
+ preset.plugins = preset.plugins.map((plugin) => normalizeShorthand(plugin, 'plugin'));
89
+ preset.themes = preset.themes.map((theme) => normalizeShorthand(theme, 'theme'));
90
+ const standalonePlugins = siteConfig.plugins.map((plugin) => normalizeShorthand(plugin, 'plugin'));
91
+ const standaloneThemes = siteConfig.themes.map((theme) => normalizeShorthand(theme, 'theme'));
92
+ const pluginConfigs = [
93
+ ...preset.plugins,
94
+ ...preset.themes,
95
+ // Site config should be the highest priority.
96
+ ...standalonePlugins,
97
+ ...standaloneThemes,
98
+ ].filter((x) => Boolean(x));
99
+ return Promise.all(pluginConfigs.map((pluginConfig) => normalizePluginConfig(pluginConfig, context.siteConfigPath, pluginRequire)));
100
+ }
101
+ exports.loadPluginConfigs = loadPluginConfigs;
@@ -4,15 +4,15 @@
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;
10
- export declare function loadPlugins({ pluginConfigs, context, }: {
11
- pluginConfigs: PluginConfig[];
12
- context: LoadContext;
13
- }): Promise<{
14
- plugins: InitPlugin[];
7
+ import type { LoadContext, RouteConfig, GlobalData, LoadedPlugin } from '@docusaurus/types';
8
+ /**
9
+ * Initializes the plugins, runs `loadContent`, `translateContent`,
10
+ * `contentLoaded`, and `translateThemeConfig`. Because `contentLoaded` is
11
+ * side-effect-ful (it generates temp files), so is this function. This function
12
+ * would also mutate `context.siteConfig.themeConfig` to translate it.
13
+ */
14
+ export declare function loadPlugins(context: LoadContext): Promise<{
15
+ plugins: LoadedPlugin[];
15
16
  pluginsRouteConfigs: RouteConfig[];
16
- globalData: unknown;
17
- themeConfigTranslated: ThemeConfig;
17
+ globalData: GlobalData;
18
18
  }>;
@@ -6,153 +6,101 @@
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
- const utils_1 = require("@docusaurus/utils");
12
- const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
13
11
  const path_1 = tslib_1.__importDefault(require("path"));
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");
12
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
13
+ const utils_1 = require("@docusaurus/utils");
14
+ const init_1 = require("./init");
15
+ const synthetic_1 = require("./synthetic");
18
16
  const translations_1 = require("../translations/translations");
19
- function sortConfig(routeConfigs) {
20
- // Sort the route config. This ensures that route with nested
21
- // routes is always placed last.
22
- routeConfigs.sort((a, b) => {
23
- if (a.routes && !b.routes) {
24
- return 1;
25
- }
26
- if (!a.routes && b.routes) {
27
- return -1;
28
- }
29
- // Higher priority get placed first.
30
- if (a.priority || b.priority) {
31
- const priorityA = a.priority || 0;
32
- const priorityB = b.priority || 0;
33
- const score = priorityB - priorityA;
34
- if (score !== 0) {
35
- return score;
36
- }
37
- }
38
- return a.path.localeCompare(b.path);
39
- });
40
- 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));
43
- });
44
- }
45
- exports.sortConfig = sortConfig;
46
- 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) {
47
25
  // 1. Plugin Lifecycle - Initialization/Constructor.
48
- const plugins = init_1.default({
49
- pluginConfigs,
50
- context,
51
- });
52
- const contentLoadedPlugins = await Promise.all(plugins.map(async (plugin) => {
53
- const content = plugin.loadContent ? await plugin.loadContent() : null;
54
- return { plugin, content };
55
- }));
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, {
59
- content: contentLoadedPlugin.content,
60
- })))) !== null && _c !== void 0 ? _c : [];
61
- const localizedTranslationFiles = await Promise.all(translationFiles.map((translationFile) => translations_1.localizePluginTranslationFile({
62
- locale: context.i18n.currentLocale,
63
- siteDir: context.siteDir,
26
+ const plugins = await (0, init_1.initPlugins)(context);
27
+ plugins.push((0, synthetic_1.createBootstrapPlugin)(context), (0, synthetic_1.createMDXFallbackPlugin)(context));
28
+ // 2. Plugin Lifecycle - loadContent.
29
+ // Currently plugins run lifecycle methods in parallel and are not
30
+ // order-dependent. We could change this in future if there are plugins which
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.
34
+ const loadedPlugins = await Promise.all(plugins.map(async (plugin) => {
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)({
38
+ localizationDir: context.localizationDir,
64
39
  translationFile,
65
- plugin: contentLoadedPlugin.plugin,
40
+ plugin,
66
41
  })));
67
- return {
68
- ...contentLoadedPlugin,
69
- translationFiles: localizedTranslationFiles,
70
- };
42
+ const translatedContent = plugin.translateContent?.({ content, translationFiles }) ?? content;
43
+ const translatedThemeConfigSlice = plugin.translateThemeConfig?.({
44
+ themeConfig: context.siteConfig.themeConfig,
45
+ translationFiles,
46
+ });
47
+ // Side-effect to merge theme config translations. A plugin should only
48
+ // translate its own slice of theme config and should make no assumptions
49
+ // about other plugins' keys, so this is safe to run in parallel.
50
+ Object.assign(context.siteConfig.themeConfig, translatedThemeConfigSlice);
51
+ return { ...plugin, content: translatedContent };
71
52
  }));
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
- })
53
+ const allContent = lodash_1.default.chain(loadedPlugins)
54
+ .groupBy((item) => item.name)
55
+ .mapValues((nameItems) => lodash_1.default.chain(nameItems)
56
+ .groupBy((item) => item.options.id)
57
+ .mapValues((idItems) => idItems[0].content)
58
+ .value())
80
59
  .value();
81
60
  // 3. Plugin Lifecycle - contentLoaded.
82
61
  const pluginsRouteConfigs = [];
83
62
  const globalData = {};
84
- await Promise.all(contentLoadedTranslatedPlugins.map(async ({ plugin, content, translationFiles }) => {
85
- var _a, _b, _c;
63
+ await Promise.all(loadedPlugins.map(async ({ content, ...plugin }) => {
86
64
  if (!plugin.contentLoaded) {
87
65
  return;
88
66
  }
89
- const pluginId = (_a = plugin.options.id) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_PLUGIN_ID;
90
- // plugins data files are namespaced by pluginName/pluginId
91
- const dataDirRoot = path_1.default.join(context.generatedFilesDir, plugin.name);
92
- const dataDir = path_1.default.join(dataDirRoot, pluginId);
93
- const addRoute = (config) => pluginsRouteConfigs.push(config);
94
- const createData = async (name, data) => {
95
- const modulePath = path_1.default.join(dataDir, name);
96
- await fs_extra_1.default.ensureDir(path_1.default.dirname(modulePath));
97
- await utils_1.generate(dataDir, name, data);
98
- return modulePath;
99
- };
100
- // the plugins global data are namespaced to avoid data conflicts:
101
- // - by plugin name
102
- // - by plugin id (allow using multiple instances of the same plugin)
103
- const setGlobalData = (data) => {
104
- var _a;
105
- globalData[plugin.name] = (_a = globalData[plugin.name]) !== null && _a !== void 0 ? _a : {};
106
- globalData[plugin.name][pluginId] = data;
67
+ const pluginId = plugin.options.id;
68
+ // Plugins data files are namespaced by pluginName/pluginId
69
+ const dataDir = path_1.default.join(context.generatedFilesDir, plugin.name, pluginId);
70
+ const pluginRouteContextModulePath = path_1.default.join(dataDir, `${(0, utils_1.docuHash)('pluginRouteContextModule')}.json`);
71
+ const pluginRouteContext = {
72
+ name: plugin.name,
73
+ id: pluginId,
107
74
  };
75
+ await (0, utils_1.generate)('/', pluginRouteContextModulePath, JSON.stringify(pluginRouteContext, null, 2));
108
76
  const actions = {
109
- addRoute,
110
- createData,
111
- setGlobalData,
77
+ addRoute(initialRouteConfig) {
78
+ // Trailing slash behavior is handled generically for all plugins
79
+ const finalRouteConfig = (0, routeConfig_1.applyRouteTrailingSlash)(initialRouteConfig, context.siteConfig);
80
+ pluginsRouteConfigs.push({
81
+ ...finalRouteConfig,
82
+ context: {
83
+ ...(finalRouteConfig.context && { data: finalRouteConfig.context }),
84
+ plugin: pluginRouteContextModulePath,
85
+ },
86
+ });
87
+ },
88
+ async createData(name, data) {
89
+ const modulePath = path_1.default.join(dataDir, name);
90
+ await (0, utils_1.generate)(dataDir, name, data);
91
+ return modulePath;
92
+ },
93
+ setGlobalData(data) {
94
+ var _a;
95
+ globalData[_a = plugin.name] ?? (globalData[_a] = {});
96
+ globalData[plugin.name][pluginId] = data;
97
+ },
112
98
  };
113
- const translatedContent = (_c = (_b = plugin.translateContent) === null || _b === void 0 ? void 0 : _b.call(plugin, { content, translationFiles })) !== null && _c !== void 0 ? _c : content;
114
- await plugin.contentLoaded({
115
- content: translatedContent,
116
- actions,
117
- allContent,
118
- });
119
- }));
120
- // 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 }) => {
125
- if (!plugin.routesLoaded) {
126
- return null;
127
- }
128
- // TODO remove this deprecated lifecycle soon
129
- // deprecated since alpha-60
130
- // 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);
99
+ await plugin.contentLoaded({ content, actions, allContent });
133
100
  }));
134
101
  // Sort the route config. This ensures that route with nested
135
102
  // routes are always placed last.
136
- sortConfig(pluginsRouteConfigs);
137
- // Apply each plugin one after the other to translate the theme config
138
- 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, {
142
- themeConfig: currentThemeConfig,
143
- translationFiles,
144
- });
145
- return {
146
- ...currentThemeConfig,
147
- ...translatedThemeConfigSlice,
148
- };
149
- }, untranslatedThemeConfig);
150
- }
151
- return {
152
- plugins,
153
- pluginsRouteConfigs,
154
- globalData,
155
- themeConfigTranslated: translateThemeConfig(context.siteConfig.themeConfig),
156
- };
103
+ (0, routeConfig_1.sortConfig)(pluginsRouteConfigs, context.siteConfig.baseUrl);
104
+ return { plugins: loadedPlugins, pluginsRouteConfigs, globalData };
157
105
  }
158
106
  exports.loadPlugins = loadPlugins;
@@ -4,12 +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
- import { DocusaurusPluginVersionInformation, LoadContext, Plugin, PluginConfig, PluginOptions } from '@docusaurus/types';
8
- export declare type InitPlugin = Plugin<unknown> & {
9
- readonly options: PluginOptions;
10
- readonly version: DocusaurusPluginVersionInformation;
11
- };
12
- export default function initPlugins({ pluginConfigs, context, }: {
13
- pluginConfigs: PluginConfig[];
14
- context: LoadContext;
15
- }): InitPlugin[];
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,102 +6,46 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.initPlugins = void 0;
9
10
  const tslib_1 = require("tslib");
10
- const module_1 = tslib_1.__importDefault(require("module"));
11
- const import_fresh_1 = tslib_1.__importDefault(require("import-fresh"));
12
- const constants_1 = require("../../constants");
13
- const versions_1 = require("../versions");
14
- const pluginIds_1 = require("./pluginIds");
11
+ const module_1 = require("module");
12
+ const path_1 = tslib_1.__importDefault(require("path"));
13
+ const utils_1 = require("@docusaurus/utils");
15
14
  const utils_validation_1 = require("@docusaurus/utils-validation");
16
- function normalizePluginConfig(pluginConfig, pluginRequire) {
17
- var _a, _b, _c, _d;
18
- // plugins: ['./plugin']
19
- if (typeof pluginConfig === 'string') {
20
- const pluginModuleImport = pluginConfig;
21
- const pluginPath = pluginRequire.resolve(pluginModuleImport);
22
- const pluginModule = import_fresh_1.default(pluginPath);
23
- return {
24
- plugin: (_a = pluginModule === null || pluginModule === void 0 ? void 0 : pluginModule.default) !== null && _a !== void 0 ? _a : pluginModule,
25
- options: {},
26
- pluginModule: {
27
- path: pluginModuleImport,
28
- module: pluginModule,
29
- },
30
- };
31
- }
32
- // plugins: [function plugin() { }]
33
- if (typeof pluginConfig === 'function') {
34
- return {
35
- plugin: pluginConfig,
36
- options: {},
37
- };
38
- }
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
- }
65
- }
66
- throw new Error(`Unexpected: cant load plugin for plugin config = ${JSON.stringify(pluginConfig)}`);
67
- }
15
+ const siteMetadata_1 = require("../siteMetadata");
16
+ const pluginIds_1 = require("./pluginIds");
17
+ const configs_1 = require("./configs");
68
18
  function getOptionValidationFunction(normalizedPluginConfig) {
69
- var _a, _b, _c, _d;
70
19
  if (normalizedPluginConfig.pluginModule) {
71
- // 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;
20
+ // Support both CommonJS and ES modules
21
+ return (normalizedPluginConfig.pluginModule.module.default?.validateOptions ??
22
+ normalizedPluginConfig.pluginModule.module.validateOptions);
76
23
  }
24
+ return normalizedPluginConfig.plugin.validateOptions;
77
25
  }
78
26
  function getThemeValidationFunction(normalizedPluginConfig) {
79
- var _a, _b;
80
27
  if (normalizedPluginConfig.pluginModule) {
81
- // 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;
28
+ // Support both CommonJS and ES modules
29
+ return (normalizedPluginConfig.pluginModule.module.default?.validateThemeConfig ??
30
+ normalizedPluginConfig.pluginModule.module.validateThemeConfig);
86
31
  }
32
+ return normalizedPluginConfig.plugin.validateThemeConfig;
87
33
  }
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;
97
- // 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' };
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.
41
+ const pluginRequire = (0, module_1.createRequire)(context.siteConfigPath);
42
+ const pluginConfigs = await (0, configs_1.loadPluginConfigs)(context);
43
+ async function doGetPluginVersion(normalizedPluginConfig) {
44
+ if (normalizedPluginConfig.pluginModule?.path) {
45
+ const pluginPath = pluginRequire.resolve(normalizedPluginConfig.pluginModule.path);
46
+ return (0, siteMetadata_1.getPluginVersion)(pluginPath, context.siteDir);
104
47
  }
48
+ return { type: 'local' };
105
49
  }
106
50
  function doValidateThemeConfig(normalizedPluginConfig) {
107
51
  const validateThemeConfig = getThemeValidationFunction(normalizedPluginConfig);
@@ -111,12 +55,9 @@ function initPlugins({ pluginConfigs, context, }) {
111
55
  themeConfig: context.siteConfig.themeConfig,
112
56
  });
113
57
  }
114
- else {
115
- return context.siteConfig.themeConfig;
116
- }
58
+ return context.siteConfig.themeConfig;
117
59
  }
118
60
  function doValidatePluginOptions(normalizedPluginConfig) {
119
- var _a;
120
61
  const validateOptions = getOptionValidationFunction(normalizedPluginConfig);
121
62
  if (validateOptions) {
122
63
  return validateOptions({
@@ -124,37 +65,31 @@ function initPlugins({ pluginConfigs, context, }) {
124
65
  options: normalizedPluginConfig.options,
125
66
  });
126
67
  }
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
- }
68
+ // Important to ensure all plugins have an id
69
+ // as we don't go through the Joi schema that adds it
70
+ return {
71
+ ...normalizedPluginConfig.options,
72
+ id: normalizedPluginConfig.options.id ?? utils_1.DEFAULT_PLUGIN_ID,
73
+ };
135
74
  }
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);
75
+ async function initializePlugin(normalizedPluginConfig) {
76
+ const pluginVersion = await doGetPluginVersion(normalizedPluginConfig);
143
77
  const pluginOptions = doValidatePluginOptions(normalizedPluginConfig);
144
78
  // Side-effect: merge the normalized theme config in the original one
145
79
  context.siteConfig.themeConfig = {
146
80
  ...context.siteConfig.themeConfig,
147
81
  ...doValidateThemeConfig(normalizedPluginConfig),
148
82
  };
149
- const pluginInstance = normalizedPluginConfig.plugin(context, pluginOptions);
83
+ const pluginInstance = await normalizedPluginConfig.plugin(context, pluginOptions);
150
84
  return {
151
85
  ...pluginInstance,
152
86
  options: pluginOptions,
153
87
  version: pluginVersion,
88
+ path: path_1.default.dirname(normalizedPluginConfig.entryPath),
154
89
  };
155
- })
156
- .filter((item) => Boolean(item));
157
- pluginIds_1.ensureUniquePluginInstanceIds(plugins);
90
+ }
91
+ const plugins = await Promise.all(pluginConfigs.map(initializePlugin));
92
+ (0, pluginIds_1.ensureUniquePluginInstanceIds)(plugins);
158
93
  return plugins;
159
94
  }
160
- exports.default = initPlugins;
95
+ exports.initPlugins = initPlugins;
@@ -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
+ /// <reference types="node" />
8
+ export declare function getNamePatterns(moduleName: string, moduleType: 'preset' | 'theme' | 'plugin'): string[];
9
+ export declare function resolveModuleName(moduleName: string, moduleRequire: NodeRequire, moduleType: 'preset' | 'theme' | 'plugin'): string;