Skip to content

Commit

Permalink
Merge branch 'main' into fix-importmap-prepend
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Aug 21, 2022
2 parents 8ef28ee + 31c2926 commit 1ca2623
Show file tree
Hide file tree
Showing 288 changed files with 3,298 additions and 1,571 deletions.
67 changes: 42 additions & 25 deletions .eslintrc.cjs
@@ -1,4 +1,5 @@
// @ts-check
const { builtinModules } = require('node:module')
const { defineConfig } = require('eslint-define-config')

module.exports = defineConfig({
Expand Down Expand Up @@ -42,18 +43,6 @@ module.exports = defineConfig({
tryExtensions: ['.ts', '.js', '.jsx', '.tsx', '.d.ts']
}
],
'node/no-restricted-require': [
'error',
Object.keys(require('./packages/vite/package.json').devDependencies).map(
(d) => ({
name: d,
message:
`devDependencies can only be imported using ESM syntax so ` +
`that they are included in the rollup bundle. If you are trying to ` +
`lazy load a dependency, use (await import('dependency')).default instead.`
})
)
],
'node/no-extraneous-import': [
'error',
{
Expand Down Expand Up @@ -93,6 +82,10 @@ module.exports = defineConfig({
{ prefer: 'type-imports' }
],

'import/no-nodejs-modules': [
'error',
{ allow: builtinModules.map((mod) => `node:${mod}`) }
],
'import/no-duplicates': 'error',
'import/order': 'error',
'sort-imports': [
Expand All @@ -107,6 +100,30 @@ module.exports = defineConfig({
]
},
overrides: [
{
files: ['packages/**'],
excludedFiles: '**/__tests__/**',
rules: {
'no-restricted-globals': ['error', 'require', '__dirname', '__filename']
}
},
{
files: 'packages/vite/**/*.*',
rules: {
'node/no-restricted-require': [
'error',
Object.keys(
require('./packages/vite/package.json').devDependencies
).map((d) => ({
name: d,
message:
`devDependencies can only be imported using ESM syntax so ` +
`that they are included in the rollup bundle. If you are trying to ` +
`lazy load a dependency, use (await import('dependency')).default instead.`
}))
]
}
},
{
files: ['packages/vite/src/node/**'],
rules: {
Expand All @@ -120,9 +137,11 @@ module.exports = defineConfig({
}
},
{
files: ['packages/plugin-*/**/*'],
files: ['packages/create-vite/template-*/**', '**/build.config.ts'],
rules: {
'no-restricted-globals': ['error', 'require', '__dirname', '__filename']
'no-undef': 'off',
'node/no-missing-import': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off'
}
},
{
Expand All @@ -132,7 +151,6 @@ module.exports = defineConfig({
'node/no-extraneous-require': 'off',
'node/no-missing-import': 'off',
'node/no-missing-require': 'off',
'no-undef': 'off',
// engine field doesn't exist in playgrounds
'node/no-unsupported-features/es-builtins': [
'error',
Expand All @@ -145,17 +163,22 @@ module.exports = defineConfig({
{
version: '^14.18.0 || >=16.0.0'
}
]
],
'@typescript-eslint/explicit-module-boundary-types': 'off'
}
},
{
files: ['packages/create-vite/template-*/**', '**/build.config.ts'],
files: ['playground/**'],
excludedFiles: '**/__tests__/**',
rules: {
'node/no-missing-import': 'off'
'no-undef': 'off',
'no-empty': 'off',
'no-constant-condition': 'off',
'@typescript-eslint/no-empty-function': 'off'
}
},
{
files: ['playground/**', '*.js'],
files: ['*.js'],
rules: {
'@typescript-eslint/explicit-module-boundary-types': 'off'
}
Expand All @@ -165,12 +188,6 @@ module.exports = defineConfig({
rules: {
'@typescript-eslint/triple-slash-reference': 'off'
}
},
{
files: 'packages/vite/**/*.*',
rules: {
'no-restricted-globals': ['error', 'require', '__dirname', '__filename']
}
}
],
reportUnusedDisableDirectives: true
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -6,6 +6,8 @@ env:
NODE_OPTIONS: --max-old-space-size=6144
# install playwright binary manually (because pnpm only runs install script once)
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: "1"
# Vitest auto retry on flaky segfault
VITEST_SEGFAULT_RETRY: 3

on:
push:
Expand Down
7 changes: 6 additions & 1 deletion docs/.vitepress/config.ts
Expand Up @@ -65,7 +65,8 @@ export default defineConfig({
['meta', { property: 'og:url', content: ogUrl }],
['meta', { property: 'og:description', content: ogDescription }],
['meta', { name: 'twitter:card', content: 'summary_large_image' }],
['meta', { name: 'twitter:site', content: '@vite_js' }]
['meta', { name: 'twitter:site', content: '@vite_js' }],
['meta', { name: 'theme-color', content: '#646cff' }]
],

vue: {
Expand Down Expand Up @@ -211,6 +212,10 @@ export default defineConfig({
text: 'Comparisons',
link: '/guide/comparisons'
},
{
text: 'Troubleshooting',
link: '/guide/troubleshooting'
},
{
text: 'Migration from v2',
link: '/guide/migration'
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/team.js
Expand Up @@ -5,7 +5,7 @@ export const core = [
title: 'Creator',
org: 'Vue.js',
orgLink: 'https://vuejs.org/',
desc: 'Husband, father of two, independent open source developer.',
desc: 'Independent open source developer, creator of Vue.js and Vite.',
links: [
{ icon: 'github', link: 'https://github.com/yyx990803' },
{ icon: 'twitter', link: 'https://twitter.com/youyuxi' }
Expand Down
7 changes: 7 additions & 0 deletions docs/config/build-options.md
Expand Up @@ -193,3 +193,10 @@ Limit for chunk size warnings (in kbs).
- **Default:** `null`

Set to `{}` to enable rollup watcher. This is mostly used in cases that involve build-only plugins or integrations processes.

::: warning Using Vite on Windows Subsystem for Linux (WSL) 2

There are cases that file system watching does not work with WSL2.
See [`server.watch`](./server-options.md#server-watch) for more details.

:::
4 changes: 2 additions & 2 deletions docs/config/preview-options.md
Expand Up @@ -65,7 +65,7 @@ Automatically open the app in the browser on server start. When the value is a s
- **Type:** `Record<string, string | ProxyOptions>`
- **Default:** [`server.proxy`](./server-options#server-proxy)

Configure custom proxy rules for the dev server. Expects an object of `{ key: options }` pairs. If the key starts with `^`, it will be interpreted as a `RegExp`. The `configure` option can be used to access the proxy instance.
Configure custom proxy rules for the preview server. Expects an object of `{ key: options }` pairs. If the key starts with `^`, it will be interpreted as a `RegExp`. The `configure` option can be used to access the proxy instance.

Uses [`http-proxy`](https://github.com/http-party/node-http-proxy). Full options [here](https://github.com/http-party/node-http-proxy#options).

Expand All @@ -74,4 +74,4 @@ Uses [`http-proxy`](https://github.com/http-party/node-http-proxy). Full options
- **Type:** `boolean | CorsOptions`
- **Default:** [`server.cors`](./server-options#server-cors)

Configure CORS for the dev server. This is enabled by default and allows any origin. Pass an [options object](https://github.com/expressjs/cors) to fine tune the behavior or `false` to disable.
Configure CORS for the preview server. This is enabled by default and allows any origin. Pass an [options object](https://github.com/expressjs/cors) to fine tune the behavior or `false` to disable.
17 changes: 15 additions & 2 deletions docs/config/server-options.md
Expand Up @@ -145,6 +145,8 @@ Set `server.hmr.overlay` to `false` to disable the server error overlay.

When `server.hmr.server` is defined, Vite will process the HMR connection requests through the provided server. If not in middleware mode, Vite will attempt to process HMR connection requests through the existing server. This can be helpful when using self-signed certificates or when you want to expose Vite over a network on a single port.

Check out [`vite-setup-catalogue`](https://github.com/sapphi-red/vite-setup-catalogue) for some examples.

::: tip NOTE

With the default configuration, reverse proxies in front of Vite are expected to support proxying WebSocket. If the Vite HMR client fails to connect WebSocket, the client will fallback to connecting the WebSocket directly to the Vite HMR server bypassing the reverse proxies:
Expand All @@ -167,8 +169,6 @@ The error that appears in the Browser when the fallback happens can be ignored.

File system watcher options to pass on to [chokidar](https://github.com/paulmillr/chokidar#api).

When running Vite on Windows Subsystem for Linux (WSL) 2, if the project folder resides in a Windows filesystem, you'll need to set this option to `{ usePolling: true }`. This is due to [a WSL2 limitation](https://github.com/microsoft/WSL/issues/4739) with the Windows filesystem.

The Vite server watcher skips `.git/` and `node_modules/` directories by default. If you want to watch a package inside `node_modules/`, you can pass a negated glob pattern to `server.watch.ignored`. That is:

```js
Expand All @@ -186,6 +186,19 @@ export default defineConfig({
})
```

::: warning Using Vite on Windows Subsystem for Linux (WSL) 2

When running Vite on WSL2, file system watching does not work when a file is edited by Windows applications (non-WSL2 process). This is due to [a WSL2 limitation](https://github.com/microsoft/WSL/issues/4739). This also applies to running on Docker with a WSL2 backend.

To fix it, you could either:

- **Recommended**: Use WSL2 applications to edit your files.
- It is also recommended to move the project folder outside of a Windows filesystem. Accessing Windows filesystem from WSL2 is slow. Removing that overhead will improve performance.
- Set `{ usePolling: true }`.
- Note that [`usePolling` leads to high CPU utilization](https://github.com/paulmillr/chokidar#performance).

:::

## server.middlewareMode

- **Type:** `boolean`
Expand Down
12 changes: 6 additions & 6 deletions docs/guide/build.md
Expand Up @@ -208,9 +208,9 @@ A user may choose to deploy in three different paths:

A single static [base](#public-base-path) isn't enough in these scenarios. Vite provides experimental support for advanced base options during build, using `experimental.renderBuiltUrl`.

```js
```ts
experimental: {
renderBuiltUrl: (filename: string, { hostType: 'js' | 'css' | 'html' }) => {
renderBuiltUrl(filename: string, { hostType }: { hostType: 'js' | 'css' | 'html' }) {
if (hostType === 'js') {
return { runtime: `window.__toCdnUrl(${JSON.stringify(filename)})` }
} else {
Expand All @@ -220,15 +220,15 @@ experimental: {
}
```

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.
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 second `context` param given to the function.

```js
```ts
experimental: {
renderBuiltUrl(filename: string, { hostType: 'js' | 'css' | 'html', type: 'public' | 'asset' }) {
renderBuiltUrl(filename: string, { hostId, hostType, type }: { hostId: string, hostType: 'js' | 'css' | 'html', type: 'public' | 'asset' }) {
if (type === 'public') {
return 'https://www.domain.com/' + filename
}
else if (path.extname(importer) === '.js') {
else if (path.extname(hostId) === '.js') {
return { runtime: `window.__assetsPath(${JSON.stringify(filename)})` }
}
else {
Expand Down
10 changes: 5 additions & 5 deletions docs/guide/env-and-mode.md
Expand Up @@ -46,7 +46,7 @@ In addition, environment variables that already exist when Vite is executed have

Loaded env variables are also exposed to your client source code via `import.meta.env` as strings.

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:
To prevent accidentally leaking env variables to the client, only variables prefixed with `VITE_` are exposed to your Vite-processed code. e.g. for the following env variables:

```
VITE_SOME_KEY=123
Expand All @@ -60,7 +60,7 @@ console.log(import.meta.env.VITE_SOME_KEY) // 123
console.log(import.meta.env.DB_PASSWORD) // undefined
```
If you want to customize env variables prefix, see [envPrefix](/config/shared-options.html#envprefix) option.
If you want to customize the env variables prefix, see the [envPrefix](/config/shared-options.html#envprefix) option.
:::warning SECURITY NOTES
Expand All @@ -71,9 +71,9 @@ If you want to customize env variables prefix, see [envPrefix](/config/shared-op
### IntelliSense for TypeScript
By default, Vite provides type definition for `import.meta.env` in [`vite/client.d.ts`](https://github.com/vitejs/vite/blob/main/packages/vite/client.d.ts). While you can define more custom env variables in `.env.[mode]` files, you may want to get TypeScript IntelliSense for user-defined env variables which prefixed with `VITE_`.
By default, Vite provides type definitions for `import.meta.env` in [`vite/client.d.ts`](https://github.com/vitejs/vite/blob/main/packages/vite/client.d.ts). While you can define more custom env variables in `.env.[mode]` files, you may want to get TypeScript IntelliSense for user-defined env variables that are prefixed with `VITE_`.
To achieve, you can create an `env.d.ts` in `src` directory, then augment `ImportMetaEnv` like this:
To achieve this, you can create an `env.d.ts` in `src` directory, then augment `ImportMetaEnv` like this:
```typescript
/// <reference types="vite/client" />
Expand All @@ -98,7 +98,7 @@ If your code relies on types from browser environments such as [DOM](https://git
## Modes
By default, the dev server (`dev` command) runs in `development` mode and the `build` command run in `production` mode.
By default, the dev server (`dev` command) runs in `development` mode and the `build` command runs in `production` mode.
This means when running `vite build`, it will load the env variables from `.env.production` if there is one:
Expand Down
18 changes: 16 additions & 2 deletions docs/guide/features.md
Expand Up @@ -102,14 +102,28 @@ This will provide the following type shims:
- Types for the Vite-injected [env variables](./env-and-mode#env-variables) on `import.meta.env`
- Types for the [HMR API](./api-hmr) on `import.meta.hot`

::: tip
To override the default typing, declare it before the triple-slash reference. For example, to make the default import of `*.svg` a React component:

```ts
declare module '*.svg' {
const content: React.FC<React.SVGProps<SVGElement>>
export default content
}

/// <reference types="vite/client" />
```

:::

## Vue

Vite provides first-class Vue support:

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

## JSX

Expand Down
6 changes: 4 additions & 2 deletions docs/guide/ssr.md
Expand Up @@ -211,7 +211,9 @@ If the routes and the data needed for certain routes are known ahead of time, we
Dependencies are "externalized" from Vite's SSR transform module system by default when running SSR. This speeds up both dev and build.
If a dependency needs to be transformed by Vite's pipeline, for example, because Vite features are used untranspiled in them, they can be added to [`ssr.noExternal`](../config/ssr-options.md#ssrnoexternal).
If a dependency needs to be transformed by Vite's pipeline, for example, because Vite features are used untranspiled in them, they can be added to [`ssr.noExternal`](../config/ssr-options.md#ssr-noexternal).
For linked dependencies, they are not externalized by default to take advantage of Vite's HMR. If this isn't desired, for example, to test dependencies as if they aren't linked, you can add it to [`ssr.external`](../config/ssr-options.md#ssr-external).
:::warning Working with Aliases
If you have configured aliases that redirects one package to another, you may want to alias the actual `node_modules` packages instead to make it work for SSR externalized dependencies. Both [Yarn](https://classic.yarnpkg.com/en/docs/cli/add/#toc-yarn-add-alias) and [pnpm](https://pnpm.js.org/en/aliases) support aliasing via the `npm:` prefix.
Expand Down Expand Up @@ -267,4 +269,4 @@ Use a post hook so that your SSR middleware runs _after_ Vite's middlewares.
## SSR Format
By default, Vite generates the SSR bundle in ESM. There is experimental support for configuring `ssr.format`, but it isn't recommended. Future efforts around SSR development will be based on ESM, and commonjs remain available for backward compatibility. If using ESM for SSR isn't possible in your project, you can set `legacy.buildSsrCjsExternalHeuristics: true` to generate a CJS bundle using the same [externalization heuristics of Vite v2](https://v2.vitejs.dev/guide/ssr.html#ssr-externals).
By default, Vite generates the SSR bundle in ESM. There is experimental support for configuring `ssr.format`, but it isn't recommended. Future efforts around SSR development will be based on ESM, and CommonJS remain available for backward compatibility. If using ESM for SSR isn't possible in your project, you can set `legacy.buildSsrCjsExternalHeuristics: true` to generate a CJS bundle using the same [externalization heuristics of Vite v2](https://v2.vitejs.dev/guide/ssr.html#ssr-externals).

0 comments on commit 1ca2623

Please sign in to comment.