@docusaurus/core 2.0.0-beta.1decd6f80 → 2.0.0-beta.20

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 (220) hide show
  1. package/bin/beforeCli.mjs +138 -0
  2. package/bin/docusaurus.mjs +228 -0
  3. package/lib/babel/preset.d.ts +8 -3
  4. package/lib/babel/preset.js +8 -7
  5. package/lib/client/.eslintrc.js +2 -4
  6. package/lib/client/App.d.ts +2 -3
  7. package/lib/client/App.js +30 -27
  8. package/lib/client/BaseUrlIssueBanner/index.d.ts +27 -0
  9. package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.js → BaseUrlIssueBanner/index.js} +24 -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 +39 -72
  17. package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.d.ts → SiteMetadataDefaults.d.ts} +1 -2
  18. package/lib/client/SiteMetadataDefaults.js +19 -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 +11 -5
  23. package/lib/client/docusaurus.js +29 -42
  24. package/lib/client/docusaurusContext.d.ts +12 -0
  25. package/lib/client/docusaurusContext.js +25 -0
  26. package/lib/client/exports/BrowserOnly.d.ts +3 -5
  27. package/lib/client/exports/BrowserOnly.js +14 -7
  28. package/lib/client/exports/ComponentCreator.d.ts +1 -2
  29. package/lib/client/exports/ComponentCreator.js +67 -41
  30. package/lib/client/exports/ErrorBoundary.d.ts +18 -0
  31. package/lib/client/exports/ErrorBoundary.js +35 -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 +44 -38
  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 +9 -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 +11 -31
  61. package/lib/client/preload.d.ts +3 -4
  62. package/lib/client/preload.js +7 -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 +91 -132
  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 +49 -115
  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 +77 -61
  81. package/lib/commands/clear.d.ts +7 -1
  82. package/lib/commands/clear.js +32 -20
  83. package/lib/commands/deploy.d.ts +5 -2
  84. package/lib/commands/deploy.js +91 -71
  85. package/lib/commands/external.d.ts +2 -2
  86. package/lib/commands/external.js +6 -11
  87. package/lib/commands/serve.d.ts +7 -2
  88. package/lib/commands/serve.js +31 -18
  89. package/lib/commands/start.d.ts +8 -2
  90. package/lib/commands/start.js +105 -91
  91. package/lib/commands/swizzle/actions.d.ts +23 -0
  92. package/lib/commands/swizzle/actions.js +102 -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 +83 -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 +116 -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 +21 -81
  111. package/lib/commands/writeTranslations.d.ts +4 -5
  112. package/lib/commands/writeTranslations.js +41 -23
  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 +65 -54
  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 +14 -6
  121. package/lib/server/configValidation.d.ts +4 -2
  122. package/lib/server/configValidation.js +96 -39
  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 +19 -55
  129. package/lib/server/index.d.ts +28 -6
  130. package/lib/server/index.js +82 -171
  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 +10 -10
  134. package/lib/server/plugins/index.js +74 -125
  135. package/lib/server/plugins/init.d.ts +6 -9
  136. package/lib/server/plugins/init.js +43 -108
  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 +6 -2
  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 +112 -0
  147. package/lib/server/routes.d.ts +39 -7
  148. package/lib/server/routes.js +185 -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 +13 -17
  152. package/lib/server/translations/translations.js +37 -61
  153. package/lib/server/translations/translationsExtractor.d.ts +10 -5
  154. package/lib/server/translations/translationsExtractor.js +159 -121
  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 +53 -55
  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 +8 -8
  166. package/lib/webpack/plugins/CleanWebpackPlugin.js +10 -16
  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 +18 -8
  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 +5 -4
  176. package/lib/webpack/utils.d.ts +16 -31
  177. package/lib/webpack/utils.js +59 -177
  178. package/package.json +77 -73
  179. package/bin/docusaurus.js +0 -326
  180. package/lib/.tsbuildinfo +0 -5682
  181. package/lib/choosePort.js +0 -105
  182. package/lib/client/.tsbuildinfo +0 -4127
  183. package/lib/client/client-lifecycles-dispatcher.d.ts +0 -12
  184. package/lib/client/client-lifecycles-dispatcher.js +0 -27
  185. package/lib/client/nprogress.css +0 -36
  186. package/lib/commands/buildRemoteBranchUrl.d.ts +0 -7
  187. package/lib/commands/buildRemoteBranchUrl.js +0 -27
  188. package/lib/commands/commandUtils.d.ts +0 -3
  189. package/lib/commands/commandUtils.js +0 -21
  190. package/lib/commands/swizzle.d.ts +0 -9
  191. package/lib/commands/swizzle.js +0 -245
  192. package/lib/constants.d.ts +0 -18
  193. package/lib/constants.js +0 -23
  194. package/lib/server/client-modules/index.js +0 -14
  195. package/lib/server/duplicateRoutes.d.ts +0 -10
  196. package/lib/server/duplicateRoutes.js +0 -38
  197. package/lib/server/html-tags/htmlTags.js +0 -39
  198. package/lib/server/html-tags/index.d.ts +0 -9
  199. package/lib/server/html-tags/index.js +0 -43
  200. package/lib/server/loadSetup.d.ts +0 -9
  201. package/lib/server/loadSetup.js +0 -25
  202. package/lib/server/presets/index.d.ts +0 -11
  203. package/lib/server/presets/index.js +0 -48
  204. package/lib/server/themes/alias.d.ts +0 -8
  205. package/lib/server/themes/alias.js +0 -39
  206. package/lib/server/themes/index.d.ts +0 -8
  207. package/lib/server/themes/index.js +0 -35
  208. package/lib/server/versions/__fixtures__/dummy-plugin.js +0 -0
  209. package/lib/server/versions/__fixtures__/package.json +0 -3
  210. package/lib/server/versions/__tests/index.test.js +0 -25
  211. package/lib/server/versions/index.d.ts +0 -10
  212. package/lib/server/versions/index.js +0 -50
  213. package/lib/webpack/react-dev-utils-webpack5/README.md +0 -11
  214. package/lib/webpack/react-dev-utils-webpack5/evalSourceMapMiddleware.js +0 -57
  215. package/lib/webpack/react-dev-utils-webpack5/formatWebpackMessages.js +0 -138
  216. package/lib/webpack/react-dev-utils-webpack5/webpackHotDevClient.js +0 -285
  217. package/lib/webpack/sharedModuleAliases.d.ts +0 -10
  218. package/lib/webpack/sharedModuleAliases.js +0 -18
  219. package/tsconfig.client.json +0 -13
  220. package/tsconfig.json +0 -13
@@ -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 { LoadedPlugin, PluginVersionInformation, SiteMetadata } from '@docusaurus/types';
8
+ export declare function getPluginVersion(pluginPath: string, siteDir: string): Promise<PluginVersionInformation>;
9
+ export declare function loadSiteMetadata({ plugins, siteDir, }: {
10
+ plugins: LoadedPlugin[];
11
+ siteDir: string;
12
+ }): Promise<SiteMetadata>;
@@ -0,0 +1,81 @@
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.loadSiteMetadata = exports.getPluginVersion = void 0;
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
+ const path_1 = tslib_1.__importDefault(require("path"));
14
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
15
+ async function getPackageJsonVersion(packageJsonPath) {
16
+ if (await fs_extra_1.default.pathExists(packageJsonPath)) {
17
+ // eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-dynamic-require, global-require
18
+ return require(packageJsonPath).version;
19
+ }
20
+ return undefined;
21
+ }
22
+ async function getPackageJsonName(packageJsonPath) {
23
+ // eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-dynamic-require, global-require
24
+ return require(packageJsonPath).name;
25
+ }
26
+ async function getPluginVersion(pluginPath, siteDir) {
27
+ let potentialPluginPackageJsonDirectory = path_1.default.dirname(pluginPath);
28
+ while (potentialPluginPackageJsonDirectory !== '/') {
29
+ const packageJsonPath = path_1.default.join(potentialPluginPackageJsonDirectory, 'package.json');
30
+ if ((await fs_extra_1.default.pathExists(packageJsonPath)) &&
31
+ (await fs_extra_1.default.lstat(packageJsonPath)).isFile()) {
32
+ if (potentialPluginPackageJsonDirectory === siteDir) {
33
+ // If the plugin belongs to the same docusaurus project, we classify it
34
+ // as local plugin.
35
+ return { type: 'project' };
36
+ }
37
+ return {
38
+ type: 'package',
39
+ name: await getPackageJsonName(packageJsonPath),
40
+ version: await getPackageJsonVersion(packageJsonPath),
41
+ };
42
+ }
43
+ potentialPluginPackageJsonDirectory = path_1.default.dirname(potentialPluginPackageJsonDirectory);
44
+ }
45
+ // In the case where a plugin is a path where no parent directory contains
46
+ // package.json, we can only classify it as local. Could happen if one puts a
47
+ // script in the parent directory of the site.
48
+ return { type: 'local' };
49
+ }
50
+ exports.getPluginVersion = getPluginVersion;
51
+ /**
52
+ * We want all `@docusaurus/*` packages to have the exact same version!
53
+ * @see https://github.com/facebook/docusaurus/issues/3371
54
+ * @see https://github.com/facebook/docusaurus/pull/3386
55
+ */
56
+ function checkDocusaurusPackagesVersion(siteMetadata) {
57
+ const { docusaurusVersion } = siteMetadata;
58
+ Object.entries(siteMetadata.pluginVersions).forEach(([plugin, versionInfo]) => {
59
+ if (versionInfo.type === 'package' &&
60
+ versionInfo.name?.startsWith('@docusaurus/') &&
61
+ versionInfo.version &&
62
+ versionInfo.version !== docusaurusVersion) {
63
+ // Should we throw instead? It still could work with different versions
64
+ logger_1.default.error `Invalid name=${plugin} version number=${versionInfo.version}.
65
+ All official @docusaurus/* packages should have the exact same version as @docusaurus/core (number=${docusaurusVersion}).
66
+ Maybe you want to check, or regenerate your yarn.lock or package-lock.json file?`;
67
+ }
68
+ });
69
+ }
70
+ async function loadSiteMetadata({ plugins, siteDir, }) {
71
+ const siteMetadata = {
72
+ docusaurusVersion: utils_1.DOCUSAURUS_VERSION,
73
+ siteVersion: await getPackageJsonVersion(path_1.default.join(siteDir, 'package.json')),
74
+ pluginVersions: Object.fromEntries(plugins
75
+ .filter(({ version: { type } }) => type !== 'synthetic')
76
+ .map(({ name, version }) => [name, version])),
77
+ };
78
+ checkDocusaurusPackagesVersion(siteMetadata);
79
+ return siteMetadata;
80
+ }
81
+ exports.loadSiteMetadata = loadSiteMetadata;
@@ -1,5 +1,10 @@
1
- import { InitPlugin } from '../plugins/init';
2
- import { TranslationFileContent, TranslationFile, TranslationMessage } 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, CodeTranslations, InitializedPlugin } from '@docusaurus/types';
3
8
  export declare type WriteTranslationsOptions = {
4
9
  override?: boolean;
5
10
  messagePrefix?: string;
@@ -8,30 +13,21 @@ declare type TranslationContext = {
8
13
  siteDir: string;
9
14
  locale: string;
10
15
  };
11
- export declare function ensureTranslationFileContent(content: unknown): asserts content is TranslationFileContent;
12
- export declare function readTranslationFileContent(filePath: string): Promise<TranslationFileContent | undefined>;
13
- export declare function writeTranslationFileContent({ filePath, content: newContent, options, }: {
14
- filePath: string;
15
- content: TranslationFileContent;
16
- options?: WriteTranslationsOptions;
17
- }): Promise<void>;
18
- export declare function getTranslationsDirPath(context: TranslationContext): string;
19
16
  export declare function getTranslationsLocaleDirPath(context: TranslationContext): string;
20
- export declare function getCodeTranslationsFilePath(context: TranslationContext): string;
21
17
  export declare function readCodeTranslationFileContent(context: TranslationContext): Promise<TranslationFileContent | undefined>;
22
18
  export declare function writeCodeTranslations(context: TranslationContext, content: TranslationFileContent, options: WriteTranslationsOptions): Promise<void>;
23
19
  export declare function writePluginTranslations({ siteDir, plugin, locale, translationFile, options, }: TranslationContext & {
24
- plugin: InitPlugin;
20
+ plugin: InitializedPlugin;
25
21
  translationFile: TranslationFile;
26
22
  options?: WriteTranslationsOptions;
27
23
  }): Promise<void>;
28
24
  export declare function localizePluginTranslationFile({ siteDir, plugin, locale, translationFile, }: TranslationContext & {
29
- plugin: InitPlugin;
25
+ plugin: InitializedPlugin;
30
26
  translationFile: TranslationFile;
31
27
  }): Promise<TranslationFile>;
32
- export declare function getPluginsDefaultCodeTranslationMessages(plugins: InitPlugin[]): Promise<Record<string, string>>;
28
+ export declare function getPluginsDefaultCodeTranslationMessages(plugins: InitializedPlugin[]): Promise<CodeTranslations>;
33
29
  export declare function applyDefaultCodeTranslations({ extractedCodeTranslations, defaultCodeMessages, }: {
34
- extractedCodeTranslations: Record<string, TranslationMessage>;
35
- defaultCodeMessages: Record<string, string>;
36
- }): Record<string, TranslationMessage>;
30
+ extractedCodeTranslations: TranslationFileContent;
31
+ defaultCodeMessages: CodeTranslations;
32
+ }): TranslationFileContent;
37
33
  export {};
@@ -1,19 +1,19 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.applyDefaultCodeTranslations = exports.getPluginsDefaultCodeTranslationMessages = exports.localizePluginTranslationFile = exports.writePluginTranslations = exports.writeCodeTranslations = exports.readCodeTranslationFileContent = exports.getCodeTranslationsFilePath = exports.getTranslationsLocaleDirPath = exports.getTranslationsDirPath = exports.writeTranslationFileContent = exports.readTranslationFileContent = exports.ensureTranslationFileContent = void 0;
4
- const tslib_1 = require("tslib");
5
2
  /**
6
3
  * Copyright (c) Facebook, Inc. and its affiliates.
7
4
  *
8
5
  * This source code is licensed under the MIT license found in the
9
6
  * LICENSE file in the root directory of this source tree.
10
7
  */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.applyDefaultCodeTranslations = exports.getPluginsDefaultCodeTranslationMessages = exports.localizePluginTranslationFile = exports.writePluginTranslations = exports.writeCodeTranslations = exports.readCodeTranslationFileContent = exports.getTranslationsLocaleDirPath = void 0;
10
+ const tslib_1 = require("tslib");
11
11
  const path_1 = tslib_1.__importDefault(require("path"));
12
12
  const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
13
- const lodash_1 = require("lodash");
13
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
14
14
  const utils_1 = require("@docusaurus/utils");
15
15
  const utils_validation_1 = require("@docusaurus/utils-validation");
16
- const chalk_1 = tslib_1.__importDefault(require("chalk"));
16
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
17
17
  const TranslationFileContentSchema = utils_validation_1.Joi.object()
18
18
  .pattern(utils_validation_1.Joi.string(), utils_validation_1.Joi.object({
19
19
  message: utils_validation_1.Joi.string().allow('').required(),
@@ -27,40 +27,35 @@ function ensureTranslationFileContent(content) {
27
27
  convert: false,
28
28
  });
29
29
  }
30
- exports.ensureTranslationFileContent = ensureTranslationFileContent;
31
30
  async function readTranslationFileContent(filePath) {
32
31
  if (await fs_extra_1.default.pathExists(filePath)) {
33
32
  try {
34
- const content = JSON.parse(await fs_extra_1.default.readFile(filePath, 'utf8'));
33
+ const content = await fs_extra_1.default.readJSON(filePath);
35
34
  ensureTranslationFileContent(content);
36
35
  return content;
37
36
  }
38
- catch (e) {
39
- throw new Error(`Invalid translation file at path=${filePath}.\n${e.message}`);
37
+ catch (err) {
38
+ logger_1.default.error `Invalid translation file at path=${filePath}.`;
39
+ throw err;
40
40
  }
41
41
  }
42
42
  return undefined;
43
43
  }
44
- exports.readTranslationFileContent = readTranslationFileContent;
45
44
  function mergeTranslationFileContent({ existingContent = {}, newContent, options, }) {
46
45
  // Apply messagePrefix to all messages
47
- const newContentTransformed = lodash_1.mapValues(newContent, (value) => {
48
- var _a;
49
- return ({
50
- ...value,
51
- message: `${(_a = options.messagePrefix) !== null && _a !== void 0 ? _a : ''}${value.message}`,
52
- });
53
- });
46
+ const newContentTransformed = lodash_1.default.mapValues(newContent, (value) => ({
47
+ ...value,
48
+ message: `${options.messagePrefix ?? ''}${value.message}`,
49
+ }));
54
50
  const result = { ...existingContent };
55
51
  // We only add missing keys here, we don't delete existing ones
56
52
  Object.entries(newContentTransformed).forEach(([key, { message, description }]) => {
57
- var _a, _b;
58
53
  result[key] = {
59
- // If the messages already exist, we don't override them (unless requested)
54
+ // If messages already exist, we don't override them (unless requested)
60
55
  message: options.override
61
56
  ? message
62
- : (_b = (_a = existingContent[key]) === null || _a === void 0 ? void 0 : _a.message) !== null && _b !== void 0 ? _b : message,
63
- description, // description
57
+ : existingContent[key]?.message ?? message,
58
+ description,
64
59
  };
65
60
  });
66
61
  return result;
@@ -68,11 +63,10 @@ function mergeTranslationFileContent({ existingContent = {}, newContent, options
68
63
  async function writeTranslationFileContent({ filePath, content: newContent, options = {}, }) {
69
64
  const existingContent = await readTranslationFileContent(filePath);
70
65
  // Warn about potential legacy keys
71
- const unknownKeys = lodash_1.difference(Object.keys(existingContent !== null && existingContent !== void 0 ? existingContent : {}), Object.keys(newContent));
66
+ const unknownKeys = lodash_1.default.difference(Object.keys(existingContent ?? {}), Object.keys(newContent));
72
67
  if (unknownKeys.length > 0) {
73
- console.warn(chalk_1.default.yellow(`Some translation keys looks unknown to us in file ${filePath}
74
- Maybe you should remove them?
75
- - ${unknownKeys.join('\n- ')}`));
68
+ logger_1.default.warn `Some translation keys looks unknown to us in file path=${filePath}.
69
+ Maybe you should remove them? ${unknownKeys}`;
76
70
  }
77
71
  const mergedContent = mergeTranslationFileContent({
78
72
  existingContent,
@@ -81,27 +75,18 @@ Maybe you should remove them?
81
75
  });
82
76
  // Avoid creating empty translation files
83
77
  if (Object.keys(mergedContent).length > 0) {
84
- console.log(`${Object.keys(mergedContent)
85
- .length.toString()
86
- .padStart(3, ' ')} translations will be written at ${utils_1.toMessageRelativeFilePath(filePath)}`);
87
- await fs_extra_1.default.ensureDir(path_1.default.dirname(filePath));
88
- await fs_extra_1.default.writeFile(filePath, JSON.stringify(mergedContent, null, 2));
78
+ logger_1.default.info `number=${Object.keys(mergedContent).length} translations will be written at path=${(0, utils_1.toMessageRelativeFilePath)(filePath)}.`;
79
+ await fs_extra_1.default.outputFile(filePath, `${JSON.stringify(mergedContent, null, 2)}\n`);
89
80
  }
90
81
  }
91
- exports.writeTranslationFileContent = writeTranslationFileContent;
92
- // should we make this configurable?
93
- function getTranslationsDirPath(context) {
94
- return path_1.default.resolve(path_1.default.join(context.siteDir, `i18n`));
95
- }
96
- exports.getTranslationsDirPath = getTranslationsDirPath;
82
+ // Should we make this configurable?
97
83
  function getTranslationsLocaleDirPath(context) {
98
- return path_1.default.join(getTranslationsDirPath(context), context.locale);
84
+ return path_1.default.join(context.siteDir, utils_1.I18N_DIR_NAME, context.locale);
99
85
  }
100
86
  exports.getTranslationsLocaleDirPath = getTranslationsLocaleDirPath;
101
87
  function getCodeTranslationsFilePath(context) {
102
- return path_1.default.join(getTranslationsLocaleDirPath(context), 'code.json');
88
+ return path_1.default.join(getTranslationsLocaleDirPath(context), utils_1.CODE_TRANSLATIONS_FILE_NAME);
103
89
  }
104
- exports.getCodeTranslationsFilePath = getCodeTranslationsFilePath;
105
90
  async function readCodeTranslationFileContent(context) {
106
91
  return readTranslationFileContent(getCodeTranslationsFilePath(context));
107
92
  }
@@ -119,12 +104,12 @@ exports.writeCodeTranslations = writeCodeTranslations;
119
104
  // (json/yaml/toml/xml...)
120
105
  function addTranslationFileExtension(translationFilePath) {
121
106
  if (translationFilePath.endsWith('.json')) {
122
- throw new Error(`Translation file path does not need to end with .json, we addt the extension automatically. translationFilePath=${translationFilePath}`);
107
+ throw new Error(`Translation file path at "${translationFilePath}" does not need to end with ".json", we add the extension automatically.`);
123
108
  }
124
109
  return `${translationFilePath}.json`;
125
110
  }
126
111
  function getPluginTranslationFilePath({ siteDir, plugin, locale, translationFilePath, }) {
127
- const dirPath = utils_1.getPluginI18nPath({
112
+ const dirPath = (0, utils_1.getPluginI18nPath)({
128
113
  siteDir,
129
114
  locale,
130
115
  pluginName: plugin.name,
@@ -156,7 +141,7 @@ async function localizePluginTranslationFile({ siteDir, plugin, locale, translat
156
141
  });
157
142
  const localizedContent = await readTranslationFileContent(filePath);
158
143
  if (localizedContent) {
159
- // localized messages "override" default unlocalized messages
144
+ // Localized messages "override" default unlocalized messages
160
145
  return {
161
146
  path: translationFile.path,
162
147
  content: {
@@ -165,32 +150,23 @@ async function localizePluginTranslationFile({ siteDir, plugin, locale, translat
165
150
  },
166
151
  };
167
152
  }
168
- else {
169
- return translationFile;
170
- }
153
+ return translationFile;
171
154
  }
172
155
  exports.localizePluginTranslationFile = localizePluginTranslationFile;
173
156
  async function getPluginsDefaultCodeTranslationMessages(plugins) {
174
- const pluginsMessages = await Promise.all(plugins.map((plugin) => { var _a, _b; return (_b = (_a = plugin.getDefaultCodeTranslationMessages) === null || _a === void 0 ? void 0 : _a.call(plugin)) !== null && _b !== void 0 ? _b : {}; }));
175
- return pluginsMessages.reduce((allMessages, pluginMessages) => {
176
- return { ...allMessages, ...pluginMessages };
177
- }, {});
157
+ const pluginsMessages = await Promise.all(plugins.map((plugin) => plugin.getDefaultCodeTranslationMessages?.() ?? {}));
158
+ return pluginsMessages.reduce((allMessages, pluginMessages) => ({ ...allMessages, ...pluginMessages }), {});
178
159
  }
179
160
  exports.getPluginsDefaultCodeTranslationMessages = getPluginsDefaultCodeTranslationMessages;
180
161
  function applyDefaultCodeTranslations({ extractedCodeTranslations, defaultCodeMessages, }) {
181
- const unusedDefaultCodeMessages = lodash_1.difference(Object.keys(defaultCodeMessages), Object.keys(extractedCodeTranslations));
162
+ const unusedDefaultCodeMessages = lodash_1.default.difference(Object.keys(defaultCodeMessages), Object.keys(extractedCodeTranslations));
182
163
  if (unusedDefaultCodeMessages.length > 0) {
183
- console.warn(chalk_1.default.yellow(`Unused default message codes found.
184
- Please report this Docusaurus issue.
185
- - ${unusedDefaultCodeMessages.join('\n- ')}
186
- `));
164
+ logger_1.default.warn `Unused default message codes found.
165
+ Please report this Docusaurus issue. name=${unusedDefaultCodeMessages}`;
187
166
  }
188
- return lodash_1.mapValues(extractedCodeTranslations, (messageTranslation, messageId) => {
189
- var _a;
190
- return {
191
- ...messageTranslation,
192
- message: (_a = defaultCodeMessages[messageId]) !== null && _a !== void 0 ? _a : messageTranslation.message,
193
- };
194
- });
167
+ return lodash_1.default.mapValues(extractedCodeTranslations, (messageTranslation, messageId) => ({
168
+ ...messageTranslation,
169
+ message: defaultCodeMessages[messageId] ?? messageTranslation.message,
170
+ }));
195
171
  }
196
172
  exports.applyDefaultCodeTranslations = applyDefaultCodeTranslations;
@@ -1,11 +1,16 @@
1
- import { TransformOptions } from '@babel/core';
2
- import { TranslationFileContent, TranslationMessage } from '@docusaurus/types';
3
- import { InitPlugin } from '../plugins/init';
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 TransformOptions } from '@babel/core';
8
+ import type { InitializedPlugin, TranslationFileContent } from '@docusaurus/types';
4
9
  export declare function globSourceCodeFilePaths(dirPaths: string[]): Promise<string[]>;
5
- export declare function extractSiteSourceCodeTranslations(siteDir: string, plugins: InitPlugin[], babelOptions: TransformOptions): Promise<TranslationFileContent>;
10
+ export declare function extractSiteSourceCodeTranslations(siteDir: string, plugins: InitializedPlugin[], babelOptions: TransformOptions, extraSourceCodeFilePaths?: string[]): Promise<TranslationFileContent>;
6
11
  declare type SourceCodeFileTranslations = {
7
12
  sourceCodeFilePath: string;
8
- translations: Record<string, TranslationMessage>;
13
+ translations: TranslationFileContent;
9
14
  warnings: string[];
10
15
  };
11
16
  export declare function extractAllSourceCodeFileTranslations(sourceCodeFilePaths: string[], babelOptions: TransformOptions): Promise<SourceCodeFileTranslations[]>;