Skip to content

Commit 5f48f08

Browse files
committed
ViteBuilder: Allow viteFinal to modify the configuration before getOptimizeDeps triggers resolveConfig
1 parent a8ad83f commit 5f48f08

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

MIGRATION.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<h1>Migration</h1>
22

3+
- [From version 8.4.x to 8.5.x](#from-version-84x-to-85x)
4+
- [Framework-specific changes in 8.5.x](#framework-specific-changes-in-85x)
5+
- [Vite: Automatic detection of additional `optimizeDeps` entries is now performed after `viteFinal`](#vite-automatic-detection-of-additional-optimizedeps-entries-is-now-performed-after-vitefinal)
36
- [From version 8.2.x to 8.3.x](#from-version-82x-to-83x)
47
- [Removed `experimental_SIDEBAR_BOTTOM` and deprecated `experimental_SIDEBAR_TOP` addon types](#removed-experimental_sidebar_bottom-and-deprecated-experimental_sidebar_top-addon-types)
58
- [New parameters format for addon backgrounds](#new-parameters-format-for-addon-backgrounds)
@@ -419,6 +422,17 @@
419422
- [Packages renaming](#packages-renaming)
420423
- [Deprecated embedded addons](#deprecated-embedded-addons)
421424

425+
## From version 8.4.x to 8.5.x
426+
427+
### Framework-specific changes in 8.5.x
428+
429+
#### Vite: Automatic detection of additional `optimizeDeps` entries is now performed after `viteFinal`
430+
431+
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).
432+
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.
433+
434+
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.
435+
422436
## From version 8.2.x to 8.3.x
423437

424438
### Removed `experimental_SIDEBAR_BOTTOM` and deprecated `experimental_SIDEBAR_TOP` addon types

code/builders/builder-vite/src/vite-server.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,19 @@ export async function createViteServer(options: Options, devServer: Server) {
2828
},
2929
},
3030
appType: 'custom' as const,
31-
optimizeDeps: await getOptimizeDeps(commonCfg, options),
3231
};
3332

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

35+
// getOptimizeDeps calls resolveConfig internally, and should therefore
36+
// be invoked on the fully finalized configuration, in case viteFinal
37+
// has applied some changes that were necessary for the configuration
38+
// to be valid.
39+
const finalConfigWithDeps = {
40+
...finalConfig,
41+
optimizeDeps: await getOptimizeDeps(finalConfig, options),
42+
};
43+
3644
const { createServer } = await import('vite');
37-
return createServer(await sanitizeEnvVars(options, finalConfig));
45+
return createServer(await sanitizeEnvVars(options, finalConfigWithDeps));
3846
}

0 commit comments

Comments
 (0)