Skip to content

Commit

Permalink
feat: support specifying mode in user config
Browse files Browse the repository at this point in the history
close #1380
  • Loading branch information
yyx990803 committed Jan 7, 2021
1 parent 9b5b352 commit 396bbf8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 42 deletions.
9 changes: 1 addition & 8 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,7 @@ export async function build(
async function doBuild(
inlineConfig: InlineConfig = {}
): Promise<RollupOutput | RollupOutput[]> {
const config = await resolveConfig(
{
...inlineConfig,
mode: inlineConfig.mode || 'production'
},
'build'
)

const config = await resolveConfig(inlineConfig, 'build', 'production')
config.logger.info(chalk.cyan(`building for production...`))

const options = config.build
Expand Down
11 changes: 4 additions & 7 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ cli
.option('--https', `[boolean] use TLS + HTTP/2`)
.option('--open [browser]', `[boolean | string] open browser on startup`)
.option('--cors', `[boolean] enable CORS`)
.option('-m, --mode <mode>', `[string] set env mode`, {
default: 'development'
})
.option('-m, --mode <mode>', `[string] set env mode`)
.option(
'--force',
`[boolean] force the optimizer to ignore the cache and re-bundle`
Expand Down Expand Up @@ -113,9 +111,7 @@ cli
`or specify minifier to use (default: terser)`
)
.option('--manifest', `[boolean] emit build manifest json`)
.option('-m, --mode <mode>', `[string] set env mode`, {
default: 'production'
})
.option('-m, --mode <mode>', `[string] set env mode`)
.action(async (root: string, options: BuildOptions & GlobalCLIOptions) => {
const { build } = await import('./build')
try {
Expand Down Expand Up @@ -150,7 +146,8 @@ cli
configFile: options.config,
logLevel: options.logLevel
},
'build'
'build',
'development'
)
await optimizeDeps(config, options.force, true)
} catch (e) {
Expand Down
18 changes: 12 additions & 6 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export interface UserConfig {
* @default process.cwd()
*/
root?: string
/**
* Explicitly set a mode to run in. This will override the default mode for
* each command, and can be overridden by the command line --mode option.
*/
mode?: string
/**
* Import aliases
*/
Expand Down Expand Up @@ -96,7 +101,6 @@ export interface UserConfig {
}

export interface InlineConfig extends UserConfig {
mode?: string
configFile?: string | false
}

Expand All @@ -121,27 +125,29 @@ export type ResolvedConfig = Readonly<

export async function resolveConfig(
inlineConfig: InlineConfig,
command: 'build' | 'serve'
command: 'build' | 'serve',
defaultMode = 'development'
): Promise<ResolvedConfig> {
let config = inlineConfig
let { configFile } = inlineConfig
const mode = inlineConfig.mode || 'development'

let mode = defaultMode
let { configFile } = config
if (configFile !== false) {
const loadResult = await loadConfigFromFile(
{
mode,
command
},
configFile,
inlineConfig.root,
config.root,
config.logLevel
)
if (loadResult) {
config = mergeConfig(loadResult.config, config)
configFile = loadResult.path
}
}
// user config may provide an alternative mode
mode = config.mode || mode

// resolve plugins
const rawUserPlugins = (config.plugins || []).flat().filter((p) => {
Expand Down
32 changes: 11 additions & 21 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,10 @@ export interface ViteDevServer {
export async function createServer(
inlineConfig: InlineConfig = {}
): Promise<ViteDevServer> {
const resolvedConfig = await resolveConfig(
{
...inlineConfig,
mode: inlineConfig.mode || 'development'
},
'serve'
)

const root = resolvedConfig.root
const logger = createLogger(resolvedConfig.logLevel)
const serverConfig = resolvedConfig.server || {}
const config = await resolveConfig(inlineConfig, 'serve', 'development')
const root = config.root
const logger = createLogger(config.logLevel)
const serverConfig = config.server || {}

const app = connect() as Connect.Server
const httpServer = await resolveHttpServer(serverConfig, app)
Expand All @@ -215,13 +208,13 @@ export async function createServer(
...watchOptions
}) as FSWatcher

const plugins = resolvedConfig.plugins
const container = await createPluginContainer(resolvedConfig, watcher)
const plugins = config.plugins
const container = await createPluginContainer(config, watcher)
const moduleGraph = new ModuleGraph(container)
const closeHttpServer = createSeverCloseFn(httpServer)

const server: ViteDevServer = {
config: resolvedConfig,
config: config,
app,
httpServer,
watcher,
Expand Down Expand Up @@ -302,7 +295,7 @@ export async function createServer(

// serve static files
app.use(rawFsStaticMiddleware())
app.use(serveStaticMiddleware(root, resolvedConfig))
app.use(serveStaticMiddleware(root, config))

// spa fallback
app.use(
Expand Down Expand Up @@ -347,14 +340,11 @@ export async function createServer(
httpServer.listen = (async (port: number, ...args: any[]) => {
await container.buildStart({})

if (resolvedConfig.optimizeCacheDir) {
if (config.optimizeCacheDir) {
// run optimizer
await optimizeDeps(resolvedConfig)
await optimizeDeps(config)
// after optimization, read updated optimization metadata
const dataPath = path.resolve(
resolvedConfig.optimizeCacheDir,
'metadata.json'
)
const dataPath = path.resolve(config.optimizeCacheDir, 'metadata.json')
if (fs.existsSync(dataPath)) {
server.optimizeDepsMetadata = JSON.parse(
fs.readFileSync(dataPath, 'utf-8')
Expand Down

0 comments on commit 396bbf8

Please sign in to comment.