Skip to content

Commit

Permalink
chore: Merge branch 'main' into fix/dynamic-import-inline
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed May 9, 2022
2 parents 7f72102 + 1878f46 commit 971c8a5
Show file tree
Hide file tree
Showing 244 changed files with 3,986 additions and 2,000 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports = defineConfig({
'node/no-extraneous-import': [
'error',
{
allowModules: ['vite', 'less', 'sass']
allowModules: ['vite', 'less', 'sass', 'vitest']
}
],
'node/no-extraneous-require': [
Expand Down Expand Up @@ -103,7 +103,7 @@ module.exports = defineConfig({
}
},
{
files: ['packages/vite/types/**'],
files: ['packages/vite/types/**', '*.spec.ts'],
rules: {
'node/no-extraneous-import': 'off'
}
Expand Down
16 changes: 11 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ on:
- feat/*
- fix/*
- perf/*
- v1
- v2
pull_request:
workflow_dispatch:

Expand All @@ -27,12 +29,17 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node_version: [12, 14, 16, 17]
node_version: [14, 16, 18]
include:
- os: macos-latest
node_version: 16
- os: macos-latest
node_version: 18
- os: windows-latest
node_version: 16
# Maybe bug with jest on windows and node-v18
# - os: windows-latest
# node_version: 18
fail-fast: false

name: "Build&Test: node-${{ matrix.node_version }}, ${{ matrix.os }}"
Expand All @@ -42,8 +49,6 @@ jobs:

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 6

- name: Set node version to ${{ matrix.node_version }}
uses: actions/setup-node@v3
Expand All @@ -63,6 +68,9 @@ jobs:
- name: Build plugin-react
run: pnpm run build-plugin-react

- name: Test unit
run: pnpm run test-unit

- name: Test serve
run: pnpm run test-serve -- --runInBand

Expand All @@ -80,8 +88,6 @@ jobs:

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 6

- name: Set node version to 16
uses: actions/setup-node@v3
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/semantic-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Semantic Pull Request

on:
pull_request_target:
types:
- opened
- edited
- synchronize

jobs:
main:
runs-on: ubuntu-latest
name: Semantic Pull Request
steps:
- name: Validate PR title
uses: amannn/action-semantic-pull-request@v4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20 changes: 16 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Some errors are masked and hidden away because of the layers of abstraction and

1. In the sources panel in the right column, click the play button to resume execution and allow the tests to run which will open a Chromium instance.

1. Focusing the Chomium instance, you can open the browser devtools and inspect the console there to find the underlying problems.
1. Focusing the Chromium instance, you can open the browser devtools and inspect the console there to find the underlying problems.

1. To close everything, just stop the test process back in your terminal.

Expand All @@ -67,13 +67,15 @@ And re-run `pnpm install` to link the package.

## Running Tests

### Integration Tests

Each package under `packages/playground/` contains a `__tests__` directory. The tests are run using [Jest](https://jestjs.io/) + [Playwright](https://playwright.dev/) with custom integrations to make writing tests simple. The detailed setup is inside `jest.config.js` and `scripts/jest*` files.

Before running the tests, make sure that [Vite has been built](#repo-setup). On Windows, you may want to [activate Developer Mode](https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development) to solve [issues with symlink creation for non-admins](https://github.com/vitejs/vite/issues/7390).
Before running the tests, make sure that [Vite has been built](#repo-setup). On Windows, you may want to [activate Developer Mode](https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development) to solve [issues with symlink creation for non-admins](https://github.com/vitejs/vite/issues/7390). Also you may want to [set git `core.symlinks` to `true` to solve issues with symlinks in git](https://github.com/vitejs/vite/issues/5242).

Each test can be run under either dev server mode or build mode.
Each integration test can be run under either dev server mode or build mode.

- `pnpm test` by default runs every test in both serve and build mode.
- `pnpm test` by default runs every integration test in both serve and build mode, and also unit tests.

- `pnpm run test-serve` runs tests only under serve mode. This is just calling `jest` so you can pass any Jest flags to this command. Since Jest will attempt to run tests in parallel, if your machine has many cores this may cause flaky test failures with multiple Playwright instances running at the same time. You can force the tests to run in series with `pnpm run test-serve -- --runInBand`.

Expand All @@ -83,6 +85,14 @@ Each test can be run under either dev server mode or build mode.

Note package matching is not available for the `pnpm test` script, which always runs all tests.

### Unit Tests

Other than tests under `packages/playground/` for integration tests, packages might contains unit tests under their `__tests__` directory. Unit tests are powered by [Vitest](https://vitest.dev/). The detailed config is inside `vitest.config.ts` files.

- `pnpm run test-unit` runs unit tests under each package.

- You can also use `pnpm run test-unit -- [match]` to run related tests.

### Test Env and Helpers

Inside playground tests, a global `page` object is automatically available, which is a Playwright [`Page`](https://playwright.dev/docs/api/class-page) instance that has already navigated to the served page of the current playground. So writing a test is as simple as:
Expand All @@ -95,6 +105,8 @@ test('should work', async () => {

Some common test helpers, e.g. `testDir`, `isBuild` or `editFile` are available in `packages/playground/testUtils.ts`.

Note: The test build environment uses a [different default set of Vite config](https://github.com/vitejs/vite/blob/9c6501d9c363eaa3c1e7708d531fb2a92b633db6/scripts/jestPerTestSetup.ts#L102-L122) to skip transpilation during tests to make it faster. This may produce a different result compared to the default production build.

### Extending the Test Suite

To add new tests, you should find a related playground to the fix or feature (or create a new one). As an example, static assets loading are tested in the [assets playground](https://github.com/vitejs/vite/tree/main/packages/playground/assets). In this Vite App, there is a test for `?raw` imports, with [a section is defined in the `index.html` for it](https://github.com/vitejs/vite/blob/71215533ac60e8ff566dc3467feabfc2c71a01e2/packages/playground/assets/index.html#L121):
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ In addition, Vite is highly extensible via its [Plugin API](https://vitejs.dev/g

## Packages

> This branch is for the upcoming v3.0, if you are looking for current stable releases, check the [`v2` branch](https://github.com/vitejs/vite/tree/v2) instead.
| Package | Version (click for changelogs) |
| ------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------- |
| [vite](packages/vite) | [![vite version](https://img.shields.io/npm/v/vite.svg?label=%20)](packages/vite/CHANGELOG.md) |
Expand Down
18 changes: 12 additions & 6 deletions docs/.vitepress/config.js → docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// @ts-check
import { defineConfig } from 'vitepress'

/**
* @type {import('vitepress').UserConfig}
*/
module.exports = {
export default defineConfig({
title: 'Vite',
description: 'Next Generation Frontend Tooling',
head: [['link', { rel: 'icon', type: 'image/svg+xml', href: '/logo.svg' }]],
Expand Down Expand Up @@ -64,6 +61,15 @@ module.exports = {
}
]
},
{
text: 'v3 (next)',
items: [
{
text: 'v2.x (stable)',
link: 'https://v2.vitejs.dev'
}
]
},
{
text: 'Languages',
items: [
Expand Down Expand Up @@ -169,4 +175,4 @@ module.exports = {
]
}
}
}
})
27 changes: 17 additions & 10 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ vite --config my-config.js
```

::: tip NOTE
Vite will replace `__filename`, `__dirname`, and `import.meta.url` in **CommonJS** and **TypeScript** config files. Using these as variable names will result in an error:
Vite will replace `__filename`, `__dirname`, and `import.meta.url` in config files and its deps. Using these as variable names will result in an error:

```js
const __filename = "value"
Expand Down Expand Up @@ -94,7 +94,7 @@ If the config needs to call async function, it can export a async function inste
export default defineConfig(async ({ command, mode }) => {
const data = await asyncFunction()
return {
// build specific config
// vite config
}
})
```
Expand All @@ -109,10 +109,14 @@ Note that Vite doesn't load `.env` files by default as the files to load can onl
import { defineConfig, loadEnv } from 'vite'

export default defineConfig(({ command, mode }) => {
// Load env file based on `mode` in the current working directory
const env = loadEnv(mode, process.cwd())
// Load env file based on `mode` in the current working directory.
// Set the third parameter to '' to load all env regardless of the `VITE_` prefix.
const env = loadEnv(mode, process.cwd(), '')
return {
// build specific config
// vite config
define: {
__APP_ENV__: env.APP_ENV
}
}
})
```
Expand Down Expand Up @@ -160,7 +164,7 @@ export default defineConfig(({ command, mode }) => {

- To be consistent with [esbuild behavior](https://esbuild.github.io/api/#define), expressions must either be a JSON object (null, boolean, number, string, array, or object) or a single identifier.

- Replacements are performed only when the match is surrounded by word boundaries (`\b`).
- Replacements are performed only when the match isn't surrounded by other letters, numbers, `_` or `$`.

::: warning
Because it's implemented as straightforward text replacements without any syntax analysis, we recommend using `define` for CONSTANTS only.
Expand Down Expand Up @@ -237,7 +241,7 @@ export default defineConfig(({ command, mode }) => {
{
"exports": {
".": {
"import": "./index.esm.js",
"import": "./index.esm.mjs",
"require": "./index.cjs.js"
}
}
Expand Down Expand Up @@ -306,7 +310,11 @@ export default defineConfig(({ command, mode }) => {

- **Type:** `string | (postcss.ProcessOptions & { plugins?: postcss.Plugin[] })`

Inline PostCSS config (expects the same format as `postcss.config.js`), or a custom directory to search PostCSS config from (default is project root). The search is done using [postcss-load-config](https://github.com/postcss/postcss-load-config) and only the supported config file names are loaded.
Inline PostCSS config or a custom directory to search PostCSS config from (default is project root).

For inline PostCSS config, it expects the same format as `postcss.config.js`. But for `plugins` property, only [array format](https://github.com/postcss/postcss-load-config/blob/main/README.md#array) can be used.

The search is done using [postcss-load-config](https://github.com/postcss/postcss-load-config) and only the supported config file names are loaded.

Note if an inline config is provided, Vite will not search for other PostCSS config sources.

Expand Down Expand Up @@ -702,7 +710,7 @@ Defines the origin of the generated asset URLs during development.
```js
export default defineConfig({
server: {
origin: 'http://127.0.0.1:8080/'
origin: 'http://127.0.0.1:8080'
}
})
```
Expand Down Expand Up @@ -1014,7 +1022,6 @@ export default defineConfig({

- `external` is also omitted, use Vite's `optimizeDeps.exclude` option
- `plugins` are merged with Vite's dep plugin
- `keepNames` takes precedence over the deprecated `optimizeDeps.keepNames`

## SSR Options

Expand Down
30 changes: 18 additions & 12 deletions docs/guide/api-hmr.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,27 @@ Vite exposes its manual HMR API via the special `import.meta.hot` object:

```ts
interface ImportMeta {
readonly hot?: {
readonly data: any
readonly hot?: ViteHotContext
}

interface ViteHotContext {
readonly data: any

accept(): void
accept(cb: (mod: any) => void): void
accept(dep: string, cb: (mod: any) => void): void
accept(deps: string[], cb: (mods: any[]) => void): void
accept(): void
accept(cb: (mod: any) => void): void
accept(dep: string, cb: (mod: any) => void): void
accept(deps: readonly string[], cb: (mods: any[]) => void): void

prune(cb: () => void): void
dispose(cb: (data: any) => void): void
decline(): void
invalidate(): void
dispose(cb: (data: any) => void): void
decline(): void
invalidate(): void

on(event: string, cb: (...args: any[]) => void): void
}
// `InferCustomEventPayload` provides types for built-in Vite events
on<T extends string>(
event: T,
cb: (payload: InferCustomEventPayload<T>) => void
): void
send<T extends string>(event: T, data?: InferCustomEventPayload<T>): void
}
```

Expand Down
22 changes: 22 additions & 0 deletions docs/guide/api-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,28 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo

Note `configureServer` is not called when running the production build so your other hooks need to guard against its absence.

### `configurePreviewServer`

- **Type:** `(server: { middlewares: Connect.Server, httpServer: http.Server }) => (() => void) | void | Promise<(() => void) | void>`
- **Kind:** `async`, `sequential`

Same as [`configureServer`](/guide/api-plugin.html#configureserver) but for the preview server. It provides the [connect](https://github.com/senchalabs/connect) server and its underlying [http server](https://nodejs.org/api/http.html). Similarly to `configureServer`, the `configurePreviewServer` hook is called before other middlewares are installed. If you want to inject a middleware **after** other middlewares, you can return a function from `configurePreviewServer`, which will be called after internal middlewares are installed:

```js
const myPlugin = () => ({
name: 'configure-preview-server',
configurePreviewServer(server) {
// return a post hook that is called after other middlewares are
// installed
return () => {
server.middlewares.use((req, res, next) => {
// custom handle request...
})
}
}
})
```

### `transformIndexHtml`

- **Type:** `IndexHtmlTransformHook | { enforce?: 'pre' | 'post', transform: IndexHtmlTransformHook }`
Expand Down
9 changes: 5 additions & 4 deletions docs/guide/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ module.exports = defineConfig({
lib: {
entry: path.resolve(__dirname, 'lib/main.js'),
name: 'MyLib',
fileName: (format) => `my-lib.${format}.js`
// the proper extensions will be added
fileName: 'my-lib'
},
rollupOptions: {
// make sure to externalize deps that shouldn't be bundled
Expand Down Expand Up @@ -159,7 +160,7 @@ Running `vite build` with this config uses a Rollup preset that is oriented towa
```
$ vite build
building for production...
[write] my-lib.es.js 0.08kb, brotli: 0.07kb
[write] my-lib.es.mjs 0.08kb, brotli: 0.07kb
[write] my-lib.umd.js 0.30kb, brotli: 0.16kb
```

Expand All @@ -170,10 +171,10 @@ Recommended `package.json` for your lib:
"name": "my-lib",
"files": ["dist"],
"main": "./dist/my-lib.umd.js",
"module": "./dist/my-lib.es.js",
"module": "./dist/my-lib.es.mjs",
"exports": {
".": {
"import": "./dist/my-lib.es.js",
"import": "./dist/my-lib.es.mjs",
"require": "./dist/my-lib.umd.js"
}
}
Expand Down
8 changes: 8 additions & 0 deletions docs/guide/env-and-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ interface ImportMeta {
}
```

If your code relies on types from browser environments such as [DOM](https://github.com/microsoft/TypeScript/blob/main/lib/lib.dom.d.ts) and [WebWorker](https://github.com/microsoft/TypeScript/blob/main/lib/lib.webworker.d.ts), you can update the [lib](https://www.typescriptlang.org/tsconfig#lib) field in `tsconfig.json`.

```json
{
"lib": ["WebWorker"]
}
```

## Modes

By default, the dev server (`dev` command) runs in `development` mode and the `build` command run in `production` mode.
Expand Down
Loading

0 comments on commit 971c8a5

Please sign in to comment.