Skip to content

Commit 19f3503

Browse files
committed
feat: pass config env to plugin config hook
1 parent 7f1cdac commit 19f3503

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

docs/guide/api-plugin.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
111111

112112
### `config`
113113

114-
- **Type:** `(config: UserConfig) => UserConfig | null | void`
114+
- **Type:** `(config: UserConfig, env: { mode: string, command: string }) => UserConfig | null | void`
115115
- **Kind:** `sync`, `sequential`
116116

117-
Modify Vite config before it's resolved. The hook receives the raw user config (CLI options merged with config file). It can return a partial config object that will be deeply merged into existing config, or directly mutate the config (if the default merging cannot achieve the desired result).
117+
Modify Vite config before it's resolved. The hook receives the raw user config (CLI options merged with config file) and the current config env which exposes the `mode` and `command` being used. It can return a partial config object that will be deeply merged into existing config, or directly mutate the config (if the default merging cannot achieve the desired result).
118118

119119
**Example**
120120

@@ -132,8 +132,10 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
132132
// mutate the config directly (use only when merging doesn't work)
133133
const mutateConfigPlugin = () => ({
134134
name: 'mutate-config',
135-
config(config) {
136-
config.root = __dirname
135+
config(config, { command }) {
136+
if (command === 'build') {
137+
config.root = __dirname
138+
}
137139
}
138140
})
139141
```
@@ -410,8 +412,8 @@ Vite normalizes paths while resolving ids to use POSIX separators ( / ) while pr
410412
So, for Vite plugins, when comparing paths against resolved ids it is important to first normalize the paths to use POSIX separators. An equivalent `normalizePath` utility function is exported from the `vite` module.
411413
412414
```js
413-
import { normalizePath } from 'vite';
415+
import { normalizePath } from 'vite'
414416
415-
normalizePath('foo\\bar'); // 'foo/bar'
416-
normalizePath('foo/bar'); // 'foo/bar'
417-
```
417+
normalizePath('foo\\bar') // 'foo/bar'
418+
normalizePath('foo/bar') // 'foo/bar'
419+
```

packages/vite/src/node/config.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,15 @@ export async function resolveConfig(
206206
process.env.NODE_ENV = 'production'
207207
}
208208

209+
const configEnv = {
210+
mode,
211+
command
212+
}
213+
209214
let { configFile } = config
210215
if (configFile !== false) {
211216
const loadResult = await loadConfigFromFile(
212-
{
213-
mode,
214-
command
215-
},
217+
configEnv,
216218
configFile,
217219
config.root,
218220
config.logLevel
@@ -237,7 +239,7 @@ export async function resolveConfig(
237239
const userPlugins = [...prePlugins, ...normalPlugins, ...postPlugins]
238240
userPlugins.forEach((p) => {
239241
if (p.config) {
240-
const res = p.config(config)
242+
const res = p.config(config, configEnv)
241243
if (res) {
242244
config = mergeConfig(config, res)
243245
}

packages/vite/src/node/plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { ServerHook } from './server'
1212
import { IndexHtmlTransform } from './plugins/html'
1313
import { ModuleNode } from './server/moduleGraph'
14-
import { ResolvedConfig } from './'
14+
import { ConfigEnv, ResolvedConfig } from './'
1515
import { HmrContext } from './server/hmr'
1616

1717
/**
@@ -61,7 +61,7 @@ export interface Plugin extends RollupPlugin {
6161
* Note: User plugins are resolved before running this hook so injecting other
6262
* plugins inside the `config` hook will have no effect.
6363
*/
64-
config?: (config: UserConfig) => UserConfig | null | void
64+
config?: (config: UserConfig, env: ConfigEnv) => UserConfig | null | void
6565
/**
6666
* Use this hook to read and store the final resolved vite config.
6767
*/

0 commit comments

Comments
 (0)