refactor: remove unnecessary default values normalized during validation (#6864)

* refactor: remove unnecessary default values normalized during validation

* more
This commit is contained in:
Joshua Chen 2022-03-07 19:23:30 +08:00 committed by GitHub
parent 7fc134ba0e
commit 8e934450d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 146 additions and 138 deletions

View file

@ -30,5 +30,5 @@ Current type: ${isValidElement(children) ? 'React element' : typeof children}`);
return <>{children()}</>;
}
return fallback || null;
return fallback ?? null;
}

View file

@ -179,7 +179,7 @@ function Link(
{...props}
onMouseEnter={onMouseEnter}
innerRef={handleRef}
to={targetLink || ''}
to={targetLink}
// avoid "React does not recognize the `activeClassName` prop on a DOM
// element"
{...(isNavLink && {isActive, activeClassName})}

View file

@ -116,14 +116,14 @@ async function doRender(locals: Locals & {path: string}) {
// manifest information.
const modulesToBeLoaded = [...manifest.entrypoints, ...Array.from(modules)];
const bundles = getBundles(manifest, modulesToBeLoaded);
const stylesheets = (bundles.css || []).map((b) => b.file);
const scripts = (bundles.js || []).map((b) => b.file);
const stylesheets = (bundles.css ?? []).map((b) => b.file);
const scripts = (bundles.js ?? []).map((b) => b.file);
const renderedHtml = renderSSRTemplate(ssrTemplate, {
appHtml,
baseUrl,
htmlAttributes: htmlAttributes || '',
bodyAttributes: bodyAttributes || '',
htmlAttributes,
bodyAttributes,
headTags,
preBodyTags,
postBodyTags,

View file

@ -12,7 +12,7 @@ import {DEFAULT_PORT} from '@docusaurus/utils';
export function getCLIOptionHost(
hostOption: HostPortCLIOptions['host'],
): string {
return hostOption || 'localhost';
return hostOption ?? 'localhost';
}
export async function getCLIOptionPort(

View file

@ -75,7 +75,7 @@ export default async function start(
logger.error(err.stack);
});
}, 500);
const {siteConfig, plugins = []} = props;
const {siteConfig, plugins} = props;
const normalizeToSiteDir = (filepath: string) => {
if (filepath && path.isAbsolute(filepath)) {
@ -84,12 +84,9 @@ export default async function start(
return posixPath(filepath);
};
const pluginPaths = ([] as string[])
.concat(
...plugins
.map((plugin) => plugin.getPathsToWatch?.() ?? [])
.filter(Boolean),
)
const pluginPaths = plugins
.flatMap((plugin) => plugin.getPathsToWatch?.() ?? [])
.filter(Boolean)
.map(normalizeToSiteDir);
const pathsToWatch = [

View file

@ -15,6 +15,7 @@ exports[`loadConfig website with valid async config 1`] = `
Object {
"baseUrl": "/",
"baseUrlIssueBanner": true,
"clientModules": Array [],
"customFields": Object {},
"i18n": Object {
"defaultLocale": "en",
@ -31,9 +32,11 @@ Object {
"plugins": Array [],
"presets": Array [],
"projectName": "hello",
"scripts": Array [],
"staticDirectories": Array [
"static",
],
"stylesheets": Array [],
"tagline": "Hello World",
"themeConfig": Object {},
"themes": Array [],
@ -47,6 +50,7 @@ exports[`loadConfig website with valid async config creator function 1`] = `
Object {
"baseUrl": "/",
"baseUrlIssueBanner": true,
"clientModules": Array [],
"customFields": Object {},
"i18n": Object {
"defaultLocale": "en",
@ -63,9 +67,11 @@ Object {
"plugins": Array [],
"presets": Array [],
"projectName": "hello",
"scripts": Array [],
"staticDirectories": Array [
"static",
],
"stylesheets": Array [],
"tagline": "Hello World",
"themeConfig": Object {},
"themes": Array [],
@ -79,6 +85,7 @@ exports[`loadConfig website with valid config creator function 1`] = `
Object {
"baseUrl": "/",
"baseUrlIssueBanner": true,
"clientModules": Array [],
"customFields": Object {},
"i18n": Object {
"defaultLocale": "en",
@ -95,9 +102,11 @@ Object {
"plugins": Array [],
"presets": Array [],
"projectName": "hello",
"scripts": Array [],
"staticDirectories": Array [
"static",
],
"stylesheets": Array [],
"tagline": "Hello World",
"themeConfig": Object {},
"themes": Array [],
@ -111,6 +120,7 @@ exports[`loadConfig website with valid siteConfig 1`] = `
Object {
"baseUrl": "/",
"baseUrlIssueBanner": true,
"clientModules": Array [],
"customFields": Object {},
"favicon": "img/docusaurus.ico",
"i18n": Object {
@ -136,9 +146,11 @@ Object {
],
"presets": Array [],
"projectName": "hello",
"scripts": Array [],
"staticDirectories": Array [
"static",
],
"stylesheets": Array [],
"tagline": "Hello World",
"themeConfig": Object {},
"themes": Array [],

View file

@ -26,6 +26,9 @@ export const DEFAULT_CONFIG: Pick<
| 'plugins'
| 'themes'
| 'presets'
| 'stylesheets'
| 'scripts'
| 'clientModules'
| 'customFields'
| 'themeConfig'
| 'titleDelimiter'
@ -41,6 +44,9 @@ export const DEFAULT_CONFIG: Pick<
plugins: [],
themes: [],
presets: [],
stylesheets: [],
scripts: [],
clientModules: [],
customFields: {},
themeConfig: {},
titleDelimiter: '|',
@ -170,25 +176,31 @@ export const ConfigSchema = Joi.object({
themes: Joi.array().items(ThemeSchema).default(DEFAULT_CONFIG.themes),
presets: Joi.array().items(PresetSchema).default(DEFAULT_CONFIG.presets),
themeConfig: Joi.object().unknown().default(DEFAULT_CONFIG.themeConfig),
scripts: Joi.array().items(
Joi.string(),
Joi.object({
src: Joi.string().required(),
async: Joi.bool(),
defer: Joi.bool(),
})
// See https://github.com/facebook/docusaurus/issues/3378
.unknown(),
),
scripts: Joi.array()
.items(
Joi.string(),
Joi.object({
src: Joi.string().required(),
async: Joi.bool(),
defer: Joi.bool(),
})
// See https://github.com/facebook/docusaurus/issues/3378
.unknown(),
)
.default(DEFAULT_CONFIG.scripts),
ssrTemplate: Joi.string(),
stylesheets: Joi.array().items(
Joi.string(),
Joi.object({
href: Joi.string().required(),
type: Joi.string(),
}).unknown(),
),
clientModules: Joi.array().items(Joi.string()),
stylesheets: Joi.array()
.items(
Joi.string(),
Joi.object({
href: Joi.string().required(),
type: Joi.string(),
}).unknown(),
)
.default(DEFAULT_CONFIG.stylesheets),
clientModules: Joi.array()
.items(Joi.string())
.default(DEFAULT_CONFIG.clientModules),
tagline: Joi.string().allow('').default(DEFAULT_CONFIG.tagline),
titleDelimiter: Joi.string().default('|'),
noIndex: Joi.bool().default(false),

View file

@ -33,7 +33,7 @@ export default function htmlTagObjectToString(tagDefinition: unknown): string {
);
}
const isVoidTag = voidHtmlTags.indexOf(tagDefinition.tagName) !== -1;
const tagAttributes = tagDefinition.attributes || {};
const tagAttributes = tagDefinition.attributes ?? {};
const attributes = Object.keys(tagAttributes)
.filter((attributeName) => tagAttributes[attributeName] !== false)
.map((attributeName) => {

View file

@ -28,7 +28,7 @@ export function loadHtmlTags(plugins: LoadedPlugin[]): InjectedHtmlTags {
return acc;
}
const {headTags, preBodyTags, postBodyTags} =
plugin.injectHtmlTags({content: plugin.content}) || {};
plugin.injectHtmlTags({content: plugin.content}) ?? {};
return {
headTags: headTags
? `${acc.headTags}\n${createHtmlTagsString(headTags)}`

View file

@ -160,10 +160,10 @@ export async function loadPluginConfigs(
presetThemes = presetThemes.map((theme) =>
normalizeShorthand(theme, 'theme'),
);
const standalonePlugins = (siteConfig.plugins || []).map((plugin) =>
const standalonePlugins = siteConfig.plugins.map((plugin) =>
normalizeShorthand(plugin, 'plugin'),
);
const standaloneThemes = (siteConfig.themes || []).map((theme) =>
const standaloneThemes = siteConfig.themes.map((theme) =>
normalizeShorthand(theme, 'theme'),
);
return [
@ -184,9 +184,9 @@ function createBootstrapPlugin({
siteConfig: DocusaurusConfig;
}): LoadedPlugin {
const {
stylesheets = [],
scripts = [],
clientModules: siteConfigClientModules = [],
stylesheets,
scripts,
clientModules: siteConfigClientModules,
} = siteConfig;
return {
name: 'docusaurus-bootstrap-plugin',
@ -314,6 +314,7 @@ export async function load(
generatedFilesDir,
'DONT-EDIT-THIS-FOLDER',
`This folder stores temp files that Docusaurus' client bundler accesses.
DO NOT hand-modify files in this folder because they will be overwritten in the
next build. You can clear all build artifacts (including this folder) with the
\`docusaurus clear\` command.
@ -326,11 +327,12 @@ next build. You can clear all build artifacts (including this folder) with the
generatedFilesDir,
DEFAULT_CONFIG_FILE_NAME,
`/*
AUTOGENERATED - DON'T EDIT
Your edits in this file will be overwritten in the next build!
Modify the docusaurus.config.js file at your site's root instead.
*/
export default ${JSON.stringify(siteConfig, null, 2)};`,
* AUTOGENERATED - DON'T EDIT
* Your edits in this file will be overwritten in the next build!
* Modify the docusaurus.config.js file at your site's root instead.
*/
export default ${JSON.stringify(siteConfig, null, 2)};
`,
);
plugins.push(createBootstrapPlugin({siteConfig}));
@ -341,11 +343,14 @@ export default ${JSON.stringify(siteConfig, null, 2)};`,
const genClientModules = generate(
generatedFilesDir,
'client-modules.js',
`export default [\n${clientModules
// import() is async so we use require() because client modules can have
// CSS and the order matters for loading CSS.
.map((module) => ` require('${escapePath(module)}'),`)
.join('\n')}\n];\n`,
`export default [
${clientModules
// import() is async so we use require() because client modules can have
// CSS and the order matters for loading CSS.
.map((module) => ` require('${escapePath(module)}'),`)
.join('\n')}
];
`,
);
// Load extra head & body html tags.
@ -367,7 +372,8 @@ ${Object.entries(registry)
chunk.modulePath,
)}', require.resolveWeak('${escapePath(chunk.modulePath)}')],`,
)
.join('\n')}};\n`,
.join('\n')}};
`,
);
const genRoutesChunkNames = generate(

View file

@ -14,7 +14,9 @@ describe('loadPresets', () => {
test('no presets', async () => {
const context = {
siteConfigPath: __dirname,
siteConfig: {},
siteConfig: {
presets: [],
},
} as LoadContext;
const presets = await loadPresets(context);
expect(presets).toMatchInlineSnapshot(`

View file

@ -10,7 +10,6 @@ import importFresh from 'import-fresh';
import type {
LoadContext,
PluginConfig,
PresetConfig,
ImportedPresetModule,
} from '@docusaurus/types';
import {resolveModuleName} from '../moduleShorthand';
@ -23,7 +22,7 @@ export default async function loadPresets(context: LoadContext): Promise<{
// siteDir's package.json declares the dependency on these presets.
const presetRequire = createRequire(context.siteConfigPath);
const presets: PresetConfig[] = context.siteConfig.presets || [];
const {presets} = context.siteConfig;
const unflatPlugins: PluginConfig[][] = [];
const unflatThemes: PluginConfig[][] = [];

View file

@ -176,7 +176,7 @@ export default class CleanWebpackPlugin {
stats.toJson({
all: false,
assets: true,
}).assets || [];
}).assets ?? [];
const assets = statsAssets.map((asset: {name: string}) => asset.name);
/**