Skip to content

Commit

Permalink
feat: pass config env to plugin config hook
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 11, 2021
1 parent 7f1cdac commit 19f3503
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
18 changes: 10 additions & 8 deletions docs/guide/api-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo

### `config`

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

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).
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).

**Example**

Expand All @@ -132,8 +132,10 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
// mutate the config directly (use only when merging doesn't work)
const mutateConfigPlugin = () => ({
name: 'mutate-config',
config(config) {
config.root = __dirname
config(config, { command }) {
if (command === 'build') {
config.root = __dirname
}
}
})
```
Expand Down Expand Up @@ -410,8 +412,8 @@ Vite normalizes paths while resolving ids to use POSIX separators ( / ) while pr
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.
```js
import { normalizePath } from 'vite';
import { normalizePath } from 'vite'
normalizePath('foo\\bar'); // 'foo/bar'
normalizePath('foo/bar'); // 'foo/bar'
```
normalizePath('foo\\bar') // 'foo/bar'
normalizePath('foo/bar') // 'foo/bar'
```
12 changes: 7 additions & 5 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,15 @@ export async function resolveConfig(
process.env.NODE_ENV = 'production'
}

const configEnv = {
mode,
command
}

let { configFile } = config
if (configFile !== false) {
const loadResult = await loadConfigFromFile(
{
mode,
command
},
configEnv,
configFile,
config.root,
config.logLevel
Expand All @@ -237,7 +239,7 @@ export async function resolveConfig(
const userPlugins = [...prePlugins, ...normalPlugins, ...postPlugins]
userPlugins.forEach((p) => {
if (p.config) {
const res = p.config(config)
const res = p.config(config, configEnv)

This comment has been minimized.

Copy link
@ElMassimo

ElMassimo Feb 12, 2021

Contributor

Using it in the latest version of vite-plugin-ruby, nice addition 👍

if (res) {
config = mergeConfig(config, res)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { ServerHook } from './server'
import { IndexHtmlTransform } from './plugins/html'
import { ModuleNode } from './server/moduleGraph'
import { ResolvedConfig } from './'
import { ConfigEnv, ResolvedConfig } from './'
import { HmrContext } from './server/hmr'

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

1 comment on commit 19f3503

@CHOYSEN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yyx990803 尤大新年快乐!网站上的内容已经落后几个 commit 了,需要更新一下

Please sign in to comment.