Skip to content

Commit

Permalink
feat(plugin): defaultConfig hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Aug 11, 2022
1 parent 976eae8 commit c31bc74
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/histoire-plugin-nuxt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -68,6 +68,7 @@ async function useNuxtViteConfig () {
return {
viteConfig: await new Promise<ViteConfig>((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 () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/histoire-plugin-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function HstVue (): Plugin {
return {
name: '@histoire/plugin-vue',

config () {
defaultConfig () {
return {
supportMatch: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/histoire-plugin-vue2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function HstVue (): Plugin {
return {
name: '@histoire/plugin-vue2',

config () {
defaultConfig () {
return {
supportMatch: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function vanillaSupport (): Plugin {
return {
name: 'builtin:vanilla-support',

config () {
defaultConfig () {
return {
supportMatch: [
{
Expand Down
18 changes: 17 additions & 1 deletion packages/histoire/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HistoireConfig> {
Expand All @@ -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<HistoireConfig> {
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<HistoireConfig>) {
return config
}
Expand Down
9 changes: 9 additions & 0 deletions packages/histoire/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HistoireConfig> | null | void | Promise<Partial<HistoireConfig> | 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
Expand Down

0 comments on commit c31bc74

Please sign in to comment.