@docusaurus/core 2.0.0-beta.ff31de0ff → 2.0.1
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.
- package/README.md +1 -0
- package/bin/beforeCli.mjs +140 -0
- package/bin/docusaurus.mjs +223 -0
- package/lib/babel/preset.d.ts +8 -3
- package/lib/babel/preset.js +10 -8
- package/lib/client/App.d.ts +2 -3
- package/lib/client/App.js +31 -28
- package/lib/client/BaseUrlIssueBanner/index.d.ts +27 -0
- package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.js → BaseUrlIssueBanner/index.js} +25 -14
- package/lib/client/{baseUrlIssueBanner → BaseUrlIssueBanner}/styles.module.css +0 -0
- package/lib/client/ClientLifecyclesDispatcher.d.ts +16 -0
- package/lib/client/ClientLifecyclesDispatcher.js +39 -0
- package/lib/client/LinksCollector.d.ts +3 -3
- package/lib/client/LinksCollector.js +8 -13
- package/lib/client/PendingNavigation.d.ts +17 -3
- package/lib/client/PendingNavigation.js +47 -72
- package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.d.ts → SiteMetadataDefaults.d.ts} +1 -2
- package/lib/client/SiteMetadataDefaults.js +21 -0
- package/lib/{choosePort.d.ts → client/browserContext.d.ts} +5 -5
- package/lib/client/browserContext.js +22 -0
- package/lib/client/clientEntry.js +14 -9
- package/lib/client/docusaurus.d.ts +12 -6
- package/lib/client/docusaurus.js +30 -43
- package/lib/client/docusaurusContext.d.ts +12 -0
- package/lib/client/docusaurusContext.js +25 -0
- package/lib/client/exports/BrowserOnly.d.ts +3 -5
- package/lib/client/exports/BrowserOnly.js +14 -7
- package/lib/client/exports/ComponentCreator.d.ts +6 -2
- package/lib/client/exports/ComponentCreator.js +75 -42
- package/lib/client/exports/ErrorBoundary.d.ts +18 -0
- package/lib/client/exports/ErrorBoundary.js +37 -0
- package/lib/client/exports/ExecutionEnvironment.js +7 -8
- package/lib/client/exports/Head.d.ts +2 -3
- package/lib/client/exports/Head.js +3 -4
- package/lib/client/exports/Interpolate.d.ts +2 -2
- package/lib/client/exports/Interpolate.js +20 -49
- package/lib/client/exports/Link.d.ts +4 -10
- package/lib/client/exports/Link.js +44 -38
- package/lib/client/exports/Translate.d.ts +1 -1
- package/lib/client/exports/Translate.js +14 -9
- package/lib/client/exports/constants.js +1 -11
- package/lib/client/exports/isInternalUrl.js +1 -1
- package/lib/client/exports/renderRoutes.d.ts +1 -2
- package/lib/client/exports/renderRoutes.js +1 -2
- package/lib/client/exports/router.d.ts +1 -1
- package/lib/client/exports/router.js +1 -1
- package/lib/client/exports/useBaseUrl.js +11 -14
- package/lib/client/exports/useDocusaurusContext.d.ts +2 -3
- package/lib/client/exports/useDocusaurusContext.js +3 -9
- package/lib/client/exports/useGlobalData.d.ts +4 -3
- package/lib/client/exports/useGlobalData.js +8 -15
- package/lib/{server/versions/__tests/index.test.d.ts → client/exports/useIsBrowser.d.ts} +1 -1
- package/lib/client/exports/useIsBrowser.js +11 -0
- package/lib/client/exports/useRouteContext.d.ts +8 -0
- package/lib/client/exports/useRouteContext.js +15 -0
- package/lib/client/flat.d.ts +12 -2
- package/lib/client/flat.js +19 -15
- package/lib/client/normalizeLocation.d.ts +2 -5
- package/lib/client/normalizeLocation.js +14 -10
- package/lib/client/prefetch.d.ts +1 -2
- package/lib/client/prefetch.js +12 -32
- package/lib/client/preload.d.ts +3 -4
- package/lib/client/preload.js +5 -12
- package/lib/client/routeContext.d.ts +13 -0
- package/lib/client/routeContext.js +31 -0
- package/lib/client/serverEntry.d.ts +10 -0
- package/lib/client/serverEntry.js +109 -136
- package/lib/client/theme-fallback/Error/index.d.ts +10 -0
- package/lib/client/theme-fallback/Error/index.js +45 -0
- package/lib/client/theme-fallback/Layout/index.d.ts +10 -0
- package/lib/client/theme-fallback/Layout/index.js +2 -26
- package/lib/client/theme-fallback/Loading/index.d.ts +9 -0
- package/lib/client/theme-fallback/Loading/index.js +50 -116
- package/lib/client/{exports/context.js → theme-fallback/NotFound/index.d.ts} +2 -2
- package/lib/client/theme-fallback/NotFound/index.js +19 -18
- package/lib/client/theme-fallback/Root/index.d.ts +10 -0
- package/lib/client/theme-fallback/Root/index.js +2 -6
- package/lib/{server/html-tags/htmlTags.d.ts → client/theme-fallback/SiteMetadata/index.d.ts} +2 -1
- package/lib/client/theme-fallback/SiteMetadata/index.js +10 -0
- package/lib/commands/build.d.ts +6 -2
- package/lib/commands/build.js +81 -64
- package/lib/commands/clear.d.ts +7 -1
- package/lib/commands/clear.js +34 -21
- package/lib/commands/deploy.d.ts +5 -2
- package/lib/commands/deploy.js +102 -81
- package/lib/commands/external.d.ts +2 -2
- package/lib/commands/external.js +9 -11
- package/lib/commands/serve.d.ts +8 -2
- package/lib/commands/serve.js +40 -20
- package/lib/commands/start.d.ts +9 -2
- package/lib/commands/start.js +111 -101
- package/lib/commands/swizzle/actions.d.ts +23 -0
- package/lib/commands/swizzle/actions.js +106 -0
- package/lib/commands/swizzle/common.d.ts +33 -0
- package/lib/commands/swizzle/common.js +56 -0
- package/lib/commands/swizzle/components.d.ts +29 -0
- package/lib/commands/swizzle/components.js +200 -0
- package/lib/commands/swizzle/config.d.ts +10 -0
- package/lib/commands/swizzle/config.js +84 -0
- package/lib/{server/client-modules/index.d.ts → commands/swizzle/context.d.ts} +2 -2
- package/lib/commands/swizzle/context.js +24 -0
- package/lib/commands/swizzle/index.d.ts +8 -0
- package/lib/commands/swizzle/index.js +119 -0
- package/lib/commands/swizzle/prompts.d.ts +12 -0
- package/lib/commands/swizzle/prompts.js +110 -0
- package/lib/{client/exports/context.d.ts → commands/swizzle/tables.d.ts} +3 -4
- package/lib/commands/swizzle/tables.js +113 -0
- package/lib/commands/swizzle/themes.d.ts +20 -0
- package/lib/commands/swizzle/themes.js +106 -0
- package/lib/commands/writeHeadingIds.d.ts +2 -6
- package/lib/commands/writeHeadingIds.js +22 -81
- package/lib/commands/writeTranslations.d.ts +4 -5
- package/lib/commands/writeTranslations.js +48 -28
- package/lib/index.d.ts +9 -9
- package/lib/index.js +14 -14
- package/lib/server/brokenLinks.d.ts +4 -17
- package/lib/server/brokenLinks.js +67 -56
- package/lib/server/clientModules.d.ts +12 -0
- package/lib/server/clientModules.js +20 -0
- package/lib/server/config.d.ts +5 -2
- package/lib/server/config.js +29 -6
- package/lib/server/configValidation.d.ts +5 -3
- package/lib/server/configValidation.js +105 -44
- package/lib/server/getHostPort.d.ts +14 -0
- package/lib/server/getHostPort.js +79 -0
- package/lib/server/htmlTags.d.ts +12 -0
- package/lib/server/htmlTags.js +62 -0
- package/lib/server/i18n.d.ts +3 -13
- package/lib/server/i18n.js +21 -55
- package/lib/server/index.d.ts +28 -6
- package/lib/server/index.js +86 -173
- package/lib/server/plugins/configs.d.ts +51 -0
- package/lib/server/plugins/configs.js +101 -0
- package/lib/server/plugins/index.d.ts +10 -10
- package/lib/server/plugins/index.js +76 -128
- package/lib/server/plugins/init.d.ts +6 -9
- package/lib/server/plugins/init.js +44 -109
- package/lib/server/plugins/moduleShorthand.d.ts +9 -0
- package/lib/server/plugins/moduleShorthand.js +46 -0
- package/lib/server/plugins/pluginIds.d.ts +6 -2
- package/lib/server/plugins/pluginIds.js +12 -7
- package/lib/server/plugins/presets.d.ts +12 -0
- package/lib/server/plugins/presets.js +49 -0
- package/lib/server/plugins/routeConfig.d.ts +11 -0
- package/lib/server/plugins/routeConfig.js +54 -0
- package/lib/server/plugins/synthetic.d.ts +20 -0
- package/lib/server/plugins/synthetic.js +111 -0
- package/lib/server/routes.d.ts +39 -7
- package/lib/server/routes.js +182 -95
- package/lib/server/siteMetadata.d.ts +12 -0
- package/lib/server/siteMetadata.js +81 -0
- package/lib/server/translations/translations.d.ts +16 -22
- package/lib/server/translations/translations.js +41 -73
- package/lib/server/translations/translationsExtractor.d.ts +10 -5
- package/lib/server/translations/translationsExtractor.js +158 -122
- package/lib/server/utils.d.ts +9 -3
- package/lib/server/utils.js +7 -9
- package/lib/webpack/aliases/index.d.ts +34 -0
- package/lib/webpack/aliases/index.js +106 -0
- package/lib/webpack/base.d.ts +3 -4
- package/lib/webpack/base.js +56 -58
- package/lib/webpack/client.d.ts +3 -3
- package/lib/webpack/client.js +12 -19
- package/lib/webpack/plugins/ChunkAssetPlugin.d.ts +13 -3
- package/lib/webpack/plugins/ChunkAssetPlugin.js +24 -17
- package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +6 -25
- package/lib/webpack/plugins/CleanWebpackPlugin.js +34 -18
- package/lib/webpack/plugins/LogPlugin.d.ts +1 -1
- package/lib/webpack/plugins/LogPlugin.js +4 -5
- package/lib/webpack/plugins/WaitPlugin.d.ts +3 -3
- package/lib/webpack/plugins/WaitPlugin.js +1 -1
- package/lib/webpack/server.d.ts +5 -5
- package/lib/webpack/server.js +23 -8
- package/lib/{client → webpack}/templates/index.html.template.ejs +0 -0
- package/lib/webpack/templates/ssr.html.template.d.ts +8 -0
- package/lib/{client → webpack}/templates/ssr.html.template.js +5 -4
- package/lib/webpack/utils.d.ts +16 -31
- package/lib/webpack/utils.js +63 -183
- package/package.json +83 -77
- package/bin/docusaurus.js +0 -326
- package/lib/.tsbuildinfo +0 -5682
- package/lib/choosePort.js +0 -105
- package/lib/client/.eslintrc.js +0 -29
- package/lib/client/.tsbuildinfo +0 -4127
- package/lib/client/client-lifecycles-dispatcher.d.ts +0 -12
- package/lib/client/client-lifecycles-dispatcher.js +0 -27
- package/lib/client/nprogress.css +0 -36
- package/lib/commands/buildRemoteBranchUrl.d.ts +0 -7
- package/lib/commands/buildRemoteBranchUrl.js +0 -27
- package/lib/commands/commandUtils.d.ts +0 -3
- package/lib/commands/commandUtils.js +0 -21
- package/lib/commands/swizzle.d.ts +0 -9
- package/lib/commands/swizzle.js +0 -245
- package/lib/constants.d.ts +0 -18
- package/lib/constants.js +0 -23
- package/lib/server/client-modules/index.js +0 -14
- package/lib/server/duplicateRoutes.d.ts +0 -10
- package/lib/server/duplicateRoutes.js +0 -38
- package/lib/server/html-tags/htmlTags.js +0 -39
- package/lib/server/html-tags/index.d.ts +0 -9
- package/lib/server/html-tags/index.js +0 -43
- package/lib/server/loadSetup.d.ts +0 -9
- package/lib/server/loadSetup.js +0 -25
- package/lib/server/presets/index.d.ts +0 -11
- package/lib/server/presets/index.js +0 -48
- package/lib/server/themes/alias.d.ts +0 -8
- package/lib/server/themes/alias.js +0 -39
- package/lib/server/themes/index.d.ts +0 -8
- package/lib/server/themes/index.js +0 -35
- package/lib/server/versions/__fixtures__/dummy-plugin.js +0 -0
- package/lib/server/versions/__fixtures__/package.json +0 -3
- package/lib/server/versions/__tests/index.test.js +0 -25
- package/lib/server/versions/index.d.ts +0 -10
- package/lib/server/versions/index.js +0 -50
- package/lib/webpack/react-dev-utils-webpack5/README.md +0 -11
- package/lib/webpack/react-dev-utils-webpack5/evalSourceMapMiddleware.js +0 -57
- package/lib/webpack/react-dev-utils-webpack5/formatWebpackMessages.js +0 -138
- package/lib/webpack/react-dev-utils-webpack5/webpackHotDevClient.js +0 -285
- package/lib/webpack/sharedModuleAliases.d.ts +0 -10
- package/lib/webpack/sharedModuleAliases.js +0 -18
- package/tsconfig.client.json +0 -13
- package/tsconfig.json +0 -13
|
@@ -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 =
|
|
33
|
+
const content = await fs_extra_1.default.readJSON(filePath);
|
|
35
34
|
ensureTranslationFileContent(content);
|
|
36
35
|
return content;
|
|
37
36
|
}
|
|
38
|
-
catch (
|
|
39
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
|
54
|
+
// If messages already exist, we don't override them (unless requested)
|
|
60
55
|
message: options.override
|
|
61
56
|
? message
|
|
62
|
-
:
|
|
63
|
-
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
|
|
66
|
+
const unknownKeys = lodash_1.default.difference(Object.keys(existingContent ?? {}), Object.keys(newContent));
|
|
72
67
|
if (unknownKeys.length > 0) {
|
|
73
|
-
|
|
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
|
-
|
|
85
|
-
|
|
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(
|
|
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
|
}
|
|
@@ -119,25 +99,23 @@ exports.writeCodeTranslations = writeCodeTranslations;
|
|
|
119
99
|
// (json/yaml/toml/xml...)
|
|
120
100
|
function addTranslationFileExtension(translationFilePath) {
|
|
121
101
|
if (translationFilePath.endsWith('.json')) {
|
|
122
|
-
throw new Error(`Translation file path does
|
|
102
|
+
throw new Error(`Translation file path at "${translationFilePath}" does not need to end with ".json", we add the extension automatically.`);
|
|
123
103
|
}
|
|
124
104
|
return `${translationFilePath}.json`;
|
|
125
105
|
}
|
|
126
|
-
function getPluginTranslationFilePath({
|
|
127
|
-
const dirPath = utils_1.getPluginI18nPath({
|
|
128
|
-
|
|
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({
|
|
115
|
+
async function writePluginTranslations({ localizationDir, plugin, translationFile, options, }) {
|
|
137
116
|
const filePath = getPluginTranslationFilePath({
|
|
138
117
|
plugin,
|
|
139
|
-
|
|
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({
|
|
128
|
+
async function localizePluginTranslationFile({ localizationDir, plugin, translationFile, }) {
|
|
151
129
|
const filePath = getPluginTranslationFilePath({
|
|
152
130
|
plugin,
|
|
153
|
-
|
|
154
|
-
locale,
|
|
131
|
+
localizationDir,
|
|
155
132
|
translationFilePath: translationFile.path,
|
|
156
133
|
});
|
|
157
134
|
const localizedContent = await readTranslationFileContent(filePath);
|
|
158
135
|
if (localizedContent) {
|
|
159
|
-
//
|
|
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
|
-
|
|
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) =>
|
|
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
|
-
|
|
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
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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,11 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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:
|
|
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:
|
|
13
|
+
translations: TranslationFileContent;
|
|
9
14
|
warnings: string[];
|
|
10
15
|
};
|
|
11
16
|
export declare function extractAllSourceCodeFileTranslations(sourceCodeFilePaths: string[], babelOptions: TransformOptions): Promise<SourceCodeFileTranslations[]>;
|
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extractSourceCodeFileTranslations = exports.extractAllSourceCodeFileTranslations = exports.extractSiteSourceCodeTranslations = exports.globSourceCodeFilePaths = 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.extractSourceCodeFileTranslations = exports.extractAllSourceCodeFileTranslations = exports.extractSiteSourceCodeTranslations = exports.globSourceCodeFilePaths = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
11
12
|
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
13
|
+
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
12
14
|
const traverse_1 = tslib_1.__importDefault(require("@babel/traverse"));
|
|
13
15
|
const generator_1 = tslib_1.__importDefault(require("@babel/generator"));
|
|
14
|
-
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
15
16
|
const core_1 = require("@babel/core");
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const constants_1 = require("../../constants");
|
|
19
|
-
const utils_1 = require("../utils");
|
|
17
|
+
const utils_1 = require("@docusaurus/utils");
|
|
18
|
+
const utils_2 = require("../utils");
|
|
20
19
|
// We only support extracting source code translations from these kind of files
|
|
21
20
|
const TranslatableSourceCodeExtension = new Set([
|
|
22
21
|
'.js',
|
|
@@ -24,7 +23,7 @@ const TranslatableSourceCodeExtension = new Set([
|
|
|
24
23
|
'.ts',
|
|
25
24
|
'.tsx',
|
|
26
25
|
// TODO support md/mdx too? (may be overkill)
|
|
27
|
-
// need to compile the MDX to JSX first and remove
|
|
26
|
+
// need to compile the MDX to JSX first and remove front matter
|
|
28
27
|
// '.md',
|
|
29
28
|
// '.mdx',
|
|
30
29
|
]);
|
|
@@ -32,44 +31,48 @@ function isTranslatableSourceCodePath(filePath) {
|
|
|
32
31
|
return TranslatableSourceCodeExtension.has(path_1.default.extname(filePath));
|
|
33
32
|
}
|
|
34
33
|
function getSiteSourceCodeFilePaths(siteDir) {
|
|
35
|
-
return [path_1.default.join(siteDir,
|
|
34
|
+
return [path_1.default.join(siteDir, utils_1.SRC_DIR_NAME)];
|
|
36
35
|
}
|
|
37
36
|
function getPluginSourceCodeFilePaths(plugin) {
|
|
38
|
-
var _a, _b, _c;
|
|
39
37
|
// The getPathsToWatch() generally returns the js/jsx/ts/tsx/md/mdx file paths
|
|
40
|
-
// We can use this method as well to know which folders we should try to
|
|
41
|
-
// Hacky/implicit, but do we want to introduce a
|
|
42
|
-
|
|
38
|
+
// We can use this method as well to know which folders we should try to
|
|
39
|
+
// extract translations from. Hacky/implicit, but do we want to introduce a
|
|
40
|
+
// new lifecycle method just for that???
|
|
41
|
+
const codePaths = plugin.getPathsToWatch?.() ?? [];
|
|
43
42
|
// We also include theme code
|
|
44
|
-
const themePath =
|
|
43
|
+
const themePath = plugin.getThemePath?.();
|
|
45
44
|
if (themePath) {
|
|
46
45
|
codePaths.push(themePath);
|
|
47
46
|
}
|
|
48
|
-
return codePaths;
|
|
47
|
+
return codePaths.map((p) => path_1.default.resolve(plugin.path, p));
|
|
49
48
|
}
|
|
50
49
|
async function globSourceCodeFilePaths(dirPaths) {
|
|
51
|
-
const filePaths = await
|
|
50
|
+
const filePaths = await (0, utils_2.safeGlobby)(dirPaths);
|
|
52
51
|
return filePaths.filter(isTranslatableSourceCodePath);
|
|
53
52
|
}
|
|
54
53
|
exports.globSourceCodeFilePaths = globSourceCodeFilePaths;
|
|
55
54
|
async function getSourceCodeFilePaths(siteDir, plugins) {
|
|
56
55
|
const sitePaths = getSiteSourceCodeFilePaths(siteDir);
|
|
57
56
|
// The getPathsToWatch() generally returns the js/jsx/ts/tsx/md/mdx file paths
|
|
58
|
-
// We can use this method as well to know which folders we should try to
|
|
59
|
-
// Hacky/implicit, but do we want to introduce a
|
|
60
|
-
|
|
57
|
+
// We can use this method as well to know which folders we should try to
|
|
58
|
+
// extract translations from. Hacky/implicit, but do we want to introduce a
|
|
59
|
+
// new lifecycle method for that???
|
|
60
|
+
const pluginsPaths = plugins.flatMap(getPluginSourceCodeFilePaths);
|
|
61
61
|
const allPaths = [...sitePaths, ...pluginsPaths];
|
|
62
62
|
return globSourceCodeFilePaths(allPaths);
|
|
63
63
|
}
|
|
64
|
-
async function extractSiteSourceCodeTranslations(siteDir, plugins, babelOptions) {
|
|
65
|
-
// Should we warn here if the same translation "key" is found in multiple
|
|
64
|
+
async function extractSiteSourceCodeTranslations(siteDir, plugins, babelOptions, extraSourceCodeFilePaths = []) {
|
|
65
|
+
// Should we warn here if the same translation "key" is found in multiple
|
|
66
|
+
// source code files?
|
|
66
67
|
function toTranslationFileContent(sourceCodeFileTranslations) {
|
|
67
|
-
return sourceCodeFileTranslations.reduce((acc, item) => {
|
|
68
|
-
return { ...acc, ...item.translations };
|
|
69
|
-
}, {});
|
|
68
|
+
return sourceCodeFileTranslations.reduce((acc, item) => ({ ...acc, ...item.translations }), {});
|
|
70
69
|
}
|
|
71
70
|
const sourceCodeFilePaths = await getSourceCodeFilePaths(siteDir, plugins);
|
|
72
|
-
const
|
|
71
|
+
const allSourceCodeFilePaths = [
|
|
72
|
+
...sourceCodeFilePaths,
|
|
73
|
+
...extraSourceCodeFilePaths,
|
|
74
|
+
];
|
|
75
|
+
const sourceCodeFilesTranslations = await extractAllSourceCodeFileTranslations(allSourceCodeFilePaths, babelOptions);
|
|
73
76
|
logSourceCodeFileTranslationsWarnings(sourceCodeFilesTranslations);
|
|
74
77
|
return toTranslationFileContent(sourceCodeFilesTranslations);
|
|
75
78
|
}
|
|
@@ -77,29 +80,31 @@ exports.extractSiteSourceCodeTranslations = extractSiteSourceCodeTranslations;
|
|
|
77
80
|
function logSourceCodeFileTranslationsWarnings(sourceCodeFilesTranslations) {
|
|
78
81
|
sourceCodeFilesTranslations.forEach(({ sourceCodeFilePath, warnings }) => {
|
|
79
82
|
if (warnings.length > 0) {
|
|
80
|
-
|
|
83
|
+
logger_1.default.warn `Translation extraction warnings for file path=${sourceCodeFilePath}: ${warnings}`;
|
|
81
84
|
}
|
|
82
85
|
});
|
|
83
86
|
}
|
|
84
87
|
async function extractAllSourceCodeFileTranslations(sourceCodeFilePaths, babelOptions) {
|
|
85
|
-
return
|
|
88
|
+
return Promise.all(sourceCodeFilePaths.flatMap((sourceFilePath) => extractSourceCodeFileTranslations(sourceFilePath, babelOptions)));
|
|
86
89
|
}
|
|
87
90
|
exports.extractAllSourceCodeFileTranslations = extractAllSourceCodeFileTranslations;
|
|
88
91
|
async function extractSourceCodeFileTranslations(sourceCodeFilePath, babelOptions) {
|
|
89
92
|
try {
|
|
90
93
|
const code = await fs_extra_1.default.readFile(sourceCodeFilePath, 'utf8');
|
|
91
|
-
const ast = core_1.parse(code, {
|
|
94
|
+
const ast = (0, core_1.parse)(code, {
|
|
92
95
|
...babelOptions,
|
|
93
96
|
ast: true,
|
|
94
|
-
// filename is important, because babel does not process the same files
|
|
95
|
-
//
|
|
97
|
+
// filename is important, because babel does not process the same files
|
|
98
|
+
// according to their js/ts extensions.
|
|
99
|
+
// See https://twitter.com/NicoloRibaudo/status/1321130735605002243
|
|
96
100
|
filename: sourceCodeFilePath,
|
|
97
101
|
});
|
|
98
|
-
|
|
102
|
+
const translations = extractSourceCodeAstTranslations(ast, sourceCodeFilePath);
|
|
103
|
+
return translations;
|
|
99
104
|
}
|
|
100
|
-
catch (
|
|
101
|
-
|
|
102
|
-
throw
|
|
105
|
+
catch (err) {
|
|
106
|
+
logger_1.default.error `Error while attempting to extract Docusaurus translations from source code file at path=${sourceCodeFilePath}.`;
|
|
107
|
+
throw err;
|
|
103
108
|
}
|
|
104
109
|
}
|
|
105
110
|
exports.extractSourceCodeFileTranslations = extractSourceCodeFileTranslations;
|
|
@@ -112,108 +117,139 @@ https://github.com/formatjs/formatjs/blob/main/packages/babel-plugin-formatjs/in
|
|
|
112
117
|
https://github.com/pugjs/babel-walk
|
|
113
118
|
*/
|
|
114
119
|
function extractSourceCodeAstTranslations(ast, sourceCodeFilePath) {
|
|
115
|
-
function
|
|
116
|
-
return
|
|
117
|
-
|
|
118
|
-
function sourceFileWarningPart(node) {
|
|
119
|
-
var _a;
|
|
120
|
-
return `File=${sourceCodeFilePath} at line=${(_a = node.loc) === null || _a === void 0 ? void 0 : _a.start.line}`;
|
|
121
|
-
}
|
|
122
|
-
function generateCode(node) {
|
|
123
|
-
return generator_1.default(node).code;
|
|
120
|
+
function sourceWarningPart(node) {
|
|
121
|
+
return `File: ${sourceCodeFilePath} at line ${node.loc?.start.line ?? '?'}
|
|
122
|
+
Full code: ${(0, generator_1.default)(node).code}`;
|
|
124
123
|
}
|
|
125
124
|
const translations = {};
|
|
126
125
|
const warnings = [];
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
126
|
+
let translateComponentName;
|
|
127
|
+
let translateFunctionName;
|
|
128
|
+
// First pass: find import declarations of Translate / translate.
|
|
129
|
+
// If not found, don't process the rest to avoid false positives
|
|
130
|
+
(0, traverse_1.default)(ast, {
|
|
131
|
+
ImportDeclaration(path) {
|
|
132
|
+
if (path.node.importKind === 'type' ||
|
|
133
|
+
path.get('source').node.value !== '@docusaurus/Translate') {
|
|
134
134
|
return;
|
|
135
135
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
.
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
136
|
+
const importSpecifiers = path.get('specifiers');
|
|
137
|
+
const defaultImport = importSpecifiers.find((specifier) => specifier.node.type === 'ImportDefaultSpecifier');
|
|
138
|
+
const callbackImport = importSpecifiers.find((specifier) => specifier.node.type === 'ImportSpecifier' &&
|
|
139
|
+
(specifier.get('imported')
|
|
140
|
+
.node.name === 'translate' ||
|
|
141
|
+
specifier.get('imported')
|
|
142
|
+
.node.value === 'translate'));
|
|
143
|
+
translateComponentName = defaultImport?.get('local').node.name;
|
|
144
|
+
translateFunctionName = callbackImport?.get('local').node.name;
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
(0, traverse_1.default)(ast, {
|
|
148
|
+
...(translateComponentName && {
|
|
149
|
+
JSXElement(path) {
|
|
150
|
+
if (!path
|
|
151
|
+
.get('openingElement')
|
|
152
|
+
.get('name')
|
|
153
|
+
.isJSXIdentifier({ name: translateComponentName })) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
function evaluateJSXProp(propName) {
|
|
157
|
+
const attributePath = path
|
|
158
|
+
.get('openingElement.attributes')
|
|
159
|
+
.find((attr) => attr.isJSXAttribute() &&
|
|
160
|
+
attr.get('name').isJSXIdentifier({ name: propName }));
|
|
161
|
+
if (attributePath) {
|
|
162
|
+
const attributeValue = attributePath.get('value');
|
|
163
|
+
const attributeValueEvaluated = attributeValue.isJSXExpressionContainer()
|
|
164
|
+
? attributeValue.get('expression').evaluate()
|
|
165
|
+
: attributeValue.evaluate();
|
|
166
|
+
if (attributeValueEvaluated.confident &&
|
|
167
|
+
typeof attributeValueEvaluated.value === 'string') {
|
|
168
|
+
return attributeValueEvaluated.value;
|
|
169
|
+
}
|
|
170
|
+
warnings.push(`<Translate> prop=${propName} should be a statically evaluable object.
|
|
171
|
+
Example: <Translate id="optional id" description="optional description">Message</Translate>
|
|
172
|
+
Dynamically constructed values are not allowed, because they prevent translations to be extracted.
|
|
173
|
+
${sourceWarningPart(path.node)}`);
|
|
154
174
|
}
|
|
175
|
+
return undefined;
|
|
155
176
|
}
|
|
156
|
-
return undefined;
|
|
157
|
-
}
|
|
158
|
-
// We only handle the optimistic case where we have a single non-empty content
|
|
159
|
-
const singleChildren = path
|
|
160
|
-
.get('children')
|
|
161
|
-
// Remove empty/useless text nodes that might be around our translation!
|
|
162
|
-
// Makes the translation system more reliable to JSX formatting issues
|
|
163
|
-
.filter((childrenPath) => !(childrenPath.isJSXText() &&
|
|
164
|
-
childrenPath.node.value.replace('\n', '').trim() === ''))
|
|
165
|
-
.pop();
|
|
166
|
-
if (singleChildren && singleChildren.isJSXText()) {
|
|
167
|
-
const message = singleChildren.node.value.trim().replace(/\s+/g, ' ');
|
|
168
|
-
const id = evaluateJSXProp('id');
|
|
169
|
-
const description = evaluateJSXProp('description');
|
|
170
|
-
translations[id !== null && id !== void 0 ? id : message] = {
|
|
171
|
-
message,
|
|
172
|
-
...(description && { description }),
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
else if (singleChildren &&
|
|
176
|
-
singleChildren.isJSXExpressionContainer() &&
|
|
177
|
-
singleChildren.get('expression').evaluate().confident) {
|
|
178
|
-
const message = singleChildren.get('expression').evaluate().value;
|
|
179
177
|
const id = evaluateJSXProp('id');
|
|
180
178
|
const description = evaluateJSXProp('description');
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
//
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
179
|
+
let message;
|
|
180
|
+
const childrenPath = path.get('children');
|
|
181
|
+
// Handle empty content
|
|
182
|
+
if (!childrenPath.length) {
|
|
183
|
+
if (!id) {
|
|
184
|
+
warnings.push(`<Translate> without children must have id prop.
|
|
185
|
+
Example: <Translate id="my-id" />
|
|
186
|
+
${sourceWarningPart(path.node)}`);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
translations[id] = {
|
|
190
|
+
message: id,
|
|
191
|
+
...(description && { description }),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
// Handle single non-empty content
|
|
197
|
+
const singleChildren = childrenPath
|
|
198
|
+
// Remove empty/useless text nodes that might be around our
|
|
199
|
+
// translation! Makes the translation system more reliable to JSX
|
|
200
|
+
// formatting issues
|
|
201
|
+
.filter((children) => !(children.isJSXText() &&
|
|
202
|
+
children.node.value.replace('\n', '').trim() === ''))
|
|
203
|
+
.pop();
|
|
204
|
+
const isJSXText = singleChildren?.isJSXText();
|
|
205
|
+
const isJSXExpressionContainer = singleChildren?.isJSXExpressionContainer() &&
|
|
206
|
+
singleChildren.get('expression').evaluate().confident;
|
|
207
|
+
if (isJSXText || isJSXExpressionContainer) {
|
|
208
|
+
message = isJSXText
|
|
209
|
+
? singleChildren.node.value.trim().replace(/\s+/g, ' ')
|
|
210
|
+
: String(singleChildren.get('expression').evaluate().value);
|
|
211
|
+
translations[id ?? message] = {
|
|
205
212
|
message,
|
|
206
213
|
...(description && { description }),
|
|
207
214
|
};
|
|
208
215
|
}
|
|
209
216
|
else {
|
|
210
|
-
warnings.push(`
|
|
217
|
+
warnings.push(`Translate content could not be extracted. It has to be a static string and use optional but static props, like <Translate id="my-id" description="my-description">text</Translate>.
|
|
218
|
+
${sourceWarningPart(path.node)}`);
|
|
211
219
|
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
220
|
+
},
|
|
221
|
+
}),
|
|
222
|
+
...(translateFunctionName && {
|
|
223
|
+
CallExpression(path) {
|
|
224
|
+
if (!path.get('callee').isIdentifier({ name: translateFunctionName })) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const args = path.get('arguments');
|
|
228
|
+
if (args.length === 1 || args.length === 2) {
|
|
229
|
+
const firstArgPath = args[0];
|
|
230
|
+
// translate("x" + "y"); => translate("xy");
|
|
231
|
+
const firstArgEvaluated = firstArgPath.evaluate();
|
|
232
|
+
if (firstArgEvaluated.confident &&
|
|
233
|
+
typeof firstArgEvaluated.value === 'object') {
|
|
234
|
+
const { message, id, description } = firstArgEvaluated.value;
|
|
235
|
+
translations[String(id ?? message)] = {
|
|
236
|
+
message: String(message ?? id),
|
|
237
|
+
...(Boolean(description) && { description: String(description) }),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
warnings.push(`translate() first arg should be a statically evaluable object.
|
|
242
|
+
Example: translate({message: "text",id: "optional.id",description: "optional description"}
|
|
243
|
+
Dynamically constructed values are not allowed, because they prevent translations to be extracted.
|
|
244
|
+
${sourceWarningPart(path.node)}`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
warnings.push(`translate() function only takes 1 or 2 args
|
|
249
|
+
${sourceWarningPart(path.node)}`);
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
}),
|
|
217
253
|
});
|
|
218
254
|
return { sourceCodeFilePath, translations, warnings };
|
|
219
255
|
}
|
package/lib/server/utils.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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 { Globby } from '@docusaurus/utils';
|
|
8
|
+
import type { RouteConfig } from '@docusaurus/types';
|
|
3
9
|
export declare function getAllFinalRoutes(routeConfig: RouteConfig[]): RouteConfig[];
|
|
4
|
-
export declare function safeGlobby(patterns: string[], options?:
|
|
10
|
+
export declare function safeGlobby(patterns: string[], options?: Globby.GlobbyOptions): Promise<string[]>;
|