Skip to content

Commit 1e5f28d

Browse files
release: v3.0.2 Merge pull request #556 from vitejs/dev
2 parents c20ede7 + 293426b commit 1e5f28d

23 files changed

+231
-33
lines changed

.vitepress/theme/components/AsideSponsors.vue

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,71 @@ const sponsors = computed(() => {
1818
</script>
1919

2020
<template>
21+
<a class="viteconf" href="https://viteconf.org" target="_blank">
22+
<img width="22" height="22" src="/viteconf.svg" />
23+
<span>
24+
<p class="extra-info">Free Online Conference</p>
25+
<p class="heading">ViteConf - Oct 11</p>
26+
<p class="extra-info">Get your ticket now!</p>
27+
</span>
28+
</a>
2129
<VPDocAsideSponsors v-if="data" :data="sponsors" />
2230
</template>
31+
32+
<style>
33+
.viteconf {
34+
margin-top: 1rem;
35+
margin-bottom: 1rem;
36+
border-radius: 14px;
37+
padding-left: 2.5rem;
38+
padding-top: 0.4rem;
39+
padding-bottom: 0.4rem;
40+
position: relative;
41+
font-size: 1rem;
42+
font-weight: 700;
43+
line-height: 1.1rem;
44+
filter: grayscale(100%);
45+
display: flex;
46+
align-items: center;
47+
justify-content: center;
48+
gap: 0.75rem;
49+
background-color: var(--vp-c-bg-alt);
50+
border: 2px solid var(--vp-c-bg-alt);
51+
transition: border-color 0.5s;
52+
}
53+
.viteconf:hover {
54+
filter: grayscale(0%);
55+
border: 2px solid var(--vp-c-brand-light);
56+
}
57+
.viteconf img {
58+
position: absolute;
59+
left: 1.5rem;
60+
transition: transform 0.5s;
61+
}
62+
.viteconf:hover img {
63+
transform: scale(1.75);
64+
}
65+
66+
.viteconf:hover .heading {
67+
background-image: linear-gradient(
68+
120deg,
69+
#bd34fe 16%,
70+
var(--vp-c-brand-light),
71+
var(--vp-c-brand-light)
72+
);
73+
background-clip: text;
74+
-webkit-background-clip: text;
75+
-webkit-text-fill-color: transparent;
76+
}
77+
78+
.viteconf .extra-info {
79+
color: var(--vp-c-text-1);
80+
opacity: 0;
81+
font-size: 0.7rem;
82+
padding-left: 0.1rem;
83+
transition: opacity 0.5s;
84+
}
85+
.viteconf:hover .extra-info {
86+
opacity: 0.9;
87+
}
88+
</style>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script setup lang="ts">
2+
defineProps<{ svg: string }>()
3+
</script>
4+
5+
<template>
6+
<figure class="svg-image-root" v-html="svg" />
7+
</template>
8+
9+
<style>
10+
.svg-image-root {
11+
background-color: #eee;
12+
border-radius: 8px;
13+
padding: 1ch;
14+
margin: 1ch 0;
15+
}
16+
html.dark .svg-image-root {
17+
background-color: #313641;
18+
}
19+
.svg-image-root svg text {
20+
font-family: var(--vp-font-family-base);
21+
}
22+
</style>

.vitepress/theme/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Theme from 'vitepress/theme'
33
import './styles/vars.css'
44
import HomeSponsors from './components/HomeSponsors.vue'
55
import AsideSponsors from './components/AsideSponsors.vue'
6+
import SvgImage from './components/SvgImage.vue'
67

78
export default {
89
...Theme,
@@ -11,5 +12,8 @@ export default {
1112
'home-features-after': () => h(HomeSponsors),
1213
'aside-ads-before': () => h(AsideSponsors)
1314
})
15+
},
16+
enhanceApp({ app }) {
17+
app.component('SvgImage', SvgImage)
1418
}
1519
}

blog/announcing-vite3.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ There are other deploy scenarios where this isn't enough. For example, if the ge
193193

194194
### Esbuild Deps Optimization at Build Time (Experimental)
195195

196-
One of the main differences between dev and build time is how Vite handles dependencies. During build time, [`@rollupjs/plugin-commonjs`](https://github.com/rollup/plugins/tree/master/packages/commonjs) is used to allow importing CJS only dependencies (like React). When using the dev server, esbuild is used instead to pre-bundle and optimize dependencies, and an inline interop scheme is applied while transforming user code importing CJS deps. During the development of Vite 3, we introduced the changes needed to also allow the use of [esbuild to optimize dependencies during build time](/guide/migration.html#using-esbuild-deps-optimization-at-build-time). [`@rollupjs/plugin-commonjs`](https://github.com/rollup/plugins/tree/master/packages/commonjs) can then be avoided, making dev and build time work in the same way.
196+
One of the main differences between dev and build time is how Vite handles dependencies. During build time, [`@rollup/plugin-commonjs`](https://github.com/rollup/plugins/tree/master/packages/commonjs) is used to allow importing CJS only dependencies (like React). When using the dev server, esbuild is used instead to pre-bundle and optimize dependencies, and an inline interop scheme is applied while transforming user code importing CJS deps. During the development of Vite 3, we introduced the changes needed to also allow the use of [esbuild to optimize dependencies during build time](/guide/migration.html#using-esbuild-deps-optimization-at-build-time). [`@rollup/plugin-commonjs`](https://github.com/rollup/plugins/tree/master/packages/commonjs) can then be avoided, making dev and build time work in the same way.
197197

198198
Given that Rollup v3 will be out in the next months, and we're going to follow up with another Vite major, we've decided to make this mode optional to reduce v3 scope and give Vite and the ecosystem more time to work out possible issues with the new CJS interop approach during build time. Frameworks may switch to using esbuild deps optimization during build time by default at their own pace before Vite 4.
199199

@@ -223,7 +223,7 @@ A triaging marathon was spearheaded by [@bluwyoo](https://twitter.com/bluwyoo),
223223

224224
## Compatibility Notes
225225

226-
- Vite no longer supports Node.js 12, which reached its EOL. Node.js 14.18+ is now required.
226+
- Vite no longer supports Node.js 12 / 13 / 15, which reached its EOL. Node.js 14.18+ / 16+ is now required.
227227
- Vite is now published as ESM, with a CJS proxy to the ESM entry for compatibility.
228228
- The Modern Browser Baseline now targets browsers which support the [native ES Modules](https://caniuse.com/es6-module), [native ESM dynamic import](https://caniuse.com/es6-module-dynamic-import), and [`import.meta`](https://caniuse.com/mdn-javascript_statements_import_meta) features.
229229
- JS file extensions in SSR and library mode now use a valid extension (`js`, `mjs`, or `cjs`) for output JS entries and chunks based on their format and the package type.

config/build-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- **默认:** `'modules'`
77
- **相关内容:** [浏览器兼容性](/guide/build#browser-compatibility)
88

9-
设置最终构建的浏览器兼容目标。默认值是一个 Vite 特有的值——`'modules'`,这是指 [支持原生 ES 模块的浏览器](https://caniuse.com/es6-module)
9+
设置最终构建的浏览器兼容目标。默认值是一个 Vite 特有的值——`'modules'`,这是指 [支持原生 ES 模块](https://caniuse.com/es6-module)[原生 ESM 动态导入](https://caniuse.com/es6-module-dynamic-import)[`import.meta`](https://caniuse.com/mdn-javascript_statements_import_meta) 的浏览器
1010

1111
另一个特殊值是 “esnext” —— 即假设有原生动态导入支持,并且将会转译得尽可能小:
1212

config/index.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,6 @@ export default {
2121
vite --config my-config.js
2222
```
2323

24-
::: tip 注意
25-
注意,Vite 会在 **CommonJS****TypeScript** 配置文件中替换 `__filename``__dirname` 以及 `import.meta.url`。如果使用这些名称作为变量名可能会导致代码报错:
26-
27-
```js
28-
const __filename = 'value' // SyntaxError: Identifier '__filename' has already been declared
29-
30-
const func = () => {
31-
const __filename = 'value' // no error
32-
}
33-
```
34-
35-
:::
36-
3724
## 配置智能提示 {#config-intellisense}
3825

3926
因为 Vite 本身附带 Typescript 类型,所以你可以通过 IDE 和 jsdoc 的配合来实现智能提示:

guide/backend-integration.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333

3434
```html
3535
<!-- 如果是在开发环境中 -->
36-
<script type="module" src="http://localhost:3000/@vite/client"></script>
36+
<script type="module" src="http://localhost:5173/@vite/client"></script>
37+
<script type="module" src="http://localhost:5173/main.js"></script>
3738
```
3839

3940
为了正确地提供资源,你有两种选项:

guide/build.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## 浏览器兼容性 {#browser-compatibility}
66

7-
用于生产环境的构建包会假设目标浏览器支持现代 JavaScript 语法。默认情况下,Vite 的目标浏览器是指能够 [支持原生 ESM script 标签](https://caniuse.com/es6-module)[支持原生 ESM 动态导入](https://caniuse.com/es6-module-dynamic-import) 的。作为参考,Vite 使用这个 [browserslist](https://github.com/browserslist/browserslist) 作为查询标准
7+
用于生产环境的构建包会假设目标浏览器支持现代 JavaScript 语法。默认情况下,Vite 的目标是能够 [支持原生 ESM script 标签](https://caniuse.com/es6-module)[支持原生 ESM 动态导入](https://caniuse.com/es6-module-dynamic-import) [`import.meta`](https://caniuse.com/mdn-javascript_statements_import_meta) 的浏览器
88

99
- Chrome >=87
1010
- Firefox >=78
@@ -187,6 +187,10 @@ dist/my-lib.umd.cjs 0.30 KiB / gzip: 0.16 KiB
187187
如果 `package.json` 不包含 `"type": "module"`,Vite 会生成不同的文件后缀名以兼容 Node.js。`.js` 会变为 `.mjs``.cjs` 会变为 `.js`.
188188
:::
189189

190+
::: tip 环境变量
191+
在库模式下,所有 `import.meta.env.*` 用法在构建生产时都会被静态替换。但是,`process.env.*` 的用法不会被替换,所以你的库的使用者可以动态地更改它。如果不想允许他们这样做,你可以使用 `define: { 'process.env.`<wbr>`NODE_ENV': '"production"' }` 例如静态替换它们。
192+
:::
193+
190194
## 进阶基础路径选项 {#advanced-base-options}
191195

192196
::: warning

guide/env-and-mode.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ DB_PASSWORD=foobar
5959
console.log(import.meta.env.VITE_SOME_KEY) // 123
6060
console.log(import.meta.env.DB_PASSWORD) // undefined
6161
```
62-
如果你想自定义 env 变量的前缀,请参阅 [envPrefix](/config/index#envprefix)。
62+
如果你想自定义 env 变量的前缀,请参阅 [envPrefix](/config/shared-options.html#envprefix)。
6363
6464
:::warning 安全注意事项
6565

guide/features.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,10 @@ Vite 会为入口 chunk 和它们在打包出的 HTML 中的直接引入自动
538538

539539
在实际项目中,Rollup 通常会生成 “共用” chunk —— 被两个或以上的其他 chunk 共享的 chunk。与动态导入相结合,会很容易出现下面这种场景:
540540

541-
![graph](/images/graph.png)
541+
<script setup>
542+
import graphSvg from '../images/graph.svg?raw'
543+
</script>
544+
<svg-image :svg="graphSvg" />
542545

543546
在无优化的情境下,当异步 chunk `A` 被导入时,浏览器将必须请求和解析 `A`,然后它才能弄清楚它也需要共用 chunk `C`。这会导致额外的网络往返:
544547

guide/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Vite 意在提供开箱即用的配置,同时它的 [插件 API](./api-plugin)
1818

1919
## 浏览器支持 {#browser-support}
2020

21-
默认的构建目标浏览器是能 [在 script 标签上支持原生 ESM](https://caniuse.com/es6-module)[原生 ESM 动态导入](https://caniuse.com/es6-module-dynamic-import)。传统浏览器可以通过官方插件 [@vitejs/plugin-legacy](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) 支持 —— 查看 [构建生产版本](./build) 章节获取更多细节。
21+
默认的构建目标是能支持 [原生 ESM 语法的 script 标签](https://caniuse.com/es6-module)[原生 ESM 动态导入](https://caniuse.com/es6-module-dynamic-import)[`import.meta`](https://caniuse.com/mdn-javascript_statements_import_meta) 的浏览器。传统浏览器可以通过官方插件 [@vitejs/plugin-legacy](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) 支持 —— 查看 [构建生产版本](./build) 章节获取更多细节。
2222

2323
## 在线试用 Vite {#trying-vite-online}
2424

@@ -38,7 +38,7 @@ Vite 意在提供开箱即用的配置,同时它的 [插件 API](./api-plugin)
3838
## 搭建第一个 Vite 项目 {#scaffolding-your-first-vite-project}
3939

4040
::: tip 兼容性注意
41-
Vite 需要 [Node.js](https://nodejs.org/en/) 版本 >= 14.18.0。然而,有些模板需要依赖更高的 Node 版本才能正常运行,当你的包管理器发出警告时,请注意升级你的 Node 版本。
41+
Vite 需要 [Node.js](https://nodejs.org/en/) 版本 14.18+,16+。然而,有些模板需要依赖更高的 Node 版本才能正常运行,当你的包管理器发出警告时,请注意升级你的 Node 版本。
4242
:::
4343

4444
使用 NPM:

guide/migration.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Node 支持 {#node-support}
44

5-
Vite 不再支持 Node v12,因为它已经进入了 EOL 阶段。现在你必须使用 Node 14.18+ 及以上版本
5+
Vite 不再支持 Node 12 / 13 / 15,因为上述版本已经进入了 EOL 阶段。现在你必须使用 Node 14.18+ / 16+ 版本
66

77
## 现代浏览器基准线变化 {#modern-browser-baseline-change}
88

@@ -107,10 +107,10 @@ export default {
107107

108108
### 在构建阶段使用 esbuild 依赖优化
109109

110-
在 v3 版本下,Vite 允许在构建阶段使用 esbuild 进行依赖优化。如果开启,it removes one of the most significant differences between dev and prod present in v2. [`@rollupjs/plugin-commonjs`](https://github.com/rollup/plugins/tree/master/packages/commonjs) is no longer needed in this case since esbuild converts CJS-only dependencies to ESM.
110+
在 v3 版本下,Vite 允许在构建阶段使用 esbuild 进行依赖优化。如果开启此项,那么它将消除 v2 版本中存在的最明显的开发与构建最终产物之间的区别。[`@rollupjs/plugin-commonjs`](https://github.com/rollup/plugins/tree/master/packages/commonjs) 在此处不再需要因为 esbuild 会将纯 CommonJS 依赖转换为 ESM
111111

112-
If you want to try this build strategy, you can use `optimizeDeps.disabled: false` (the default in v3 is `disabled: 'build'`). `@rollup/plugin-commonjs`
113-
can be removed by passing `build.commonjsOptions: { include: [] }`
112+
如果你想尝试该构建策略,你可以使用 `optimizeDeps.disabled: false`(在 v3 中默认是 `disabled: 'build'`)。`@rollup/plugin-commonjs`
113+
可以通过设置 `build.commonjsOptions: { include: [] }` 来移除。
114114

115115
## 进阶 {#advanced}
116116

guide/ssr.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ if (import.meta.env.SSR) {
6161
6262
**server.js**
6363
64-
```js{17-19}
64+
```js{15-18}
6565
import fs from 'fs'
6666
import path from 'path'
67+
import { fileURLToPath } from 'url'
6768
import express from 'express'
68-
import {createServer as createViteServer} from 'vite'
69+
import { createServer as createViteServer } from 'vite'
70+
71+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
6972

7073
async function createServer() {
7174
const app = express()

guide/static-deploy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ $ npm run preview
6464

6565
2. 在你的项目中,创建一个 `deploy.sh` 脚本,包含以下内容(注意高亮的行,按需使用),运行脚本来部署站点:
6666

67-
```bash{13,20,23}
67+
```bash{13,21,24}
6868
#!/usr/bin/env sh
6969
7070
# 发生错误时终止

guide/why.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
时过境迁,我们见证了诸如 [webpack](https://webpack.js.org/)[Rollup](https://rollupjs.org)[Parcel](https://parceljs.org/) 等工具的变迁,它们极大地改善了前端开发者的开发体验。
88

9-
然而,当我们开始构建越来越大型的应用时,需要处理的 JavaScript 代码量也呈指数级增长。包含数千个模块的大型项目相当普遍。基于 JavaScript 开发的工具就会开始遇到性能瓶颈:通常需要很长时间(甚至是几分钟!)才能启动开发服务器,即使使用 HMR,文件修改后的效果也需要几秒钟才能在浏览器中反映出来。如此循环往复,迟钝的反馈会极大地影响开发者的开发效率和幸福感。
9+
然而,当我们开始构建越来越大型的应用时,需要处理的 JavaScript 代码量也呈指数级增长。包含数千个模块的大型项目相当普遍。基于 JavaScript 开发的工具就会开始遇到性能瓶颈:通常需要很长时间(甚至是几分钟!)才能启动开发服务器,即使使用模块热替换(HMR,文件修改后的效果也需要几秒钟才能在浏览器中反映出来。如此循环往复,迟钝的反馈会极大地影响开发者的开发效率和幸福感。
1010

1111
Vite 旨在利用生态系统中的新进展解决上述问题:浏览器开始原生支持 ES 模块,且越来越多 JavaScript 工具使用编译型语言编写。
1212

@@ -24,9 +24,12 @@ Vite 通过在一开始将应用中的模块区分为 **依赖** 和 **源码**
2424

2525
Vite 以 [原生 ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) 方式提供源码。这实际上是让浏览器接管了打包程序的部分工作:Vite 只需要在浏览器请求源码时进行转换并按需提供源码。根据情景动态导入代码,即只在当前屏幕上实际使用时才会被处理。
2626

27-
![基于打包器的开发服务器](/images/bundler.png)
28-
29-
![基于 ESM 的开发服务器](/images/esm.png)
27+
<script setup>
28+
import bundlerSvg from '../images/bundler.svg?raw'
29+
import esmSvg from '../images/esm.svg?raw'
30+
</script>
31+
<svg-image :svg="bundlerSvg" />
32+
<svg-image :svg="esmSvg" />
3033

3134
### 缓慢的更新 {#slow-updates}
3235

images/bundler.png

-29.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)