Skip to content

Commit

Permalink
wip: environment config overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 13, 2024
1 parent 60f7f2b commit 81abf6e
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 47 deletions.
66 changes: 36 additions & 30 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import {
ESBUILD_MODULES_TARGET,
VERSION,
} from './constants'
import type { InlineConfig, ResolvedConfig } from './config'
import type {
BuildEnvironmentConfig,
InlineConfig,
ResolvedConfig,
} from './config'
import { resolveConfig } from './config'
import { buildReporterPlugin } from './plugins/reporter'
import { buildEsbuildPlugin } from './plugins/esbuild'
Expand Down Expand Up @@ -469,10 +473,7 @@ export async function build(
'production',
'production',
)
return buildEnvironment(config)
}

async function buildEnvironment(config: ResolvedConfig) {
const options = config.build
const ssr = !!options.ssr
const libOptions = options.lib
Expand Down Expand Up @@ -1253,10 +1254,13 @@ function areSeparateFolders(a: string, b: string) {

export class BuildEnvironment extends Environment {
mode = 'build' as const
config: BuildOptions
constructor(id: string, options: { type: string; config?: BuildOptions }) {
config: BuildEnvironmentConfig
constructor(
id: string,
options: { type: string; config?: BuildEnvironmentConfig },
) {
super(id, options)
this.config = options.config ?? {}
this.config = options.config ?? { build: {} }
}
}

Expand All @@ -1268,64 +1272,66 @@ export interface ViteBuilder {
export async function createViteBuilder(
inlineConfig: InlineConfig = {},
): Promise<ViteBuilder> {
const config = await resolveConfig(
const defaultConfig = await resolveConfig(
inlineConfig,
'build',
'production',
'production',
)

if (config.build.lib) {
if (defaultConfig.build.lib) {
throw new Error('Library mode is not supported in ViteBuilder')
}
if (config.build.watch) {
if (defaultConfig.build.watch) {
throw new Error('Watch mode is not yet supported in ViteBuilder')
}

const environments = new Map<string, BuildEnvironment>()

const browserEnvironment = new BuildEnvironment('browser', {
type: 'browser',
config: { ssr: false },
config: { build: { ssr: false } },
})
environments.set('browser', browserEnvironment)

if (config.build.ssr) {
// Backward compatibility for `ssr` option
if (defaultConfig.build.ssr) {
const ssrEnvironment = new BuildEnvironment('ssr', { type: 'node' })
environments.set('ssr', ssrEnvironment)
}

function resolveEnvironmentBuildOptions(environment: BuildEnvironment) {
return resolveBuildOptions(
{ ...config.build, ...environment.config },
config.logger,
config.root,
)
}

const builder: ViteBuilder = {
environments,
async build() {
for (const environment of environments.values()) {
const environmentConfig = {
...config,
build: {
...config.build,
...resolveEnvironmentBuildOptions(environment),
},
// We need to resolve the config again so we can properly merge options
// and get a new set of plugins for each build environment. The ecosystem
// expects plugins to be run for the same environment once they are created
// and to process a single bundle at a time (contrary to dev mode where
// plugins are built to handle multiple environments concurrently).

let userConfig = inlineConfig
const inlineConfigEnvironmentOverrides =
inlineConfig.environment?.[environment.id]
if (inlineConfigEnvironmentOverrides) {
userConfig = mergeConfig(userConfig, inlineConfigEnvironmentOverrides)
}
await buildEnvironment(environmentConfig)
if (environment.config) {
userConfig = mergeConfig(userConfig, environment.config)
}

await build(userConfig)

config.logger.info('')
defaultConfig.logger.info('')
}
},
}

// call configureBuildEnvironments hooks
for (const hook of config.getSortedPluginHooks(
for (const hook of defaultConfig.getSortedPluginHooks(
'configureBuildEnvironments',
)) {
await hook(environments, config)
await hook(environments, defaultConfig)
}

return builder
Expand Down
93 changes: 76 additions & 17 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,70 @@ export type PluginOption =
| PluginOption[]
| Promise<Plugin | false | null | undefined | PluginOption[]>

export interface UserConfig {
export interface DevOptions {
/**
* The files to be pre-transformed. Supports glob patterns.
*/
warmup?: { files: string[] }
/**
* Pre-transform known direct imports
* @default true
*/
preTransformRequests?: boolean
/**
* Enables sourcemaps during dev
* @default { js: true }
* @experimental
*/
sourcemap?: boolean | { js?: boolean; css?: boolean }
/**
* Whether or not to ignore-list source files in the dev server sourcemap, used to populate
* the [`x_google_ignoreList` source map extension](https://developer.chrome.com/blog/devtools-better-angular-debugging/#the-x_google_ignorelist-source-map-extension).
*
* By default, it excludes all paths containing `node_modules`. You can pass `false` to
* disable this behavior, or, for full control, a function that takes the source path and
* sourcemap path and returns whether to ignore the source path.
*/
sourcemapIgnoreList?:
| false
| ((sourcePath: string, sourcemapPath: string) => boolean)
}

export interface SharedEnvironmentConfig {
/**
* Configure resolver
*/
resolve?: ResolveOptions & { alias?: AliasOptions }
/**
* Dep optimization options
*/
optimizeDeps?: DepOptimizationOptions
}

export interface EnvironmentConfig extends SharedEnvironmentConfig {
/**
* Dev specific options
*/
dev?: DevOptions
/**
* Build specific options
*/
build?: BuildOptions
}

export interface DevEnvironmentConfig extends SharedEnvironmentConfig {
dev?: DevOptions
}

export interface BuildEnvironmentConfig extends SharedEnvironmentConfig {
build?: BuildOptions
}

export interface BuildEnvironmentConfig extends EnvironmentConfig {
build?: BuildOptions
}

export interface UserConfig extends EnvironmentConfig {
/**
* Project root directory. Can be an absolute path, or a path relative from
* the location of the config file itself.
Expand Down Expand Up @@ -169,10 +232,6 @@ export interface UserConfig {
* Array of vite plugins to use.
*/
plugins?: PluginOption[]
/**
* Configure resolver
*/
resolve?: ResolveOptions & { alias?: AliasOptions }
/**
* CSS related options (preprocessors and CSS modules)
*/
Expand All @@ -194,22 +253,10 @@ export interface UserConfig {
* Server specific options, e.g. host, port, https...
*/
server?: ServerOptions
/**
* Build specific options
*/
build?: BuildOptions
/**
* Preview specific options, e.g. host, port, https...
*/
preview?: PreviewOptions
/**
* Dep optimization options
*/
optimizeDeps?: DepOptimizationOptions
/**
* SSR specific options
*/
ssr?: SSROptions
/**
* Experimental features
*
Expand Down Expand Up @@ -279,6 +326,18 @@ export interface UserConfig {
* @default 'spa'
*/
appType?: AppType
/**
* SSR specific options
* Some of the options previously under `ssr` (like resolve.conditions) are deprecated
* and moved to environment: { ssr: { ... } }
*/
ssr?: SSROptions
/**
* Environment overrides
*/
environment?: {
[key: string]: EnvironmentConfig
}
}

export interface ExperimentalOptions {
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export interface CSSOptions {
* Enables css sourcemaps during dev
* @default false
* @experimental
* @deprecated use dev.sourcemap instead
*/
devSourcemap?: boolean

Expand Down
30 changes: 30 additions & 0 deletions packages/vite/src/node/server/environment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Environment } from '../environment'
import type { ViteDevServer } from '../server'
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../plugins/optimizedDeps'
import type { DevEnvironmentConfig } from '../config'
import { mergeConfig } from '../utils'
import { EnvironmentModuleGraph } from './moduleGraph'
import type { HMRChannel } from './hmr'
import { createNoopHMRChannel } from './hmr'
Expand All @@ -15,10 +17,36 @@ export class ModuleExecutionEnvironment extends Environment {
get server(): ViteDevServer {
return this._getServer()
}
get config(): DevEnvironmentConfig {
if (!this._config) {
// Merge the resolved configs, TODO: make generic on DevEnvironmentConfig
const { resolve, optimizeDeps, dev } = this.server.config
let resolvedConfig: DevEnvironmentConfig = { resolve, optimizeDeps, dev }
if (this.server.config.environment?.[this.id]) {
resolvedConfig = mergeConfig(
resolvedConfig,
this.server.config.environment[this.id],
)
}
if (this._inlineConfig) {
resolvedConfig = mergeConfig(resolvedConfig, this._inlineConfig)
}
this._config = resolvedConfig
}
return this._config
}
/**
* @internal
*/
_getServer: () => ViteDevServer
/**
* @internal
*/
_config: DevEnvironmentConfig | undefined
/**
* @internal
*/
_inlineConfig: DevEnvironmentConfig | undefined
/**
* HMR channel for this environment. If not provided or disabled,
* it will be a noop channel that does nothing.
Expand All @@ -34,6 +62,7 @@ export class ModuleExecutionEnvironment extends Environment {
type: string
// TODO: use `transport` instead to support any hmr channel?
hot?: false | HMRChannel
config?: DevEnvironmentConfig
},
) {
super(id, options)
Expand All @@ -45,6 +74,7 @@ export class ModuleExecutionEnvironment extends Environment {
}),
)
this.hot = options.hot || createNoopHMRChannel()
this._inlineConfig = options.config
}

transformRequest(url: string): Promise<TransformResult | null> {
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface ServerOptions extends CommonServerOptions {
/**
* Warm-up files to transform and cache the results in advance. This improves the
* initial page load during server starts and prevents transform waterfalls.
* @deprecated use dev.warmup / environment.ssr.dev.warmup
*/
warmup?: {
/**
Expand Down Expand Up @@ -142,6 +143,7 @@ export interface ServerOptions extends CommonServerOptions {
/**
* Pre-transform known direct imports
* @default true
* @deprecated use dev.preTransformRequests
*/
preTransformRequests?: boolean
/**
Expand All @@ -151,6 +153,7 @@ export interface ServerOptions extends CommonServerOptions {
* By default, it excludes all paths containing `node_modules`. You can pass `false` to
* disable this behavior, or, for full control, a function that takes the source path and
* sourcemap path and returns whether to ignore the source path.
* @deprecated use dev.sourcemapIgnoreList
*/
sourcemapIgnoreList?:
| false
Expand Down

0 comments on commit 81abf6e

Please sign in to comment.