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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/README.md +1 -0
  2. package/bin/beforeCli.mjs +140 -0
  3. package/bin/docusaurus.mjs +221 -0
  4. package/lib/babel/preset.d.ts +8 -3
  5. package/lib/babel/preset.js +10 -8
  6. package/lib/client/App.d.ts +2 -3
  7. package/lib/client/App.js +31 -28
  8. package/lib/client/BaseUrlIssueBanner/index.d.ts +27 -0
  9. package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.js → BaseUrlIssueBanner/index.js} +25 -14
  10. package/lib/client/{baseUrlIssueBanner → BaseUrlIssueBanner}/styles.module.css +0 -0
  11. package/lib/client/ClientLifecyclesDispatcher.d.ts +16 -0
  12. package/lib/client/ClientLifecyclesDispatcher.js +39 -0
  13. package/lib/client/LinksCollector.d.ts +3 -3
  14. package/lib/client/LinksCollector.js +8 -13
  15. package/lib/client/PendingNavigation.d.ts +17 -3
  16. package/lib/client/PendingNavigation.js +47 -72
  17. package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.d.ts → SiteMetadataDefaults.d.ts} +1 -2
  18. package/lib/client/SiteMetadataDefaults.js +21 -0
  19. package/lib/{choosePort.d.ts → client/browserContext.d.ts} +5 -5
  20. package/lib/client/browserContext.js +22 -0
  21. package/lib/client/clientEntry.js +14 -9
  22. package/lib/client/docusaurus.d.ts +12 -6
  23. package/lib/client/docusaurus.js +30 -43
  24. package/lib/client/docusaurusContext.d.ts +12 -0
  25. package/lib/client/docusaurusContext.js +25 -0
  26. package/lib/client/exports/BrowserOnly.d.ts +3 -5
  27. package/lib/client/exports/BrowserOnly.js +14 -8
  28. package/lib/client/exports/ComponentCreator.d.ts +6 -2
  29. package/lib/client/exports/ComponentCreator.js +75 -42
  30. package/lib/client/exports/ErrorBoundary.d.ts +18 -0
  31. package/lib/client/exports/ErrorBoundary.js +37 -0
  32. package/lib/client/exports/Head.d.ts +2 -3
  33. package/lib/client/exports/Head.js +3 -4
  34. package/lib/client/exports/Interpolate.d.ts +2 -2
  35. package/lib/client/exports/Interpolate.js +20 -49
  36. package/lib/client/exports/Link.d.ts +4 -10
  37. package/lib/client/exports/Link.js +40 -40
  38. package/lib/client/exports/Translate.d.ts +1 -1
  39. package/lib/client/exports/Translate.js +14 -9
  40. package/lib/client/exports/constants.js +1 -11
  41. package/lib/client/exports/isInternalUrl.js +1 -1
  42. package/lib/client/exports/renderRoutes.d.ts +1 -2
  43. package/lib/client/exports/renderRoutes.js +1 -2
  44. package/lib/client/exports/router.d.ts +1 -1
  45. package/lib/client/exports/router.js +1 -1
  46. package/lib/client/exports/useBaseUrl.js +11 -14
  47. package/lib/client/exports/useDocusaurusContext.d.ts +2 -3
  48. package/lib/client/exports/useDocusaurusContext.js +3 -9
  49. package/lib/client/exports/useGlobalData.d.ts +4 -3
  50. package/lib/client/exports/useGlobalData.js +6 -13
  51. package/lib/{server/versions/__tests/index.test.d.ts → client/exports/useIsBrowser.d.ts} +1 -1
  52. package/lib/client/exports/useIsBrowser.js +11 -0
  53. package/lib/client/exports/useRouteContext.d.ts +8 -0
  54. package/lib/client/exports/useRouteContext.js +15 -0
  55. package/lib/client/flat.d.ts +12 -2
  56. package/lib/client/flat.js +19 -15
  57. package/lib/client/normalizeLocation.d.ts +2 -5
  58. package/lib/client/normalizeLocation.js +14 -10
  59. package/lib/client/prefetch.d.ts +1 -2
  60. package/lib/client/prefetch.js +12 -32
  61. package/lib/client/preload.d.ts +3 -4
  62. package/lib/client/preload.js +5 -12
  63. package/lib/client/routeContext.d.ts +13 -0
  64. package/lib/client/routeContext.js +31 -0
  65. package/lib/client/serverEntry.d.ts +10 -0
  66. package/lib/client/serverEntry.js +108 -146
  67. package/lib/client/theme-fallback/Error/index.d.ts +10 -0
  68. package/lib/client/theme-fallback/Error/index.js +45 -0
  69. package/lib/client/theme-fallback/Layout/index.d.ts +10 -0
  70. package/lib/client/theme-fallback/Layout/index.js +2 -26
  71. package/lib/client/theme-fallback/Loading/index.d.ts +9 -0
  72. package/lib/client/theme-fallback/Loading/index.js +50 -116
  73. package/lib/client/{exports/context.js → theme-fallback/NotFound/index.d.ts} +2 -2
  74. package/lib/client/theme-fallback/NotFound/index.js +19 -18
  75. package/lib/client/theme-fallback/Root/index.d.ts +10 -0
  76. package/lib/client/theme-fallback/Root/index.js +2 -6
  77. package/lib/{server/html-tags/htmlTags.d.ts → client/theme-fallback/SiteMetadata/index.d.ts} +2 -1
  78. package/lib/client/theme-fallback/SiteMetadata/index.js +10 -0
  79. package/lib/commands/build.d.ts +6 -2
  80. package/lib/commands/build.js +81 -64
  81. package/lib/commands/clear.d.ts +7 -1
  82. package/lib/commands/clear.js +34 -21
  83. package/lib/commands/deploy.d.ts +5 -2
  84. package/lib/commands/deploy.js +104 -81
  85. package/lib/commands/external.d.ts +2 -2
  86. package/lib/commands/external.js +9 -11
  87. package/lib/commands/serve.d.ts +8 -2
  88. package/lib/commands/serve.js +29 -28
  89. package/lib/commands/start.d.ts +9 -2
  90. package/lib/commands/start.js +109 -101
  91. package/lib/commands/swizzle/actions.d.ts +23 -0
  92. package/lib/commands/swizzle/actions.js +101 -0
  93. package/lib/commands/swizzle/common.d.ts +33 -0
  94. package/lib/commands/swizzle/common.js +56 -0
  95. package/lib/commands/swizzle/components.d.ts +29 -0
  96. package/lib/commands/swizzle/components.js +200 -0
  97. package/lib/commands/swizzle/config.d.ts +10 -0
  98. package/lib/commands/swizzle/config.js +84 -0
  99. package/lib/{server/client-modules/index.d.ts → commands/swizzle/context.d.ts} +2 -2
  100. package/lib/commands/swizzle/context.js +24 -0
  101. package/lib/commands/swizzle/index.d.ts +8 -0
  102. package/lib/commands/swizzle/index.js +118 -0
  103. package/lib/commands/swizzle/prompts.d.ts +12 -0
  104. package/lib/commands/swizzle/prompts.js +110 -0
  105. package/lib/{client/exports/context.d.ts → commands/swizzle/tables.d.ts} +3 -4
  106. package/lib/commands/swizzle/tables.js +113 -0
  107. package/lib/commands/swizzle/themes.d.ts +20 -0
  108. package/lib/commands/swizzle/themes.js +106 -0
  109. package/lib/commands/writeHeadingIds.d.ts +2 -6
  110. package/lib/commands/writeHeadingIds.js +22 -81
  111. package/lib/commands/writeTranslations.d.ts +4 -5
  112. package/lib/commands/writeTranslations.js +46 -25
  113. package/lib/index.d.ts +9 -9
  114. package/lib/index.js +14 -14
  115. package/lib/server/brokenLinks.d.ts +4 -17
  116. package/lib/server/brokenLinks.js +67 -56
  117. package/lib/server/clientModules.d.ts +12 -0
  118. package/lib/server/clientModules.js +20 -0
  119. package/lib/server/config.d.ts +5 -2
  120. package/lib/server/config.js +29 -6
  121. package/lib/server/configValidation.d.ts +4 -4
  122. package/lib/server/configValidation.js +86 -41
  123. package/lib/server/getHostPort.d.ts +14 -0
  124. package/lib/server/getHostPort.js +79 -0
  125. package/lib/server/htmlTags.d.ts +12 -0
  126. package/lib/server/htmlTags.js +62 -0
  127. package/lib/server/i18n.d.ts +3 -13
  128. package/lib/server/i18n.js +21 -55
  129. package/lib/server/index.d.ts +28 -13
  130. package/lib/server/index.js +83 -165
  131. package/lib/server/plugins/configs.d.ts +51 -0
  132. package/lib/server/plugins/configs.js +101 -0
  133. package/lib/server/plugins/index.d.ts +9 -8
  134. package/lib/server/plugins/index.js +73 -134
  135. package/lib/server/plugins/init.d.ts +6 -5
  136. package/lib/server/plugins/init.js +44 -109
  137. package/lib/server/plugins/moduleShorthand.d.ts +9 -0
  138. package/lib/server/plugins/moduleShorthand.js +46 -0
  139. package/lib/server/plugins/pluginIds.d.ts +5 -1
  140. package/lib/server/plugins/pluginIds.js +12 -7
  141. package/lib/server/plugins/presets.d.ts +12 -0
  142. package/lib/server/plugins/presets.js +49 -0
  143. package/lib/server/plugins/routeConfig.d.ts +11 -0
  144. package/lib/server/plugins/routeConfig.js +54 -0
  145. package/lib/server/plugins/synthetic.d.ts +20 -0
  146. package/lib/server/plugins/synthetic.js +111 -0
  147. package/lib/server/routes.d.ts +39 -7
  148. package/lib/server/routes.js +182 -95
  149. package/lib/server/siteMetadata.d.ts +12 -0
  150. package/lib/server/siteMetadata.js +81 -0
  151. package/lib/server/translations/translations.d.ts +14 -19
  152. package/lib/server/translations/translations.js +40 -72
  153. package/lib/server/translations/translationsExtractor.d.ts +10 -4
  154. package/lib/server/translations/translationsExtractor.js +158 -122
  155. package/lib/server/utils.d.ts +9 -3
  156. package/lib/server/utils.js +7 -9
  157. package/lib/webpack/aliases/index.d.ts +34 -0
  158. package/lib/webpack/aliases/index.js +106 -0
  159. package/lib/webpack/base.d.ts +3 -4
  160. package/lib/webpack/base.js +45 -57
  161. package/lib/webpack/client.d.ts +3 -3
  162. package/lib/webpack/client.js +12 -19
  163. package/lib/webpack/plugins/ChunkAssetPlugin.d.ts +13 -3
  164. package/lib/webpack/plugins/ChunkAssetPlugin.js +24 -17
  165. package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +6 -25
  166. package/lib/webpack/plugins/CleanWebpackPlugin.js +33 -17
  167. package/lib/webpack/plugins/LogPlugin.d.ts +1 -1
  168. package/lib/webpack/plugins/LogPlugin.js +4 -5
  169. package/lib/webpack/plugins/WaitPlugin.d.ts +3 -3
  170. package/lib/webpack/plugins/WaitPlugin.js +1 -1
  171. package/lib/webpack/server.d.ts +5 -5
  172. package/lib/webpack/server.js +22 -9
  173. package/lib/{client → webpack}/templates/index.html.template.ejs +0 -0
  174. package/lib/webpack/templates/ssr.html.template.d.ts +8 -0
  175. package/lib/{client → webpack}/templates/ssr.html.template.js +3 -2
  176. package/lib/webpack/utils.d.ts +15 -31
  177. package/lib/webpack/utils.js +61 -182
  178. package/package.json +81 -77
  179. package/bin/docusaurus.js +0 -325
  180. package/lib/.tsbuildinfo +0 -1
  181. package/lib/choosePort.js +0 -105
  182. package/lib/client/.eslintrc.js +0 -29
  183. package/lib/client/.tsbuildinfo +0 -1
  184. package/lib/client/client-lifecycles-dispatcher.d.ts +0 -12
  185. package/lib/client/client-lifecycles-dispatcher.js +0 -27
  186. package/lib/client/nprogress.css +0 -36
  187. package/lib/commands/buildRemoteBranchUrl.d.ts +0 -7
  188. package/lib/commands/buildRemoteBranchUrl.js +0 -27
  189. package/lib/commands/commandUtils.d.ts +0 -3
  190. package/lib/commands/commandUtils.js +0 -21
  191. package/lib/commands/swizzle.d.ts +0 -9
  192. package/lib/commands/swizzle.js +0 -245
  193. package/lib/constants.d.ts +0 -18
  194. package/lib/constants.js +0 -23
  195. package/lib/server/client-modules/index.js +0 -14
  196. package/lib/server/duplicateRoutes.d.ts +0 -10
  197. package/lib/server/duplicateRoutes.js +0 -38
  198. package/lib/server/html-tags/htmlTags.js +0 -40
  199. package/lib/server/html-tags/index.d.ts +0 -9
  200. package/lib/server/html-tags/index.js +0 -43
  201. package/lib/server/loadSetup.d.ts +0 -9
  202. package/lib/server/loadSetup.js +0 -25
  203. package/lib/server/plugins/applyRouteTrailingSlash.d.ts +0 -8
  204. package/lib/server/plugins/applyRouteTrailingSlash.js +0 -19
  205. package/lib/server/presets/index.d.ts +0 -11
  206. package/lib/server/presets/index.js +0 -48
  207. package/lib/server/themes/alias.d.ts +0 -8
  208. package/lib/server/themes/alias.js +0 -40
  209. package/lib/server/themes/index.d.ts +0 -12
  210. package/lib/server/themes/index.js +0 -47
  211. package/lib/server/versions/__fixtures__/dummy-plugin.js +0 -0
  212. package/lib/server/versions/__fixtures__/package.json +0 -3
  213. package/lib/server/versions/__tests/index.test.js +0 -25
  214. package/lib/server/versions/index.d.ts +0 -10
  215. package/lib/server/versions/index.js +0 -50
  216. package/lib/webpack/react-dev-utils-webpack5/README.md +0 -11
  217. package/lib/webpack/react-dev-utils-webpack5/evalSourceMapMiddleware.js +0 -57
  218. package/lib/webpack/react-dev-utils-webpack5/formatWebpackMessages.js +0 -138
  219. package/lib/webpack/react-dev-utils-webpack5/webpackHotDevClient.js +0 -285
  220. package/lib/webpack/sharedModuleAliases.d.ts +0 -10
  221. package/lib/webpack/sharedModuleAliases.js +0 -18
  222. package/tsconfig.client.json +0 -13
  223. package/tsconfig.json +0 -13
@@ -0,0 +1,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 fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
12
+ const path_1 = tslib_1.__importDefault(require("path"));
13
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
14
+ const utils_1 = require("@docusaurus/utils");
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,36 +1,31 @@
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, CodeTranslations, InitializedPlugin } from '@docusaurus/types';
2
8
  export declare type WriteTranslationsOptions = {
3
9
  override?: boolean;
4
10
  messagePrefix?: string;
5
11
  };
6
12
  declare type TranslationContext = {
7
- siteDir: string;
8
- locale: string;
13
+ localizationDir: string;
9
14
  };
10
- export declare function ensureTranslationFileContent(content: unknown): asserts content is TranslationFileContent;
11
- export declare function readTranslationFileContent(filePath: string): Promise<TranslationFileContent | undefined>;
12
- export declare function writeTranslationFileContent({ filePath, content: newContent, options, }: {
13
- filePath: string;
14
- content: TranslationFileContent;
15
- options?: WriteTranslationsOptions;
16
- }): Promise<void>;
17
- export declare function getTranslationsDirPath(context: TranslationContext): string;
18
- export declare function getTranslationsLocaleDirPath(context: TranslationContext): string;
19
- export declare function getCodeTranslationsFilePath(context: TranslationContext): string;
20
15
  export declare function readCodeTranslationFileContent(context: TranslationContext): Promise<TranslationFileContent | undefined>;
21
16
  export declare function writeCodeTranslations(context: TranslationContext, content: TranslationFileContent, options: WriteTranslationsOptions): Promise<void>;
22
- export declare function writePluginTranslations({ siteDir, plugin, locale, translationFile, options, }: TranslationContext & {
17
+ export declare function writePluginTranslations({ localizationDir, plugin, translationFile, options, }: TranslationContext & {
23
18
  plugin: InitializedPlugin;
24
19
  translationFile: TranslationFile;
25
20
  options?: WriteTranslationsOptions;
26
21
  }): Promise<void>;
27
- export declare function localizePluginTranslationFile({ siteDir, plugin, locale, translationFile, }: TranslationContext & {
22
+ export declare function localizePluginTranslationFile({ localizationDir, plugin, translationFile, }: TranslationContext & {
28
23
  plugin: InitializedPlugin;
29
24
  translationFile: TranslationFile;
30
25
  }): Promise<TranslationFile>;
31
- export declare function getPluginsDefaultCodeTranslationMessages(plugins: InitializedPlugin[]): Promise<Record<string, string>>;
26
+ export declare function getPluginsDefaultCodeTranslationMessages(plugins: InitializedPlugin[]): Promise<CodeTranslations>;
32
27
  export declare function applyDefaultCodeTranslations({ extractedCodeTranslations, defaultCodeMessages, }: {
33
- extractedCodeTranslations: Record<string, TranslationMessage>;
34
- defaultCodeMessages: Record<string, string>;
35
- }): Record<string, TranslationMessage>;
28
+ extractedCodeTranslations: TranslationFileContent;
29
+ defaultCodeMessages: CodeTranslations;
30
+ }): TranslationFileContent;
36
31
  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 = 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
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
14
15
  const utils_1 = require("@docusaurus/utils");
15
16
  const utils_validation_1 = require("@docusaurus/utils-validation");
16
- const chalk_1 = tslib_1.__importDefault(require("chalk"));
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 ${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,13 @@ 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;
97
- function getTranslationsLocaleDirPath(context) {
98
- return path_1.default.join(getTranslationsDirPath(context), context.locale);
99
- }
100
- exports.getTranslationsLocaleDirPath = getTranslationsLocaleDirPath;
101
82
  function getCodeTranslationsFilePath(context) {
102
- return path_1.default.join(getTranslationsLocaleDirPath(context), 'code.json');
83
+ return path_1.default.join(context.localizationDir, utils_1.CODE_TRANSLATIONS_FILE_NAME);
103
84
  }
104
- exports.getCodeTranslationsFilePath = getCodeTranslationsFilePath;
105
85
  async function readCodeTranslationFileContent(context) {
106
86
  return readTranslationFileContent(getCodeTranslationsFilePath(context));
107
87
  }
@@ -123,21 +103,19 @@ function addTranslationFileExtension(translationFilePath) {
123
103
  }
124
104
  return `${translationFilePath}.json`;
125
105
  }
126
- function getPluginTranslationFilePath({ siteDir, plugin, locale, translationFilePath, }) {
127
- const dirPath = utils_1.getPluginI18nPath({
128
- siteDir,
129
- locale,
106
+ function getPluginTranslationFilePath({ localizationDir, plugin, translationFilePath, }) {
107
+ const dirPath = (0, utils_1.getPluginI18nPath)({
108
+ localizationDir,
130
109
  pluginName: plugin.name,
131
110
  pluginId: plugin.options.id,
132
111
  });
133
112
  const filePath = addTranslationFileExtension(translationFilePath);
134
113
  return path_1.default.join(dirPath, filePath);
135
114
  }
136
- async function writePluginTranslations({ siteDir, plugin, locale, translationFile, options, }) {
115
+ async function writePluginTranslations({ localizationDir, plugin, translationFile, options, }) {
137
116
  const filePath = getPluginTranslationFilePath({
138
117
  plugin,
139
- siteDir,
140
- locale,
118
+ localizationDir,
141
119
  translationFilePath: translationFile.path,
142
120
  });
143
121
  await writeTranslationFileContent({
@@ -147,16 +125,15 @@ async function writePluginTranslations({ siteDir, plugin, locale, translationFil
147
125
  });
148
126
  }
149
127
  exports.writePluginTranslations = writePluginTranslations;
150
- async function localizePluginTranslationFile({ siteDir, plugin, locale, translationFile, }) {
128
+ async function localizePluginTranslationFile({ localizationDir, plugin, translationFile, }) {
151
129
  const filePath = getPluginTranslationFilePath({
152
130
  plugin,
153
- siteDir,
154
- locale,
131
+ localizationDir,
155
132
  translationFilePath: translationFile.path,
156
133
  });
157
134
  const localizedContent = await readTranslationFileContent(filePath);
158
135
  if (localizedContent) {
159
- // localized messages "override" default unlocalized messages
136
+ // Localized messages "override" default unlocalized messages
160
137
  return {
161
138
  path: translationFile.path,
162
139
  content: {
@@ -165,32 +142,23 @@ async function localizePluginTranslationFile({ siteDir, plugin, locale, translat
165
142
  },
166
143
  };
167
144
  }
168
- else {
169
- return translationFile;
170
- }
145
+ return translationFile;
171
146
  }
172
147
  exports.localizePluginTranslationFile = localizePluginTranslationFile;
173
148
  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
- }, {});
149
+ const pluginsMessages = await Promise.all(plugins.map((plugin) => plugin.getDefaultCodeTranslationMessages?.() ?? {}));
150
+ return pluginsMessages.reduce((allMessages, pluginMessages) => ({ ...allMessages, ...pluginMessages }), {});
178
151
  }
179
152
  exports.getPluginsDefaultCodeTranslationMessages = getPluginsDefaultCodeTranslationMessages;
180
153
  function applyDefaultCodeTranslations({ extractedCodeTranslations, defaultCodeMessages, }) {
181
- const unusedDefaultCodeMessages = lodash_1.difference(Object.keys(defaultCodeMessages), Object.keys(extractedCodeTranslations));
154
+ const unusedDefaultCodeMessages = lodash_1.default.difference(Object.keys(defaultCodeMessages), Object.keys(extractedCodeTranslations));
182
155
  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
- `));
156
+ logger_1.default.warn `Unused default message codes found.
157
+ Please report this Docusaurus issue. name=${unusedDefaultCodeMessages}`;
187
158
  }
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
- });
159
+ return lodash_1.default.mapValues(extractedCodeTranslations, (messageTranslation, messageId) => ({
160
+ ...messageTranslation,
161
+ message: defaultCodeMessages[messageId] ?? messageTranslation.message,
162
+ }));
195
163
  }
196
164
  exports.applyDefaultCodeTranslations = applyDefaultCodeTranslations;
@@ -1,10 +1,16 @@
1
- import { TransformOptions } from '@babel/core';
2
- import { InitializedPlugin, TranslationFileContent, 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 TransformOptions } from '@babel/core';
8
+ import type { InitializedPlugin, TranslationFileContent } from '@docusaurus/types';
3
9
  export declare function globSourceCodeFilePaths(dirPaths: string[]): Promise<string[]>;
4
- export declare function extractSiteSourceCodeTranslations(siteDir: string, plugins: InitializedPlugin[], babelOptions: TransformOptions): Promise<TranslationFileContent>;
10
+ export declare function extractSiteSourceCodeTranslations(siteDir: string, plugins: InitializedPlugin[], babelOptions: TransformOptions, extraSourceCodeFilePaths?: string[]): Promise<TranslationFileContent>;
5
11
  declare type SourceCodeFileTranslations = {
6
12
  sourceCodeFilePath: string;
7
- translations: Record<string, TranslationMessage>;
13
+ translations: TranslationFileContent;
8
14
  warnings: string[];
9
15
  };
10
16
  export declare function extractAllSourceCodeFileTranslations(sourceCodeFilePaths: string[], babelOptions: TransformOptions): Promise<SourceCodeFileTranslations[]>;