refactor(core): reduce code verbosity (#6734)

* refactor(core): reduce code verbosity

* fix

* fix
This commit is contained in:
Joshua Chen 2022-02-21 19:31:22 +08:00 committed by GitHub
parent c38200ba5b
commit 671873a681
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 42 deletions

View file

@ -206,11 +206,7 @@ async function buildLocale({
await compile([clientConfig, serverConfig]); await compile([clientConfig, serverConfig]);
// Remove server.bundle.js because it is not needed. // Remove server.bundle.js because it is not needed.
if ( if (typeof serverConfig.output?.filename === 'string') {
serverConfig.output &&
serverConfig.output.filename &&
typeof serverConfig.output.filename === 'string'
) {
const serverBundle = path.join(outDir, serverConfig.output.filename); const serverBundle = path.join(outDir, serverConfig.output.filename);
if (await fs.pathExists(serverBundle)) { if (await fs.pathExists(serverBundle)) {
await fs.unlink(serverBundle); await fs.unlink(serverBundle);

View file

@ -5,12 +5,7 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import { import {loadI18n, localizePath, getDefaultLocaleConfig} from '../i18n';
loadI18n,
localizePath,
getDefaultLocaleConfig,
shouldWarnAboutNodeVersion,
} from '../i18n';
import {DEFAULT_I18N_CONFIG} from '../configValidation'; import {DEFAULT_I18N_CONFIG} from '../configValidation';
import path from 'path'; import path from 'path';
import type {I18nConfig} from '@docusaurus/types'; 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', () => { describe('loadI18n', () => {
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(); const consoleSpy = jest.spyOn(console, 'warn').mockImplementation();
beforeEach(() => { beforeEach(() => {
@ -215,7 +196,7 @@ describe('localizePath', () => {
}, },
options: {localizePath: true}, 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', () => { test('should localize path for default locale, if requested', () => {

View file

@ -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( export async function loadI18n(
config: DocusaurusConfig, config: DocusaurusConfig,
options: {locale?: string} = {}, options: {locale?: string} = {},
@ -86,21 +77,19 @@ export function localizePath({
options?: {localizePath?: boolean}; options?: {localizePath?: boolean};
}): string { }): string {
const shouldLocalizePath: boolean = const shouldLocalizePath: boolean =
typeof options.localizePath === 'undefined' // By default, we don't localize the path of defaultLocale
? // By default, we don't localize the path of defaultLocale options.localizePath ?? i18n.currentLocale !== i18n.defaultLocale;
i18n.currentLocale !== i18n.defaultLocale
: options.localizePath;
if (!shouldLocalizePath) { if (!shouldLocalizePath) {
return originalPath; return originalPath;
} }
// FS paths need special care, for Windows support // FS paths need special care, for Windows support
if (pathType === 'fs') { 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') { if (pathType === 'url') {
return normalizeUrl([originalPath, '/', i18n.currentLocale, '/']); return normalizeUrl([originalPath, i18n.currentLocale, '/']);
} }
// should never happen // should never happen
throw new Error(`Unhandled path type "${pathType}".`); throw new Error(`Unhandled path type "${pathType}".`);