feat(core): allow plugin/preset config to contain false/null (#7124)

This commit is contained in:
Joshua Chen 2022-04-07 21:27:20 +08:00 committed by GitHub
parent 0963bff5e7
commit f9c0a5a6d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 9 deletions

View file

@ -27,9 +27,15 @@ export type PluginConfig =
| string | string
| [string, PluginOptions] | [string, PluginOptions]
| [PluginModule, PluginOptions] | [PluginModule, PluginOptions]
| PluginModule; | PluginModule
| false
| null;
export type PresetConfig = string | [string, {[key: string]: unknown}]; export type PresetConfig =
| string
| [string, {[key: string]: unknown}]
| false
| null;
export type ThemeConfig = { export type ThemeConfig = {
[key: string]: unknown; [key: string]: unknown;

View file

@ -173,6 +173,7 @@ describe('normalizeConfig', () => {
'should accept [function, object] for plugin', 'should accept [function, object] for plugin',
[[() => {}, {it: 'should work'}]], [[() => {}, {it: 'should work'}]],
], ],
['should accept false/null for plugin', [false, null, 'classic']],
])(`%s for the input of: %p`, (_message, plugins) => { ])(`%s for the input of: %p`, (_message, plugins) => {
expect(() => { expect(() => {
normalizeConfig({ normalizeConfig({
@ -211,6 +212,7 @@ describe('normalizeConfig', () => {
'should accept [function, object] for theme', 'should accept [function, object] for theme',
[[function theme() {}, {it: 'should work'}]], [[function theme() {}, {it: 'should work'}]],
], ],
['should accept false/null for themes', [false, null, 'classic']],
])(`%s for the input of: %p`, (_message, themes) => { ])(`%s for the input of: %p`, (_message, themes) => {
expect(() => { expect(() => {
normalizeConfig({ normalizeConfig({
@ -254,6 +256,14 @@ describe('normalizeConfig', () => {
`); `);
}); });
it('accepts presets as false / null', () => {
expect(() => {
normalizeConfig({
presets: [false, null, 'classic'],
});
}).not.toThrow();
});
it("throws error if scripts doesn't have src", () => { it("throws error if scripts doesn't have src", () => {
expect(() => { expect(() => {
normalizeConfig({ normalizeConfig({

View file

@ -71,7 +71,7 @@ function createPluginSchema(theme: boolean) {
Joi.array() Joi.array()
.ordered(Joi.string().required(), Joi.object().required()) .ordered(Joi.string().required(), Joi.object().required())
.length(2), .length(2),
Joi.bool().equal(false), // In case of conditional adding of plugins. Joi.any().valid(false, null),
) )
// @ts-expect-error: bad lib def, doesn't recognize an array of reports // @ts-expect-error: bad lib def, doesn't recognize an array of reports
.error((errors) => { .error((errors) => {
@ -119,6 +119,7 @@ const PresetSchema = Joi.alternatives()
Joi.array() Joi.array()
.items(Joi.string().required(), Joi.object().required()) .items(Joi.string().required(), Joi.object().required())
.length(2), .length(2),
Joi.any().valid(false, null),
) )
.messages({ .messages({
'alternatives.types': `{#label} does not look like a valid preset config. A preset config entry should be one of: 'alternatives.types': `{#label} does not look like a valid preset config. A preset config entry should be one of:

View file

@ -7,7 +7,7 @@
module.exports = function preset(context, opts = {}) { module.exports = function preset(context, opts = {}) {
return { return {
themes: [['@docusaurus/theme-classic', opts.test]], themes: [['@docusaurus/theme-classic', opts.test], null],
plugins: [['@docusaurus/plugin-test', opts.test]], plugins: [['@docusaurus/plugin-test', opts.test], false],
}; };
}; };

View file

@ -107,6 +107,7 @@ exports[`loadPresets mixed form with themes 1`] = `
"@docusaurus/plugin-test", "@docusaurus/plugin-test",
undefined, undefined,
], ],
false,
], ],
"themes": [ "themes": [
[ [
@ -121,6 +122,7 @@ exports[`loadPresets mixed form with themes 1`] = `
"@docusaurus/theme-classic", "@docusaurus/theme-classic",
undefined, undefined,
], ],
null,
], ],
} }
`; `;

View file

@ -17,7 +17,7 @@ import type {
} from '@docusaurus/types'; } from '@docusaurus/types';
async function normalizePluginConfig( async function normalizePluginConfig(
pluginConfig: PluginConfig, pluginConfig: Exclude<PluginConfig, false | null>,
configPath: string, configPath: string,
pluginRequire: NodeRequire, pluginRequire: NodeRequire,
): Promise<NormalizedPluginConfig> { ): Promise<NormalizedPluginConfig> {
@ -120,7 +120,7 @@ export async function loadPluginConfigs(
// Site config should be the highest priority. // Site config should be the highest priority.
...standalonePlugins, ...standalonePlugins,
...standaloneThemes, ...standaloneThemes,
]; ].filter(<T>(x: T | null | false): x is T => Boolean(x));
return Promise.all( return Promise.all(
pluginConfigs.map((pluginConfig) => pluginConfigs.map((pluginConfig) =>
normalizePluginConfig( normalizePluginConfig(

View file

@ -33,6 +33,9 @@ export async function loadPresets(
presets.forEach((presetItem) => { presets.forEach((presetItem) => {
let presetModuleImport: string; let presetModuleImport: string;
let presetOptions = {}; let presetOptions = {};
if (!presetItem) {
return;
}
if (typeof presetItem === 'string') { if (typeof presetItem === 'string') {
presetModuleImport = presetItem; presetModuleImport = presetItem;
} else { } else {
@ -53,10 +56,10 @@ export async function loadPresets(
); );
if (preset.plugins) { if (preset.plugins) {
plugins.push(...preset.plugins.filter(Boolean)); plugins.push(...preset.plugins);
} }
if (preset.themes) { if (preset.themes) {
themes.push(...preset.themes.filter(Boolean)); themes.push(...preset.themes);
} }
}); });