@docusaurus/core 0.0.0-4797 → 0.0.0-4800

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 (74) hide show
  1. package/lib/commands/build.d.ts +1 -1
  2. package/lib/commands/build.js +6 -3
  3. package/lib/commands/clear.d.ts +1 -1
  4. package/lib/commands/clear.js +2 -1
  5. package/lib/commands/commandUtils.js +2 -3
  6. package/lib/commands/deploy.d.ts +1 -1
  7. package/lib/commands/deploy.js +6 -4
  8. package/lib/commands/external.d.ts +1 -1
  9. package/lib/commands/external.js +5 -6
  10. package/lib/commands/serve.d.ts +1 -1
  11. package/lib/commands/serve.js +6 -5
  12. package/lib/commands/start.d.ts +1 -1
  13. package/lib/commands/start.js +4 -2
  14. package/lib/commands/swizzle/common.d.ts +1 -2
  15. package/lib/commands/swizzle/context.js +6 -10
  16. package/lib/commands/swizzle/index.d.ts +1 -1
  17. package/lib/commands/swizzle/index.js +2 -1
  18. package/lib/commands/writeHeadingIds.d.ts +1 -1
  19. package/lib/commands/writeHeadingIds.js +5 -8
  20. package/lib/commands/writeTranslations.d.ts +1 -1
  21. package/lib/commands/writeTranslations.js +6 -8
  22. package/lib/index.d.ts +9 -10
  23. package/lib/index.js +18 -19
  24. package/lib/server/choosePort.d.ts +12 -0
  25. package/lib/{choosePort.js → server/choosePort.js} +8 -10
  26. package/lib/server/{client-modules/index.d.ts → clientModules.d.ts} +5 -1
  27. package/lib/server/{client-modules/index.js → clientModules.js} +6 -1
  28. package/lib/server/config.d.ts +5 -2
  29. package/lib/server/config.js +11 -6
  30. package/lib/server/htmlTags.d.ts +12 -0
  31. package/lib/server/htmlTags.js +62 -0
  32. package/lib/server/i18n.d.ts +2 -11
  33. package/lib/server/i18n.js +1 -18
  34. package/lib/server/index.d.ts +26 -11
  35. package/lib/server/index.js +40 -201
  36. package/lib/server/plugins/configs.d.ts +14 -0
  37. package/lib/server/plugins/configs.js +101 -0
  38. package/lib/server/plugins/index.d.ts +6 -6
  39. package/lib/server/plugins/index.js +29 -64
  40. package/lib/server/plugins/init.d.ts +6 -19
  41. package/lib/server/plugins/init.js +14 -65
  42. package/lib/server/{moduleShorthand.d.ts → plugins/moduleShorthand.d.ts} +0 -0
  43. package/lib/server/{moduleShorthand.js → plugins/moduleShorthand.js} +0 -0
  44. package/lib/server/plugins/pluginIds.d.ts +4 -0
  45. package/lib/server/plugins/pluginIds.js +4 -2
  46. package/lib/server/plugins/presets.d.ts +12 -0
  47. package/lib/server/{presets/index.js → plugins/presets.js} +9 -4
  48. package/lib/server/plugins/{applyRouteTrailingSlash.d.ts → routeConfig.d.ts} +2 -1
  49. package/lib/server/plugins/routeConfig.js +53 -0
  50. package/lib/server/plugins/synthetic.d.ts +20 -0
  51. package/lib/server/plugins/synthetic.js +112 -0
  52. package/lib/server/routes.d.ts +3 -2
  53. package/lib/server/routes.js +50 -24
  54. package/lib/server/{versions/index.d.ts → siteMetadata.d.ts} +5 -2
  55. package/lib/server/{versions/index.js → siteMetadata.js} +36 -3
  56. package/lib/server/translations/translations.js +1 -4
  57. package/lib/webpack/aliases/index.d.ts +29 -0
  58. package/lib/webpack/aliases/index.js +106 -0
  59. package/lib/webpack/base.d.ts +0 -3
  60. package/lib/webpack/base.js +4 -21
  61. package/package.json +10 -11
  62. package/lib/choosePort.d.ts +0 -11
  63. package/lib/server/duplicateRoutes.d.ts +0 -8
  64. package/lib/server/duplicateRoutes.js +0 -42
  65. package/lib/server/html-tags/htmlTags.d.ts +0 -7
  66. package/lib/server/html-tags/htmlTags.js +0 -38
  67. package/lib/server/html-tags/index.d.ts +0 -8
  68. package/lib/server/html-tags/index.js +0 -42
  69. package/lib/server/plugins/applyRouteTrailingSlash.js +0 -19
  70. package/lib/server/presets/index.d.ts +0 -11
  71. package/lib/server/themes/alias.d.ts +0 -9
  72. package/lib/server/themes/alias.js +0 -50
  73. package/lib/server/themes/index.d.ts +0 -12
  74. package/lib/server/themes/index.js +0 -47
@@ -6,55 +6,25 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.loadPlugins = exports.sortConfig = void 0;
9
+ exports.loadPlugins = void 0;
10
10
  const tslib_1 = require("tslib");
11
11
  const utils_1 = require("@docusaurus/utils");
12
12
  const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
13
13
  const path_1 = tslib_1.__importDefault(require("path"));
14
- const init_1 = tslib_1.__importDefault(require("./init"));
14
+ const init_1 = require("./init");
15
+ const synthetic_1 = require("./synthetic");
15
16
  const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
16
17
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
17
18
  const translations_1 = require("../translations/translations");
18
- const applyRouteTrailingSlash_1 = tslib_1.__importDefault(require("./applyRouteTrailingSlash"));
19
- function sortConfig(routeConfigs, baseUrl = '/') {
20
- // Sort the route config. This ensures that route with nested
21
- // routes is always placed last.
22
- routeConfigs.sort((a, b) => {
23
- // Root route should get placed last.
24
- if (a.path === baseUrl && b.path !== baseUrl) {
25
- return 1;
26
- }
27
- if (a.path !== baseUrl && b.path === baseUrl) {
28
- return -1;
29
- }
30
- if (a.routes && !b.routes) {
31
- return 1;
32
- }
33
- if (!a.routes && b.routes) {
34
- return -1;
35
- }
36
- // Higher priority get placed first.
37
- if (a.priority || b.priority) {
38
- const priorityA = a.priority || 0;
39
- const priorityB = b.priority || 0;
40
- const score = priorityB - priorityA;
41
- if (score !== 0) {
42
- return score;
43
- }
44
- }
45
- return a.path.localeCompare(b.path);
46
- });
47
- routeConfigs.forEach((routeConfig) => {
48
- routeConfig.routes?.sort((a, b) => a.path.localeCompare(b.path));
49
- });
50
- }
51
- exports.sortConfig = sortConfig;
52
- async function loadPlugins({ pluginConfigs, context, }) {
19
+ const routeConfig_1 = require("./routeConfig");
20
+ /**
21
+ * Initializes the plugins, runs `loadContent`, `translateContent`,
22
+ * `contentLoaded`, and `translateThemeConfig`.
23
+ */
24
+ async function loadPlugins(context) {
53
25
  // 1. Plugin Lifecycle - Initialization/Constructor.
54
- const plugins = await (0, init_1.default)({
55
- pluginConfigs,
56
- context,
57
- });
26
+ const plugins = await (0, init_1.initPlugins)(context);
27
+ plugins.push((0, synthetic_1.createBootstrapPlugin)(context), (0, synthetic_1.createMDXFallbackPlugin)(context));
58
28
  // 2. Plugin Lifecycle - loadContent.
59
29
  // Currently plugins run lifecycle methods in parallel and are not
60
30
  // order-dependent. We could change this in future if there are plugins which
@@ -63,18 +33,18 @@ async function loadPlugins({ pluginConfigs, context, }) {
63
33
  const content = await plugin.loadContent?.();
64
34
  return { ...plugin, content };
65
35
  }));
66
- const contentLoadedTranslatedPlugins = await Promise.all(loadedPlugins.map(async (contentLoadedPlugin) => {
67
- const translationFiles = (await contentLoadedPlugin?.getTranslationFiles?.({
68
- content: contentLoadedPlugin.content,
36
+ const contentLoadedTranslatedPlugins = await Promise.all(loadedPlugins.map(async (plugin) => {
37
+ const translationFiles = (await plugin?.getTranslationFiles?.({
38
+ content: plugin.content,
69
39
  })) ?? [];
70
40
  const localizedTranslationFiles = await Promise.all(translationFiles.map((translationFile) => (0, translations_1.localizePluginTranslationFile)({
71
41
  locale: context.i18n.currentLocale,
72
42
  siteDir: context.siteDir,
73
43
  translationFile,
74
- plugin: contentLoadedPlugin,
44
+ plugin,
75
45
  })));
76
46
  return {
77
- ...contentLoadedPlugin,
47
+ ...plugin,
78
48
  translationFiles: localizedTranslationFiles,
79
49
  };
80
50
  }));
@@ -111,7 +81,7 @@ async function loadPlugins({ pluginConfigs, context, }) {
111
81
  const pluginRouteContextModulePath = await createData(`${(0, utils_1.docuHash)('pluginRouteContextModule')}.json`, JSON.stringify(pluginRouteContext, null, 2));
112
82
  const addRoute = (initialRouteConfig) => {
113
83
  // Trailing slash behavior is handled in a generic way for all plugins
114
- const finalRouteConfig = (0, applyRouteTrailingSlash_1.default)(initialRouteConfig, {
84
+ const finalRouteConfig = (0, routeConfig_1.applyRouteTrailingSlash)(initialRouteConfig, {
115
85
  trailingSlash: context.siteConfig.trailingSlash,
116
86
  baseUrl: context.siteConfig.baseUrl,
117
87
  });
@@ -143,9 +113,6 @@ async function loadPlugins({ pluginConfigs, context, }) {
143
113
  });
144
114
  }));
145
115
  // 4. Plugin Lifecycle - routesLoaded.
146
- // Currently plugins run lifecycle methods in parallel and are not
147
- // order-dependent. We could change this in future if there are plugins which
148
- // need to run in certain order or depend on others for data.
149
116
  await Promise.all(contentLoadedTranslatedPlugins.map(async (plugin) => {
150
117
  if (!plugin.routesLoaded) {
151
118
  return;
@@ -158,25 +125,23 @@ async function loadPlugins({ pluginConfigs, context, }) {
158
125
  }));
159
126
  // Sort the route config. This ensures that route with nested
160
127
  // routes are always placed last.
161
- sortConfig(pluginsRouteConfigs, context.siteConfig.baseUrl);
128
+ (0, routeConfig_1.sortConfig)(pluginsRouteConfigs, context.siteConfig.baseUrl);
162
129
  // Apply each plugin one after the other to translate the theme config
163
- function translateThemeConfig(untranslatedThemeConfig) {
164
- return contentLoadedTranslatedPlugins.reduce((currentThemeConfig, plugin) => {
165
- const translatedThemeConfigSlice = plugin.translateThemeConfig?.({
166
- themeConfig: currentThemeConfig,
167
- translationFiles: plugin.translationFiles,
168
- });
169
- return {
170
- ...currentThemeConfig,
171
- ...translatedThemeConfigSlice,
172
- };
173
- }, untranslatedThemeConfig);
174
- }
130
+ const themeConfigTranslated = contentLoadedTranslatedPlugins.reduce((currentThemeConfig, plugin) => {
131
+ const translatedThemeConfigSlice = plugin.translateThemeConfig?.({
132
+ themeConfig: currentThemeConfig,
133
+ translationFiles: plugin.translationFiles,
134
+ });
135
+ return {
136
+ ...currentThemeConfig,
137
+ ...translatedThemeConfigSlice,
138
+ };
139
+ }, context.siteConfig.themeConfig);
175
140
  return {
176
141
  plugins: loadedPlugins,
177
142
  pluginsRouteConfigs,
178
143
  globalData,
179
- themeConfigTranslated: translateThemeConfig(context.siteConfig.themeConfig),
144
+ themeConfigTranslated,
180
145
  };
181
146
  }
182
147
  exports.loadPlugins = loadPlugins;
@@ -4,22 +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 type { ImportedPluginModule, LoadContext, PluginModule, PluginConfig, PluginOptions, InitializedPlugin } from '@docusaurus/types';
8
- export declare type NormalizedPluginConfig = {
9
- plugin: PluginModule;
10
- options: PluginOptions;
11
- pluginModule?: {
12
- path: string;
13
- module: ImportedPluginModule;
14
- };
15
- /**
16
- * Different from pluginModule.path, this one is always an absolute path used
17
- * to resolve relative paths returned from lifecycles
18
- */
19
- entryPath: string;
20
- };
21
- export declare function normalizePluginConfigs(pluginConfigs: PluginConfig[], configPath: string): Promise<NormalizedPluginConfig[]>;
22
- export default function initPlugins({ pluginConfigs, context, }: {
23
- pluginConfigs: PluginConfig[];
24
- context: LoadContext;
25
- }): Promise<InitializedPlugin[]>;
7
+ import type { LoadContext, InitializedPlugin } from '@docusaurus/types';
8
+ /**
9
+ * Runs the plugin constructors and returns their return values. It would load
10
+ * plugin configs from `plugins`, `themes`, and `presets`.
11
+ */
12
+ export declare function initPlugins(context: LoadContext): Promise<InitializedPlugin[]>;
@@ -6,70 +6,15 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.normalizePluginConfigs = void 0;
9
+ exports.initPlugins = void 0;
10
10
  const tslib_1 = require("tslib");
11
11
  const module_1 = require("module");
12
12
  const path_1 = tslib_1.__importDefault(require("path"));
13
- const import_fresh_1 = tslib_1.__importDefault(require("import-fresh"));
14
13
  const utils_1 = require("@docusaurus/utils");
15
- const versions_1 = require("../versions");
14
+ const siteMetadata_1 = require("../siteMetadata");
16
15
  const pluginIds_1 = require("./pluginIds");
17
16
  const utils_validation_1 = require("@docusaurus/utils-validation");
18
- async function normalizePluginConfig(pluginConfig, configPath) {
19
- const pluginRequire = (0, module_1.createRequire)(configPath);
20
- // plugins: ['./plugin']
21
- if (typeof pluginConfig === 'string') {
22
- const pluginModuleImport = pluginConfig;
23
- const pluginPath = pluginRequire.resolve(pluginModuleImport);
24
- const pluginModule = (0, import_fresh_1.default)(pluginPath);
25
- return {
26
- plugin: pluginModule?.default ?? pluginModule,
27
- options: {},
28
- pluginModule: {
29
- path: pluginModuleImport,
30
- module: pluginModule,
31
- },
32
- entryPath: pluginPath,
33
- };
34
- }
35
- // plugins: [function plugin() { }]
36
- if (typeof pluginConfig === 'function') {
37
- return {
38
- plugin: pluginConfig,
39
- options: {},
40
- entryPath: configPath,
41
- };
42
- }
43
- // plugins: [
44
- // ['./plugin',options],
45
- // ]
46
- if (typeof pluginConfig[0] === 'string') {
47
- const pluginModuleImport = pluginConfig[0];
48
- const pluginPath = pluginRequire.resolve(pluginModuleImport);
49
- const pluginModule = (0, import_fresh_1.default)(pluginPath);
50
- return {
51
- plugin: pluginModule?.default ?? pluginModule,
52
- options: pluginConfig[1],
53
- pluginModule: {
54
- path: pluginModuleImport,
55
- module: pluginModule,
56
- },
57
- entryPath: pluginPath,
58
- };
59
- }
60
- // plugins: [
61
- // [function plugin() { },options],
62
- // ]
63
- return {
64
- plugin: pluginConfig[0],
65
- options: pluginConfig[1],
66
- entryPath: configPath,
67
- };
68
- }
69
- async function normalizePluginConfigs(pluginConfigs, configPath) {
70
- return Promise.all(pluginConfigs.map((pluginConfig) => normalizePluginConfig(pluginConfig, configPath)));
71
- }
72
- exports.normalizePluginConfigs = normalizePluginConfigs;
17
+ const configs_1 = require("./configs");
73
18
  function getOptionValidationFunction(normalizedPluginConfig) {
74
19
  if (normalizedPluginConfig.pluginModule) {
75
20
  // support both commonjs and ES modules
@@ -86,16 +31,20 @@ function getThemeValidationFunction(normalizedPluginConfig) {
86
31
  }
87
32
  return normalizedPluginConfig.plugin.validateThemeConfig;
88
33
  }
89
- async function initPlugins({ pluginConfigs, context, }) {
90
- // We need to resolve plugins from the perspective of the siteDir, since the
91
- // siteDir's package.json declares the dependency on these plugins.
34
+ /**
35
+ * Runs the plugin constructors and returns their return values. It would load
36
+ * plugin configs from `plugins`, `themes`, and `presets`.
37
+ */
38
+ async function initPlugins(context) {
39
+ // We need to resolve plugins from the perspective of the site config, as if
40
+ // we are using `require.resolve` on those module names.
92
41
  const pluginRequire = (0, module_1.createRequire)(context.siteConfigPath);
93
- const pluginConfigsNormalized = await normalizePluginConfigs(pluginConfigs, context.siteConfigPath);
42
+ const pluginConfigs = await (0, configs_1.loadPluginConfigs)(context);
94
43
  async function doGetPluginVersion(normalizedPluginConfig) {
95
44
  // get plugin version
96
45
  if (normalizedPluginConfig.pluginModule?.path) {
97
46
  const pluginPath = pluginRequire.resolve(normalizedPluginConfig.pluginModule?.path);
98
- return (0, versions_1.getPluginVersion)(pluginPath, context.siteDir);
47
+ return (0, siteMetadata_1.getPluginVersion)(pluginPath, context.siteDir);
99
48
  }
100
49
  return { type: 'local' };
101
50
  }
@@ -140,8 +89,8 @@ async function initPlugins({ pluginConfigs, context, }) {
140
89
  path: path_1.default.dirname(normalizedPluginConfig.entryPath),
141
90
  };
142
91
  }
143
- const plugins = await Promise.all(pluginConfigsNormalized.map(initializePlugin));
92
+ const plugins = await Promise.all(pluginConfigs.map(initializePlugin));
144
93
  (0, pluginIds_1.ensureUniquePluginInstanceIds)(plugins);
145
94
  return plugins;
146
95
  }
147
- exports.default = initPlugins;
96
+ exports.initPlugins = initPlugins;
@@ -5,4 +5,8 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import type { InitializedPlugin } from '@docusaurus/types';
8
+ /**
9
+ * It is forbidden to have 2 plugins of the same name sharing the same ID.
10
+ * This is required to support multi-instance plugins without conflict.
11
+ */
8
12
  export declare function ensureUniquePluginInstanceIds(plugins: InitializedPlugin[]): void;
@@ -10,8 +10,10 @@ exports.ensureUniquePluginInstanceIds = void 0;
10
10
  const tslib_1 = require("tslib");
11
11
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
12
12
  const utils_1 = require("@docusaurus/utils");
13
- // It is forbidden to have 2 plugins of the same name sharing the same id
14
- // this is required to support multi-instance plugins without conflict
13
+ /**
14
+ * It is forbidden to have 2 plugins of the same name sharing the same ID.
15
+ * This is required to support multi-instance plugins without conflict.
16
+ */
15
17
  function ensureUniquePluginInstanceIds(plugins) {
16
18
  const pluginsByName = lodash_1.default.groupBy(plugins, (p) => p.name);
17
19
  Object.entries(pluginsByName).forEach(([pluginName, pluginInstances]) => {
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { LoadContext, DocusaurusConfig } from '@docusaurus/types';
8
+ /**
9
+ * Calls preset functions, aggregates each of their return values, and returns
10
+ * the plugin and theme configs.
11
+ */
12
+ export declare function loadPresets(context: LoadContext): Promise<Pick<DocusaurusConfig, 'plugins' | 'themes'>>;
@@ -6,13 +6,18 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.loadPresets = void 0;
9
10
  const tslib_1 = require("tslib");
10
11
  const module_1 = require("module");
11
12
  const import_fresh_1 = tslib_1.__importDefault(require("import-fresh"));
12
- const moduleShorthand_1 = require("../moduleShorthand");
13
+ const moduleShorthand_1 = require("./moduleShorthand");
14
+ /**
15
+ * Calls preset functions, aggregates each of their return values, and returns
16
+ * the plugin and theme configs.
17
+ */
13
18
  async function loadPresets(context) {
14
- // We need to resolve presets from the perspective of the siteDir, since the
15
- // siteDir's package.json declares the dependency on these presets.
19
+ // We need to resolve plugins from the perspective of the site config, as if
20
+ // we are using `require.resolve` on those module names.
16
21
  const presetRequire = (0, module_1.createRequire)(context.siteConfigPath);
17
22
  const { presets } = context.siteConfig;
18
23
  const plugins = [];
@@ -38,4 +43,4 @@ async function loadPresets(context) {
38
43
  });
39
44
  return { plugins, themes };
40
45
  }
41
- exports.default = loadPresets;
46
+ exports.loadPresets = loadPresets;
@@ -6,4 +6,5 @@
6
6
  */
7
7
  import type { RouteConfig } from '@docusaurus/types';
8
8
  import { type ApplyTrailingSlashParams } from '@docusaurus/utils-common';
9
- export default function applyRouteTrailingSlash(route: RouteConfig, params: ApplyTrailingSlashParams): RouteConfig;
9
+ export declare function applyRouteTrailingSlash(route: RouteConfig, params: ApplyTrailingSlashParams): RouteConfig;
10
+ export declare function sortConfig(routeConfigs: RouteConfig[], baseUrl?: string): void;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.sortConfig = exports.applyRouteTrailingSlash = void 0;
10
+ const utils_common_1 = require("@docusaurus/utils-common");
11
+ function applyRouteTrailingSlash(route, params) {
12
+ return {
13
+ ...route,
14
+ path: (0, utils_common_1.applyTrailingSlash)(route.path, params),
15
+ ...(route.routes && {
16
+ routes: route.routes.map((subroute) => applyRouteTrailingSlash(subroute, params)),
17
+ }),
18
+ };
19
+ }
20
+ exports.applyRouteTrailingSlash = applyRouteTrailingSlash;
21
+ function sortConfig(routeConfigs, baseUrl = '/') {
22
+ // Sort the route config. This ensures that route with nested
23
+ // routes is always placed last.
24
+ routeConfigs.sort((a, b) => {
25
+ // Root route should get placed last.
26
+ if (a.path === baseUrl && b.path !== baseUrl) {
27
+ return 1;
28
+ }
29
+ if (a.path !== baseUrl && b.path === baseUrl) {
30
+ return -1;
31
+ }
32
+ if (a.routes && !b.routes) {
33
+ return 1;
34
+ }
35
+ if (!a.routes && b.routes) {
36
+ return -1;
37
+ }
38
+ // Higher priority get placed first.
39
+ if (a.priority || b.priority) {
40
+ const priorityA = a.priority || 0;
41
+ const priorityB = b.priority || 0;
42
+ const score = priorityB - priorityA;
43
+ if (score !== 0) {
44
+ return score;
45
+ }
46
+ }
47
+ return a.path.localeCompare(b.path);
48
+ });
49
+ routeConfigs.forEach((routeConfig) => {
50
+ routeConfig.routes?.sort((a, b) => a.path.localeCompare(b.path));
51
+ });
52
+ }
53
+ exports.sortConfig = sortConfig;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { LoadedPlugin, LoadContext } from '@docusaurus/types';
8
+ /**
9
+ * Make a synthetic plugin to:
10
+ * - Inject site client modules
11
+ * - Inject scripts/stylesheets
12
+ */
13
+ export declare function createBootstrapPlugin({ siteDir, siteConfig, }: LoadContext): LoadedPlugin;
14
+ /**
15
+ * Configure Webpack fallback mdx loader for md/mdx files out of content-plugin
16
+ * folders. Adds a "fallback" mdx loader for mdx files that are not processed by
17
+ * content plugins. This allows to do things such as importing repo/README.md as
18
+ * a partial from another doc. Not ideal solution, but good enough for now
19
+ */
20
+ export declare function createMDXFallbackPlugin({ siteDir, siteConfig, }: LoadContext): LoadedPlugin;
@@ -0,0 +1,112 @@
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.createMDXFallbackPlugin = exports.createBootstrapPlugin = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const path_1 = tslib_1.__importDefault(require("path"));
12
+ const remark_admonitions_1 = tslib_1.__importDefault(require("remark-admonitions"));
13
+ /**
14
+ * Make a synthetic plugin to:
15
+ * - Inject site client modules
16
+ * - Inject scripts/stylesheets
17
+ */
18
+ function createBootstrapPlugin({ siteDir, siteConfig, }) {
19
+ const { stylesheets, scripts, clientModules: siteConfigClientModules, } = siteConfig;
20
+ return {
21
+ name: 'docusaurus-bootstrap-plugin',
22
+ content: null,
23
+ options: {
24
+ id: 'default',
25
+ },
26
+ version: { type: 'synthetic' },
27
+ path: siteDir,
28
+ getClientModules() {
29
+ return siteConfigClientModules;
30
+ },
31
+ injectHtmlTags: () => {
32
+ const stylesheetsTags = stylesheets.map((source) => typeof source === 'string'
33
+ ? `<link rel="stylesheet" href="${source}">`
34
+ : {
35
+ tagName: 'link',
36
+ attributes: {
37
+ rel: 'stylesheet',
38
+ ...source,
39
+ },
40
+ });
41
+ const scriptsTags = scripts.map((source) => typeof source === 'string'
42
+ ? `<script src="${source}"></script>`
43
+ : {
44
+ tagName: 'script',
45
+ attributes: {
46
+ ...source,
47
+ },
48
+ });
49
+ return {
50
+ headTags: [...stylesheetsTags, ...scriptsTags],
51
+ };
52
+ },
53
+ };
54
+ }
55
+ exports.createBootstrapPlugin = createBootstrapPlugin;
56
+ /**
57
+ * Configure Webpack fallback mdx loader for md/mdx files out of content-plugin
58
+ * folders. Adds a "fallback" mdx loader for mdx files that are not processed by
59
+ * content plugins. This allows to do things such as importing repo/README.md as
60
+ * a partial from another doc. Not ideal solution, but good enough for now
61
+ */
62
+ function createMDXFallbackPlugin({ siteDir, siteConfig, }) {
63
+ return {
64
+ name: 'docusaurus-mdx-fallback-plugin',
65
+ content: null,
66
+ options: {
67
+ id: 'default',
68
+ },
69
+ version: { type: 'synthetic' },
70
+ // Synthetic, the path doesn't matter much
71
+ path: '.',
72
+ configureWebpack(config, isServer, { getJSLoader }) {
73
+ // We need the mdx fallback loader to exclude files that were already
74
+ // processed by content plugins mdx loaders. This works, but a bit
75
+ // hacky... Not sure there's a way to handle that differently in webpack
76
+ function getMDXFallbackExcludedPaths() {
77
+ const rules = config?.module?.rules;
78
+ return rules.flatMap((rule) => {
79
+ const isMDXRule = rule.test instanceof RegExp && rule.test.test('x.mdx');
80
+ return isMDXRule ? rule.include : [];
81
+ });
82
+ }
83
+ const mdxLoaderOptions = {
84
+ staticDirs: siteConfig.staticDirectories.map((dir) => path_1.default.resolve(siteDir, dir)),
85
+ siteDir,
86
+ // External MDX files are always meant to be imported as partials
87
+ isMDXPartial: () => true,
88
+ // External MDX files might have front matter, just disable the warning
89
+ isMDXPartialFrontMatterWarningDisabled: true,
90
+ remarkPlugins: [remark_admonitions_1.default],
91
+ };
92
+ return {
93
+ module: {
94
+ rules: [
95
+ {
96
+ test: /\.mdx?$/i,
97
+ exclude: getMDXFallbackExcludedPaths(),
98
+ use: [
99
+ getJSLoader({ isServer }),
100
+ {
101
+ loader: require.resolve('@docusaurus/mdx-loader'),
102
+ options: mdxLoaderOptions,
103
+ },
104
+ ],
105
+ },
106
+ ],
107
+ },
108
+ };
109
+ },
110
+ };
111
+ }
112
+ exports.createMDXFallbackPlugin = createMDXFallbackPlugin;
@@ -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 type { ChunkRegistry, RouteConfig, ChunkNames } from '@docusaurus/types';
8
- export default function loadRoutes(pluginsRouteConfigs: RouteConfig[], baseUrl: string): Promise<{
7
+ import type { ChunkRegistry, RouteConfig, ChunkNames, ReportingSeverity } from '@docusaurus/types';
8
+ export declare function handleDuplicateRoutes(pluginsRouteConfigs: RouteConfig[], onDuplicateRoutes: ReportingSeverity): void;
9
+ export declare function loadRoutes(pluginsRouteConfigs: RouteConfig[], baseUrl: string, onDuplicateRoutes: ReportingSeverity): Promise<{
9
10
  registry: {
10
11
  [chunkName: string]: ChunkRegistry;
11
12
  };
@@ -6,8 +6,10 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.loadRoutes = exports.handleDuplicateRoutes = void 0;
9
10
  const utils_1 = require("@docusaurus/utils");
10
11
  const querystring_1 = require("querystring");
12
+ const utils_2 = require("./utils");
11
13
  function indent(str) {
12
14
  const spaces = ' ';
13
15
  return `${spaces}${str.replace(/\n/g, `\n${spaces}`)}`;
@@ -77,7 +79,53 @@ function getModulePath(target) {
77
79
  const queryStr = target.query ? `?${(0, querystring_1.stringify)(target.query)}` : '';
78
80
  return `${target.path}${queryStr}`;
79
81
  }
80
- async function loadRoutes(pluginsRouteConfigs, baseUrl) {
82
+ function genRouteChunkNames(
83
+ // TODO instead of passing a mutating the registry, return a registry slice?
84
+ registry, value, prefix, name) {
85
+ if (!value) {
86
+ return null;
87
+ }
88
+ if (Array.isArray(value)) {
89
+ return value.map((val, index) => genRouteChunkNames(registry, val, `${index}`, name));
90
+ }
91
+ if (isModule(value)) {
92
+ const modulePath = getModulePath(value);
93
+ const chunkName = (0, utils_1.genChunkName)(modulePath, prefix, name);
94
+ const loader = `() => import(/* webpackChunkName: '${chunkName}' */ '${(0, utils_1.escapePath)(modulePath)}')`;
95
+ registry[chunkName] = { loader, modulePath };
96
+ return chunkName;
97
+ }
98
+ const newValue = {};
99
+ Object.entries(value).forEach(([key, v]) => {
100
+ newValue[key] = genRouteChunkNames(registry, v, key, name);
101
+ });
102
+ return newValue;
103
+ }
104
+ function handleDuplicateRoutes(pluginsRouteConfigs, onDuplicateRoutes) {
105
+ if (onDuplicateRoutes === 'ignore') {
106
+ return;
107
+ }
108
+ const allRoutes = (0, utils_2.getAllFinalRoutes)(pluginsRouteConfigs).map((routeConfig) => routeConfig.path);
109
+ const seenRoutes = new Set();
110
+ const duplicatePaths = allRoutes.filter((route) => {
111
+ if (seenRoutes.has(route)) {
112
+ return true;
113
+ }
114
+ seenRoutes.add(route);
115
+ return false;
116
+ });
117
+ if (duplicatePaths.length > 0) {
118
+ const finalMessage = `Duplicate routes found!
119
+ ${duplicatePaths
120
+ .map((duplicateRoute) => `- Attempting to create page at ${duplicateRoute}, but a page already exists at this route.`)
121
+ .join('\n')}
122
+ This could lead to non-deterministic routing behavior.`;
123
+ (0, utils_1.reportMessage)(finalMessage, onDuplicateRoutes);
124
+ }
125
+ }
126
+ exports.handleDuplicateRoutes = handleDuplicateRoutes;
127
+ async function loadRoutes(pluginsRouteConfigs, baseUrl, onDuplicateRoutes) {
128
+ handleDuplicateRoutes(pluginsRouteConfigs, onDuplicateRoutes);
81
129
  const registry = {};
82
130
  const routesPaths = [(0, utils_1.normalizeUrl)([baseUrl, '404.html'])];
83
131
  const routesChunkNames = {};
@@ -127,26 +175,4 @@ ${indent(NotFoundRouteCode)}
127
175
  routesPaths,
128
176
  };
129
177
  }
130
- exports.default = loadRoutes;
131
- function genRouteChunkNames(
132
- // TODO instead of passing a mutating the registry, return a registry slice?
133
- registry, value, prefix, name) {
134
- if (!value) {
135
- return null;
136
- }
137
- if (Array.isArray(value)) {
138
- return value.map((val, index) => genRouteChunkNames(registry, val, `${index}`, name));
139
- }
140
- if (isModule(value)) {
141
- const modulePath = getModulePath(value);
142
- const chunkName = (0, utils_1.genChunkName)(modulePath, prefix, name);
143
- const loader = `() => import(/* webpackChunkName: '${chunkName}' */ '${(0, utils_1.escapePath)(modulePath)}')`;
144
- registry[chunkName] = { loader, modulePath };
145
- return chunkName;
146
- }
147
- const newValue = {};
148
- Object.entries(value).forEach(([key, v]) => {
149
- newValue[key] = genRouteChunkNames(registry, v, key, name);
150
- });
151
- return newValue;
152
- }
178
+ exports.loadRoutes = loadRoutes;