@@ -23,14 +23,17 @@ export default {
23
23
vite --config my-config.js
24
24
```
25
25
26
- 注意,Vite 会替换 ` __filename ` ,` __dirname ` 以及 ` import.meta.url ` 。如果使用这些名称作为变量名可能会导致代码报错:
26
+ ::: tip 注意
27
+ 注意,Vite 会在 ** CommonJS** 和 ** TypeScript** 配置文件中替换 ` __filename ` ,` __dirname ` 以及 ` import.meta.url ` 。如果使用这些名称作为变量名可能会导致代码报错:
27
28
28
29
``` js
29
30
const __filename = " value"
30
31
// will be transformed to
31
32
const "path /vite .config .js " = " value"
32
33
```
33
34
35
+ :::
36
+
34
37
### 配置智能提示 {#config-intellisense}
35
38
36
39
因为 Vite 本身附带 Typescript 类型,所以你可以通过 IDE 和 jsdoc 的配合来实现智能提示:
@@ -92,6 +95,23 @@ export default defineConfig(async ({ command, mode }) => {
92
95
})
93
96
```
94
97
98
+ ### Environment Variables {#environment-variables}
99
+
100
+ Vite 默认是不加载 ` .env ` 文件的,因为这些文件需要在执行完 Vite 配置后才能确定加载哪一个,举个例子,` root ` 和 ` envDir ` 选项会影响加载行为。不过当你的确需要时,你可以使用 Vite 导出的 ` loadEnv ` 函数来加载指定的 ` .env ` 文件
101
+
102
+ ``` js
103
+ import { defineConfig , loadEnv } from ' vite'
104
+
105
+ export default defineConfig (({ command, mode }) => {
106
+ // 根据当前工作目录中的 `mode` 加载 .env 文件
107
+ const env = loadEnv (mode, process .cwd ())
108
+ return {
109
+ // 构建特定配置
110
+ }
111
+ })
112
+ ```
113
+
114
+
95
115
## 共享配置 {#shared-options}
96
116
97
117
### root {#root}
@@ -131,13 +151,15 @@ export default defineConfig(async ({ command, mode }) => {
131
151
132
152
定义全局常量替换方式。其中每项在开发环境下会被定义在全局,而在构建时被静态替换。
133
153
134
- - 从 ` 2.0.0-beta.70 ` 版本开始,字符串值将直接作为一个表达式,所以如果定义为了一个字符串常量,它需要被显式地引用(例如:通过 ` JSON.stringify ` ) 。
154
+ - 为了与 [ esbuild 的行为 ] ( https://esbuild.github.io/api/#define ) 保持一致,表达式必须为一个 JSON 对象(null、boolean、number、string、数组或对象),亦或是一个单独的标识符 。
135
155
136
156
- 替换只会在匹配到周围是单词边界(` \b ` )时执行。
137
157
158
+ ::: warning
138
159
因为它是不经过任何语法分析,直接替换文本实现的,所以我们建议只对 CONSTANTS 使用 ` define ` 。
139
160
140
161
例如,` process.env.FOO ` 和 ` __APP_VERSION__ ` 就非常适合。但 ` process ` 或 ` global ` 不应使用此选项。变量相关应使用 shim 或 polyfill 代替。
162
+ :::
141
163
142
164
::: tip NOTE
143
165
对于使用 TypeScript 的开发者来说,请确保在 ` env.d.ts ` 或 ` vite-env.d.ts ` 文件中添加类型声明,以获得类型检查以及代码提示。
@@ -220,6 +242,10 @@ export default defineConfig(async ({ command, mode }) => {
220
242
221
243
Vite 有一个“允许的情景”列表,并且会匹配列表中第一个情景。默认允许的情景是:` import ` ,` module ` ,` browser ` ,` default ` 和基于当前情景为 ` production/development ` 。` resolve.conditions ` 配置项使得我们可以指定其他允许的情景。
222
244
245
+ :::warning 解决子路径导出问题
246
+ 导出以“/”结尾的 key 已被 Node 弃用,可能无法正常工作。请联系包的作者改为使用 [ ` * ` 子路径模式] ( https://nodejs.org/api/packages.html#package-entry-points ) 。
247
+ :::
248
+
223
249
### resolve.mainFields {#resolve-mainfields}
224
250
225
251
- ** 类型:** ` string[] `
@@ -272,28 +298,39 @@ export default defineConfig(async ({ command, mode }) => {
272
298
273
299
- ** 类型:** ` string | (postcss.ProcessOptions & { plugins?: postcss.Plugin[] }) `
274
300
275
- 内联的 PostCSS 配置(格式同 ` postcss.config.js ` ),或者一个(默认基于项目根目录的)自定义的 PostCSS 配置路径。其路径搜索是通过 [ postcss-load-config] ( https://github.com/postcss/postcss-load-config ) 实现的。
301
+ 内联的 PostCSS 配置(格式同 ` postcss.config.js ` ),或者一个(默认基于项目根目录的)自定义的 PostCSS 配置路径。其路径搜索是通过 [ postcss-load-config] ( https://github.com/postcss/postcss-load-config ) 实现的,并且只加载支持的配置文件名称 。
276
302
277
303
注意:如果提供了该内联配置,Vite 将不会搜索其他 PostCSS 配置源。
278
304
279
305
### css.preprocessorOptions {#css-preprocessoroptions}
280
306
281
307
- ** 类型:** ` Record<string, object> `
282
308
283
- 指定传递给 CSS 预处理器的选项。例如:
309
+ 指定传递给 CSS 预处理器的选项。文件扩展名用作选项的键,例如:
284
310
285
311
``` js
286
312
export default defineConfig ({
287
313
css: {
288
314
preprocessorOptions: {
289
315
scss: {
290
316
additionalData: ` $injectedColor: orange;`
317
+ },
318
+ styl: {
319
+ additionalData: ` $injectedColor ?= orange`
291
320
}
292
321
}
293
322
}
294
323
})
295
324
```
296
325
326
+ ### css.devSourcemap
327
+
328
+ - ** 实验性**
329
+ - ** 类型:** ` boolean `
330
+ - ** 默认:** ` false `
331
+
332
+ 在开发过程中是否启用 sourcemap。
333
+
297
334
### json.namedExports {#json-namedexports}
298
335
299
336
- ** 类型:** ` boolean `
@@ -325,7 +362,7 @@ export default defineConfig(async ({ command, mode }) => {
325
362
})
326
363
```
327
364
328
- 默认情况下,ESbuild 会被应用在 ` ts ` 、` jsx ` 、` tsx ` 文件。你可以通过 ` esbuild.include ` 和 ` esbuild.exclude ` 对要处理的文件类型进行配置,这两个配置的类型应为 ` string | RegExp | (string | RegExp)[] ` 。
365
+ 默认情况下,ESbuild 会被应用在 ` ts ` 、` jsx ` 、` tsx ` 文件。你可以通过 ` esbuild.include ` 和 ` esbuild.exclude ` 对要处理的文件类型进行配置,这两个配置的值可以是一个正则表达式、一个 [ picomatch ] ( https://github.com/micromatch/picomatch#globbing-features ) 模式,或是一个值为这两种类型的数组 。
329
366
330
367
此外,你还可以通过 ` esbuild.jsxInject ` 来自动为每一个被 ESbuild 转换的文件注入 JSX helper。
331
368
@@ -344,7 +381,7 @@ export default defineConfig(async ({ command, mode }) => {
344
381
- ** 类型:** ` string | RegExp | (string | RegExp)[] `
345
382
- ** 相关内容:** [ 静态资源处理] ( /guide/assets )
346
383
347
- 指定额外的 [ picomatch 模式] ( https://github.com/micromatch/picomatch ) 作为静态资源处理,因此:
384
+ 指定额外的 [ picomatch 模式] ( https://github.com/micromatch/picomatch#globbing-features ) 作为静态资源处理,因此:
348
385
349
386
- 当从 HTML 引用它们或直接通过 ` fetch ` 或 XHR 请求它们时,它们将被插件转换管道排除在外。
350
387
@@ -494,7 +531,13 @@ export default defineConfig(async ({ command, mode }) => {
494
531
495
532
为开发服务器配置 CORS。默认启用并允许任何源,传递一个 [ 选项对象] ( https://github.com/expressjs/cors ) 来调整行为或设为 ` false ` 表示禁用。
496
533
497
- ### server.force {#server-force}
534
+ ### server.headers ${server-hmr}
535
+
536
+ - ** 类型:** ` OutgoingHttpHeaders `
537
+
538
+ 指定服务器响应的 header。
539
+
540
+ ### server.force
498
541
499
542
- ** 类型:** ` boolean `
500
543
- ** 相关内容:** [ 依赖预构建] ( /guide/dep-pre-bundling )
@@ -575,6 +618,12 @@ async function createServer() {
575
618
createServer ()
576
619
```
577
620
621
+ ### server.base {#server-base}
622
+
623
+ - ** 类型:** ` string | undefined `
624
+
625
+ 在 HTTP 请求中预留此文件夹,用于代理 Vite 作为子文件夹时使用。应该以 ` / ` 字符开始和结束。
626
+
578
627
### server.fs.strict {#server-fs-strict}
579
628
580
629
- ** 类型:** ` boolean `
@@ -918,9 +967,9 @@ export default defineConfig({
918
967
919
968
- ** 类型:** ` string | string[] `
920
969
921
- 默认情况下,Vite 会抓取你的 ` index.html ` 来检测需要预构建的依赖项。如果指定了 ` build.rollupOptions.input ` ,Vite 将转而去抓取这些入口点。
970
+ 默认情况下,Vite 会抓取你的 ` index.html ` 来检测需要预构建的依赖项(忽略了 ` node_modules ` 、 ` build.outDir ` 、 ` __tests__ ` 和 ` coverage ` ) 。如果指定了 ` build.rollupOptions.input ` ,Vite 将转而去抓取这些入口点。
922
971
923
- 如果这两者都不合你意,则可以使用此选项指定自定义条目——该值需要遵循 [ fast-glob 模式] ( https://github.com/mrmlnc/fast-glob#basic-syntax ) ,或者是相对于 Vite 项目根的模式数组。这将覆盖掉默认条目推断 。
972
+ 如果这两者都不合你意,则可以使用此选项指定自定义条目——该值需要遵循 [ fast-glob 模式] ( https://github.com/mrmlnc/fast-glob#basic-syntax ) ,或者是相对于 Vite 项目根目录的匹配模式数组。当显式声明了 ` optimizeDeps.entries ` 时默认只有 ` node_modules ` 和 ` build.outDir ` 文件夹会被忽略。如果还需忽略其他文件夹,你可以在模式列表中使用以 ` ! ` 为前缀的、用来匹配忽略项的模式 。
924
973
925
974
### optimizeDeps.exclude {#optimizedeps-exclude}
926
975
0 commit comments