diff --git a/packages/histoire-plugin-nuxt/src/index.ts b/packages/histoire-plugin-nuxt/src/index.ts index b2c6f891..13fd7a01 100644 --- a/packages/histoire-plugin-nuxt/src/index.ts +++ b/packages/histoire-plugin-nuxt/src/index.ts @@ -17,7 +17,7 @@ export function HstNuxt (): Plugin { name: '@histoire/plugin-nuxt', // @ts-expect-error Different versions of Vite in devDependencies @TODO update when Nuxt switches to Vite 3 - async config () { + async defaultConfig () { const nuxtConfig = await useNuxtViteConfig() nuxt = nuxtConfig.nuxt const plugins = nuxtConfig.viteConfig.plugins.filter((p: any) => !ignorePlugins.includes(p?.name)) @@ -68,6 +68,7 @@ async function useNuxtViteConfig () { return { viteConfig: await new Promise((resolve) => { nuxt.hook('vite:extendConfig', (config, { isClient }) => { + // @ts-expect-error Different versions of Vite in devDependencies @TODO update when Nuxt switches to Vite 3 if (isClient) resolve({ ...config }) }) nuxt.ready().then(async () => { diff --git a/packages/histoire-plugin-vue/src/index.ts b/packages/histoire-plugin-vue/src/index.ts index 865c8973..d08a1c50 100644 --- a/packages/histoire-plugin-vue/src/index.ts +++ b/packages/histoire-plugin-vue/src/index.ts @@ -6,7 +6,7 @@ export function HstVue (): Plugin { return { name: '@histoire/plugin-vue', - config () { + defaultConfig () { return { supportMatch: [ { diff --git a/packages/histoire-plugin-vue2/src/index.ts b/packages/histoire-plugin-vue2/src/index.ts index b53d0bd2..8a3584b6 100644 --- a/packages/histoire-plugin-vue2/src/index.ts +++ b/packages/histoire-plugin-vue2/src/index.ts @@ -5,7 +5,7 @@ export function HstVue (): Plugin { return { name: '@histoire/plugin-vue2', - config () { + defaultConfig () { return { supportMatch: [ { diff --git a/packages/histoire/src/node/builtin-plugins/vanilla-support/plugin.ts b/packages/histoire/src/node/builtin-plugins/vanilla-support/plugin.ts index 1937cadd..ce4db75c 100644 --- a/packages/histoire/src/node/builtin-plugins/vanilla-support/plugin.ts +++ b/packages/histoire/src/node/builtin-plugins/vanilla-support/plugin.ts @@ -8,7 +8,7 @@ export function vanillaSupport (): Plugin { return { name: 'builtin:vanilla-support', - config () { + defaultConfig () { return { supportMatch: [ { diff --git a/packages/histoire/src/node/config.ts b/packages/histoire/src/node/config.ts index 8a85ac76..22ae58e1 100644 --- a/packages/histoire/src/node/config.ts +++ b/packages/histoire/src/node/config.ts @@ -409,7 +409,11 @@ export async function resolveConfig (cwd: string = process.cwd(), mode: ConfigMo } const viteConfig = await resolveViteConfig({}, 'serve') const viteHistoireConfig = (viteConfig.histoire ?? {}) as HistoireConfig - return processConfig(mergeConfig(result, viteHistoireConfig, getDefaultConfig()), mode) + + const preUserConfig = mergeConfig(result, viteHistoireConfig) + const processedDefaultConfig = await processDefaultConfig(getDefaultConfig(), preUserConfig, mode) + + return processConfig(mergeConfig(preUserConfig, processedDefaultConfig), mode) } export async function processConfig (config: HistoireConfig, mode: ConfigMode): Promise { @@ -425,6 +429,18 @@ export async function processConfig (config: HistoireConfig, mode: ConfigMode): return config } +export async function processDefaultConfig (defaultConfig: HistoireConfig, preUserConfig: HistoireConfig, mode: ConfigMode): Promise { + for (const plugin of [...defaultConfig.plugins, ...preUserConfig.plugins]) { + if (plugin.defaultConfig) { + const result = await plugin.defaultConfig(defaultConfig, mode) + if (result) { + defaultConfig = mergeConfig(result, defaultConfig) + } + } + } + return defaultConfig +} + export function defineConfig (config: Partial) { return config } diff --git a/packages/histoire/src/node/plugin.ts b/packages/histoire/src/node/plugin.ts index 9ce759b3..caf07b98 100644 --- a/packages/histoire/src/node/plugin.ts +++ b/packages/histoire/src/node/plugin.ts @@ -14,6 +14,15 @@ export interface Plugin { * Name of the plugin */ name: string + /** + * Modify histoire default config. The hook can either mutate the passed config or + * return a partial config object that will be deeply merged into the existing + * config. User config will have higher priority than default config. + * + * Note: User plugins are resolved before running this hook so injecting other + * plugins inside the `config` hook will have no effect. + */ + defaultConfig?: (defaultConfig: HistoireConfig, mode: ConfigMode) => Partial | null | void | Promise | null | void> /** * Modify histoire config. The hook can either mutate the passed config or * return a partial config object that will be deeply merged into the existing