@docusaurus/core 2.0.0-beta.15a2b59f9 → 2.0.0-beta.17
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/bin/beforeCli.mjs +136 -0
- package/bin/{docusaurus.js → docusaurus.mjs} +63 -108
- package/lib/babel/preset.d.ts +8 -3
- package/lib/babel/preset.js +8 -7
- package/lib/choosePort.js +29 -38
- package/lib/client/.eslintrc.js +0 -1
- package/lib/client/App.d.ts +1 -2
- package/lib/client/App.js +19 -24
- package/lib/client/LinksCollector.d.ts +3 -3
- package/lib/client/LinksCollector.js +5 -9
- package/lib/client/PendingNavigation.d.ts +24 -1
- package/lib/client/PendingNavigation.js +5 -7
- package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.d.ts +13 -0
- package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.js +15 -10
- package/lib/client/client-lifecycles-dispatcher.d.ts +2 -5
- package/lib/client/client-lifecycles-dispatcher.js +6 -10
- package/lib/client/clientEntry.js +11 -5
- package/lib/client/docusaurus.d.ts +6 -0
- package/lib/client/docusaurus.js +16 -25
- package/lib/client/exports/BrowserOnly.d.ts +2 -3
- package/lib/client/exports/BrowserOnly.js +13 -7
- package/lib/client/exports/ComponentCreator.d.ts +1 -2
- package/lib/client/exports/ComponentCreator.js +7 -6
- package/lib/client/exports/ErrorBoundary.d.ts +18 -0
- package/lib/client/exports/ErrorBoundary.js +35 -0
- package/lib/client/exports/Head.d.ts +2 -3
- package/lib/client/exports/Head.js +3 -4
- package/lib/client/exports/Interpolate.d.ts +1 -1
- package/lib/client/exports/Interpolate.js +16 -22
- package/lib/client/exports/Link.d.ts +11 -5
- package/lib/client/exports/Link.js +24 -19
- package/lib/client/exports/Translate.d.ts +3 -3
- package/lib/client/exports/Translate.js +14 -10
- package/lib/client/exports/browserContext.d.ts +11 -0
- package/lib/client/exports/browserContext.js +22 -0
- package/lib/client/exports/constants.js +1 -11
- package/lib/client/exports/docusaurusContext.d.ts +12 -0
- package/lib/client/exports/docusaurusContext.js +25 -0
- 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 +3 -5
- package/lib/client/exports/useDocusaurusContext.d.ts +2 -3
- package/lib/client/exports/useDocusaurusContext.js +3 -9
- package/lib/client/exports/useGlobalData.js +1 -5
- 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/flat.d.ts +2 -2
- package/lib/client/flat.js +8 -11
- package/lib/client/normalizeLocation.d.ts +2 -5
- package/lib/client/normalizeLocation.js +1 -2
- package/lib/client/nprogress.css +2 -2
- package/lib/client/prefetch.d.ts +1 -2
- package/lib/client/prefetch.js +1 -3
- package/lib/client/preload.d.ts +3 -2
- package/lib/client/preload.js +2 -1
- package/lib/client/serverEntry.d.ts +10 -0
- package/lib/client/serverEntry.js +90 -142
- package/lib/client/theme-fallback/Error/index.d.ts +10 -0
- package/lib/client/theme-fallback/Error/index.js +39 -0
- package/lib/client/theme-fallback/Layout/index.d.ts +10 -0
- package/lib/client/theme-fallback/Layout/index.js +10 -19
- package/lib/client/theme-fallback/Loading/index.d.ts +9 -0
- package/lib/client/theme-fallback/Loading/index.js +47 -115
- package/lib/client/{exports/context.js → theme-fallback/NotFound/index.d.ts} +2 -2
- package/lib/client/theme-fallback/NotFound/index.js +9 -16
- package/lib/{webpack/sharedModuleAliases.d.ts → client/theme-fallback/Root/index.d.ts} +4 -4
- package/lib/client/theme-fallback/Root/index.js +2 -7
- package/lib/commands/build.d.ts +1 -1
- package/lib/commands/build.js +50 -54
- package/lib/commands/clear.d.ts +6 -0
- package/lib/commands/clear.js +30 -19
- package/lib/commands/commandUtils.d.ts +7 -1
- package/lib/commands/commandUtils.js +6 -6
- package/lib/commands/deploy.d.ts +4 -1
- package/lib/commands/deploy.js +102 -66
- package/lib/commands/external.d.ts +2 -2
- package/lib/commands/external.js +3 -3
- package/lib/commands/serve.d.ts +1 -1
- package/lib/commands/serve.js +13 -19
- package/lib/commands/start.d.ts +1 -1
- package/lib/commands/start.js +93 -83
- package/lib/commands/swizzle/actions.d.ts +23 -0
- package/lib/commands/swizzle/actions.js +102 -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 +162 -0
- package/lib/commands/swizzle/config.d.ts +10 -0
- package/lib/commands/swizzle/config.js +80 -0
- package/lib/{server/loadSetup.d.ts → commands/swizzle/context.d.ts} +2 -3
- package/lib/commands/swizzle/context.js +30 -0
- package/lib/commands/swizzle/index.d.ts +8 -0
- package/lib/commands/swizzle/index.js +115 -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 +116 -0
- package/lib/commands/swizzle/themes.d.ts +20 -0
- package/lib/commands/swizzle/themes.js +103 -0
- package/lib/commands/writeHeadingIds.d.ts +9 -6
- package/lib/commands/writeHeadingIds.js +41 -43
- package/lib/commands/writeTranslations.d.ts +2 -2
- package/lib/commands/writeTranslations.js +35 -14
- package/lib/index.d.ts +10 -9
- package/lib/index.js +20 -19
- package/lib/server/brokenLinks.d.ts +1 -1
- package/lib/server/brokenLinks.js +57 -38
- package/lib/server/client-modules/index.d.ts +1 -1
- package/lib/server/client-modules/index.js +1 -3
- package/lib/server/config.d.ts +2 -2
- package/lib/server/config.js +7 -4
- package/lib/server/configValidation.d.ts +2 -2
- package/lib/server/configValidation.js +43 -28
- package/lib/server/duplicateRoutes.d.ts +1 -1
- package/lib/server/duplicateRoutes.js +10 -6
- package/lib/server/html-tags/htmlTags.js +2 -4
- package/lib/server/html-tags/index.d.ts +1 -1
- package/lib/server/html-tags/index.js +1 -1
- package/lib/server/i18n.d.ts +1 -2
- package/lib/server/i18n.js +32 -46
- package/lib/server/index.d.ts +2 -2
- package/lib/server/index.js +147 -68
- package/lib/server/moduleShorthand.d.ts +9 -0
- package/lib/server/moduleShorthand.js +46 -0
- package/lib/server/plugins/applyRouteTrailingSlash.d.ts +2 -2
- package/lib/server/plugins/applyRouteTrailingSlash.js +1 -1
- package/lib/server/plugins/index.d.ts +2 -2
- package/lib/server/plugins/index.js +34 -35
- package/lib/server/plugins/init.d.ts +12 -2
- package/lib/server/plugins/init.js +48 -58
- package/lib/server/plugins/pluginIds.d.ts +1 -1
- package/lib/server/plugins/pluginIds.js +8 -5
- package/lib/server/presets/index.d.ts +3 -3
- package/lib/server/presets/index.js +12 -13
- package/lib/server/routes.d.ts +1 -1
- package/lib/server/routes.js +50 -29
- package/lib/server/themes/alias.d.ts +3 -2
- package/lib/server/themes/alias.js +22 -14
- package/lib/server/themes/index.d.ts +3 -3
- package/lib/server/themes/index.js +26 -26
- package/lib/server/translations/translations.d.ts +7 -1
- package/lib/server/translations/translations.js +30 -45
- package/lib/server/translations/translationsExtractor.d.ts +9 -3
- package/lib/server/translations/translationsExtractor.js +159 -120
- package/lib/server/utils.d.ts +9 -3
- package/lib/server/utils.js +7 -9
- package/lib/server/versions/__fixtures__/dummy-plugin.d.ts +0 -0
- package/lib/server/versions/index.d.ts +3 -4
- package/lib/server/versions/index.js +22 -21
- package/lib/webpack/base.d.ts +4 -4
- package/lib/webpack/base.js +38 -33
- 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 +17 -10
- package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +6 -6
- package/lib/webpack/plugins/CleanWebpackPlugin.js +8 -14
- package/lib/webpack/plugins/LogPlugin.d.ts +1 -1
- package/lib/webpack/plugins/LogPlugin.js +4 -5
- package/lib/webpack/plugins/WaitPlugin.d.ts +1 -1
- package/lib/webpack/plugins/WaitPlugin.js +1 -1
- package/lib/webpack/server.d.ts +3 -3
- package/lib/webpack/server.js +9 -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 +3 -2
- package/lib/webpack/utils.d.ts +7 -29
- package/lib/webpack/utils.js +54 -171
- package/package.json +74 -69
- package/lib/.tsbuildinfo +0 -1
- package/lib/client/.tsbuildinfo +0 -1
- package/lib/commands/buildRemoteBranchUrl.d.ts +0 -7
- package/lib/commands/buildRemoteBranchUrl.js +0 -27
- 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/loadSetup.js +0 -25
- package/lib/server/versions/__tests/index.test.js +0 -25
- 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.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 = exports.getCodeTranslationsFilePath = exports.getTranslationsLocaleDirPath = exports.getTranslationsDirPath = exports.writeTranslationFileContent = exports.readTranslationFileContent = exports.ensureTranslationFileContent = 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
|
|
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(),
|
|
@@ -35,8 +35,9 @@ async function readTranslationFileContent(filePath) {
|
|
|
35
35
|
ensureTranslationFileContent(content);
|
|
36
36
|
return content;
|
|
37
37
|
}
|
|
38
|
-
catch (
|
|
39
|
-
|
|
38
|
+
catch (err) {
|
|
39
|
+
logger_1.default.error `Invalid translation file at path=${filePath}.`;
|
|
40
|
+
throw err;
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
43
|
return undefined;
|
|
@@ -44,22 +45,18 @@ async function readTranslationFileContent(filePath) {
|
|
|
44
45
|
exports.readTranslationFileContent = readTranslationFileContent;
|
|
45
46
|
function mergeTranslationFileContent({ existingContent = {}, newContent, options, }) {
|
|
46
47
|
// 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
|
-
});
|
|
48
|
+
const newContentTransformed = lodash_1.default.mapValues(newContent, (value) => ({
|
|
49
|
+
...value,
|
|
50
|
+
message: `${options.messagePrefix ?? ''}${value.message}`,
|
|
51
|
+
}));
|
|
54
52
|
const result = { ...existingContent };
|
|
55
53
|
// We only add missing keys here, we don't delete existing ones
|
|
56
54
|
Object.entries(newContentTransformed).forEach(([key, { message, description }]) => {
|
|
57
|
-
var _a, _b;
|
|
58
55
|
result[key] = {
|
|
59
|
-
// If
|
|
56
|
+
// If messages already exist, we don't override them (unless requested)
|
|
60
57
|
message: options.override
|
|
61
58
|
? message
|
|
62
|
-
:
|
|
59
|
+
: existingContent[key]?.message ?? message,
|
|
63
60
|
description, // description
|
|
64
61
|
};
|
|
65
62
|
});
|
|
@@ -68,11 +65,10 @@ function mergeTranslationFileContent({ existingContent = {}, newContent, options
|
|
|
68
65
|
async function writeTranslationFileContent({ filePath, content: newContent, options = {}, }) {
|
|
69
66
|
const existingContent = await readTranslationFileContent(filePath);
|
|
70
67
|
// Warn about potential legacy keys
|
|
71
|
-
const unknownKeys = lodash_1.difference(Object.keys(existingContent
|
|
68
|
+
const unknownKeys = lodash_1.default.difference(Object.keys(existingContent ?? {}), Object.keys(newContent));
|
|
72
69
|
if (unknownKeys.length > 0) {
|
|
73
|
-
|
|
74
|
-
Maybe you should remove them?
|
|
75
|
-
- ${unknownKeys.join('\n- ')}`));
|
|
70
|
+
logger_1.default.warn `Some translation keys looks unknown to us in file path=${filePath}.
|
|
71
|
+
Maybe you should remove them? ${unknownKeys}`;
|
|
76
72
|
}
|
|
77
73
|
const mergedContent = mergeTranslationFileContent({
|
|
78
74
|
existingContent,
|
|
@@ -81,11 +77,9 @@ Maybe you should remove them?
|
|
|
81
77
|
});
|
|
82
78
|
// Avoid creating empty translation files
|
|
83
79
|
if (Object.keys(mergedContent).length > 0) {
|
|
84
|
-
|
|
85
|
-
.length.toString()
|
|
86
|
-
.padStart(3, ' ')} translations will be written at "${utils_1.toMessageRelativeFilePath(filePath)}".`);
|
|
80
|
+
logger_1.default.info `number=${Object.keys(mergedContent).length} translations will be written at path=${(0, utils_1.toMessageRelativeFilePath)(filePath)}.`;
|
|
87
81
|
await fs_extra_1.default.ensureDir(path_1.default.dirname(filePath));
|
|
88
|
-
await fs_extra_1.default.writeFile(filePath, JSON.stringify(mergedContent, null, 2));
|
|
82
|
+
await fs_extra_1.default.writeFile(filePath, `${JSON.stringify(mergedContent, null, 2)}\n`);
|
|
89
83
|
}
|
|
90
84
|
}
|
|
91
85
|
exports.writeTranslationFileContent = writeTranslationFileContent;
|
|
@@ -124,7 +118,7 @@ function addTranslationFileExtension(translationFilePath) {
|
|
|
124
118
|
return `${translationFilePath}.json`;
|
|
125
119
|
}
|
|
126
120
|
function getPluginTranslationFilePath({ siteDir, plugin, locale, translationFilePath, }) {
|
|
127
|
-
const dirPath = utils_1.getPluginI18nPath({
|
|
121
|
+
const dirPath = (0, utils_1.getPluginI18nPath)({
|
|
128
122
|
siteDir,
|
|
129
123
|
locale,
|
|
130
124
|
pluginName: plugin.name,
|
|
@@ -165,32 +159,23 @@ async function localizePluginTranslationFile({ siteDir, plugin, locale, translat
|
|
|
165
159
|
},
|
|
166
160
|
};
|
|
167
161
|
}
|
|
168
|
-
|
|
169
|
-
return translationFile;
|
|
170
|
-
}
|
|
162
|
+
return translationFile;
|
|
171
163
|
}
|
|
172
164
|
exports.localizePluginTranslationFile = localizePluginTranslationFile;
|
|
173
165
|
async function getPluginsDefaultCodeTranslationMessages(plugins) {
|
|
174
|
-
const pluginsMessages = await Promise.all(plugins.map((plugin) =>
|
|
175
|
-
return pluginsMessages.reduce((allMessages, pluginMessages) => {
|
|
176
|
-
return { ...allMessages, ...pluginMessages };
|
|
177
|
-
}, {});
|
|
166
|
+
const pluginsMessages = await Promise.all(plugins.map((plugin) => plugin.getDefaultCodeTranslationMessages?.() ?? {}));
|
|
167
|
+
return pluginsMessages.reduce((allMessages, pluginMessages) => ({ ...allMessages, ...pluginMessages }), {});
|
|
178
168
|
}
|
|
179
169
|
exports.getPluginsDefaultCodeTranslationMessages = getPluginsDefaultCodeTranslationMessages;
|
|
180
170
|
function applyDefaultCodeTranslations({ extractedCodeTranslations, defaultCodeMessages, }) {
|
|
181
|
-
const unusedDefaultCodeMessages = lodash_1.difference(Object.keys(defaultCodeMessages), Object.keys(extractedCodeTranslations));
|
|
171
|
+
const unusedDefaultCodeMessages = lodash_1.default.difference(Object.keys(defaultCodeMessages), Object.keys(extractedCodeTranslations));
|
|
182
172
|
if (unusedDefaultCodeMessages.length > 0) {
|
|
183
|
-
|
|
184
|
-
Please report this Docusaurus issue.
|
|
185
|
-
- ${unusedDefaultCodeMessages.join('\n- ')}
|
|
186
|
-
`));
|
|
173
|
+
logger_1.default.warn `Unused default message codes found.
|
|
174
|
+
Please report this Docusaurus issue. name=${unusedDefaultCodeMessages}`;
|
|
187
175
|
}
|
|
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
|
-
});
|
|
176
|
+
return lodash_1.default.mapValues(extractedCodeTranslations, (messageTranslation, messageId) => ({
|
|
177
|
+
...messageTranslation,
|
|
178
|
+
message: defaultCodeMessages[messageId] ?? messageTranslation.message,
|
|
179
|
+
}));
|
|
195
180
|
}
|
|
196
181
|
exports.applyDefaultCodeTranslations = applyDefaultCodeTranslations;
|
|
@@ -1,7 +1,13 @@
|
|
|
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 { type TransformOptions } from '@babel/core';
|
|
8
|
+
import type { InitializedPlugin, TranslationFileContent, TranslationMessage } 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
13
|
translations: Record<string, TranslationMessage>;
|
|
@@ -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
11
|
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
12
12
|
const traverse_1 = tslib_1.__importDefault(require("@babel/traverse"));
|
|
13
13
|
const generator_1 = tslib_1.__importDefault(require("@babel/generator"));
|
|
14
|
-
const
|
|
14
|
+
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
15
15
|
const core_1 = require("@babel/core");
|
|
16
|
-
const lodash_1 = require("lodash");
|
|
17
16
|
const path_1 = tslib_1.__importDefault(require("path"));
|
|
18
|
-
const
|
|
19
|
-
const
|
|
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
47
|
return codePaths;
|
|
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 = await 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,142 @@ 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
|
|
161
|
+
.get('name')
|
|
162
|
+
.isJSXIdentifier({ name: propName }));
|
|
163
|
+
if (attributePath) {
|
|
164
|
+
const attributeValue = attributePath.get('value');
|
|
165
|
+
const attributeValueEvaluated = attributeValue.isJSXExpressionContainer()
|
|
166
|
+
? attributeValue.get('expression').evaluate()
|
|
167
|
+
: attributeValue.evaluate();
|
|
168
|
+
if (attributeValueEvaluated.confident &&
|
|
169
|
+
typeof attributeValueEvaluated.value === 'string') {
|
|
170
|
+
return attributeValueEvaluated.value;
|
|
171
|
+
}
|
|
172
|
+
warnings.push(`<Translate> prop=${propName} should be a statically evaluable object.
|
|
173
|
+
Example: <Translate id="optional id" description="optional description">Message</Translate>
|
|
174
|
+
Dynamically constructed values are not allowed, because they prevent translations to be extracted.
|
|
175
|
+
${sourceWarningPart(path.node)}`);
|
|
154
176
|
}
|
|
177
|
+
return undefined;
|
|
155
178
|
}
|
|
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
179
|
const id = evaluateJSXProp('id');
|
|
169
180
|
const description = evaluateJSXProp('description');
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
translations[id !== null && id !== void 0 ? id : message] = {
|
|
181
|
+
let message;
|
|
182
|
+
const childrenPath = path.get('children');
|
|
183
|
+
// Handle empty content
|
|
184
|
+
if (!childrenPath.length) {
|
|
185
|
+
if (!id) {
|
|
186
|
+
warnings.push(`<Translate> without children must have id prop.
|
|
187
|
+
Example: <Translate id="my-id" />
|
|
188
|
+
${sourceWarningPart(path.node)}`);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
translations[id] = {
|
|
192
|
+
message: id,
|
|
193
|
+
...(description && { description }),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
// Handle single non-empty content
|
|
199
|
+
const singleChildren = childrenPath
|
|
200
|
+
// Remove empty/useless text nodes that might be around our
|
|
201
|
+
// translation! Makes the translation system more reliable to JSX
|
|
202
|
+
// formatting issues
|
|
203
|
+
.filter((children) => !(children.isJSXText() &&
|
|
204
|
+
children.node.value.replace('\n', '').trim() === ''))
|
|
205
|
+
.pop();
|
|
206
|
+
const isJSXText = singleChildren && singleChildren.isJSXText();
|
|
207
|
+
const isJSXExpressionContainer = singleChildren &&
|
|
208
|
+
singleChildren.isJSXExpressionContainer() &&
|
|
209
|
+
singleChildren.get('expression').evaluate().confident;
|
|
210
|
+
if (isJSXText || isJSXExpressionContainer) {
|
|
211
|
+
message = isJSXText
|
|
212
|
+
? singleChildren.node.value.trim().replace(/\s+/g, ' ')
|
|
213
|
+
: singleChildren.get('expression').evaluate().value;
|
|
214
|
+
translations[id ?? message] = {
|
|
205
215
|
message,
|
|
206
216
|
...(description && { description }),
|
|
207
217
|
};
|
|
208
218
|
}
|
|
209
219
|
else {
|
|
210
|
-
warnings.push(`
|
|
220
|
+
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>.
|
|
221
|
+
${sourceWarningPart(path.node)}`);
|
|
211
222
|
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
223
|
+
},
|
|
224
|
+
}),
|
|
225
|
+
...(translateFunctionName && {
|
|
226
|
+
CallExpression(path) {
|
|
227
|
+
if (!path.get('callee').isIdentifier({ name: translateFunctionName })) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const args = path.get('arguments');
|
|
231
|
+
if (args.length === 1 || args.length === 2) {
|
|
232
|
+
const firstArgPath = args[0];
|
|
233
|
+
// translate("x" + "y"); => translate("xy");
|
|
234
|
+
const firstArgEvaluated = firstArgPath.evaluate();
|
|
235
|
+
if (firstArgEvaluated.confident &&
|
|
236
|
+
typeof firstArgEvaluated.value === 'object') {
|
|
237
|
+
const { message, id, description } = firstArgEvaluated.value;
|
|
238
|
+
translations[id ?? message] = {
|
|
239
|
+
message: message ?? id,
|
|
240
|
+
...(description && { description }),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
warnings.push(`translate() first arg should be a statically evaluable object.
|
|
245
|
+
Example: translate({message: "text",id: "optional.id",description: "optional description"}
|
|
246
|
+
Dynamically constructed values are not allowed, because they prevent translations to be extracted.
|
|
247
|
+
${sourceWarningPart(path.node)}`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
warnings.push(`translate() function only takes 1 or 2 args
|
|
252
|
+
${sourceWarningPart(path.node)}`);
|
|
253
|
+
}
|
|
254
|
+
},
|
|
255
|
+
}),
|
|
217
256
|
});
|
|
218
257
|
return { sourceCodeFilePath, translations, warnings };
|
|
219
258
|
}
|
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 type { RouteConfig } from '@docusaurus/types';
|
|
8
|
+
import { Globby } from '@docusaurus/utils';
|
|
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[]>;
|
package/lib/server/utils.js
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.safeGlobby = exports.getAllFinalRoutes = 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
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.safeGlobby = exports.getAllFinalRoutes = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
13
11
|
const path_1 = tslib_1.__importDefault(require("path"));
|
|
14
12
|
const utils_1 = require("@docusaurus/utils");
|
|
15
13
|
// Recursively get the final routes (routes with no subroutes)
|
|
16
14
|
function getAllFinalRoutes(routeConfig) {
|
|
17
15
|
function getFinalRoutes(route) {
|
|
18
|
-
return route.routes ?
|
|
16
|
+
return route.routes ? route.routes.flatMap(getFinalRoutes) : [route];
|
|
19
17
|
}
|
|
20
|
-
return
|
|
18
|
+
return routeConfig.flatMap(getFinalRoutes);
|
|
21
19
|
}
|
|
22
20
|
exports.getAllFinalRoutes = getAllFinalRoutes;
|
|
23
21
|
// Globby that fix Windows path patterns
|
|
@@ -25,7 +23,7 @@ exports.getAllFinalRoutes = getAllFinalRoutes;
|
|
|
25
23
|
async function safeGlobby(patterns, options) {
|
|
26
24
|
// Required for Windows support, as paths using \ should not be used by globby
|
|
27
25
|
// (also using the windows hard drive prefix like c: is not a good idea)
|
|
28
|
-
const globPaths = patterns.map((dirPath) => utils_1.posixPath(path_1.default.relative(process.cwd(), dirPath)));
|
|
29
|
-
return
|
|
26
|
+
const globPaths = patterns.map((dirPath) => (0, utils_1.posixPath)(path_1.default.relative(process.cwd(), dirPath)));
|
|
27
|
+
return (0, utils_1.Globby)(globPaths, options);
|
|
30
28
|
}
|
|
31
29
|
exports.safeGlobby = safeGlobby;
|
|
File without changes
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { DocusaurusPluginVersionInformation } from '@docusaurus/types';
|
|
8
|
-
export declare function getPackageJsonVersion(packageJsonPath: string): string | undefined
|
|
9
|
-
export declare function
|
|
10
|
-
export declare function getPluginVersion(pluginPath: string, siteDir: string): DocusaurusPluginVersionInformation;
|
|
7
|
+
import type { DocusaurusPluginVersionInformation } from '@docusaurus/types';
|
|
8
|
+
export declare function getPackageJsonVersion(packageJsonPath: string): Promise<string | undefined>;
|
|
9
|
+
export declare function getPluginVersion(pluginPath: string, siteDir: string): Promise<DocusaurusPluginVersionInformation>;
|