From ad2d4398a8e1f16dbd8065b67acc75a548efb4f8 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Tue, 22 Aug 2023 18:24:10 +0200 Subject: [PATCH] add override presets for framework presets --- code/lib/core-server/package.json | 8 +++++++- code/lib/core-server/src/build-dev.ts | 9 +++++++-- code/lib/core-server/src/build-static.ts | 9 +++++++-- .../src/presets/common-override-preset.ts | 14 ++++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 code/lib/core-server/src/presets/common-override-preset.ts diff --git a/code/lib/core-server/package.json b/code/lib/core-server/package.json index 305d010bb28f..ac658435f818 100644 --- a/code/lib/core-server/package.json +++ b/code/lib/core-server/package.json @@ -36,6 +36,11 @@ "node": "./dist/presets/common-preset.js", "require": "./dist/presets/common-preset.js" }, + "./dist/presets/common-override-preset": { + "types": "./dist/presets/common-override-preset.d.ts", + "node": "./dist/presets/common-override-preset.js", + "require": "./dist/presets/common-override-preset.js" + }, "./public/favicon.svg": "./public/favicon.svg", "./package.json": "./package.json" }, @@ -119,7 +124,8 @@ "entries": [ "./src/index.ts", "./src/presets/babel-cache-preset.ts", - "./src/presets/common-preset.ts" + "./src/presets/common-preset.ts", + "./src/presets/common-override-preset.ts" ], "platform": "node" }, diff --git a/code/lib/core-server/src/build-dev.ts b/code/lib/core-server/src/build-dev.ts index ee045cde1a3d..7785afdee7b7 100644 --- a/code/lib/core-server/src/build-dev.ts +++ b/code/lib/core-server/src/build-dev.ts @@ -80,7 +80,9 @@ export async function buildDevStandalone( // We hope to remove this in SB8 let presets = await loadAllPresets({ corePresets, - overridePresets: [], + overridePresets: [ + require.resolve('@storybook/core-server/dist/presets/common-override-preset'), + ], ...options, }); @@ -112,7 +114,10 @@ export async function buildDevStandalone( ...corePresets, require.resolve('@storybook/core-server/dist/presets/babel-cache-preset'), ], - overridePresets: previewBuilder.overridePresets ?? [], + overridePresets: [ + ...(previewBuilder.overridePresets || []), + require.resolve('@storybook/core-server/dist/presets/common-override-preset'), + ], ...options, }); diff --git a/code/lib/core-server/src/build-static.ts b/code/lib/core-server/src/build-static.ts index 4f22e5c8ec11..a9cfcaefbfc4 100644 --- a/code/lib/core-server/src/build-static.ts +++ b/code/lib/core-server/src/build-static.ts @@ -85,7 +85,9 @@ export async function buildStaticStandalone(options: BuildStaticStandaloneOption require.resolve('@storybook/core-server/dist/presets/common-preset'), ...corePresets, ], - overridePresets: [], + overridePresets: [ + require.resolve('@storybook/core-server/dist/presets/common-override-preset'), + ], ...options, }); @@ -103,7 +105,10 @@ export async function buildStaticStandalone(options: BuildStaticStandaloneOption ...corePresets, require.resolve('@storybook/core-server/dist/presets/babel-cache-preset'), ], - overridePresets: previewBuilder.overridePresets || [], + overridePresets: [ + ...(previewBuilder.overridePresets || []), + require.resolve('@storybook/core-server/dist/presets/common-override-preset'), + ], ...options, }); diff --git a/code/lib/core-server/src/presets/common-override-preset.ts b/code/lib/core-server/src/presets/common-override-preset.ts new file mode 100644 index 000000000000..55a55bb0f117 --- /dev/null +++ b/code/lib/core-server/src/presets/common-override-preset.ts @@ -0,0 +1,14 @@ +import type { PresetProperty, StorybookConfig } from '@storybook/types'; + +export const framework: PresetProperty<'framework', StorybookConfig> = async (config) => { + // This will get called with the values from the user's main config, but before + // framework preset from framework packages e.g. react-webpack5 gets called. + // This means we can add default values to the framework config, before it's requested by other packages. + const name = typeof config === 'string' ? config : config?.name; + const options = typeof config === 'string' ? {} : config?.options || {}; + + return { + name, + options, + }; +};