mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-31 09:57:03 +02:00
refactor(core): reduce code verbosity (#6734)
* refactor(core): reduce code verbosity * fix * fix
This commit is contained in:
parent
c38200ba5b
commit
671873a681
3 changed files with 8 additions and 42 deletions
|
@ -206,11 +206,7 @@ async function buildLocale({
|
|||
await compile([clientConfig, serverConfig]);
|
||||
|
||||
// Remove server.bundle.js because it is not needed.
|
||||
if (
|
||||
serverConfig.output &&
|
||||
serverConfig.output.filename &&
|
||||
typeof serverConfig.output.filename === 'string'
|
||||
) {
|
||||
if (typeof serverConfig.output?.filename === 'string') {
|
||||
const serverBundle = path.join(outDir, serverConfig.output.filename);
|
||||
if (await fs.pathExists(serverBundle)) {
|
||||
await fs.unlink(serverBundle);
|
||||
|
|
|
@ -5,12 +5,7 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {
|
||||
loadI18n,
|
||||
localizePath,
|
||||
getDefaultLocaleConfig,
|
||||
shouldWarnAboutNodeVersion,
|
||||
} from '../i18n';
|
||||
import {loadI18n, localizePath, getDefaultLocaleConfig} from '../i18n';
|
||||
import {DEFAULT_I18N_CONFIG} from '../configValidation';
|
||||
import path from 'path';
|
||||
import type {I18nConfig} from '@docusaurus/types';
|
||||
|
@ -83,20 +78,6 @@ describe('defaultLocaleConfig', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('shouldWarnAboutNodeVersion', () => {
|
||||
test('warns for old NodeJS version and [en,fr]', () => {
|
||||
expect(shouldWarnAboutNodeVersion(12, ['en', 'fr'])).toEqual(true);
|
||||
});
|
||||
|
||||
test('not warn for old NodeJS version and [en]', () => {
|
||||
expect(shouldWarnAboutNodeVersion(12, ['en'])).toEqual(false);
|
||||
});
|
||||
|
||||
test('not warn for recent NodeJS version and [en,fr]', () => {
|
||||
expect(shouldWarnAboutNodeVersion(14, ['en', 'fr'])).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadI18n', () => {
|
||||
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation();
|
||||
beforeEach(() => {
|
||||
|
@ -215,7 +196,7 @@ describe('localizePath', () => {
|
|||
},
|
||||
options: {localizePath: true},
|
||||
}),
|
||||
).toEqual(`${path.sep}baseFsPath${path.sep}fr${path.sep}`);
|
||||
).toEqual(`${path.sep}baseFsPath${path.sep}fr`);
|
||||
});
|
||||
|
||||
test('should localize path for default locale, if requested', () => {
|
||||
|
|
|
@ -28,15 +28,6 @@ export function getDefaultLocaleConfig(locale: string): I18nLocaleConfig {
|
|||
};
|
||||
}
|
||||
|
||||
export function shouldWarnAboutNodeVersion(
|
||||
version: number,
|
||||
locales: string[],
|
||||
): boolean {
|
||||
const isOnlyEnglish = locales.length === 1 && locales.includes('en');
|
||||
const isOlderNodeVersion = version < 14;
|
||||
return isOlderNodeVersion && !isOnlyEnglish;
|
||||
}
|
||||
|
||||
export async function loadI18n(
|
||||
config: DocusaurusConfig,
|
||||
options: {locale?: string} = {},
|
||||
|
@ -86,21 +77,19 @@ export function localizePath({
|
|||
options?: {localizePath?: boolean};
|
||||
}): string {
|
||||
const shouldLocalizePath: boolean =
|
||||
typeof options.localizePath === 'undefined'
|
||||
? // By default, we don't localize the path of defaultLocale
|
||||
i18n.currentLocale !== i18n.defaultLocale
|
||||
: options.localizePath;
|
||||
// By default, we don't localize the path of defaultLocale
|
||||
options.localizePath ?? i18n.currentLocale !== i18n.defaultLocale;
|
||||
|
||||
if (!shouldLocalizePath) {
|
||||
return originalPath;
|
||||
}
|
||||
// FS paths need special care, for Windows support
|
||||
if (pathType === 'fs') {
|
||||
return path.join(originalPath, path.sep, i18n.currentLocale, path.sep);
|
||||
return path.join(originalPath, i18n.currentLocale);
|
||||
}
|
||||
// Url paths
|
||||
// Url paths; add a trailing slash so it's a valid base URL
|
||||
if (pathType === 'url') {
|
||||
return normalizeUrl([originalPath, '/', i18n.currentLocale, '/']);
|
||||
return normalizeUrl([originalPath, i18n.currentLocale, '/']);
|
||||
}
|
||||
// should never happen
|
||||
throw new Error(`Unhandled path type "${pathType}".`);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue