Skip to content

Commit

Permalink
Merge branch 'main' into chore/terser-optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jun 8, 2022
2 parents 8e1d22b + 408e5a7 commit 92113ec
Show file tree
Hide file tree
Showing 70 changed files with 989 additions and 566 deletions.
2 changes: 1 addition & 1 deletion docs/.vitepress/config.ts
Expand Up @@ -150,7 +150,7 @@ export default defineConfig({
link: '/guide/comparisons'
},
{
text: 'Migration from v1',
text: 'Migration from v2',
link: '/guide/migration'
}
]
Expand Down
9 changes: 9 additions & 0 deletions docs/guide/features.md
Expand Up @@ -210,6 +210,15 @@ Vite improves `@import` resolving for Sass and Less so that Vite aliases are als

You can also use CSS modules combined with pre-processors by prepending `.module` to the file extension, for example `style.module.scss`.

### Disabling CSS injection into the page

The automatic injection of CSS contents can be turned off via the `?inline` query parameter. In this case, the processed CSS string is returned as the module's default export as usual, but the styles aren't injected to the page.

```js
import styles from './foo.css' // will be injected into the page
import otherStyles from './bar.css?inline' // will not be injected into the page
```

## Static Assets

Importing a static asset will return the resolved public URL when it is served:
Expand Down
176 changes: 78 additions & 98 deletions docs/guide/migration.md
@@ -1,133 +1,113 @@
# Migration from v1
# Migration from v2

## Config Options Change
## Node Support

- The following options have been removed and should be implemented via [plugins](./api-plugin):
Vite no longer supports Node v12, which reached its EOL. Node 14.6+ is now required.

- `resolvers`
- `transforms`
- `indexHtmlTransforms`
## Modern Browser Baseline change

- `jsx` and `enableEsbuild` have been removed; Use the new [`esbuild`](/config/#esbuild) option instead.
The production bundle assumes support for modern JavaScript. By default, Vite targets browsers which support the [native ES Modules](https://caniuse.com/es6-module) and [native ESM dynamic import](https://caniuse.com/es6-module-dynamic-import) and [`import.meta`](https://caniuse.com/mdn-javascript_statements_import_meta):

- [CSS related options](/config/#css-modules) are now nested under `css`.
- Chrome >=87
- Firefox >=78
- Safari >=13
- Edge >=88

- All [build-specific options](/config/#build-options) are now nested under `build`.
A small fraction of users will now require using [@vitejs/plugin-legacy](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy), which will automatically generate legacy chunks and corresponding ES language feature polyfills.

- `rollupInputOptions` and `rollupOutputOptions` are replaced by [`build.rollupOptions`](/config/#build-rollupoptions).
- `esbuildTarget` is now [`build.target`](/config/#build-target).
- `emitManifest` is now [`build.manifest`](/config/#build-manifest).
- The following build options have been removed since they can be achieved via plugin hooks or other options:
- `entry`
- `rollupDedupe`
- `emitAssets`
- `emitIndex`
- `shouldPreload`
- `configureBuild`
## Config Options Changes

- All [server-specific options](/config/#server-options) are now nested under
`server`.
- The following options that were already deprecated in v2 have been removed:

- `hostname` is now [`server.host`](/config/#server-host).
- `httpsOptions` has been removed. [`server.https`](/config/#server-https) can directly accept the options object.
- `chokidarWatchOptions` is now [`server.watch`](/config/#server-watch).
- `alias` (switch to [`resolve.alias`](../config/shared-options.md#resolvealias))
- `dedupe` (switch to [`resolve.dedupe`](../config/shared-options.md#resolvededupe))
- `build.base` (switch to [`base`](../config/shared-options.md#base))
- `build.brotliSize` (switch to [`build.reportCompressedSize`](../config/build-options.md#build-reportcompressedsize))
- `build.cleanCssOptions` (Vite now uses esbuild for CSS minification)
- `build.polyfillDynamicImport` (use [`@vitejs/plugin-legacy`](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) for browsers without dynamic import support)
- `optimizeDeps.keepNames` (switch to [`optimizeDeps.esbuildOptions.keepNames`](../config/dep-optimization-options.md#optimizedepsesbuildoptions))

- [`assetsInclude`](/config/#assetsinclude) now expects `string | RegExp | (string | RegExp)[]` instead of a function.
## Dev Server Changes

- All Vue specific options are removed; Pass options to the Vue plugin instead.
Vite's default dev server port is now 5173. You can use [`server.port`](../config/server-options.md#server-port) to set it to 3000.

## Alias Behavior Change
Vite optimizes dependencies with esbuild to both convert CJS-only deps to ESM and to reduce the number of modules the browser needs to request. In v3, the default strategy to discover and batch dependencies has changed. Vite no longer pre-scans user code with esbuild to get an initial list of dependencies on cold start. Instead, it delays the first dependency optimization run until every imported user module on load is processed.

[`alias`](/config/#resolve-alias) is now being passed to `@rollup/plugin-alias` and no longer require start/ending slashes. The behavior is now a direct replacement, so 1.0-style directory alias key should remove the ending slash:
To get back the v2 strategy, you can use [`optimizeDeps.devScan`](../config/dep-optimization-options.md#optimizedepsdevscan).

```diff
- alias: { '/@foo/': path.resolve(__dirname, 'some-special-dir') }
+ alias: { '/@foo': path.resolve(__dirname, 'some-special-dir') }
```
## Build Changes

Alternatively, you can use the `[{ find: RegExp, replacement: string }]` option format for more precise control.
In v3, Vite uses esbuild to optimize dependencies by default. Doing so, it removes one of the most significant differences between dev and prod present in v2. Because esbuild converts CJS-only dependencies to ESM, [`@rollupjs/plugin-commonjs`](https://github.com/rollup/plugins/tree/master/packages/commonjs) is no longer used.

## Vue Support
If you need to get back to the v2 strategy, you can use [`optimizeDeps.disabled: 'build'`](../config/dep-optimization-options.md#optimizedepsdisabled).

Vite 2.0 core is now framework agnostic. Vue support is now provided via [`@vitejs/plugin-vue`](https://github.com/vitejs/vite/tree/main/packages/plugin-vue). Simply install it and add it in the Vite config:
## SSR Changes

```js
import vue from '@vitejs/plugin-vue'
import { defineConfig } from 'vite'
Vite v3 uses ESM for the SSR build by default. When using ESM, the [SSR externalization heuristics](https://vitejs.dev/guide/ssr.html#ssr-externals) are no longer needed. By default, all dependencies are externalized. You can use [`ssr.noExternal`](../config/ssr-options.md#ssrnoexternal) to control what dependencies to include in the SSR bundle.

export default defineConfig({
plugins: [vue()]
})
```
If using ESM for SSR isn't possible in your project, you can set `ssr.format: 'cjs'` to generate a CJS bundle. In this case, the same externalization strategy of Vite v2 will be used.

### Custom Blocks Transforms

A custom plugin can be used to transform Vue custom blocks like the one below:

```ts
// vite.config.js
import vue from '@vitejs/plugin-vue'
import { defineConfig } from 'vite'

const vueI18nPlugin = {
name: 'vue-i18n',
transform(code, id) {
if (!/vue&type=i18n/.test(id)) {
return
}
if (/\.ya?ml$/.test(id)) {
code = JSON.stringify(require('js-yaml').load(code.trim()))
}
return `export default Comp => {
Comp.i18n = ${code}
}`
}
}
## General Changes

export default defineConfig({
plugins: [vue(), vueI18nPlugin]
})
```
- JS file extensions in SSR and lib mode now use a valid extension (`js`, `mjs`, or `cjs`) for output JS entries and chunks based on their format and the package type.

## React Support
### `import.meta.glob`

React Fast Refresh support is now provided via [`@vitejs/plugin-react`](https://github.com/vitejs/vite/tree/main/packages/plugin-react).
- [Raw `import.meta.glob`](features.md#glob-import-as) switched from `{ assert: { type: 'raw' }}` to `{ as: 'raw' }`
- Keys of `import.meta.glob` are now relative to the current module.

## HMR API Change
```diff
// file: /foo/index.js
const modules = import.meta.glob('../foo/*.js')

`import.meta.hot.acceptDeps()` have been deprecated. [`import.meta.hot.accept()`](./api-hmr#hot-accept-deps-cb) can now accept single or multiple deps.
// transformed:
const modules = {
- '../foo/bar.js': () => {}
+ './bar.js': () => {}
}
```

## Manifest Format Change
- When using an alias with `import.meta.glob`, the keys are always absolute.
- `import.meta.globEager` is now deprecated. Use `import.meta.glob('*', { eager: true })` instead.

The build manifest now uses the following format:
### WebAssembly support

```json
{
"index.js": {
"file": "assets/index.acaf2b48.js",
"imports": [...]
},
"index.css": {
"file": "assets/index.7b7dbd85.css"
}
"asset.png": {
"file": "assets/asset.0ab0f9cd.png"
}
}
`import init from 'example.wasm'` syntax is dropped to prevent future collision with ["ESM integration for Wasm"](https://github.com/WebAssembly/esm-integration).
You can use `?init` which is similar to the previous behavior.

```diff
-import init from 'example.wasm'
+import init from 'example.wasm?init'

-init().then((instance) => {
+init().then(({ exports }) => {
exports.test()
})
```

For entry JS chunks, it also lists its imported chunks which can be used to render preload directives.
## Advanced

There are some changes which only affects plugin/tool creators.

## For Plugin Authors
- [[#5868] refactor: remove deprecated api for 3.0](https://github.com/vitejs/vite/pull/5868)
- `printHttpServerUrls` is removed
- `server.app`, `server.transformWithEsbuild` are removed
- `import.meta.hot.acceptDeps` is removed
- [[#7995] chore: do not fixStacktrace](https://github.com/vitejs/vite/pull/7995)
- `ssrLoadModule`'s `fixStacktrace` option's default is now `false`
- [[#8178] feat!: migrate to ESM](https://github.com/vitejs/vite/pull/8178)
- `formatPostcssSourceMap` is now async
- `resolvePackageEntry`, `resolvePackageData` are no longer available from CJS build (dynamic import is needed to use in CJS)

Vite 2 uses a completely redesigned plugin interface which extends Rollup plugins. Please read the new [Plugin Development Guide](./api-plugin).
Also there are other breaking changes which only affect few users.

Some general pointers on migrating a v1 plugin to v2:
- [[#5018] feat: enable `generatedCode: 'es2015'` for rollup build](https://github.com/vitejs/vite/pull/5018)
- Transpile to ES5 is now necessary even if the user code only includes ES5.
- [[#7877] fix: vite client types](https://github.com/vitejs/vite/pull/7877)
- `/// <reference lib="dom" />` is removed from `vite/client.d.ts`. `{ "lib": ["dom"] }` or `{ "lib": ["webworker"] }` is necessary in `tsconfig.json`.
- [[#8280] feat: non-blocking esbuild optimization at build time](https://github.com/vitejs/vite/pull/8280)
- `server.force` option was removed in favor of `force` option.

- `resolvers` -> use the [`resolveId`](https://rollupjs.org/guide/en/#resolveid) hook
- `transforms` -> use the [`transform`](https://rollupjs.org/guide/en/#transform) hook
- `indexHtmlTransforms` -> use the [`transformIndexHtml`](./api-plugin#transformindexhtml) hook
- Serving virtual files -> use [`resolveId`](https://rollupjs.org/guide/en/#resolveid) + [`load`](https://rollupjs.org/guide/en/#load) hooks
- Adding `alias`, `define` or other config options -> use the [`config`](./api-plugin#config) hook
## Migration from v1

Since most of the logic should be done via plugin hooks instead of middlewares, the need for middlewares is greatly reduced. The internal server app is now a good old [connect](https://github.com/senchalabs/connect) instance instead of Koa.
Check the [Migration from v1 Guide](https://v2.vitejs.dev/guide/migration.html) in the Vite v2 docs first to see the needed changes to port your app to Vite v2, and then proceed with the changes on this page.
11 changes: 8 additions & 3 deletions docs/guide/static-deploy.md
Expand Up @@ -266,12 +266,17 @@ You can also deploy to a [custom domain](http://surge.sh/help/adding-a-custom-do

# creates a new app with a specified name
$ heroku apps:create example
```

6. Set buildpacks. We use `heroku/nodejs` to build the project and `heroku-buildpack-static` to serve it.

# set buildpack for static sites
$ heroku buildpacks:set https://github.com/heroku/heroku-buildpack-static.git
```bash
# set buildpacks
$ heroku buildpacks:set heroku/nodejs
$ heroku buildpacks:add https://github.com/heroku/heroku-buildpack-static.git
```

6. Deploy your site:
7. Deploy your site:

```bash
# publish site
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Expand Up @@ -14,10 +14,10 @@ hero:
actions:
- theme: brand
text: Get Started
link: /guide/why
link: /guide/
- theme: alt
text: Why Vite?
link: /guide/
link: /guide/why
- theme: alt
text: View on GitHub
link: https://github.com/vitejs/vite
Expand Down
14 changes: 7 additions & 7 deletions package.json
Expand Up @@ -62,21 +62,21 @@
"conventional-changelog-cli": "^2.2.2",
"cross-env": "^7.0.3",
"esbuild": "^0.14.38",
"eslint": "^8.16.0",
"eslint-define-config": "^1.4.1",
"eslint": "^8.17.0",
"eslint-define-config": "^1.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"esno": "^0.16.3",
"execa": "^5.1.1",
"fs-extra": "^10.1.0",
"kill-port": "^1.6.1",
"lint-staged": "^12.4.3",
"lint-staged": "^13.0.0",
"minimist": "^1.2.6",
"node-fetch": "^2.6.7",
"npm-run-all": "^4.1.5",
"picocolors": "^1.0.0",
"playwright-chromium": "^1.22.2",
"pnpm": "^7.1.7",
"pnpm": "^7.1.9",
"prettier": "2.6.2",
"prompts": "^2.4.2",
"rimraf": "^3.0.2",
Expand All @@ -89,8 +89,8 @@
"unbuild": "^0.7.4",
"vite": "workspace:*",
"vitepress": "1.0.0-draft.8",
"vitest": "^0.13.0",
"vue": "^3.2.36"
"vitest": "^0.14.1",
"vue": "^3.2.37"
},
"simple-git-hooks": {
"pre-commit": "pnpm exec lint-staged --concurrent false",
Expand All @@ -110,7 +110,7 @@
"eslint --fix"
]
},
"packageManager": "pnpm@7.1.7",
"packageManager": "pnpm@7.1.9",
"pnpm": {
"overrides": {
"vite": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-vite/template-preact-ts/package.json
Expand Up @@ -9,7 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"preact": "^10.7.2"
"preact": "^10.7.3"
},
"devDependencies": {
"@preact/preset-vite": "^2.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-vite/template-preact/package.json
Expand Up @@ -9,7 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"preact": "^10.7.2"
"preact": "^10.7.3"
},
"devDependencies": {
"@preact/preset-vite": "^2.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-vite/template-react-ts/package.json
Expand Up @@ -13,7 +13,7 @@
"react-dom": "^18.1.0"
},
"devDependencies": {
"@types/react": "^18.0.9",
"@types/react": "^18.0.12",
"@types/react-dom": "^18.0.5",
"@vitejs/plugin-react": "^1.3.2",
"typescript": "^4.6.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-vite/template-react/package.json
Expand Up @@ -13,7 +13,7 @@
"react-dom": "^18.1.0"
},
"devDependencies": {
"@types/react": "^18.0.9",
"@types/react": "^18.0.12",
"@types/react-dom": "^18.0.5",
"@vitejs/plugin-react": "^1.3.2",
"vite": "^2.9.9"
Expand Down
6 changes: 3 additions & 3 deletions packages/create-vite/template-svelte-ts/package.json
Expand Up @@ -10,11 +10,11 @@
"check": "svelte-check --tsconfig ./tsconfig.json"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.46",
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.47",
"@tsconfig/svelte": "^3.0.0",
"svelte": "^3.48.0",
"svelte-check": "^2.7.1",
"svelte-preprocess": "^4.10.6",
"svelte-check": "^2.7.2",
"svelte-preprocess": "^4.10.7",
"tslib": "^2.4.0",
"typescript": "^4.6.4",
"vite": "^2.9.9"
Expand Down
2 changes: 1 addition & 1 deletion packages/create-vite/template-svelte/package.json
Expand Up @@ -9,7 +9,7 @@
"preview": "vite preview"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.46",
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.47",
"svelte": "^3.48.0",
"vite": "^2.9.9"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/create-vite/template-vue-ts/package.json
Expand Up @@ -9,12 +9,12 @@
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.2.36"
"vue": "^3.2.37"
},
"devDependencies": {
"@vitejs/plugin-vue": "^2.3.3",
"typescript": "^4.6.4",
"vite": "^2.9.9",
"vue-tsc": "^0.35.2"
"vue-tsc": "^0.37.0"
}
}

0 comments on commit 92113ec

Please sign in to comment.