-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19026 from storybookjs/vite-autoconfig
Vite: Automatically use vite.config.js
- Loading branch information
Showing
12 changed files
with
195 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,19 @@ | ||
import { build as viteBuild } from 'vite'; | ||
import { stringifyProcessEnvs } from './envs'; | ||
import { commonConfig } from './vite-config'; | ||
|
||
import type { EnvsRaw, ExtendedOptions } from './types'; | ||
import type { ExtendedOptions } from './types'; | ||
|
||
export async function build(options: ExtendedOptions) { | ||
const { presets } = options; | ||
|
||
const baseConfig = await commonConfig(options, 'build'); | ||
const config = { | ||
...baseConfig, | ||
build: { | ||
outDir: options.outputDir, | ||
emptyOutDir: false, // do not clean before running Vite build - Storybook has already added assets in there! | ||
sourcemap: true, | ||
}, | ||
const config = await commonConfig(options, 'build'); | ||
config.build = { | ||
outDir: options.outputDir, | ||
emptyOutDir: false, // do not clean before running Vite build - Storybook has already added assets in there! | ||
sourcemap: true, | ||
}; | ||
|
||
const finalConfig = await presets.apply('viteFinal', config, options); | ||
|
||
const envsRaw = await presets.apply<Promise<EnvsRaw>>('env'); | ||
// Stringify env variables after getting `envPrefix` from the final config | ||
const envs = stringifyProcessEnvs(envsRaw, finalConfig.envPrefix); | ||
// Update `define` | ||
finalConfig.define = { | ||
...finalConfig.define, | ||
...envs, | ||
}; | ||
|
||
await viteBuild(finalConfig); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
code/lib/builder-vite/src/plugins/strip-story-hmr-boundaries.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Plugin } from 'vite'; | ||
import { createFilter } from 'vite'; | ||
import MagicString from 'magic-string'; | ||
|
||
/** | ||
* This plugin removes HMR `accept` calls in story files. Stories should not be treated | ||
* as hmr boundaries, but vite has a bug which causes them to be treated as boundaries | ||
* (https://github.com/vitejs/vite/issues/9869). | ||
*/ | ||
export function stripStoryHMRBoundary(): Plugin { | ||
const filter = createFilter(/\.stories\.([tj])sx?$/); | ||
return { | ||
name: 'storybook:strip-hmr-boundary', | ||
enforce: 'post', | ||
async transform(src: string, id: string) { | ||
if (!filter(id)) return undefined; | ||
|
||
const s = new MagicString(src); | ||
s.replace(/import\.meta\.hot\.accept\(\);/, ''); | ||
|
||
return { | ||
code: s.toString(), | ||
map: s.generateMap({ hires: true, source: id }), | ||
}; | ||
}, | ||
}; | ||
} |
Oops, something went wrong.