@docusaurus/core 2.0.0-beta.2 → 2.0.0-beta.22

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 (223) 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 -8
  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/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 -49
  36. package/lib/client/exports/Link.d.ts +4 -10
  37. package/lib/client/exports/Link.js +40 -40
  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 +11 -14
  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 +6 -13
  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/client/exports/useRouteContext.d.ts +8 -0
  54. package/lib/client/exports/useRouteContext.js +15 -0
  55. package/lib/client/flat.d.ts +12 -2
  56. package/lib/client/flat.js +19 -15
  57. package/lib/client/normalizeLocation.d.ts +2 -5
  58. package/lib/client/normalizeLocation.js +14 -10
  59. package/lib/client/prefetch.d.ts +1 -2
  60. package/lib/client/prefetch.js +12 -32
  61. package/lib/client/preload.d.ts +3 -4
  62. package/lib/client/preload.js +5 -12
  63. package/lib/client/routeContext.d.ts +13 -0
  64. package/lib/client/routeContext.js +31 -0
  65. package/lib/client/serverEntry.d.ts +10 -0
  66. package/lib/client/serverEntry.js +108 -146
  67. package/lib/client/theme-fallback/Error/index.d.ts +10 -0
  68. package/lib/client/theme-fallback/Error/index.js +45 -0
  69. package/lib/client/theme-fallback/Layout/index.d.ts +10 -0
  70. package/lib/client/theme-fallback/Layout/index.js +2 -26
  71. package/lib/client/theme-fallback/Loading/index.d.ts +9 -0
  72. package/lib/client/theme-fallback/Loading/index.js +50 -116
  73. package/lib/client/{exports/context.js → theme-fallback/NotFound/index.d.ts} +2 -2
  74. package/lib/client/theme-fallback/NotFound/index.js +19 -18
  75. package/lib/client/theme-fallback/Root/index.d.ts +10 -0
  76. package/lib/client/theme-fallback/Root/index.js +2 -6
  77. package/lib/{server/html-tags/htmlTags.d.ts → client/theme-fallback/SiteMetadata/index.d.ts} +2 -1
  78. package/lib/client/theme-fallback/SiteMetadata/index.js +10 -0
  79. package/lib/commands/build.d.ts +6 -2
  80. package/lib/commands/build.js +81 -64
  81. package/lib/commands/clear.d.ts +7 -1
  82. package/lib/commands/clear.js +34 -21
  83. package/lib/commands/deploy.d.ts +5 -2
  84. package/lib/commands/deploy.js +104 -81
  85. package/lib/commands/external.d.ts +2 -2
  86. package/lib/commands/external.js +9 -11
  87. package/lib/commands/serve.d.ts +8 -2
  88. package/lib/commands/serve.js +29 -28
  89. package/lib/commands/start.d.ts +9 -2
  90. package/lib/commands/start.js +109 -101
  91. package/lib/commands/swizzle/actions.d.ts +23 -0
  92. package/lib/commands/swizzle/actions.js +101 -0
  93. package/lib/commands/swizzle/common.d.ts +33 -0
  94. package/lib/commands/swizzle/common.js +56 -0
  95. package/lib/commands/swizzle/components.d.ts +29 -0
  96. package/lib/commands/swizzle/components.js +200 -0
  97. package/lib/commands/swizzle/config.d.ts +10 -0
  98. package/lib/commands/swizzle/config.js +84 -0
  99. package/lib/{server/client-modules/index.d.ts → commands/swizzle/context.d.ts} +2 -2
  100. package/lib/commands/swizzle/context.js +24 -0
  101. package/lib/commands/swizzle/index.d.ts +8 -0
  102. package/lib/commands/swizzle/index.js +118 -0
  103. package/lib/commands/swizzle/prompts.d.ts +12 -0
  104. package/lib/commands/swizzle/prompts.js +110 -0
  105. package/lib/{client/exports/context.d.ts → commands/swizzle/tables.d.ts} +3 -4
  106. package/lib/commands/swizzle/tables.js +113 -0
  107. package/lib/commands/swizzle/themes.d.ts +20 -0
  108. package/lib/commands/swizzle/themes.js +106 -0
  109. package/lib/commands/writeHeadingIds.d.ts +2 -6
  110. package/lib/commands/writeHeadingIds.js +22 -81
  111. package/lib/commands/writeTranslations.d.ts +4 -5
  112. package/lib/commands/writeTranslations.js +46 -25
  113. package/lib/index.d.ts +9 -9
  114. package/lib/index.js +14 -14
  115. package/lib/server/brokenLinks.d.ts +4 -17
  116. package/lib/server/brokenLinks.js +67 -56
  117. package/lib/server/clientModules.d.ts +12 -0
  118. package/lib/server/clientModules.js +20 -0
  119. package/lib/server/config.d.ts +5 -2
  120. package/lib/server/config.js +29 -6
  121. package/lib/server/configValidation.d.ts +4 -4
  122. package/lib/server/configValidation.js +86 -41
  123. package/lib/server/getHostPort.d.ts +14 -0
  124. package/lib/server/getHostPort.js +79 -0
  125. package/lib/server/htmlTags.d.ts +12 -0
  126. package/lib/server/htmlTags.js +62 -0
  127. package/lib/server/i18n.d.ts +3 -13
  128. package/lib/server/i18n.js +21 -55
  129. package/lib/server/index.d.ts +28 -13
  130. package/lib/server/index.js +83 -165
  131. package/lib/server/plugins/configs.d.ts +51 -0
  132. package/lib/server/plugins/configs.js +101 -0
  133. package/lib/server/plugins/index.d.ts +9 -8
  134. package/lib/server/plugins/index.js +73 -134
  135. package/lib/server/plugins/init.d.ts +6 -5
  136. package/lib/server/plugins/init.js +44 -109
  137. package/lib/server/plugins/moduleShorthand.d.ts +9 -0
  138. package/lib/server/plugins/moduleShorthand.js +46 -0
  139. package/lib/server/plugins/pluginIds.d.ts +5 -1
  140. package/lib/server/plugins/pluginIds.js +12 -7
  141. package/lib/server/plugins/presets.d.ts +12 -0
  142. package/lib/server/plugins/presets.js +49 -0
  143. package/lib/server/plugins/routeConfig.d.ts +11 -0
  144. package/lib/server/plugins/routeConfig.js +54 -0
  145. package/lib/server/plugins/synthetic.d.ts +20 -0
  146. package/lib/server/plugins/synthetic.js +111 -0
  147. package/lib/server/routes.d.ts +39 -7
  148. package/lib/server/routes.js +182 -95
  149. package/lib/server/siteMetadata.d.ts +12 -0
  150. package/lib/server/siteMetadata.js +81 -0
  151. package/lib/server/translations/translations.d.ts +14 -19
  152. package/lib/server/translations/translations.js +40 -72
  153. package/lib/server/translations/translationsExtractor.d.ts +10 -4
  154. package/lib/server/translations/translationsExtractor.js +158 -122
  155. package/lib/server/utils.d.ts +9 -3
  156. package/lib/server/utils.js +7 -9
  157. package/lib/webpack/aliases/index.d.ts +34 -0
  158. package/lib/webpack/aliases/index.js +106 -0
  159. package/lib/webpack/base.d.ts +3 -4
  160. package/lib/webpack/base.js +45 -57
  161. package/lib/webpack/client.d.ts +3 -3
  162. package/lib/webpack/client.js +12 -19
  163. package/lib/webpack/plugins/ChunkAssetPlugin.d.ts +13 -3
  164. package/lib/webpack/plugins/ChunkAssetPlugin.js +24 -17
  165. package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +6 -25
  166. package/lib/webpack/plugins/CleanWebpackPlugin.js +33 -17
  167. package/lib/webpack/plugins/LogPlugin.d.ts +1 -1
  168. package/lib/webpack/plugins/LogPlugin.js +4 -5
  169. package/lib/webpack/plugins/WaitPlugin.d.ts +3 -3
  170. package/lib/webpack/plugins/WaitPlugin.js +1 -1
  171. package/lib/webpack/server.d.ts +5 -5
  172. package/lib/webpack/server.js +22 -9
  173. package/lib/{client → webpack}/templates/index.html.template.ejs +0 -0
  174. package/lib/webpack/templates/ssr.html.template.d.ts +8 -0
  175. package/lib/{client → webpack}/templates/ssr.html.template.js +3 -2
  176. package/lib/webpack/utils.d.ts +15 -31
  177. package/lib/webpack/utils.js +61 -182
  178. package/package.json +81 -77
  179. package/bin/docusaurus.js +0 -325
  180. package/lib/.tsbuildinfo +0 -1
  181. package/lib/choosePort.js +0 -105
  182. package/lib/client/.eslintrc.js +0 -29
  183. package/lib/client/.tsbuildinfo +0 -1
  184. package/lib/client/client-lifecycles-dispatcher.d.ts +0 -12
  185. package/lib/client/client-lifecycles-dispatcher.js +0 -27
  186. package/lib/client/nprogress.css +0 -36
  187. package/lib/commands/buildRemoteBranchUrl.d.ts +0 -7
  188. package/lib/commands/buildRemoteBranchUrl.js +0 -27
  189. package/lib/commands/commandUtils.d.ts +0 -3
  190. package/lib/commands/commandUtils.js +0 -21
  191. package/lib/commands/swizzle.d.ts +0 -9
  192. package/lib/commands/swizzle.js +0 -245
  193. package/lib/constants.d.ts +0 -18
  194. package/lib/constants.js +0 -23
  195. package/lib/server/client-modules/index.js +0 -14
  196. package/lib/server/duplicateRoutes.d.ts +0 -10
  197. package/lib/server/duplicateRoutes.js +0 -38
  198. package/lib/server/html-tags/htmlTags.js +0 -40
  199. package/lib/server/html-tags/index.d.ts +0 -9
  200. package/lib/server/html-tags/index.js +0 -43
  201. package/lib/server/loadSetup.d.ts +0 -9
  202. package/lib/server/loadSetup.js +0 -25
  203. package/lib/server/plugins/applyRouteTrailingSlash.d.ts +0 -8
  204. package/lib/server/plugins/applyRouteTrailingSlash.js +0 -19
  205. package/lib/server/presets/index.d.ts +0 -11
  206. package/lib/server/presets/index.js +0 -48
  207. package/lib/server/themes/alias.d.ts +0 -8
  208. package/lib/server/themes/alias.js +0 -40
  209. package/lib/server/themes/index.d.ts +0 -12
  210. package/lib/server/themes/index.js +0 -47
  211. package/lib/server/versions/__fixtures__/dummy-plugin.js +0 -0
  212. package/lib/server/versions/__fixtures__/package.json +0 -3
  213. package/lib/server/versions/__tests/index.test.js +0 -25
  214. package/lib/server/versions/index.d.ts +0 -10
  215. package/lib/server/versions/index.js +0 -50
  216. package/lib/webpack/react-dev-utils-webpack5/README.md +0 -11
  217. package/lib/webpack/react-dev-utils-webpack5/evalSourceMapMiddleware.js +0 -57
  218. package/lib/webpack/react-dev-utils-webpack5/formatWebpackMessages.js +0 -138
  219. package/lib/webpack/react-dev-utils-webpack5/webpackHotDevClient.js +0 -285
  220. package/lib/webpack/sharedModuleAliases.d.ts +0 -10
  221. package/lib/webpack/sharedModuleAliases.js +0 -18
  222. package/tsconfig.client.json +0 -13
  223. package/tsconfig.json +0 -13
@@ -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,14 +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, LoadedPlugin } from '@docusaurus/types';
8
- export declare function sortConfig(routeConfigs: RouteConfig[]): void;
9
- export declare function loadPlugins({ pluginConfigs, context, }: {
10
- pluginConfigs: PluginConfig[];
11
- context: LoadContext;
12
- }): Promise<{
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<{
13
15
  plugins: LoadedPlugin[];
14
16
  pluginsRouteConfigs: RouteConfig[];
15
- globalData: unknown;
16
- themeConfigTranslated: ThemeConfig;
17
+ globalData: GlobalData;
17
18
  }>;
@@ -6,162 +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
- const applyRouteTrailingSlash_1 = tslib_1.__importDefault(require("./applyRouteTrailingSlash"));
20
- function sortConfig(routeConfigs) {
21
- // Sort the route config. This ensures that route with nested
22
- // routes is always placed last.
23
- routeConfigs.sort((a, b) => {
24
- if (a.routes && !b.routes) {
25
- return 1;
26
- }
27
- if (!a.routes && b.routes) {
28
- return -1;
29
- }
30
- // Higher priority get placed first.
31
- if (a.priority || b.priority) {
32
- const priorityA = a.priority || 0;
33
- const priorityB = b.priority || 0;
34
- const score = priorityB - priorityA;
35
- if (score !== 0) {
36
- return score;
37
- }
38
- }
39
- return a.path.localeCompare(b.path);
40
- });
41
- routeConfigs.forEach((routeConfig) => {
42
- var _a;
43
- (_a = routeConfig.routes) === null || _a === void 0 ? void 0 : _a.sort((a, b) => a.path.localeCompare(b.path));
44
- });
45
- }
46
- exports.sortConfig = sortConfig;
47
- 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) {
48
25
  // 1. Plugin Lifecycle - Initialization/Constructor.
49
- const plugins = init_1.default({
50
- pluginConfigs,
51
- context,
52
- });
26
+ const plugins = await (0, init_1.initPlugins)(context);
27
+ plugins.push((0, synthetic_1.createBootstrapPlugin)(context), (0, synthetic_1.createMDXFallbackPlugin)(context));
53
28
  // 2. Plugin Lifecycle - loadContent.
54
- // Currently plugins run lifecycle methods in parallel and are not order-dependent.
55
- // We could change this in future if there are plugins which need to
56
- // run in certain order or depend on others for data.
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.
57
34
  const loadedPlugins = await Promise.all(plugins.map(async (plugin) => {
58
- const content = plugin.loadContent ? await plugin.loadContent() : null;
59
- return { ...plugin, content };
60
- }));
61
- const contentLoadedTranslatedPlugins = await Promise.all(loadedPlugins.map(async (contentLoadedPlugin) => {
62
- var _a, _b;
63
- const translationFiles = (_b = (await ((_a = contentLoadedPlugin === null || contentLoadedPlugin === void 0 ? void 0 : contentLoadedPlugin.getTranslationFiles) === null || _a === void 0 ? void 0 : _a.call(contentLoadedPlugin, {
64
- content: contentLoadedPlugin.content,
65
- })))) !== null && _b !== void 0 ? _b : [];
66
- const localizedTranslationFiles = await Promise.all(translationFiles.map((translationFile) => translations_1.localizePluginTranslationFile({
67
- locale: context.i18n.currentLocale,
68
- siteDir: context.siteDir,
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,
69
39
  translationFile,
70
- plugin: contentLoadedPlugin,
40
+ plugin,
71
41
  })));
72
- return {
73
- ...contentLoadedPlugin,
74
- translationFiles: localizedTranslationFiles,
75
- };
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 };
76
52
  }));
77
- const allContent = lodash_1.chain(loadedPlugins)
53
+ const allContent = lodash_1.default.chain(loadedPlugins)
78
54
  .groupBy((item) => item.name)
79
- .mapValues((nameItems) => {
80
- return lodash_1.chain(nameItems)
81
- .groupBy((item) => { var _a; return (_a = item.options.id) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_PLUGIN_ID; })
82
- .mapValues((idItems) => idItems[0].content)
83
- .value();
84
- })
55
+ .mapValues((nameItems) => lodash_1.default.chain(nameItems)
56
+ .groupBy((item) => item.options.id)
57
+ .mapValues((idItems) => idItems[0].content)
58
+ .value())
85
59
  .value();
86
60
  // 3. Plugin Lifecycle - contentLoaded.
87
61
  const pluginsRouteConfigs = [];
88
62
  const globalData = {};
89
- await Promise.all(contentLoadedTranslatedPlugins.map(async ({ content, translationFiles, ...plugin }) => {
90
- var _a, _b, _c;
63
+ await Promise.all(loadedPlugins.map(async ({ content, ...plugin }) => {
91
64
  if (!plugin.contentLoaded) {
92
65
  return;
93
66
  }
94
- const pluginId = (_a = plugin.options.id) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_PLUGIN_ID;
95
- // plugins data files are namespaced by pluginName/pluginId
96
- const dataDirRoot = path_1.default.join(context.generatedFilesDir, plugin.name);
97
- const dataDir = path_1.default.join(dataDirRoot, pluginId);
98
- const addRoute = (initialRouteConfig) => {
99
- // Trailing slash behavior is handled in a generic way for all plugins
100
- const finalRouteConfig = applyRouteTrailingSlash_1.default(initialRouteConfig, context.siteConfig.trailingSlash);
101
- pluginsRouteConfigs.push(finalRouteConfig);
102
- };
103
- const createData = async (name, data) => {
104
- const modulePath = path_1.default.join(dataDir, name);
105
- await fs_extra_1.default.ensureDir(path_1.default.dirname(modulePath));
106
- await utils_1.generate(dataDir, name, data);
107
- return modulePath;
108
- };
109
- // the plugins global data are namespaced to avoid data conflicts:
110
- // - by plugin name
111
- // - by plugin id (allow using multiple instances of the same plugin)
112
- const setGlobalData = (data) => {
113
- var _a;
114
- globalData[plugin.name] = (_a = globalData[plugin.name]) !== null && _a !== void 0 ? _a : {};
115
- 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,
116
74
  };
75
+ await (0, utils_1.generate)('/', pluginRouteContextModulePath, JSON.stringify(pluginRouteContext, null, 2));
117
76
  const actions = {
118
- addRoute,
119
- createData,
120
- 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
+ },
121
98
  };
122
- const translatedContent = (_c = (_b = plugin.translateContent) === null || _b === void 0 ? void 0 : _b.call(plugin, { content, translationFiles })) !== null && _c !== void 0 ? _c : content;
123
- await plugin.contentLoaded({
124
- content: translatedContent,
125
- actions,
126
- allContent,
127
- });
128
- }));
129
- // 4. Plugin Lifecycle - routesLoaded.
130
- // Currently plugins run lifecycle methods in parallel and are not order-dependent.
131
- // We could change this in future if there are plugins which need to
132
- // run in certain order or depend on others for data.
133
- await Promise.all(contentLoadedTranslatedPlugins.map(async (plugin) => {
134
- if (!plugin.routesLoaded) {
135
- return null;
136
- }
137
- // TODO remove this deprecated lifecycle soon
138
- // deprecated since alpha-60
139
- // TODO, 1 user reported usage of this lifecycle! https://github.com/facebook/docusaurus/issues/3918
140
- 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'));
141
- return plugin.routesLoaded(pluginsRouteConfigs);
99
+ await plugin.contentLoaded({ content, actions, allContent });
142
100
  }));
143
101
  // Sort the route config. This ensures that route with nested
144
102
  // routes are always placed last.
145
- sortConfig(pluginsRouteConfigs);
146
- // Apply each plugin one after the other to translate the theme config
147
- function translateThemeConfig(untranslatedThemeConfig) {
148
- return contentLoadedTranslatedPlugins.reduce((currentThemeConfig, plugin) => {
149
- var _a;
150
- const translatedThemeConfigSlice = (_a = plugin.translateThemeConfig) === null || _a === void 0 ? void 0 : _a.call(plugin, {
151
- themeConfig: currentThemeConfig,
152
- translationFiles: plugin.translationFiles,
153
- });
154
- return {
155
- ...currentThemeConfig,
156
- ...translatedThemeConfigSlice,
157
- };
158
- }, untranslatedThemeConfig);
159
- }
160
- return {
161
- plugins: loadedPlugins,
162
- pluginsRouteConfigs,
163
- globalData,
164
- themeConfigTranslated: translateThemeConfig(context.siteConfig.themeConfig),
165
- };
103
+ (0, routeConfig_1.sortConfig)(pluginsRouteConfigs, context.siteConfig.baseUrl);
104
+ return { plugins: loadedPlugins, pluginsRouteConfigs, globalData };
166
105
  }
167
106
  exports.loadPlugins = loadPlugins;
@@ -4,8 +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 { LoadContext, PluginConfig, InitializedPlugin } from '@docusaurus/types';
8
- export default function initPlugins({ pluginConfigs, context, }: {
9
- pluginConfigs: PluginConfig[];
10
- context: LoadContext;
11
- }): 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,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: can't load plugin for following plugin config.\n${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;
@@ -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;
@@ -4,5 +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 { InitializedPlugin } from '@docusaurus/types';
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;