Hi! We're really excited that you're interested in contributing to Vite! Before submitting your contribution, please read through the following guide.
This repo is a monorepo using pnpm workspaces. The package manager used to install and link dependencies must be pnpm.
Vite uses pnpm v7. If you are working on multiple projects with different versions of pnpm, it's recommended to enable Corepack by running
corepack enable
.
-
Checkout a topic branch from a base branch (e.g.
main
), and merge back against that branch. -
If adding a new feature:
- Add accompanying test case.
- Provide a convincing reason to add this feature. Ideally, you should open a suggestion issue first, and have it approved before working on it.
-
If fixing a bug:
- If you are resolving a special issue, add
(fix #xxxx[,#xxxx])
(#xxxx is the issue id) in your PR title for a better release log (e.g.fix: update entities encoding/decoding (fix #3899)
). - Provide a detailed description of the bug in the PR. Live demo preferred.
- Add appropriate test coverage if applicable.
- If you are resolving a special issue, add
-
It's OK to have multiple small commits as you work on the PR. GitHub can automatically squash them before merging.
-
Make sure tests pass!
-
No need to worry about code style as long as you have installed the dev dependencies. Modified files are automatically formatted with Prettier on commit (by invoking Git Hooks via simple-git-hooks).
-
PR title must follow the commit message convention so that changelogs can be automatically generated.
Each package under playground/
contains a __tests__
directory. The tests are run using Vitest + Playwright with custom integrations to make writing tests simple. The detailed setup is inside vitest.config.e2e.ts
and playground/vitest*
files.
Before running the tests, make sure that you built the plugin. On Windows, you may want to activate Developer Mode to resolve issues with symlink creation for non-admins. Also, you may want to set git core.symlinks
to true
to resolve issues with symlinks in git.
Each integration test can be run under either dev server mode or build mode.
-
pnpm test
by default runs every integration test in both serve and build mode. -
pnpm run test-serve
runs tests only under serve mode. -
pnpm run test-build
runs tests only under build mode. -
pnpm run test-serve [match]
orpnpm run test-build [match]
runs tests in specific packages that match the given filter.Note package matching is not available for the
pnpm test
script, which always runs all tests.
Inside playground tests, you can import the page
object from ~utils
, which is a Playwright Page
instance that has already navigated to the served page of the current playground. So, writing a test is as simple as:
import { page } from '~utils'
test('should work', async () => {
expect(await page.textContent('.foo')).toMatch('foo')
})
Some common test helpers (e.g. testDir
, isBuild
, or editFile
) are also available in the utils. Source code is located at playground/test-utils.ts
.
Note: The test build environment uses a different default set of Vite config to skip transpilation during tests to make it faster. This may produce a different result compared to the default production build.
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 is tested in the assets playground. In this Vite app, there is a test for ?raw
imports with a section defined in the index.html
for it:
<h2>?raw import</h2>
<code class="raw"></code>
This will be modified with the result of a file import:
import rawSvg from './nested/fragment.svg?raw'
text('.raw', rawSvg)
...where the text
util is defined as:
function text(el, text) {
document.querySelector(el).textContent = text
}
In the spec tests, the modifications to the DOM listed above are used to test this feature:
test('?raw import', async () => {
expect(await page.textContent('.raw')).toMatch('SVG')
})
To use breakpoints and explore code execution, you can use the "Run and Debug" feature from VS Code.
-
Add a
debugger
statement where you want to stop the code execution. -
Click the "Run and Debug" icon in the activity bar of the editor, which opens the Run and Debug view.
-
Click the "JavaScript Debug Terminal" button in the Run and Debug view, which opens a terminal in VS Code.
-
From that terminal, go to
playground/xxx
, and runpnpm run dev
. -
The execution will stop at the
debugger
statement, and you can use the Debug toolbar to continue, step over, and restart the process...
Some errors are masked and hidden away because of the layers of abstraction and sandboxed nature added by Vitest, Playwright, and Chromium. In order to see what's actually going wrong and the contents of the devtools console in those instances, follow this setup:
-
Add a
debugger
statement to theplayground/vitestSetup.ts
->afterAll
hook. This will pause execution before the tests quit and the Playwright browser instance exits. You can also adddebugger
statements in your tests (.spec.ts
) files. -
Run the tests with the
debug-serve
script command, which will enable remote debugging:pnpm run debug-serve react-sourcemap
. Remember to run this command within the "JavaScript Debug Terminal" of VS Code. -
Wait for inspector devtools to open in your browser and the debugger to attach.
-
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.
-
Focusing the Chromium instance, you can open the browser devtools and inspect the console there to find the underlying problems.
-
To close everything, just stop the test process back in your terminal.
In many test cases, we need to mock dependencies using link:
and file:
protocols. pnpm
treats link:
as symlinks and file:
as hardlinks. To test dependencies as if they were copied into node_modules
, use the file:
protocol. Otherwise, use the link:
protocol.
For a mock dependency, make sure you add a @vitejs/test-
prefix to the package name. This will avoid possible issues like false-positive alerts.