8000 feat(core): allow plugin/preset config to contain false/null by Josh-Cena · Pull Request #7124 · facebook/docusaurus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus/src/server/configValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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:
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ exports[`loadPresets mixed form with themes 1`] = `
"@docusaurus/plugin-test",
undefined,
],
false,
],
"themes": [
[
Expand All @@ -121,6 +122,7 @@ exports[`loadPresets mixed form with themes 1`] = `
"@docusaurus/theme-classic",
undefined,
],
null,
],
}
`;
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus/src/server/plugins/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions packages/docusaurus/src/server/plugins/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
});

Expand Down
0