-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vite: use vite.config.js as config base
- Loading branch information
Showing
5 changed files
with
89 additions
and
136 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
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
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,40 +1,30 @@ | ||
import type { Server } from 'http'; | ||
import { createServer } from 'vite'; | ||
import { stringifyProcessEnvs } from './envs'; | ||
import { getOptimizeDeps } from './optimizeDeps'; | ||
import { commonConfig } from './vite-config'; | ||
import type { EnvsRaw, ExtendedOptions } from './types'; | ||
import type { ExtendedOptions } from './types'; | ||
import { getOptimizeDeps } from './optimizeDeps'; | ||
|
||
export async function createViteServer(options: ExtendedOptions, devServer: Server) { | ||
const { port, presets } = options; | ||
const { presets } = options; | ||
|
||
const config = await commonConfig(options, 'development'); | ||
|
||
const baseConfig = await commonConfig(options, 'development'); | ||
const defaultConfig = { | ||
...baseConfig, | ||
server: { | ||
middlewareMode: true, | ||
hmr: { | ||
port, | ||
server: devServer, | ||
}, | ||
fs: { | ||
strict: true, | ||
}, | ||
// Set up dev server | ||
config.server = { | ||
middlewareMode: true, | ||
hmr: { | ||
port: options.port, | ||
server: devServer, | ||
}, | ||
fs: { | ||
strict: true, | ||
}, | ||
appType: 'custom' as const, | ||
optimizeDeps: await getOptimizeDeps(baseConfig, options), | ||
}; | ||
config.appType = 'custom'; | ||
|
||
const finalConfig = await presets.apply('viteFinal', defaultConfig, 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, | ||
}; | ||
// TODO: find a way to avoid having to do this in a separate step. | ||
config.optimizeDeps = await getOptimizeDeps(config, options); | ||
|
||
const finalConfig = await presets.apply('viteFinal', config, options); | ||
return createServer(finalConfig); | ||
} |