@docusaurus/core 2.0.0-beta.15a2b59f9 → 2.0.0-beta.17

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 (189) hide show
  1. package/bin/beforeCli.mjs +136 -0
  2. package/bin/{docusaurus.js → docusaurus.mjs} +63 -108
  3. package/lib/babel/preset.d.ts +8 -3
  4. package/lib/babel/preset.js +8 -7
  5. package/lib/choosePort.js +29 -38
  6. package/lib/client/.eslintrc.js +0 -1
  7. package/lib/client/App.d.ts +1 -2
  8. package/lib/client/App.js +19 -24
  9. package/lib/client/LinksCollector.d.ts +3 -3
  10. package/lib/client/LinksCollector.js +5 -9
  11. package/lib/client/PendingNavigation.d.ts +24 -1
  12. package/lib/client/PendingNavigation.js +5 -7
  13. package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.d.ts +13 -0
  14. package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.js +15 -10
  15. package/lib/client/client-lifecycles-dispatcher.d.ts +2 -5
  16. package/lib/client/client-lifecycles-dispatcher.js +6 -10
  17. package/lib/client/clientEntry.js +11 -5
  18. package/lib/client/docusaurus.d.ts +6 -0
  19. package/lib/client/docusaurus.js +16 -25
  20. package/lib/client/exports/BrowserOnly.d.ts +2 -3
  21. package/lib/client/exports/BrowserOnly.js +13 -7
  22. package/lib/client/exports/ComponentCreator.d.ts +1 -2
  23. package/lib/client/exports/ComponentCreator.js +7 -6
  24. package/lib/client/exports/ErrorBoundary.d.ts +18 -0
  25. package/lib/client/exports/ErrorBoundary.js +35 -0
  26. package/lib/client/exports/Head.d.ts +2 -3
  27. package/lib/client/exports/Head.js +3 -4
  28. package/lib/client/exports/Interpolate.d.ts +1 -1
  29. package/lib/client/exports/Interpolate.js +16 -22
  30. package/lib/client/exports/Link.d.ts +11 -5
  31. package/lib/client/exports/Link.js +24 -19
  32. package/lib/client/exports/Translate.d.ts +3 -3
  33. package/lib/client/exports/Translate.js +14 -10
  34. package/lib/client/exports/browserContext.d.ts +11 -0
  35. package/lib/client/exports/browserContext.js +22 -0
  36. package/lib/client/exports/constants.js +1 -11
  37. package/lib/client/exports/docusaurusContext.d.ts +12 -0
  38. package/lib/client/exports/docusaurusContext.js +25 -0
  39. package/lib/client/exports/isInternalUrl.js +1 -1
  40. package/lib/client/exports/renderRoutes.d.ts +1 -2
  41. package/lib/client/exports/renderRoutes.js +1 -2
  42. package/lib/client/exports/router.d.ts +1 -1
  43. package/lib/client/exports/router.js +1 -1
  44. package/lib/client/exports/useBaseUrl.js +3 -5
  45. package/lib/client/exports/useDocusaurusContext.d.ts +2 -3
  46. package/lib/client/exports/useDocusaurusContext.js +3 -9
  47. package/lib/client/exports/useGlobalData.js +1 -5
  48. package/lib/{server/versions/__tests/index.test.d.ts → client/exports/useIsBrowser.d.ts} +1 -1
  49. package/lib/client/exports/useIsBrowser.js +11 -0
  50. package/lib/client/flat.d.ts +2 -2
  51. package/lib/client/flat.js +8 -11
  52. package/lib/client/normalizeLocation.d.ts +2 -5
  53. package/lib/client/normalizeLocation.js +1 -2
  54. package/lib/client/nprogress.css +2 -2
  55. package/lib/client/prefetch.d.ts +1 -2
  56. package/lib/client/prefetch.js +1 -3
  57. package/lib/client/preload.d.ts +3 -2
  58. package/lib/client/preload.js +2 -1
  59. package/lib/client/serverEntry.d.ts +10 -0
  60. package/lib/client/serverEntry.js +90 -142
  61. package/lib/client/theme-fallback/Error/index.d.ts +10 -0
  62. package/lib/client/theme-fallback/Error/index.js +39 -0
  63. package/lib/client/theme-fallback/Layout/index.d.ts +10 -0
  64. package/lib/client/theme-fallback/Layout/index.js +10 -19
  65. package/lib/client/theme-fallback/Loading/index.d.ts +9 -0
  66. package/lib/client/theme-fallback/Loading/index.js +47 -115
  67. package/lib/client/{exports/context.js → theme-fallback/NotFound/index.d.ts} +2 -2
  68. package/lib/client/theme-fallback/NotFound/index.js +9 -16
  69. package/lib/{webpack/sharedModuleAliases.d.ts → client/theme-fallback/Root/index.d.ts} +4 -4
  70. package/lib/client/theme-fallback/Root/index.js +2 -7
  71. package/lib/commands/build.d.ts +1 -1
  72. package/lib/commands/build.js +50 -54
  73. package/lib/commands/clear.d.ts +6 -0
  74. package/lib/commands/clear.js +30 -19
  75. package/lib/commands/commandUtils.d.ts +7 -1
  76. package/lib/commands/commandUtils.js +6 -6
  77. package/lib/commands/deploy.d.ts +4 -1
  78. package/lib/commands/deploy.js +102 -66
  79. package/lib/commands/external.d.ts +2 -2
  80. package/lib/commands/external.js +3 -3
  81. package/lib/commands/serve.d.ts +1 -1
  82. package/lib/commands/serve.js +13 -19
  83. package/lib/commands/start.d.ts +1 -1
  84. package/lib/commands/start.js +93 -83
  85. package/lib/commands/swizzle/actions.d.ts +23 -0
  86. package/lib/commands/swizzle/actions.js +102 -0
  87. package/lib/commands/swizzle/common.d.ts +33 -0
  88. package/lib/commands/swizzle/common.js +56 -0
  89. package/lib/commands/swizzle/components.d.ts +29 -0
  90. package/lib/commands/swizzle/components.js +162 -0
  91. package/lib/commands/swizzle/config.d.ts +10 -0
  92. package/lib/commands/swizzle/config.js +80 -0
  93. package/lib/{server/loadSetup.d.ts → commands/swizzle/context.d.ts} +2 -3
  94. package/lib/commands/swizzle/context.js +30 -0
  95. package/lib/commands/swizzle/index.d.ts +8 -0
  96. package/lib/commands/swizzle/index.js +115 -0
  97. package/lib/commands/swizzle/prompts.d.ts +12 -0
  98. package/lib/commands/swizzle/prompts.js +110 -0
  99. package/lib/{client/exports/context.d.ts → commands/swizzle/tables.d.ts} +3 -4
  100. package/lib/commands/swizzle/tables.js +116 -0
  101. package/lib/commands/swizzle/themes.d.ts +20 -0
  102. package/lib/commands/swizzle/themes.js +103 -0
  103. package/lib/commands/writeHeadingIds.d.ts +9 -6
  104. package/lib/commands/writeHeadingIds.js +41 -43
  105. package/lib/commands/writeTranslations.d.ts +2 -2
  106. package/lib/commands/writeTranslations.js +35 -14
  107. package/lib/index.d.ts +10 -9
  108. package/lib/index.js +20 -19
  109. package/lib/server/brokenLinks.d.ts +1 -1
  110. package/lib/server/brokenLinks.js +57 -38
  111. package/lib/server/client-modules/index.d.ts +1 -1
  112. package/lib/server/client-modules/index.js +1 -3
  113. package/lib/server/config.d.ts +2 -2
  114. package/lib/server/config.js +7 -4
  115. package/lib/server/configValidation.d.ts +2 -2
  116. package/lib/server/configValidation.js +43 -28
  117. package/lib/server/duplicateRoutes.d.ts +1 -1
  118. package/lib/server/duplicateRoutes.js +10 -6
  119. package/lib/server/html-tags/htmlTags.js +2 -4
  120. package/lib/server/html-tags/index.d.ts +1 -1
  121. package/lib/server/html-tags/index.js +1 -1
  122. package/lib/server/i18n.d.ts +1 -2
  123. package/lib/server/i18n.js +32 -46
  124. package/lib/server/index.d.ts +2 -2
  125. package/lib/server/index.js +147 -68
  126. package/lib/server/moduleShorthand.d.ts +9 -0
  127. package/lib/server/moduleShorthand.js +46 -0
  128. package/lib/server/plugins/applyRouteTrailingSlash.d.ts +2 -2
  129. package/lib/server/plugins/applyRouteTrailingSlash.js +1 -1
  130. package/lib/server/plugins/index.d.ts +2 -2
  131. package/lib/server/plugins/index.js +34 -35
  132. package/lib/server/plugins/init.d.ts +12 -2
  133. package/lib/server/plugins/init.js +48 -58
  134. package/lib/server/plugins/pluginIds.d.ts +1 -1
  135. package/lib/server/plugins/pluginIds.js +8 -5
  136. package/lib/server/presets/index.d.ts +3 -3
  137. package/lib/server/presets/index.js +12 -13
  138. package/lib/server/routes.d.ts +1 -1
  139. package/lib/server/routes.js +50 -29
  140. package/lib/server/themes/alias.d.ts +3 -2
  141. package/lib/server/themes/alias.js +22 -14
  142. package/lib/server/themes/index.d.ts +3 -3
  143. package/lib/server/themes/index.js +26 -26
  144. package/lib/server/translations/translations.d.ts +7 -1
  145. package/lib/server/translations/translations.js +30 -45
  146. package/lib/server/translations/translationsExtractor.d.ts +9 -3
  147. package/lib/server/translations/translationsExtractor.js +159 -120
  148. package/lib/server/utils.d.ts +9 -3
  149. package/lib/server/utils.js +7 -9
  150. package/lib/server/versions/__fixtures__/dummy-plugin.d.ts +0 -0
  151. package/lib/server/versions/index.d.ts +3 -4
  152. package/lib/server/versions/index.js +22 -21
  153. package/lib/webpack/base.d.ts +4 -4
  154. package/lib/webpack/base.js +38 -33
  155. package/lib/webpack/client.d.ts +3 -3
  156. package/lib/webpack/client.js +12 -19
  157. package/lib/webpack/plugins/ChunkAssetPlugin.d.ts +13 -3
  158. package/lib/webpack/plugins/ChunkAssetPlugin.js +17 -10
  159. package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +6 -6
  160. package/lib/webpack/plugins/CleanWebpackPlugin.js +8 -14
  161. package/lib/webpack/plugins/LogPlugin.d.ts +1 -1
  162. package/lib/webpack/plugins/LogPlugin.js +4 -5
  163. package/lib/webpack/plugins/WaitPlugin.d.ts +1 -1
  164. package/lib/webpack/plugins/WaitPlugin.js +1 -1
  165. package/lib/webpack/server.d.ts +3 -3
  166. package/lib/webpack/server.js +9 -8
  167. package/lib/{client → webpack}/templates/index.html.template.ejs +0 -0
  168. package/lib/webpack/templates/ssr.html.template.d.ts +8 -0
  169. package/lib/{client → webpack}/templates/ssr.html.template.js +3 -2
  170. package/lib/webpack/utils.d.ts +7 -29
  171. package/lib/webpack/utils.js +54 -171
  172. package/package.json +74 -69
  173. package/lib/.tsbuildinfo +0 -1
  174. package/lib/client/.tsbuildinfo +0 -1
  175. package/lib/commands/buildRemoteBranchUrl.d.ts +0 -7
  176. package/lib/commands/buildRemoteBranchUrl.js +0 -27
  177. package/lib/commands/swizzle.d.ts +0 -9
  178. package/lib/commands/swizzle.js +0 -245
  179. package/lib/constants.d.ts +0 -18
  180. package/lib/constants.js +0 -23
  181. package/lib/server/loadSetup.js +0 -25
  182. package/lib/server/versions/__tests/index.test.js +0 -25
  183. package/lib/webpack/react-dev-utils-webpack5/README.md +0 -11
  184. package/lib/webpack/react-dev-utils-webpack5/evalSourceMapMiddleware.js +0 -57
  185. package/lib/webpack/react-dev-utils-webpack5/formatWebpackMessages.js +0 -138
  186. package/lib/webpack/react-dev-utils-webpack5/webpackHotDevClient.js +0 -285
  187. package/lib/webpack/sharedModuleAliases.js +0 -18
  188. package/tsconfig.client.json +0 -13
  189. package/tsconfig.json +0 -13
@@ -6,22 +6,22 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.normalizePluginConfigs = void 0;
9
10
  const tslib_1 = require("tslib");
10
- const module_1 = tslib_1.__importDefault(require("module"));
11
+ const module_1 = require("module");
11
12
  const import_fresh_1 = tslib_1.__importDefault(require("import-fresh"));
12
- const constants_1 = require("../../constants");
13
+ const utils_1 = require("@docusaurus/utils");
13
14
  const versions_1 = require("../versions");
14
15
  const pluginIds_1 = require("./pluginIds");
15
16
  const utils_validation_1 = require("@docusaurus/utils-validation");
16
- function normalizePluginConfig(pluginConfig, pluginRequire) {
17
- var _a, _b, _c, _d;
17
+ async function normalizePluginConfig(pluginConfig, pluginRequire) {
18
18
  // plugins: ['./plugin']
19
19
  if (typeof pluginConfig === 'string') {
20
20
  const pluginModuleImport = pluginConfig;
21
21
  const pluginPath = pluginRequire.resolve(pluginModuleImport);
22
- const pluginModule = import_fresh_1.default(pluginPath);
22
+ const pluginModule = (0, import_fresh_1.default)(pluginPath);
23
23
  return {
24
- plugin: (_a = pluginModule === null || pluginModule === void 0 ? void 0 : pluginModule.default) !== null && _a !== void 0 ? _a : pluginModule,
24
+ plugin: pluginModule?.default ?? pluginModule,
25
25
  options: {},
26
26
  pluginModule: {
27
27
  path: pluginModuleImport,
@@ -43,10 +43,10 @@ function normalizePluginConfig(pluginConfig, pluginRequire) {
43
43
  if (typeof pluginConfig[0] === 'string') {
44
44
  const pluginModuleImport = pluginConfig[0];
45
45
  const pluginPath = pluginRequire.resolve(pluginModuleImport);
46
- const pluginModule = import_fresh_1.default(pluginPath);
46
+ const pluginModule = (0, import_fresh_1.default)(pluginPath);
47
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 : {},
48
+ plugin: pluginModule?.default ?? pluginModule,
49
+ options: pluginConfig[1] ?? {},
50
50
  pluginModule: {
51
51
  path: pluginModuleImport,
52
52
  module: pluginModule,
@@ -59,49 +59,44 @@ function normalizePluginConfig(pluginConfig, pluginRequire) {
59
59
  if (typeof pluginConfig[0] === 'function') {
60
60
  return {
61
61
  plugin: pluginConfig[0],
62
- options: (_d = pluginConfig[1]) !== null && _d !== void 0 ? _d : {},
62
+ options: pluginConfig[1] ?? {},
63
63
  };
64
64
  }
65
65
  }
66
66
  throw new Error(`Unexpected: can't load plugin for following plugin config.\n${JSON.stringify(pluginConfig)}`);
67
67
  }
68
+ async function normalizePluginConfigs(pluginConfigs, pluginRequire) {
69
+ return Promise.all(pluginConfigs.map((pluginConfig) => normalizePluginConfig(pluginConfig, pluginRequire)));
70
+ }
71
+ exports.normalizePluginConfigs = normalizePluginConfigs;
68
72
  function getOptionValidationFunction(normalizedPluginConfig) {
69
- var _a, _b, _c, _d;
70
73
  if (normalizedPluginConfig.pluginModule) {
71
74
  // 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;
75
+ return (normalizedPluginConfig.pluginModule.module?.default?.validateOptions ??
76
+ normalizedPluginConfig.pluginModule.module?.validateOptions);
76
77
  }
78
+ return normalizedPluginConfig.plugin.validateOptions;
77
79
  }
78
80
  function getThemeValidationFunction(normalizedPluginConfig) {
79
- var _a, _b;
80
81
  if (normalizedPluginConfig.pluginModule) {
81
82
  // 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;
83
+ return (normalizedPluginConfig.pluginModule.module.default?.validateThemeConfig ??
84
+ normalizedPluginConfig.pluginModule.module.validateThemeConfig);
86
85
  }
86
+ return normalizedPluginConfig.plugin.validateThemeConfig;
87
87
  }
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;
88
+ async function initPlugins({ pluginConfigs, context, }) {
89
+ // We need to resolve plugins from the perspective of the siteDir, since the
90
+ // siteDir's package.json declares the dependency on these plugins.
91
+ const pluginRequire = (0, module_1.createRequire)(context.siteConfigPath);
92
+ const pluginConfigsNormalized = await normalizePluginConfigs(pluginConfigs, pluginRequire);
93
+ async function doGetPluginVersion(normalizedPluginConfig) {
97
94
  // 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' };
95
+ if (normalizedPluginConfig.pluginModule?.path) {
96
+ const pluginPath = pluginRequire.resolve(normalizedPluginConfig.pluginModule?.path);
97
+ return (0, versions_1.getPluginVersion)(pluginPath, context.siteDir);
104
98
  }
99
+ return { type: 'local' };
105
100
  }
106
101
  function doValidateThemeConfig(normalizedPluginConfig) {
107
102
  const validateThemeConfig = getThemeValidationFunction(normalizedPluginConfig);
@@ -111,12 +106,9 @@ function initPlugins({ pluginConfigs, context, }) {
111
106
  themeConfig: context.siteConfig.themeConfig,
112
107
  });
113
108
  }
114
- else {
115
- return context.siteConfig.themeConfig;
116
- }
109
+ return context.siteConfig.themeConfig;
117
110
  }
118
111
  function doValidatePluginOptions(normalizedPluginConfig) {
119
- var _a;
120
112
  const validateOptions = getOptionValidationFunction(normalizedPluginConfig);
121
113
  if (validateOptions) {
122
114
  return validateOptions({
@@ -124,37 +116,35 @@ function initPlugins({ pluginConfigs, context, }) {
124
116
  options: normalizedPluginConfig.options,
125
117
  });
126
118
  }
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
- }
119
+ // Important to ensure all plugins have an id
120
+ // as we don't go through the Joi schema that adds it
121
+ return {
122
+ ...normalizedPluginConfig.options,
123
+ id: normalizedPluginConfig.options.id ?? utils_1.DEFAULT_PLUGIN_ID,
124
+ };
135
125
  }
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);
126
+ async function initializePlugin(normalizedPluginConfig) {
127
+ const pluginVersion = await doGetPluginVersion(normalizedPluginConfig);
143
128
  const pluginOptions = doValidatePluginOptions(normalizedPluginConfig);
144
129
  // Side-effect: merge the normalized theme config in the original one
145
130
  context.siteConfig.themeConfig = {
146
131
  ...context.siteConfig.themeConfig,
147
132
  ...doValidateThemeConfig(normalizedPluginConfig),
148
133
  };
149
- const pluginInstance = normalizedPluginConfig.plugin(context, pluginOptions);
134
+ const pluginInstance = await normalizedPluginConfig.plugin(context, pluginOptions);
150
135
  return {
151
136
  ...pluginInstance,
152
137
  options: pluginOptions,
153
138
  version: pluginVersion,
154
139
  };
155
- })
156
- .filter((item) => Boolean(item));
157
- pluginIds_1.ensureUniquePluginInstanceIds(plugins);
140
+ }
141
+ const plugins = (await Promise.all(pluginConfigsNormalized.map((pluginConfig) => {
142
+ if (!pluginConfig) {
143
+ return null;
144
+ }
145
+ return initializePlugin(pluginConfig);
146
+ }))).filter((item) => Boolean(item));
147
+ (0, pluginIds_1.ensureUniquePluginInstanceIds)(plugins);
158
148
  return plugins;
159
149
  }
160
150
  exports.default = initPlugins;
@@ -4,5 +4,5 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import { InitializedPlugin } from '@docusaurus/types';
7
+ import type { InitializedPlugin } from '@docusaurus/types';
8
8
  export declare function ensureUniquePluginInstanceIds(plugins: InitializedPlugin[]): void;
@@ -7,17 +7,20 @@
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.ensureUniquePluginInstanceIds = void 0;
10
- const lodash_1 = require("lodash");
11
- const constants_1 = require("../../constants");
10
+ const tslib_1 = require("tslib");
11
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
12
+ const utils_1 = require("@docusaurus/utils");
12
13
  // It is forbidden to have 2 plugins of the same name sharing the same id
13
14
  // this is required to support multi-instance plugins without conflict
14
15
  function ensureUniquePluginInstanceIds(plugins) {
15
- const pluginsByName = lodash_1.groupBy(plugins, (p) => p.name);
16
+ const pluginsByName = lodash_1.default.groupBy(plugins, (p) => p.name);
16
17
  Object.entries(pluginsByName).forEach(([pluginName, pluginInstances]) => {
17
- const pluginInstancesById = lodash_1.groupBy(pluginInstances, (p) => { var _a; return (_a = p.options.id) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_PLUGIN_ID; });
18
+ const pluginInstancesById = lodash_1.default.groupBy(pluginInstances, (p) => p.options.id ?? utils_1.DEFAULT_PLUGIN_ID);
18
19
  Object.entries(pluginInstancesById).forEach(([pluginId, pluginInstancesWithId]) => {
19
20
  if (pluginInstancesWithId.length !== 1) {
20
- throw new Error(`Plugin "${pluginName}" is used ${pluginInstancesWithId.length} times with id ${pluginId}.\nTo use the same plugin multiple times on a Docusaurus site, you need to assign a unique id to each plugin instance.`);
21
+ throw new Error(`Plugin "${pluginName}" is used ${pluginInstancesWithId.length} times with ID "${pluginId}".\nTo use the same plugin multiple times on a Docusaurus site, you need to assign a unique ID to each plugin instance.${pluginId === utils_1.DEFAULT_PLUGIN_ID
22
+ ? `\n\nThe plugin ID is "${utils_1.DEFAULT_PLUGIN_ID}" by default. It's possible that the preset you are using already includes a plugin instance, in which case you either want to disable the plugin in the preset (to use a single instance), or assign another ID to your extra plugin instance (to use multiple instances).`
23
+ : ''}`);
21
24
  }
22
25
  });
23
26
  });
@@ -4,8 +4,8 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import { LoadContext, PluginConfig } from '@docusaurus/types';
8
- export default function loadPresets(context: LoadContext): {
7
+ import type { LoadContext, PluginConfig } from '@docusaurus/types';
8
+ export default function loadPresets(context: LoadContext): Promise<{
9
9
  plugins: PluginConfig[];
10
10
  themes: PluginConfig[];
11
- };
11
+ }>;
@@ -7,16 +7,14 @@
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  const tslib_1 = require("tslib");
10
- const module_1 = tslib_1.__importDefault(require("module"));
10
+ const module_1 = require("module");
11
11
  const import_fresh_1 = tslib_1.__importDefault(require("import-fresh"));
12
- function loadPresets(context) {
13
- // We need to resolve plugins from the perspective of the siteDir, since the siteDir's package.json
14
- // declares the dependency on these plugins.
15
- // We need to fallback to createRequireFromPath since createRequire is only available in node v12.
16
- // See: https://nodejs.org/api/modules.html#modules_module_createrequire_filename
17
- const createRequire = module_1.default.createRequire || module_1.default.createRequireFromPath;
18
- const pluginRequire = createRequire(context.siteConfigPath);
19
- const presets = (context.siteConfig || {}).presets || [];
12
+ const moduleShorthand_1 = require("../moduleShorthand");
13
+ 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.
16
+ const presetRequire = (0, module_1.createRequire)(context.siteConfigPath);
17
+ const presets = context.siteConfig.presets || [];
20
18
  const unflatPlugins = [];
21
19
  const unflatThemes = [];
22
20
  presets.forEach((presetItem) => {
@@ -31,8 +29,9 @@ function loadPresets(context) {
31
29
  else {
32
30
  throw new Error('Invalid presets format detected in config.');
33
31
  }
34
- const presetModule = import_fresh_1.default(pluginRequire.resolve(presetModuleImport));
35
- const preset = (presetModule.default || presetModule)(context, presetOptions);
32
+ const presetName = (0, moduleShorthand_1.resolveModuleName)(presetModuleImport, presetRequire, 'preset');
33
+ const presetModule = (0, import_fresh_1.default)(presetRequire.resolve(presetName));
34
+ const preset = (presetModule.default ?? presetModule)(context, presetOptions);
36
35
  if (preset.plugins) {
37
36
  unflatPlugins.push(preset.plugins);
38
37
  }
@@ -41,8 +40,8 @@ function loadPresets(context) {
41
40
  }
42
41
  });
43
42
  return {
44
- plugins: [].concat(...unflatPlugins).filter(Boolean),
45
- themes: [].concat(...unflatThemes).filter(Boolean),
43
+ plugins: unflatPlugins.flat().filter(Boolean),
44
+ themes: unflatThemes.flat().filter(Boolean),
46
45
  };
47
46
  }
48
47
  exports.default = loadPresets;
@@ -4,7 +4,7 @@
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 { ChunkRegistry, RouteConfig, ChunkNames } from '@docusaurus/types';
7
+ import type { ChunkRegistry, RouteConfig, ChunkNames } from '@docusaurus/types';
8
8
  declare type LoadedRoutes = {
9
9
  registry: {
10
10
  [chunkName: string]: ChunkRegistry;
@@ -7,20 +7,34 @@
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  const utils_1 = require("@docusaurus/utils");
10
- const lodash_1 = require("lodash");
11
10
  const querystring_1 = require("querystring");
12
- const createRouteCodeString = ({ routePath, routeHash, exact, subroutesCodeStrings, }) => {
13
- const str = `{
14
- path: '${routePath}',
15
- component: ComponentCreator('${routePath}','${routeHash}'),
16
- ${exact ? `exact: true,` : ''}
17
- ${subroutesCodeStrings
18
- ? ` routes: [
19
- ${utils_1.removeSuffix(subroutesCodeStrings.join(',\n'), ',\n')},
20
- ]
21
- `
22
- : ''}}`;
23
- return str;
11
+ function indent(str) {
12
+ const spaces = ' ';
13
+ return `${spaces}${str.replace(/\n/g, `\n${spaces}`)}`;
14
+ }
15
+ const createRouteCodeString = ({ routePath, routeHash, exact, subroutesCodeStrings, props, }) => {
16
+ const parts = [
17
+ `path: '${routePath}'`,
18
+ `component: ComponentCreator('${routePath}','${routeHash}')`,
19
+ ];
20
+ if (exact) {
21
+ parts.push(`exact: true`);
22
+ }
23
+ if (subroutesCodeStrings) {
24
+ parts.push(`routes: [
25
+ ${indent((0, utils_1.removeSuffix)(subroutesCodeStrings.join(',\n'), ',\n'))}
26
+ ]`);
27
+ }
28
+ Object.entries(props).forEach(([propName, propValue]) => {
29
+ // Figure out how to "unquote" JS attributes that don't need to be quoted
30
+ // Is this lib reliable? https://github.com/armanozak/should-quote
31
+ const shouldQuote = true; // TODO
32
+ const key = shouldQuote ? `'${propName}'` : propName;
33
+ parts.push(`${key}: ${JSON.stringify(propValue)}`);
34
+ });
35
+ return `{
36
+ ${indent(parts.join(',\n'))}
37
+ }`;
24
38
  };
25
39
  const NotFoundRouteCode = `{
26
40
  path: '*',
@@ -31,10 +45,13 @@ const RoutesImportsCode = [
31
45
  `import ComponentCreator from '@docusaurus/ComponentCreator';`,
32
46
  ].join('\n');
33
47
  function isModule(value) {
34
- if (lodash_1.isString(value)) {
48
+ if (typeof value === 'string') {
35
49
  return true;
36
50
  }
37
- if (lodash_1.isPlainObject(value) && lodash_1.has(value, '__import') && lodash_1.has(value, 'path')) {
51
+ if (typeof value === 'object' &&
52
+ // eslint-disable-next-line no-underscore-dangle
53
+ value?.__import &&
54
+ value?.path) {
38
55
  return true;
39
56
  }
40
57
  return false;
@@ -43,29 +60,31 @@ function getModulePath(target) {
43
60
  if (typeof target === 'string') {
44
61
  return target;
45
62
  }
46
- const queryStr = target.query ? `?${querystring_1.stringify(target.query)}` : '';
63
+ const queryStr = target.query ? `?${(0, querystring_1.stringify)(target.query)}` : '';
47
64
  return `${target.path}${queryStr}`;
48
65
  }
49
66
  async function loadRoutes(pluginsRouteConfigs, baseUrl) {
50
67
  const registry = {};
51
- const routesPaths = [utils_1.normalizeUrl([baseUrl, '404.html'])];
68
+ const routesPaths = [(0, utils_1.normalizeUrl)([baseUrl, '404.html'])];
52
69
  const routesChunkNames = {};
53
70
  // This is the higher level overview of route code generation.
54
71
  function generateRouteCode(routeConfig) {
55
- const { path: routePath, component, modules = {}, routes: subroutes, exact, } = routeConfig;
56
- if (!lodash_1.isString(routePath) || !component) {
57
- throw new Error(`Invalid route config: path must be a string and component is required.\n${JSON.stringify(routeConfig)}`);
72
+ const { path: routePath, component, modules = {}, routes: subroutes, exact, priority, ...props } = routeConfig;
73
+ if (typeof routePath !== 'string' || !component) {
74
+ throw new Error(`Invalid route config: path must be a string and component is required.
75
+ ${JSON.stringify(routeConfig)}`);
58
76
  }
59
77
  // Collect all page paths for injecting it later in the plugin lifecycle
60
78
  // This is useful for plugins like sitemaps, redirects etc...
61
- // If a route has subroutes, it is not necessarily a valid page path (more likely to be a wrapper)
79
+ // If a route has subroutes, it is not necessarily a valid page path (more
80
+ // likely to be a wrapper)
62
81
  if (!subroutes) {
63
82
  routesPaths.push(routePath);
64
83
  }
65
84
  // We hash the route to generate the key, because 2 routes can conflict with
66
85
  // each others if they have the same path, ex: parent=/docs, child=/docs
67
86
  // see https://github.com/facebook/docusaurus/issues/2917
68
- const routeHash = utils_1.simpleHash(JSON.stringify(routeConfig), 3);
87
+ const routeHash = (0, utils_1.simpleHash)(JSON.stringify(routeConfig), 3);
69
88
  const chunkNamesKey = `${routePath}-${routeHash}`;
70
89
  routesChunkNames[chunkNamesKey] = {
71
90
  ...genRouteChunkNames(registry, { component }, 'component', component),
@@ -75,15 +94,18 @@ async function loadRoutes(pluginsRouteConfigs, baseUrl) {
75
94
  routePath: routeConfig.path.replace(/'/g, "\\'"),
76
95
  routeHash,
77
96
  exact,
78
- subroutesCodeStrings: subroutes === null || subroutes === void 0 ? void 0 : subroutes.map(generateRouteCode),
97
+ subroutesCodeStrings: subroutes?.map(generateRouteCode),
98
+ props,
79
99
  });
80
100
  }
81
101
  const routesConfig = `
82
102
  ${RoutesImportsCode}
103
+
83
104
  export default [
84
- ${pluginsRouteConfigs.map(generateRouteCode).join(',\n')},
85
- ${NotFoundRouteCode}
86
- ];\n`;
105
+ ${indent(`${pluginsRouteConfigs.map(generateRouteCode).join(',\n')},`)}
106
+ ${indent(NotFoundRouteCode)}
107
+ ];
108
+ `;
87
109
  return {
88
110
  registry,
89
111
  routesConfig,
@@ -103,9 +125,8 @@ registry, value, prefix, name) {
103
125
  }
104
126
  if (isModule(value)) {
105
127
  const modulePath = getModulePath(value);
106
- const chunkName = utils_1.genChunkName(modulePath, prefix, name);
107
- // We need to JSON.stringify so that if its on windows, backslashes are escaped.
108
- const loader = `() => import(/* webpackChunkName: '${chunkName}' */ ${JSON.stringify(modulePath)})`;
128
+ const chunkName = (0, utils_1.genChunkName)(modulePath, prefix, name);
129
+ const loader = `() => import(/* webpackChunkName: '${chunkName}' */ '${(0, utils_1.escapePath)(modulePath)}')`;
109
130
  registry[chunkName] = {
110
131
  loader,
111
132
  modulePath,
@@ -4,5 +4,6 @@
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 { ThemeAliases } from '@docusaurus/types';
8
- export default function themeAlias(themePath: string, addOriginalAlias: boolean): ThemeAliases;
7
+ import type { ThemeAliases } from '@docusaurus/types';
8
+ export declare function sortAliases(aliases: ThemeAliases): ThemeAliases;
9
+ export default function themeAlias(themePath: string, addOriginalAlias: boolean): Promise<ThemeAliases>;
@@ -6,35 +6,43 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.sortAliases = void 0;
9
10
  const tslib_1 = require("tslib");
10
- const globby_1 = tslib_1.__importDefault(require("globby"));
11
11
  const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
12
12
  const path_1 = tslib_1.__importDefault(require("path"));
13
13
  const utils_1 = require("@docusaurus/utils");
14
- const lodash_1 = require("lodash");
15
- // TODO make async
16
- function themeAlias(themePath, addOriginalAlias) {
17
- if (!fs_extra_1.default.pathExistsSync(themePath)) {
14
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
15
+ // Order of Webpack aliases is important because one alias can shadow another
16
+ // This ensure @theme/NavbarItem alias is after @theme/NavbarItem/LocaleDropdown
17
+ // See https://github.com/facebook/docusaurus/pull/3922
18
+ // See https://github.com/facebook/docusaurus/issues/5382
19
+ function sortAliases(aliases) {
20
+ // Alphabetical order by default
21
+ const entries = lodash_1.default.sortBy(Object.entries(aliases), ([alias]) => alias);
22
+ // @theme/NavbarItem should be after @theme/NavbarItem/LocaleDropdown
23
+ entries.sort(([alias1], [alias2]) => alias1.includes(`${alias2}/`) ? -1 : 0);
24
+ return Object.fromEntries(entries);
25
+ }
26
+ exports.sortAliases = sortAliases;
27
+ async function themeAlias(themePath, addOriginalAlias) {
28
+ if (!(await fs_extra_1.default.pathExists(themePath))) {
18
29
  return {};
19
30
  }
20
- const themeComponentFiles = globby_1.default.sync(['**/*.{js,jsx,ts,tsx}'], {
31
+ const themeComponentFiles = await (0, utils_1.Globby)(['**/*.{js,jsx,ts,tsx}'], {
21
32
  cwd: themePath,
22
33
  });
23
- // See https://github.com/facebook/docusaurus/pull/3922
24
- // ensure @theme/NavbarItem alias is created after @theme/NavbarItem/LocaleDropdown
25
- const sortedThemeComponentFiles = lodash_1.sortBy(themeComponentFiles, (file) => file.endsWith('/index.js'));
26
34
  const aliases = {};
27
- sortedThemeComponentFiles.forEach((relativeSource) => {
35
+ themeComponentFiles.forEach((relativeSource) => {
28
36
  const filePath = path_1.default.join(themePath, relativeSource);
29
- const fileName = utils_1.fileToPath(relativeSource);
30
- const aliasName = utils_1.posixPath(utils_1.normalizeUrl(['@theme', fileName]).replace(/\/$/, ''));
37
+ const fileName = (0, utils_1.fileToPath)(relativeSource);
38
+ const aliasName = (0, utils_1.posixPath)((0, utils_1.normalizeUrl)(['@theme', fileName]).replace(/\/$/, ''));
31
39
  aliases[aliasName] = filePath;
32
40
  if (addOriginalAlias) {
33
41
  // For swizzled components to access the original.
34
- const originalAliasName = utils_1.posixPath(utils_1.normalizeUrl(['@theme-original', fileName]).replace(/\/$/, ''));
42
+ const originalAliasName = (0, utils_1.posixPath)((0, utils_1.normalizeUrl)(['@theme-original', fileName]).replace(/\/$/, ''));
35
43
  aliases[originalAliasName] = filePath;
36
44
  }
37
45
  });
38
- return aliases;
46
+ return sortAliases(aliases);
39
47
  }
40
48
  exports.default = themeAlias;
@@ -4,9 +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 { ThemeAliases, LoadedPlugin } from '@docusaurus/types';
8
- export declare function loadThemeAliases(themePaths: string[], userThemePaths?: string[]): ThemeAliases;
7
+ import type { ThemeAliases, LoadedPlugin } from '@docusaurus/types';
8
+ export declare function loadThemeAliases(themePaths: string[], userThemePaths: string[]): Promise<ThemeAliases>;
9
9
  export declare function loadPluginsThemeAliases({ siteDir, plugins, }: {
10
10
  siteDir: string;
11
11
  plugins: LoadedPlugin[];
12
- }): ThemeAliases;
12
+ }): Promise<ThemeAliases>;
@@ -9,39 +9,39 @@ Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.loadPluginsThemeAliases = exports.loadThemeAliases = void 0;
10
10
  const tslib_1 = require("tslib");
11
11
  const path_1 = tslib_1.__importDefault(require("path"));
12
- const constants_1 = require("../../constants");
13
- const alias_1 = tslib_1.__importDefault(require("./alias"));
12
+ const utils_1 = require("@docusaurus/utils");
13
+ const alias_1 = tslib_1.__importStar(require("./alias"));
14
14
  const ThemeFallbackDir = path_1.default.resolve(__dirname, '../../client/theme-fallback');
15
- function buildThemeAliases(themeAliases, aliases = {}) {
16
- Object.keys(themeAliases).forEach((aliasKey) => {
17
- if (aliasKey in aliases) {
18
- const componentName = aliasKey.substring(aliasKey.indexOf('/') + 1);
19
- // eslint-disable-next-line no-param-reassign
20
- aliases[`@theme-init/${componentName}`] = aliases[aliasKey];
21
- }
22
- // eslint-disable-next-line no-param-reassign
23
- aliases[aliasKey] = themeAliases[aliasKey];
24
- });
25
- return aliases;
26
- }
27
- function loadThemeAliases(themePaths, userThemePaths = []) {
28
- let aliases = {}; // TODO refactor, inelegant side-effect
29
- themePaths.forEach((themePath) => {
30
- const themeAliases = alias_1.default(themePath, true);
31
- aliases = { ...aliases, ...buildThemeAliases(themeAliases, aliases) };
32
- });
33
- userThemePaths.forEach((themePath) => {
34
- const userThemeAliases = alias_1.default(themePath, false);
35
- aliases = { ...aliases, ...buildThemeAliases(userThemeAliases, aliases) };
36
- });
37
- return aliases;
15
+ async function loadThemeAliases(themePaths, userThemePaths) {
16
+ const aliases = {};
17
+ for (const themePath of themePaths) {
18
+ const themeAliases = await (0, alias_1.default)(themePath, true);
19
+ Object.keys(themeAliases).forEach((aliasKey) => {
20
+ // If this alias shadows a previous one, use @theme-init to preserve the
21
+ // initial one. @theme-init is only applied once: to the initial theme
22
+ // that provided this component
23
+ if (aliasKey in aliases) {
24
+ const componentName = aliasKey.substring(aliasKey.indexOf('/') + 1);
25
+ const initAlias = `@theme-init/${componentName}`;
26
+ if (!(initAlias in aliases)) {
27
+ aliases[initAlias] = aliases[aliasKey];
28
+ }
29
+ }
30
+ aliases[aliasKey] = themeAliases[aliasKey];
31
+ });
32
+ }
33
+ for (const themePath of userThemePaths) {
34
+ const userThemeAliases = await (0, alias_1.default)(themePath, false);
35
+ Object.assign(aliases, userThemeAliases);
36
+ }
37
+ return (0, alias_1.sortAliases)(aliases);
38
38
  }
39
39
  exports.loadThemeAliases = loadThemeAliases;
40
40
  function loadPluginsThemeAliases({ siteDir, plugins, }) {
41
41
  const pluginThemes = plugins
42
42
  .map((plugin) => (plugin.getThemePath ? plugin.getThemePath() : undefined))
43
43
  .filter((x) => Boolean(x));
44
- const userTheme = path_1.default.resolve(siteDir, constants_1.THEME_PATH);
44
+ const userTheme = path_1.default.resolve(siteDir, utils_1.THEME_PATH);
45
45
  return loadThemeAliases([ThemeFallbackDir, ...pluginThemes], [userTheme]);
46
46
  }
47
47
  exports.loadPluginsThemeAliases = loadPluginsThemeAliases;
@@ -1,4 +1,10 @@
1
- import { TranslationFileContent, TranslationFile, TranslationMessage, InitializedPlugin } from '@docusaurus/types';
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 { TranslationFileContent, TranslationFile, TranslationMessage, InitializedPlugin } from '@docusaurus/types';
2
8
  export declare type WriteTranslationsOptions = {
3
9
  override?: boolean;
4
10
  messagePrefix?: string;