# vite ⚡ [![npm][npm-img]][npm-url] [![node][node-img]][node-url] [![unix CI status][unix-ci-img]][unix-ci-url] [![windows CI status][windows-ci-img]][windows-ci-url] Vite is an opinionated web dev build tool that serves your code via native ES Module imports during dev and bundles it with [Rollup](https://rollupjs.org/) for production. - Lightning-fast cold server start - Instant hot module replacement (HMR) - True on-demand compilation - More details in [How and Why](#how-and-why) ## Status In beta and will likely release 1.0 soon. ## Getting Started > Note to Vue users: Vite currently only works with Vue 3.x. This also means you can't use libraries that are not yet compatible with Vue 3. ```bash $ npm init vite-app $ cd $ npm install $ npm run dev ``` If using Yarn: ```bash $ yarn create vite-app $ cd $ yarn $ yarn dev ``` > Although Vite is primarily designed to work with Vue 3, it can support other frameworks as well. For example, try `npm init vite-app --template react` or `--template preact`. ### Using master branch If you can't wait for a new release to test the latest features, clone the `vite` to your local machine and execute the following commands: ``` yarn yarn build yarn link ``` Then go to your vite based project and run `yarn link vite`. Now restart the development server (`yarn dev`) to ride on the bleeding edge! ## Browser Support Vite requires [native ES module imports](https://caniuse.com/#feat=es6-module) during development. The production build also relies on dynamic imports for code-splitting (which can be [polyfilled](https://github.com/GoogleChromeLabs/dynamic-import-polyfill)). Vite assumes you are targeting modern browsers and by default only transpiles your code to `es2019` during build (so that optional chaining can work with terser minification). You can specify the target range via the `esbuildTarget` config option, where the lowest target available is `es2015`. ## Features - [Bare Module Resolving](#bare-module-resolving) - [Hot Module Replacement](#hot-module-replacement) - [TypeScript](#typescript) - [CSS / JSON Importing](#css--json-importing) - [Asset URL Handling](#asset-url-handling) - [PostCSS](#postcss) - [CSS Modules](#css-modules) - [CSS Pre-processors](#css-pre-processors) - [JSX](#jsx) - [Web Assembly](#web-assembly) - [Inline Web Workers](#inline-web-workers) - [Custom Blocks](#custom-blocks) - [Config File](#config-file) - [HTTPS/2](#https2) - [Dev Server Proxy](#dev-server-proxy) - [Production Build](#production-build) - [Modes and Environment Variables](#modes-and-environment-variables) - [Using Vite with Traditional Backend](#using-vite-with-traditional-backend) Vite tries to mirror the default configuration in [vue-cli](http://cli.vuejs.org/) as much as possible. If you've used `vue-cli` or other webpack-based boilerplates before, you should feel right at home. That said, do expect things to be different here and there. ### Bare Module Resolving Native ES imports don't support bare module imports like ```js import { createApp } from 'vue' ``` The above will throw an error by default. Vite detects such bare module imports in all served `.js` files and rewrites them with special paths like `/@modules/vue`. Under these special paths, Vite performs module resolution to locate the correct files from your installed dependencies. Note that `vue` has special treatment - if it isn't installed in the project locally, Vite will fallback to the version from its own dependencies. If you have Vite installed globally, this makes it possible to quickly prototype with Vue without installing anything locally. ### Hot Module Replacement - The `vue`, `react` and `preact` templates of `create-vite-app` all come with HMR out of the box. - For manual HMR, an API is provided via `import.meta.hot`. For a module to self-accept, use `import.meta.hot.accept`: ```js export const count = 1 // the conditional check is required so that HMR related code can be // dropped in production if (import.meta.hot) { import.meta.hot.accept((newModule) => { console.log('updated: count is now ', newModule.count) }) } ``` A module can also accept updates from direct dependencies without reloading itself, using `import.meta.hot.acceptDeps`: ```js import { foo } from './foo.js' foo() if (import.meta.hot) { import.meta.hot.acceptDeps('./foo.js', (newFoo) => { // the callback receives the updated './foo.js' module newFoo.foo() }) // Can also accept an array of dep modules: import.meta.hot.acceptDeps(['./foo.js', './bar.js'], ([newFooModule, newBarModule]) => { // the callback receives the updated modules in an Array }) } ``` A self-accepting module or a module that expects to be accepted by others can use `hot.dispose` to clean-up any persistent side effects created by its updated copy: ```js function setupSideEffect() {} setupSideEffect() if (import.meta.hot) { import.meta.hot.dispose((data) => { // cleanup side effect }) } ``` For the full API, consult [importMeta.d.ts](https://github.com/vitejs/vite/blob/master/importMeta.d.ts). Note that Vite's HMR does not actually swap the originally imported module: if an accepting module re-exports imports from a dep, then it is responsible for updating those re-exports (and these exports must be using `let`). In addition, importers up the chain from the accepting module will not be notified of the change. This simplified HMR implementation is sufficient for most dev use cases, while allowing us to skip the expensive work of generating proxy modules. ### TypeScript Vite supports importing `.ts` files and ` ``` Also make sure the server is configured to serve static assets in the Viter working directory, otherwise assets such as images won't be loaded properly. 3. For production: after running `vite build`, a `manifest.json` file will be generated alongside other asset files. You can use this file to render links with hashed filenames (syntax here for explnatation only, substitute with your server templating language): ```html ``` ## API ### Dev Server You can customize the server using the API. The server can accept plugins which have access to the internal Koa app instance: ```js const { createServer } = require('vite') const myPlugin = ({ root, // project root directory, absolute path app, // Koa app instance server, // raw http server instance watcher // chokidar file watcher instance }) => { app.use(async (ctx, next) => { // You can do pre-processing here - this will be the raw incoming requests // before vite touches it. if (ctx.path.endsWith('.scss')) { // Note vue