Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vue-cli-plugin-vite

IndexXuan799MIT1.5.0

out-of-box vite dev for vue-cli project

vite-plguin, vue-cli-plugin, vite, vue-cli

readme

Use Vite Today

out-of-the-box for vue-cli projects without any codebase modifications.

demo/boilerplate

logo

wakatime npm version downloads download monthly
CI Maintenance License: MIT

Table of Contents

Usage

# 1. first step
vue add vite

# 2. second step
# NOTE you cannot directly use `vite` or `npx vite` since it is origin vite not this plugin.
yarn vite // or npm run vite

# 3. add optimizeDeps#include (optional and will speedup devServer start time a lot)
# added in vue.config.js#pluginOptions.vite.optimizeDeps.include
# e.g.: ['vue', 'vue-router', 'vuex']
# all scanned deps(logged in terminal) can be added for speedup.

Motivation

  • We have lots of exists vue-cli(3.x / 4.x) projects.
  • In Production: vue-cli based on webpack is still the best practice for bundling webapp(with code spliting, legecy-build for old browsers).
  • In Development: instant server start and lightning fast HMR by vite is interesting.
  • Why not use them together ?

Options

// vue.config.js
{
  // ...
  pluginOptions: {
    vite: {
      /**
       * Plugin[]
       * @default []
       */
      plugins: [], // other vite plugins list, will be merge into this plugin\'s underlying vite.config.ts
      /**
       * Vite UserConfig.optimizeDeps options
       * recommended set `include` for speedup page-loaded time, e.g. include: ['vue', 'vue-router', '@scope/xxx']
       * @default {}
       */
      optimizeDeps: {},
      /**
       * type-checker, recommended disabled for large-scale old project.
       * @default false
       */
      disabledTypeChecker: true,
      /**
       * lint code by eslint
       * @default false
       */
      disabledLint: false,
    }
  },
}

Underlying principle

Compatibility

  • NO EXTRA files, code and dependencies injected
    • injected one devDependency vue-cli-plugin-vite
    • injected one line code in package.json#scripts#vite and one file at bin/vite
  • auto-resolved as much options as we can from vue.config.js (publicPath, alias, outputDir...)
  • auto reuse public/index.html as vite html entry template
  • compatible the differences between vue-cli and vite(environment variables, special syntax...)

Differences between vue-cli and vite

Dimension vue-cli vite
Plugin 1. based on webpack.
2. have service and generator lifecycles.
3. hooks based on each webpack plugin hooks
1. based on rollup.
2. no generator lifecycle.
3. universal hooks based on rollup plugin hooks and vite self designed
Environment Variables 1. loaded on process.env.
2. prefixed by VUE_APP_.
3. client-side use process.env.VUE_APP_XXX by webpack definePlugin
1. not loaded on process.env.
2. prefixed by VITE_.
3. client-side use import.meta.env.VITE_XXX by vite inner define plugin
Entry Files 1. main.{js,ts}. 1. *.html
Config File 1. vue.config.js 1. vite.config.ts.
2. support use --config to locate
MPA Support 1. native support by options.pages.
2. with history rewrite support
1. native support by rollupOptions.input
Special Syntax 1. require(by webpack)
2. require.context(by webpack)
2. use ~some-module/dist/index.css(by css-loader)
3. module.hot for HMR
1. import.meta.glob/globEager
2. native support by vite, use module/dist/index.css directly
3. import.meta.hot for HMR
Local devServer 1. webpack dev-server
2. express-style middleware and many extension api.
1. connect
2. connect middleware
Type Checker 1. fork-ts-checker-webpack-plugin 1. No built-in, we can use vite-plugin-checker(based on vetur and vue-tsc)
Lint 1. @vue/cli-plugin-eslint 1. No built-in we can use vite-plugin-eslint,
Jest 1. @vue/cli-plugin-jest 1. will have first-class jest support

Milestones

  • Done ✅ vs WIP ⬜️ vs ❌ Won't support
  • ✅ Plugin
    • ✅ we can do nothing but rewrite corresponding vite-plugin, most code and tools can be reused
  • ✅ Environment Variables Compatibility
    • ✅ load to process.env.XXX (all env with or without prefix will be loaded)
    • ✅ recognize VUE_APP_ prefix
    • ✅ define as process.env.${PREFIX}_XXX for client-side
  • ✅ Entry Files (we can do nothing)
  • ✅ Config File (vue.config.js Options auto-resolved)
    • ✅ vite#base - resolved from process.env.PUBLIC_URL || vue.config.js#publicPath || baseUrl
    • ✅ vite#css - resolved from vue.config.js#css
      • ✅ preprocessorOptions: css.loaderOptions
    • ✅ vite#server- resolved from vue.config.js#devServer
      • ✅ host - resolved from process.env.DEV_HOST || devServer.public
      • ✅ port - resolved from Number(process.env.PORT) || devServer.port
      • ✅ https - resolved from devServer.https
      • ✅ open - resolved from process.platform === 'darwin' || devServer.open
      • ✅ proxy - resolved from devServer.proxy
      • ✅ before
        • use middlewares to improve viteDevServer(connect instance) to express instance
    • ✅ vite#build
      • ✅ outDir - resolved from vue.config.js#outputDir
      • ✅ cssCodeSplit - resolved from css.extract
      • ✅ sourcemap - resolved from process.env.GENERATE_SOURCEMAP === 'true' || productionSourceMap || css.sourceMap
    • ✅ Alias - resolved from configureWebpack or chainWebpack
      • ✅ also resolved from vue.config.js#runtimeCompiler
  • ✅ MPA Support
    • ✅ same development experience and build result
  • ✅ Build Support (as of 1.0.0-rc.0, no real html entry file generated, just reuse public/index.html of vue-cli)
    • ✅ Support SPA Build
    • ✅ Support MPA Build
  • ✅ Special Synatax
    • ❌ require('xxx') or require('xxx').default, most of the case, it can be replaced by dynamicImport ( import('xxx') or import('xxx').then(module => module.default) )
    • ✅ import '~some-module/theme/index.css' syntax for Import CSS supported by vite#2185)
    • ✅ import '~@some-module/theme/index.css' syntax for Import CSS supported by vite#2185)
    • ✅ ~public & ~/public support
    • ✅ require.context compatibility
    • ✅ module.hot compatibilite
  • ✅ Type Checker
  • ✅ Lint
  • ⬜️ Eject and Codemod
    • ⬜️ eject vite.config.ts
    • ⬜️ eject vite deps
    • ⬜️ remove vue-cli and webpack deps
    • ⬜️ codemod webpack special syntax to vite-specific( import.meta.{hot, env} )

Examples

you can clone/fork this repo, under examples/*

Troubleshooting

Benefits

Best development-experience right now

  • Instant server start and lightning fast HMR

Migration to vite smoothly

  • In the future, migration to vite is only the replacement of special syntax between webpack and vite

Lint the codebase

Use vue-cli ecosystem

Relevant Vite Plugins

changelog

1.5.0 (2021-10-12)

Bug Fixes

  • process.env issue and use vite 2.6+ for SFC template ts support (#48 #52) (010c24b)

1.4.11 (2021-09-22)

Bug Fixes

  • fix require.context on lazy mode (#51) (2f9f0d3)

1.4.10 (2021-09-15)

Bug Fixes

  • dont use Nullish Coalescing Operator for old version nodejs (40ada1c)

1.4.9 (2021-09-15)

Features

1.4.8 (2021-09-11)

Features

  • config: support config file return function (b3ef3f4)

1.4.7 (2021-09-11)

Features

  • config: detect vue.config.js call error (#38) (41a0e62)

1.4.6 (2021-08-31)

Features

  • config: add resolve.extensions support (cd14b61)

1.4.5 (2021-08-29)

Bug Fixes

  • build: fix sourcemap warning by update vite-plugin-vue-cli (65554e3)

1.4.4 (2021-08-26)

Bug Fixes

  • improve css.loaderOptions handler and update checker plugin (#44) (4b1cf81)

1.4.3 (2021-08-20)

Bug Fixes

1.4.2 (2021-08-20)

Features

  • deps: update vite deps & update vue-cli for custom config file (7a604e1)
  • add CLI_CONFIG_FILE env variable (2615a25)

1.4.1 (2021-08-09)

Bug Fixes

  • fix url with public folder (5c7ea2d)
  • replace vite-plugin-vue2 to vite-plugin-vue2-plus (5da6b88)

1.4.0 (2021-08-09)

Features

  • support dynamic injected env & improve css-loader compat & assets without protocol like http/https (ca0daa1)

1.3.6 (2021-08-09)

1.3.5 (2021-07-06)

Bug Fixes

  • checker: fix vls-checker log format (1175480)

1.3.4 (2021-07-06)

Features

  • checker: update deps of vite and vite-plugin-checker (b47f5e1)

1.3.3 (2021-06-28)

Bug Fixes

  • fix vue & VTC version mismatch issue & update deps (65b91c5)

1.3.2 (2021-06-25)

Features

  • add switch for eslint & cssLoaderCompat (df587b8)

1.3.1 (2021-06-25)

Bug Fixes

  • fix url with ~ and alias again (729a3e1)

1.3.0 (2021-06-25)

Bug Fixes

1.2.4 (2021-06-24)

Bug Fixes

  • remove .eslintcache by update eslint-plugin & vue-cli plugin (d6e97ca)

1.2.3 (2021-06-22)

Features

  • enable checker & eslint when dev & update vue-cli plugin (2df378a)

1.2.2 (2021-06-18)

Bug Fixes

  • type checker and eslint only for serve (d1c5319)

1.2.1 (2021-06-18)

Features

  • support checker plugin overlay & NODE_ENV default to development (77cd456)

1.2.0 (2021-06-17)

Features

  • add vite-plugin-checker & vite-plugin-eslint (6ead769)
  • update vite-plugin-vue-cli, check before parsing module keywords (3653a95)

1.1.6 (2021-06-17)

Features

  • update deps for vite-plugin-vue2 & mpa (790f965)

1.1.5 (2021-06-02)

1.1.4 (2021-05-27)

Features

  • optimize for vue.config.js#pages (c508c98)

1.1.3 (2021-05-26)

Bug Fixes

  • fix vite auto pre-bundle deps (1f20002)

1.1.2 (2021-05-26)

Bug Fixes

1.1.1 (2021-05-20)

Bug Fixes

  • fix history mode SPA fallback(html template) (a7d1a28)

1.1.0 (2021-05-20)

Features

  • disabled html-template if needed (b6e04e7)

1.0.3 (2021-05-19)

Bug Fixes

1.0.2 (2021-05-18)

Bug Fixes

  • fix MPA entry file injection (71418c9)

1.0.1 (2021-05-10)

Features

  • remove pluginOptions#vite.alias (c4b7ca8)

1.0.0 (2021-05-08)

Features

  • update deps, ready for v1.0.0 (3b69722)

1.0.0-rc.6 (2021-05-08)

Features

  • update deps, better entry file injection (9f81269)

1.0.0-rc.5 (2021-04-22)

Features

  • support import css by ~ prefix(for compatibility) (755a494)

1.0.0-rc.4 (2021-04-20)

Features

1.0.0-rc.3 (2021-04-15)

Features

  • update html-template plugin for more vars supported in html template (94a6df5)

1.0.0-rc.2 (2021-04-14)

Bug Fixes

  • use cross-spawn(for windows) (close #22) (50c2e51)

1.0.0-rc.1 (2021-04-08)

Features

  • support build(with vite-plugin-html-template) (5ec5a98)

1.0.0-rc.0 (2021-04-07)

Features

  • add vite-plugin-html-template(no need for main.html anymore) (df6ab6f)

0.4.4 (2021-04-06)

  • inner changes

0.4.3 (2021-04-03)

Features

0.4.2 (2021-04-01)

Bug Fixes

0.4.1 (2021-04-01)

Bug Fixes

  • support to replace webpack plugin var (e6160f2)

0.4.0 (2021-03-29)

Bug Fixes

Features

0.3.3 (2021-03-11)

Bug Fixes

  • pre define process.env.VITE to fix env not in .env.* files (d595122)

0.3.2 (2021-03-05)

Bug Fixes

  • fix require.context nested components in Windows (#13) (6e78028)

0.3.1 (2021-03-03)

Features

  • update mpa plugin & deps (e34eb3d)

0.3.0 (2021-03-01)

Features

  • support devServer.before (close #6) (da41d9a)

0.2.3 (2021-03-01)

Features

  • support vite#optimizeDeps options (close #11) (9b959f3)

0.2.2 (2021-02-25)

Bug Fixes

  • fix chainWebpack maybe be undefined (c05808c)

0.2.1 (2021-02-25)

Features

0.2.0 (2021-02-24)

Features

  • auto-resolve alias support (closes #4) (25b8873)
  • module.hot support (closes #2) (7fcdbd4)

0.1.3 (2021-02-22)

Bug Fixes

  • generator fix bin/vite for Windows (#1) (dec2739)

0.1.2 (2021-02-22)

Features

  • update mpa plugin & update README.md (ef5c36c)

0.1.1 (2021-02-19)

Bug Fixes

  • user provided alias for @ & vitePluginVue2Options (951eff0)

Features

Features

  • first release