Skip to content

Commit

Permalink
fix swizzle context case
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Jul 10, 2024
1 parent 3dc85f3 commit 5dd12e1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 19 deletions.
39 changes: 28 additions & 11 deletions packages/docusaurus/src/commands/swizzle/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,41 @@
*/

import {loadContext} from '../../server/site';
import {initPlugins} from '../../server/plugins/init';
import {initPluginsConfigs} from '../../server/plugins/init';
import {loadPluginConfigs} from '../../server/plugins/configs';
import type {SwizzleCLIOptions, SwizzleContext} from './common';
import type {SwizzleCLIOptions, SwizzleContext, SwizzlePlugin} from './common';
import type {LoadContext} from '@docusaurus/types';

async function getSwizzlePlugins(
context: LoadContext,
): Promise<SwizzlePlugin[]> {
const pluginConfigs = await loadPluginConfigs(context);
const pluginConfigInitResults = await initPluginsConfigs(
context,
pluginConfigs,
);

return pluginConfigInitResults.flatMap((initResult) => {
// Ignore self-disabling plugins returning null
if (initResult.plugin === null) {
return [];
}
return [
// TODO this is a bit confusing, need refactor
{
plugin: initResult.config,
instance: initResult.plugin,
},
];
});
}

export async function initSwizzleContext(
siteDir: string,
options: SwizzleCLIOptions,
): Promise<SwizzleContext> {
const context = await loadContext({siteDir, config: options.config});
const plugins = await initPlugins(context);
const pluginConfigs = await loadPluginConfigs(context);

// TODO same index assumption is now wrong!

return {
plugins: plugins.map((plugin, pluginIndex) => ({
plugin: pluginConfigs[pluginIndex]!,
instance: plugin,
})),
plugins: await getSwizzlePlugins(context),
};
}
52 changes: 44 additions & 8 deletions packages/docusaurus/src/server/plugins/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,32 @@ function getThemeValidationFunction(
return normalizedPluginConfig.plugin.validateThemeConfig;
}

type PluginConfigInitResult = {
config: NormalizedPluginConfig;
// Plugins might self-disable during initialization by returning null
plugin: InitializedPlugin | null;
};

// This filters self-disabling plugins and returns only the initialized ones
function onlyInitializedPlugins(
initPluginsConfigsResults: PluginConfigInitResult[],
): InitializedPlugin[] {
return initPluginsConfigsResults
.map((results) => results.plugin)
.filter((p) => p !== null);
}

/**
* Runs the plugin constructors and returns their return values. It would load
* plugin configs from `plugins`, `themes`, and `presets`.
*/
export async function initPlugins(
export async function initPluginsConfigs(
context: LoadContext,
): Promise<InitializedPlugin[]> {
pluginConfigs: NormalizedPluginConfig[],
): Promise<PluginConfigInitResult[]> {
// We need to resolve plugins from the perspective of the site config, as if
// we are using `require.resolve` on those module names.
const pluginRequire = createRequire(context.siteConfigPath);
const pluginConfigs = await loadPluginConfigs(context);

async function doLoadPluginVersion(
normalizedPluginConfig: NormalizedPluginConfig,
Expand Down Expand Up @@ -108,7 +123,7 @@ export async function initPlugins(

async function initializePlugin(
normalizedPluginConfig: NormalizedPluginConfig,
): Promise<InitializedPlugin | null> {
): Promise<PluginConfigInitResult> {
const pluginVersion: PluginVersionInformation = await doLoadPluginVersion(
normalizedPluginConfig,
);
Expand All @@ -131,7 +146,7 @@ export async function initPlugins(
// It's a way for plugins to self-disable depending on context
// See https://github.com/facebook/docusaurus/pull/10286
if (pluginInstance === null) {
return null;
return {config: normalizedPluginConfig, plugin: null};
}
if (pluginInstance === undefined) {
throw new Error(
Expand All @@ -148,19 +163,40 @@ Note that even inline/anonymous plugin functions require a 'name' property.`,
);
}

return {
const plugin: InitializedPlugin = {
...pluginInstance,
options: pluginOptions,
version: pluginVersion,
path: path.dirname(normalizedPluginConfig.entryPath),
};

return {
config: normalizedPluginConfig,
plugin,
};
}

const plugins: InitializedPlugin[] = (
const plugins: PluginConfigInitResult[] = (
await Promise.all(pluginConfigs.map(initializePlugin))
).filter((p) => p !== null);

ensureUniquePluginInstanceIds(plugins);
ensureUniquePluginInstanceIds(onlyInitializedPlugins(plugins));

return plugins;
}

/**
* Runs the plugin constructors and returns their return values
* for all the site context plugins that do not return null to self-disable.
*/
export async function initPlugins(
context: LoadContext,
): Promise<InitializedPlugin[]> {
const pluginConfigs = await loadPluginConfigs(context);
const initPluginsConfigsResults = await initPluginsConfigs(
context,
pluginConfigs,
);

return onlyInitializedPlugins(initPluginsConfigsResults);
}

0 comments on commit 5dd12e1

Please sign in to comment.