@docusaurus/core 3.1.0 → 3.2.0

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 (88) hide show
  1. package/bin/docusaurus.mjs +10 -5
  2. package/lib/client/BrokenLinksContext.js +2 -2
  3. package/lib/client/exports/Link.js +10 -2
  4. package/lib/client/{serverRenderer.d.ts → renderToHtml.d.ts} +1 -1
  5. package/lib/client/{serverRenderer.js → renderToHtml.js} +1 -1
  6. package/lib/client/serverEntry.d.ts +3 -4
  7. package/lib/client/serverEntry.js +12 -113
  8. package/lib/client/theme-fallback/Error/index.js +22 -8
  9. package/lib/commands/build.d.ts +3 -3
  10. package/lib/commands/build.js +133 -112
  11. package/lib/commands/deploy.d.ts +2 -2
  12. package/lib/commands/deploy.js +3 -3
  13. package/lib/commands/external.js +2 -2
  14. package/lib/commands/serve.d.ts +2 -2
  15. package/lib/commands/serve.js +4 -4
  16. package/lib/commands/{start.d.ts → start/start.d.ts} +3 -3
  17. package/lib/commands/start/start.js +47 -0
  18. package/lib/commands/start/utils.d.ts +31 -0
  19. package/lib/commands/start/utils.js +87 -0
  20. package/lib/commands/start/watcher.d.ts +42 -0
  21. package/lib/commands/start/watcher.js +78 -0
  22. package/lib/commands/start/webpack.d.ts +15 -0
  23. package/lib/commands/start/webpack.js +133 -0
  24. package/lib/commands/swizzle/common.d.ts +1 -0
  25. package/lib/commands/swizzle/common.js +1 -0
  26. package/lib/commands/swizzle/context.js +2 -2
  27. package/lib/commands/swizzle/index.js +32 -3
  28. package/lib/commands/writeHeadingIds.js +2 -2
  29. package/lib/commands/writeTranslations.d.ts +2 -2
  30. package/lib/commands/writeTranslations.js +3 -3
  31. package/lib/index.d.ts +1 -1
  32. package/lib/index.js +1 -1
  33. package/lib/server/brokenLinks.js +126 -39
  34. package/lib/server/clientModules.d.ts +1 -1
  35. package/lib/server/clientModules.js +3 -3
  36. package/lib/server/codegen/codegen.d.ts +20 -0
  37. package/lib/server/codegen/codegen.js +65 -0
  38. package/lib/server/codegen/codegenRoutes.d.ts +49 -0
  39. package/lib/server/codegen/codegenRoutes.js +190 -0
  40. package/lib/server/configValidation.js +6 -4
  41. package/lib/server/getHostPort.js +4 -1
  42. package/lib/server/i18n.d.ts +2 -2
  43. package/lib/server/i18n.js +20 -2
  44. package/lib/server/plugins/actions.d.ts +19 -0
  45. package/lib/server/plugins/actions.js +62 -0
  46. package/lib/server/plugins/init.js +3 -3
  47. package/lib/server/plugins/plugins.d.ts +21 -0
  48. package/lib/server/plugins/plugins.js +188 -0
  49. package/lib/server/plugins/pluginsUtils.d.ts +16 -0
  50. package/lib/server/plugins/pluginsUtils.js +75 -0
  51. package/lib/server/plugins/routeConfig.d.ts +1 -1
  52. package/lib/server/plugins/routeConfig.js +4 -4
  53. package/lib/server/plugins/synthetic.d.ts +3 -3
  54. package/lib/server/plugins/synthetic.js +0 -2
  55. package/lib/server/routes.d.ts +3 -45
  56. package/lib/server/routes.js +21 -165
  57. package/lib/server/{index.d.ts → site.d.ts} +12 -5
  58. package/lib/server/site.js +164 -0
  59. package/lib/server/siteMetadata.d.ts +5 -4
  60. package/lib/server/siteMetadata.js +14 -10
  61. package/lib/server/translations/translations.d.ts +9 -2
  62. package/lib/server/translations/translations.js +21 -4
  63. package/lib/server/utils.d.ts +0 -2
  64. package/lib/server/utils.js +1 -9
  65. package/lib/ssg.d.ts +31 -0
  66. package/lib/ssg.js +167 -0
  67. package/lib/templates/templates.d.ts +28 -0
  68. package/lib/templates/templates.js +63 -0
  69. package/lib/utils.d.ts +9 -0
  70. package/lib/utils.js +78 -0
  71. package/lib/webpack/base.d.ts +5 -1
  72. package/lib/webpack/base.js +4 -6
  73. package/lib/webpack/client.d.ts +15 -1
  74. package/lib/webpack/client.js +78 -23
  75. package/lib/webpack/minification.d.ts +8 -0
  76. package/lib/webpack/minification.js +97 -0
  77. package/lib/webpack/server.d.ts +5 -3
  78. package/lib/webpack/server.js +41 -50
  79. package/lib/webpack/utils.d.ts +13 -4
  80. package/lib/webpack/utils.js +27 -83
  81. package/package.json +13 -11
  82. package/lib/commands/start.js +0 -212
  83. package/lib/server/index.js +0 -154
  84. package/lib/server/plugins/index.d.ts +0 -18
  85. package/lib/server/plugins/index.js +0 -106
  86. /package/lib/{webpack/templates/index.html.template.ejs → templates/dev.html.template.ejs} +0 -0
  87. /package/lib/{webpack/templates → templates}/ssr.html.template.d.ts +0 -0
  88. /package/lib/{webpack/templates → templates}/ssr.html.template.js +0 -0
@@ -1,154 +0,0 @@
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.load = exports.loadContext = void 0;
10
- const tslib_1 = require("tslib");
11
- const path_1 = tslib_1.__importDefault(require("path"));
12
- const lodash_1 = tslib_1.__importDefault(require("lodash"));
13
- const utils_1 = require("@docusaurus/utils");
14
- const config_1 = require("./config");
15
- const clientModules_1 = require("./clientModules");
16
- const plugins_1 = require("./plugins");
17
- const routes_1 = require("./routes");
18
- const htmlTags_1 = require("./htmlTags");
19
- const siteMetadata_1 = require("./siteMetadata");
20
- const i18n_1 = require("./i18n");
21
- const translations_1 = require("./translations/translations");
22
- /**
23
- * Loading context is the very first step in site building. Its options are
24
- * directly acquired from CLI options. It mainly loads `siteConfig` and the i18n
25
- * context (which includes code translations). The `LoadContext` will be passed
26
- * to plugin constructors.
27
- */
28
- async function loadContext(options) {
29
- const { siteDir, outDir: baseOutDir = utils_1.DEFAULT_BUILD_DIR_NAME, locale, config: customConfigFilePath, } = options;
30
- const generatedFilesDir = path_1.default.resolve(siteDir, utils_1.GENERATED_FILES_DIR_NAME);
31
- const { siteConfig: initialSiteConfig, siteConfigPath } = await (0, config_1.loadSiteConfig)({
32
- siteDir,
33
- customConfigFilePath,
34
- });
35
- const i18n = await (0, i18n_1.loadI18n)(initialSiteConfig, { locale });
36
- const baseUrl = (0, utils_1.localizePath)({
37
- path: initialSiteConfig.baseUrl,
38
- i18n,
39
- options,
40
- pathType: 'url',
41
- });
42
- const outDir = (0, utils_1.localizePath)({
43
- path: path_1.default.resolve(siteDir, baseOutDir),
44
- i18n,
45
- options,
46
- pathType: 'fs',
47
- });
48
- const siteConfig = { ...initialSiteConfig, baseUrl };
49
- const localizationDir = path_1.default.resolve(siteDir, i18n.path, i18n.localeConfigs[i18n.currentLocale].path);
50
- const codeTranslationFileContent = (await (0, translations_1.readCodeTranslationFileContent)({ localizationDir })) ?? {};
51
- // We only need key->message for code translations
52
- const codeTranslations = lodash_1.default.mapValues(codeTranslationFileContent, (value) => value.message);
53
- return {
54
- siteDir,
55
- generatedFilesDir,
56
- localizationDir,
57
- siteConfig,
58
- siteConfigPath,
59
- outDir,
60
- baseUrl,
61
- i18n,
62
- codeTranslations,
63
- };
64
- }
65
- exports.loadContext = loadContext;
66
- /**
67
- * This is the crux of the Docusaurus server-side. It reads everything it needs—
68
- * code translations, config file, plugin modules... Plugins then use their
69
- * lifecycles to generate content and other data. It is side-effect-ful because
70
- * it generates temp files in the `.docusaurus` folder for the bundler.
71
- */
72
- async function load(options) {
73
- const { siteDir } = options;
74
- const context = await loadContext(options);
75
- const { generatedFilesDir, siteConfig, siteConfigPath, outDir, baseUrl, i18n, localizationDir, codeTranslations: siteCodeTranslations, } = context;
76
- const { plugins, pluginsRouteConfigs, globalData } = await (0, plugins_1.loadPlugins)(context);
77
- const clientModules = (0, clientModules_1.loadClientModules)(plugins);
78
- const { headTags, preBodyTags, postBodyTags } = (0, htmlTags_1.loadHtmlTags)(plugins);
79
- const { registry, routesChunkNames, routesConfig, routesPaths } = (0, routes_1.loadRoutes)(pluginsRouteConfigs, baseUrl, siteConfig.onDuplicateRoutes);
80
- const codeTranslations = {
81
- ...(await (0, translations_1.getPluginsDefaultCodeTranslationMessages)(plugins)),
82
- ...siteCodeTranslations,
83
- };
84
- const siteMetadata = await (0, siteMetadata_1.loadSiteMetadata)({ plugins, siteDir });
85
- // === Side-effects part ===
86
- const genWarning = (0, utils_1.generate)(generatedFilesDir,
87
- // cSpell:ignore DONT
88
- 'DONT-EDIT-THIS-FOLDER', `This folder stores temp files that Docusaurus' client bundler accesses.
89
-
90
- DO NOT hand-modify files in this folder because they will be overwritten in the
91
- next build. You can clear all build artifacts (including this folder) with the
92
- \`docusaurus clear\` command.
93
- `);
94
- const genSiteConfig = (0, utils_1.generate)(generatedFilesDir, `${utils_1.DEFAULT_CONFIG_FILE_NAME}.mjs`, `/*
95
- * AUTOGENERATED - DON'T EDIT
96
- * Your edits in this file will be overwritten in the next build!
97
- * Modify the docusaurus.config.js file at your site's root instead.
98
- */
99
- export default ${JSON.stringify(siteConfig, null, 2)};
100
- `);
101
- const genClientModules = (0, utils_1.generate)(generatedFilesDir, 'client-modules.js', `export default [
102
- ${clientModules
103
- // Use `require()` because `import()` is async but client modules can have CSS
104
- // and the order matters for loading CSS.
105
- .map((clientModule) => ` require("${(0, utils_1.escapePath)(clientModule)}"),`)
106
- .join('\n')}
107
- ];
108
- `);
109
- const genRegistry = (0, utils_1.generate)(generatedFilesDir, 'registry.js', `export default {
110
- ${Object.entries(registry)
111
- .sort((a, b) => a[0].localeCompare(b[0]))
112
- .map(([chunkName, modulePath]) =>
113
- // modulePath is already escaped by escapePath
114
- ` "${chunkName}": [() => import(/* webpackChunkName: "${chunkName}" */ "${modulePath}"), "${modulePath}", require.resolveWeak("${modulePath}")],`)
115
- .join('\n')}};
116
- `);
117
- const genRoutesChunkNames = (0, utils_1.generate)(generatedFilesDir, 'routesChunkNames.json', JSON.stringify(routesChunkNames, null, 2));
118
- const genRoutes = (0, utils_1.generate)(generatedFilesDir, 'routes.js', routesConfig);
119
- const genGlobalData = (0, utils_1.generate)(generatedFilesDir, 'globalData.json', JSON.stringify(globalData, null, 2));
120
- const genI18n = (0, utils_1.generate)(generatedFilesDir, 'i18n.json', JSON.stringify(i18n, null, 2));
121
- const genCodeTranslations = (0, utils_1.generate)(generatedFilesDir, 'codeTranslations.json', JSON.stringify(codeTranslations, null, 2));
122
- const genSiteMetadata = (0, utils_1.generate)(generatedFilesDir, 'site-metadata.json', JSON.stringify(siteMetadata, null, 2));
123
- await Promise.all([
124
- genWarning,
125
- genClientModules,
126
- genSiteConfig,
127
- genRegistry,
128
- genRoutesChunkNames,
129
- genRoutes,
130
- genGlobalData,
131
- genSiteMetadata,
132
- genI18n,
133
- genCodeTranslations,
134
- ]);
135
- return {
136
- siteConfig,
137
- siteConfigPath,
138
- siteMetadata,
139
- siteDir,
140
- outDir,
141
- baseUrl,
142
- i18n,
143
- localizationDir,
144
- generatedFilesDir,
145
- routes: pluginsRouteConfigs,
146
- routesPaths,
147
- plugins,
148
- headTags,
149
- preBodyTags,
150
- postBodyTags,
151
- codeTranslations,
152
- };
153
- }
154
- exports.load = load;
@@ -1,18 +0,0 @@
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, RouteConfig, GlobalData, LoadedPlugin } from '@docusaurus/types';
8
- /**
9
- * Initializes the plugins, runs `loadContent`, `translateContent`,
10
- * `contentLoaded`, and `translateThemeConfig`. Because `contentLoaded` is
11
- * side-effect-ful (it generates temp files), so is this function. This function
12
- * would also mutate `context.siteConfig.themeConfig` to translate it.
13
- */
14
- export declare function loadPlugins(context: LoadContext): Promise<{
15
- plugins: LoadedPlugin[];
16
- pluginsRouteConfigs: RouteConfig[];
17
- globalData: GlobalData;
18
- }>;
@@ -1,106 +0,0 @@
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.loadPlugins = void 0;
10
- const tslib_1 = require("tslib");
11
- const path_1 = tslib_1.__importDefault(require("path"));
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");
16
- const translations_1 = require("../translations/translations");
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) {
25
- // 1. Plugin Lifecycle - Initialization/Constructor.
26
- const plugins = await (0, init_1.initPlugins)(context);
27
- plugins.push((0, synthetic_1.createBootstrapPlugin)(context), (0, synthetic_1.createMDXFallbackPlugin)(context));
28
- // 2. Plugin Lifecycle - loadContent.
29
- // Currently plugins run lifecycle methods in parallel and are not
30
- // order-dependent. We could change this in future if there are plugins which
31
- // need to run in certain order or depend on others for data.
32
- // This would also translate theme config and content upfront, given the
33
- // translation files that the plugin declares.
34
- const loadedPlugins = await Promise.all(plugins.map(async (plugin) => {
35
- const content = await plugin.loadContent?.();
36
- const rawTranslationFiles = (await plugin.getTranslationFiles?.({ content })) ?? [];
37
- const translationFiles = await Promise.all(rawTranslationFiles.map((translationFile) => (0, translations_1.localizePluginTranslationFile)({
38
- localizationDir: context.localizationDir,
39
- translationFile,
40
- plugin,
41
- })));
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 };
52
- }));
53
- const allContent = lodash_1.default.chain(loadedPlugins)
54
- .groupBy((item) => item.name)
55
- .mapValues((nameItems) => lodash_1.default.chain(nameItems)
56
- .groupBy((item) => item.options.id)
57
- .mapValues((idItems) => idItems[0].content)
58
- .value())
59
- .value();
60
- // 3. Plugin Lifecycle - contentLoaded.
61
- const pluginsRouteConfigs = [];
62
- const globalData = {};
63
- await Promise.all(loadedPlugins.map(async ({ content, ...plugin }) => {
64
- if (!plugin.contentLoaded) {
65
- return;
66
- }
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,
74
- };
75
- await (0, utils_1.generate)('/', pluginRouteContextModulePath, JSON.stringify(pluginRouteContext, null, 2));
76
- const actions = {
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
- },
98
- };
99
- await plugin.contentLoaded({ content, actions, allContent });
100
- }));
101
- // Sort the route config. This ensures that route with nested
102
- // routes are always placed last.
103
- (0, routeConfig_1.sortConfig)(pluginsRouteConfigs, context.siteConfig.baseUrl);
104
- return { plugins: loadedPlugins, pluginsRouteConfigs, globalData };
105
- }
106
- exports.loadPlugins = loadPlugins;