Skip to content
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

ViteBuilder: Allow viteFinal to modify the configuration before getOptimizeDeps triggers resolveConfig #29790

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<h1>Migration</h1>

- [From version 8.4.x to 8.5.x](#from-version-84x-to-85x)
- [Framework-specific changes in 8.5.x](#framework-specific-changes-in-85x)
- [Vite: Automatic detection of additional `optimizeDeps` entries is now performed after `viteFinal`](#vite-automatic-detection-of-additional-optimizedeps-entries-is-now-performed-after-vitefinal)
- [Indexing behavior of @storybook/experimental-addon-test is changed](#indexing-behavior-of-storybookexperimental-addon-test-is-changed)
- [From version 8.2.x to 8.3.x](#from-version-82x-to-83x)
- [Removed `experimental_SIDEBAR_BOTTOM` and deprecated `experimental_SIDEBAR_TOP` addon types](#removed-experimental_sidebar_bottom-and-deprecated-experimental_sidebar_top-addon-types)
Expand Down Expand Up @@ -423,6 +425,15 @@

## From version 8.4.x to 8.5.x

### Framework-specific changes in 8.5.x

#### Vite: Automatic detection of additional `optimizeDeps` entries is now performed after `viteFinal`

By default, the [Storybook Vite builder](https://storybook.js.org/docs/builders/vite) automatically precompiles a [number of dependencies](https://github.com/storybookjs/storybook/blob/a8ad83fe63dd126a90d78d37c0aa3fa5557fb480/code/builders/builder-vite/src/optimizeDeps.ts#L11-L112) (excluding those not present in the current project) to ensure compatibility with ECMAScript Modules (ESM).
Due to implementation restrictions, determining the set of dependencies to be optimized requires resolving a preliminary configuration that is then used to determine which dependencies are actually installed.

Starting with 8.5, the [`viteFinal` hook](https://storybook.js.org/docs/builders/vite#typescript) is now called *before* the automatically detected dependencies are added, so it has a chance to modify the resolution process and other settings.

### Indexing behavior of @storybook/experimental-addon-test is changed

The Storybook test addon used to index stories based on the `test.include` field in the Vitest config file. This caused indexing issues with Storybook, because stories could have been indexed by Storybook and not Vitest, and vice versa. Starting in Storybook 8.5.0-alpha.18, we changed the indexing behavior so that it always uses the globs defined in the `stories` field in `.storybook/main.js` for a more consistent experience. It is now discouraged to use `test.include`, please remove it.
Expand Down
12 changes: 10 additions & 2 deletions code/builders/builder-vite/src/vite-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ export async function createViteServer(options: Options, devServer: Server) {
},
},
appType: 'custom' as const,
optimizeDeps: await getOptimizeDeps(commonCfg, options),
};

const finalConfig = await presets.apply('viteFinal', config, options);

// getOptimizeDeps calls resolveConfig internally, and should therefore
// be invoked on the fully finalized configuration, in case viteFinal
// has applied some changes that were necessary for the configuration
// to be valid.
const finalConfigWithDeps = {
...finalConfig,
optimizeDeps: await getOptimizeDeps(finalConfig, options),
};

const { createServer } = await import('vite');
return createServer(await sanitizeEnvVars(options, finalConfig));
return createServer(await sanitizeEnvVars(options, finalConfigWithDeps));
}
Loading