Skip to content

Commit 07aef1b

Browse files
authored
docs: cleanup changes (#8989)
1 parent 33d6177 commit 07aef1b

File tree

13 files changed

+97
-121
lines changed

13 files changed

+97
-121
lines changed

docs/config/server-options.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ Create Vite server in middleware mode.
198198
- **Example:**
199199

200200
```js
201-
const express = require('express')
202-
const { createServer: createViteServer } = require('vite')
201+
import express from 'express'
202+
import { createServer as createViteServer } from 'vite'
203203

204204
async function createServer() {
205205
const app = express()

docs/guide/api-javascript.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ async function createServer(inlineConfig?: InlineConfig): Promise<ViteDevServer>
1313
**Example Usage:**
1414

1515
```js
16-
const { createServer } = require('vite')
16+
import { fileURLToPath } from 'url'
17+
import { createServer } from 'vite'
18+
19+
const __dirname = fileURLToPath(new URL('.', import.meta.url))
1720
1821
;(async () => {
1922
const server = await createServer({
@@ -134,8 +137,11 @@ async function build(
134137
**Example Usage:**
135138

136139
```js
137-
const path = require('path')
138-
const { build } = require('vite')
140+
import path from 'path'
141+
import { fileURLToPath } from 'url'
142+
import { build } from 'vite'
143+
144+
const __dirname = fileURLToPath(new URL('.', import.meta.url))
139145
140146
;(async () => {
141147
await build({
@@ -161,8 +167,7 @@ async function preview(inlineConfig?: InlineConfig): Promise<PreviewServer>
161167
**Example Usage:**
162168

163169
```js
164-
const { preview } = require('vite')
165-
170+
import { preview } from 'vite'
166171
;(async () => {
167172
const previewServer = await preview({
168173
// any valid user config options, plus `mode` and `configFile`

docs/guide/api-plugin.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Vite strives to offer established patterns out of the box, so before creating a
1111
When creating a plugin, you can inline it in your `vite.config.js`. There is no need to create a new package for it. Once you see that a plugin was useful in your projects, consider sharing it to help others [in the ecosystem](https://chat.vitejs.dev).
1212

1313
::: tip
14-
When learning, debugging, or authoring plugins we suggest including [vite-plugin-inspect](https://github.com/antfu/vite-plugin-inspect) in your project. It allows you to inspect the intermediate state of Vite plugins. After installing, you can visit `localhost:5173/__inspect/` to inspect the modules and transformation stack of your project. Check out install instructions in the [vite-plugin-inspect docs](https://github.com/antfu/vite-plugin-inspect).
14+
When learning, debugging, or authoring plugins, we suggest including [vite-plugin-inspect](https://github.com/antfu/vite-plugin-inspect) in your project. It allows you to inspect the intermediate state of Vite plugins. After installing, you can visit `localhost:5173/__inspect/` to inspect the modules and transformation stack of your project. Check out install instructions in the [vite-plugin-inspect docs](https://github.com/antfu/vite-plugin-inspect).
1515
![vite-plugin-inspect](/images/vite-plugin-inspect.png)
1616
:::
1717

@@ -199,7 +199,7 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
199199
name: 'mutate-config',
200200
config(config, { command }) {
201201
if (command === 'build') {
202-
config.root = __dirname
202+
config.root = 'foo'
203203
}
204204
}
205205
})

docs/guide/assets.md

-4
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,3 @@ const imgUrl = new URL(imagePath, import.meta.url).href
113113
::: warning Does not work with SSR
114114
This pattern does not work if you are using Vite for Server-Side Rendering, because `import.meta.url` have different semantics in browsers vs. Node.js. The server bundle also cannot determine the client host URL ahead of time.
115115
:::
116-
117-
::: warning `target` needs to be `es2020` or higher
118-
This pattern will not work if [build-target](/config/build-options.md#build-target) or [optimizedeps.esbuildoptions.target](/config/dep-optimization-options.md#optimizedeps-esbuildoptions) is set to a value lower than `es2020`.
119-
:::

docs/guide/build.md

+20-20
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The build can be customized via various [build config options](/config/build-opt
3535

3636
```js
3737
// vite.config.js
38-
module.exports = defineConfig({
38+
export default defineConfig({
3939
build: {
4040
rollupOptions: {
4141
// https://rollupjs.org/guide/en/#big-list-of-options
@@ -53,7 +53,7 @@ You can configure how chunks are split using `build.rollupOptions.output.manualC
5353
```js
5454
// vite.config.js
5555
import { splitVendorChunkPlugin } from 'vite'
56-
module.exports = defineConfig({
56+
export default defineConfig({
5757
plugins: [splitVendorChunkPlugin()]
5858
})
5959
```
@@ -66,7 +66,7 @@ You can enable rollup watcher with `vite build --watch`. Or, you can directly ad
6666

6767
```js
6868
// vite.config.js
69-
module.exports = defineConfig({
69+
export default defineConfig({
7070
build: {
7171
watch: {
7272
// https://rollupjs.org/guide/en/#watch-options
@@ -97,10 +97,10 @@ During build, all you need to do is to specify multiple `.html` files as entry p
9797

9898
```js
9999
// vite.config.js
100-
const { resolve } = require('path')
101-
const { defineConfig } = require('vite')
100+
import { resolve } from 'path'
101+
import { defineConfig } from 'vite'
102102

103-
module.exports = defineConfig({
103+
export default defineConfig({
104104
build: {
105105
rollupOptions: {
106106
input: {
@@ -122,10 +122,10 @@ When it is time to bundle your library for distribution, use the [`build.lib` co
122122

123123
```js
124124
// vite.config.js
125-
const path = require('path')
126-
const { defineConfig } = require('vite')
125+
import { resolve } from 'path'
126+
import { defineConfig } from 'vite'
127127

128-
module.exports = defineConfig({
128+
export default defineConfig({
129129
build: {
130130
lib: {
131131
entry: path.resolve(__dirname, 'lib/main.js'),
@@ -214,17 +214,17 @@ experimental: {
214214
If the hashed assets and public files aren't deployed together, options for each group can be defined independently using asset `type` included in the third `context` param given to the function.
215215

216216
```js
217-
experimental: {
218-
renderBuiltUrl(filename: string, { hostType: 'js' | 'css' | 'html', type: 'public' | 'asset' }) {
219-
if (type === 'public') {
220-
return 'https://www.domain.com/' + filename
221-
}
222-
else if (path.extname(importer) === '.js') {
223-
return { runtime: `window.__assetsPath(${JSON.stringify(filename)})` }
224-
}
225-
else {
226-
return 'https://cdn.domain.com/assets/' + filename
227-
}
217+
experimental: {
218+
renderBuiltUrl(filename: string, { hostType: 'js' | 'css' | 'html', type: 'public' | 'asset' }) {
219+
if (type === 'public') {
220+
return 'https://www.domain.com/' + filename
221+
}
222+
else if (path.extname(importer) === '.js') {
223+
return { runtime: `window.__assetsPath(${JSON.stringify(filename)})` }
224+
}
225+
else {
226+
return 'https://cdn.domain.com/assets/' + filename
228227
}
229228
}
229+
}
230230
```

docs/guide/env-and-mode.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Vite exposes env variables on the special **`import.meta.env`** object. Some bui
1212

1313
- **`import.meta.env.DEV`**: {boolean} whether the app is running in development (always the opposite of `import.meta.env.PROD`)
1414

15+
- **`import.meta.env.SSR`**: {boolean} whether the app is running in the [server](./ssr.md#conditional-logic).
16+
1517
### Production Replacement
1618

1719
During production, these env variables are **statically replaced**. It is therefore necessary to always reference them using the full static string. For example, dynamic key access like `import.meta.env[key]` will not work.
@@ -47,12 +49,17 @@ Loaded env variables are also exposed to your client source code via `import.met
4749
To prevent accidentally leaking env variables to the client, only variables prefixed with `VITE_` are exposed to your Vite-processed code. e.g. the following file:
4850

4951
```
50-
DB_PASSWORD=foobar
5152
VITE_SOME_KEY=123
53+
DB_PASSWORD=foobar
5254
```
5355

5456
Only `VITE_SOME_KEY` will be exposed as `import.meta.env.VITE_SOME_KEY` to your client source code, but `DB_PASSWORD` will not.
5557

58+
```js
59+
console.log(import.meta.env.VITE_SOME_KEY) // 123
60+
console.log(import.meta.env.DB_PASSWORD) // undefined
61+
```
62+
5663
If you want to customize env variables prefix, see [envPrefix](/config/index#envprefix) option.
5764
5865
:::warning SECURITY NOTES

docs/guide/features.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,12 @@ Vite provides first-class Vue support:
108108

109109
- Vue 3 SFC support via [@vitejs/plugin-vue](https://github.com/vitejs/vite/tree/main/packages/plugin-vue)
110110
- Vue 3 JSX support via [@vitejs/plugin-vue-jsx](https://github.com/vitejs/vite/tree/main/packages/plugin-vue-jsx)
111-
- Vue 2 support via [underfin/vite-plugin-vue2](https://github.com/underfin/vite-plugin-vue2)
111+
- Vue 2.7 support via [vitejs/vite-plugin-vue2](https://github.com/vitejs/vite-plugin-vue2)
112+
- Vue <2.7 support via [underfin/vite-plugin-vue2](https://github.com/underfin/vite-plugin-vue2)
112113

113114
## JSX
114115

115-
`.jsx` and `.tsx` files are also supported out of the box. JSX transpilation is also handled via [esbuild](https://esbuild.github.io), and defaults to the React 16 flavor. React 17 style JSX support in esbuild is tracked [here](https://github.com/evanw/esbuild/issues/334).
116+
`.jsx` and `.tsx` files are also supported out of the box. JSX transpilation is also handled via [esbuild](https://esbuild.github.io).
116117

117118
Vue users should use the official [@vitejs/plugin-vue-jsx](https://github.com/vitejs/vite/tree/main/packages/plugin-vue-jsx) plugin, which provides Vue 3 specific features including HMR, global component resolving, directives and slots.
118119

docs/guide/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ You can learn more about the rationale behind the project in the [Why Vite](./wh
1818

1919
## Browser Support
2020

21-
- The default build targets browsers that support both [native ES Modules](https://caniuse.com/es6-module) and [native ESM dynamic import](https://caniuse.com/es6-module-dynamic-import). Legacy browsers can be supported via the official [@vitejs/plugin-legacy](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) - see the [Building for Production](./build) section for more details.
21+
The default build targets browsers that support both [native ES Modules](https://caniuse.com/es6-module) and [native ESM dynamic import](https://caniuse.com/es6-module-dynamic-import). Legacy browsers can be supported via the official [@vitejs/plugin-legacy](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) - see the [Building for Production](./build) section for more details.
2222

2323
## Trying Vite Online
2424

docs/guide/migration.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ A small fraction of users will now require using [@vitejs/plugin-legacy](https:/
1717

1818
## Config Options Changes
1919

20-
- The following options that were already deprecated in v2 have been removed:
20+
The following options that were already deprecated in v2 have been removed:
2121

22-
- `alias` (switch to [`resolve.alias`](../config/shared-options.md#resolve-alias))
23-
- `dedupe` (switch to [`resolve.dedupe`](../config/shared-options.md#resolve-dedupe))
24-
- `build.base` (switch to [`base`](../config/shared-options.md#base))
25-
- `build.brotliSize` (switch to [`build.reportCompressedSize`](../config/build-options.md#build-reportcompressedsize))
26-
- `build.cleanCssOptions` (Vite now uses esbuild for CSS minification)
27-
- `build.polyfillDynamicImport` (use [`@vitejs/plugin-legacy`](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) for browsers without dynamic import support)
28-
- `optimizeDeps.keepNames` (switch to [`optimizeDeps.esbuildOptions.keepNames`](../config/dep-optimization-options.md#optimizedeps-esbuildoptions))
22+
- `alias` (switch to [`resolve.alias`](../config/shared-options.md#resolve-alias))
23+
- `dedupe` (switch to [`resolve.dedupe`](../config/shared-options.md#resolve-dedupe))
24+
- `build.base` (switch to [`base`](../config/shared-options.md#base))
25+
- `build.brotliSize` (switch to [`build.reportCompressedSize`](../config/build-options.md#build-reportcompressedsize))
26+
- `build.cleanCssOptions` (Vite now uses esbuild for CSS minification)
27+
- `build.polyfillDynamicImport` (use [`@vitejs/plugin-legacy`](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) for browsers without dynamic import support)
28+
- `optimizeDeps.keepNames` (switch to [`optimizeDeps.esbuildOptions.keepNames`](../config/dep-optimization-options.md#optimizedeps-esbuildoptions))
2929

30-
## Architecture changes and legacy Options
30+
## Architecture Changes and Legacy Options
3131

3232
This section describes the biggest architecture changes in Vite v3. To allow projects to migrate from v2 in case of a compat issue, legacy options have been added to revert to the Vite v2 strategies.
3333

@@ -72,7 +72,7 @@ Also [`build.rollupOptions.output.inlineDynamicImports`](https://rollupjs.org/gu
7272
- When using an alias with `import.meta.glob`, the keys are always absolute.
7373
- `import.meta.globEager` is now deprecated. Use `import.meta.glob('*', { eager: true })` instead.
7474

75-
### WebAssembly support
75+
### WebAssembly Support
7676

7777
`import init from 'example.wasm'` syntax is dropped to prevent future collision with ["ESM integration for Wasm"](https://github.com/WebAssembly/esm-integration).
7878
You can use `?init` which is similar to the previous behavior.
@@ -87,7 +87,7 @@ You can use `?init` which is similar to the previous behavior.
8787
})
8888
```
8989

90-
### Automatic https certificate generation
90+
### Automatic https Certificate Generation
9191

9292
A valid certificate is needed when using `https`. In Vite v2, if no certificate was configured, a self-signed certificate was automatically created and cached.
9393
Since Vite v3, we recommend manually creating your certificates. If you still want to use the automatic generation from v2, this feature can be enabled back by adding [@vitejs/plugin-basic-ssl](https://github.com/vitejs/vite-plugin-basic-ssl) to the project plugins.

docs/guide/ssr.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ When building an SSR app, you likely want to have full control over your main se
6666
**server.js**
6767
6868
```js{17-19}
69-
const fs = require('fs')
70-
const path = require('path')
71-
const express = require('express')
72-
const { createServer: createViteServer } = require('vite')
69+
import fs from 'fs'
70+
import path from 'path'
71+
import express from 'express'
72+
import {createServer as createViteServer} from 'vite'
7373

7474
async function createServer() {
7575
const app = express()
@@ -154,7 +154,7 @@ The `dev` script in `package.json` should also be changed to use the server scri
154154
To ship an SSR project for production, we need to:
155155
156156
1. Produce a client build as normal;
157-
2. Produce an SSR build, which can be directly loaded via `require()` so that we don't have to go through Vite's `ssrLoadModule`;
157+
2. Produce an SSR build, which can be directly loaded via `import()` so that we don't have to go through Vite's `ssrLoadModule`;
158158
159159
Our scripts in `package.json` will look like this:
160160
@@ -174,7 +174,7 @@ Then, in `server.js` we need to add some production specific logic by checking `
174174
175175
- Instead of reading the root `index.html`, use the `dist/client/index.html` as the template instead, since it contains the correct asset links to the client build.
176176
177-
- Instead of `await vite.ssrLoadModule('/src/entry-server.js')`, use `require('./dist/server/entry-server.js')` instead (this file is the result of the SSR build).
177+
- Instead of `await vite.ssrLoadModule('/src/entry-server.js')`, use `import('./dist/server/entry-server.js')` instead (this file is the result of the SSR build).
178178
179179
- Move the creation and all usage of the `vite` dev server behind dev-only conditional branches, then add static file serving middlewares to serve files from `dist/client`.
180180

0 commit comments

Comments
 (0)