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, 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 = {
[key: string]: unknown;

View file

@ -173,6 +173,7 @@ describe('normalizeConfig', () => {
'should accept [function, object] for plugin',
[[() => {}, {it: 'should work'}]],
],
['should accept false/null for plugin', [false, null, 'classic']],
])(`%s for the input of: %p`, (_message, plugins) => {
expect(() => {
normalizeConfig({
@ -211,6 +212,7 @@ describe('normalizeConfig', () => {
'should accept [function, object] for theme',
[[function theme() {}, {it: 'should work'}]],
],
['should accept false/null for themes', [false, null, 'classic']],
])(`%s for the input of: %p`, (_message, themes) => {
expect(() => {
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", () => {
expect(() => {
normalizeConfig({

View file

@ -71,7 +71,7 @@ function createPluginSchema(theme: boolean) {
Joi.array()
.ordered(Joi.string().required(), Joi.object().required())
.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
.error((errors) => {
@ -119,6 +119,7 @@ const PresetSchema = Joi.alternatives()
Joi.array()
.items(Joi.string().required(), Joi.object().required())
.length(2),
Joi.any().valid(false, null),
)
.messages({
'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 = {}) {
return {
themes: [['@docusaurus/theme-classic', opts.test]],
plugins: [['@docusaurus/plugin-test', opts.test]],
themes: [['@docusaurus/theme-classic', opts.test], null],
plugins: [['@docusaurus/plugin-test', opts.test], false],
};
};

View file

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

View file

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

View file

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