All Projects → IndexXuan → Vue Cli Plugin Vite

IndexXuan / Vue Cli Plugin Vite

Licence: mit
Use Vite Today, with vue-cli

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Vue Cli Plugin Vite

Admin One Vue Bulma Dashboard
Admin One — Free Vue.js Bulma Admin Dashboard SPA/PWA
Stars: ✭ 138 (-15.85%)
Mutual labels:  vue-cli
Vue Wechat
🔥 基于Vue2.0高仿微信App的单页应用
Stars: ✭ 1,832 (+1017.07%)
Mutual labels:  vue-cli
Fantastic Admin
一款开箱即用的 Vue 中后台管理系统框架,基于ElementUI,兼容PC、移动端,vue-admin, vue-element-admin, vue后台
Stars: ✭ 153 (-6.71%)
Mutual labels:  vue-cli
Elementui
http://recklesslmz.com/elementUI/dist/#/
Stars: ✭ 138 (-15.85%)
Mutual labels:  vue-cli
Vue Plugin Boilerplate
🔩 Boilerplate for Vue.js plugin
Stars: ✭ 143 (-12.8%)
Mutual labels:  vue-cli
Vue Cli Multi Page
基于vue-cli模板的多页面多路由项目,一个PC端页面入口,一个移动端页面入口,且有各自的路由, vue+webpack+vue-router+vuex+mock+axios
Stars: ✭ 145 (-11.59%)
Mutual labels:  vue-cli
Vue Admin
VUE2.0增删改查附编辑添加model(弹框)组件共用
Stars: ✭ 133 (-18.9%)
Mutual labels:  vue-cli
Awesome Vue Cli3 Example
🦅 Awesome example for rapid Vue.js development using vue-cli3 .
Stars: ✭ 160 (-2.44%)
Mutual labels:  vue-cli
Ibiu
Build Vue Spa Faster 🍏 🍔 🍜
Stars: ✭ 144 (-12.2%)
Mutual labels:  vue-cli
Vue Cli Plugin Qiankun
🚀 qiankun plugin for vue-cli
Stars: ✭ 151 (-7.93%)
Mutual labels:  vue-cli
Vue Stepbystep
a vue tutorial step by step
Stars: ✭ 138 (-15.85%)
Mutual labels:  vue-cli
Ve Charts
📈 ECharts 4.x for Vue.js 2.x.
Stars: ✭ 142 (-13.41%)
Mutual labels:  vue-cli
Douban Movie
🎥The douban-movie Application built with webpack + vue + vuex + vue-router + iView.
Stars: ✭ 147 (-10.37%)
Mutual labels:  vue-cli
Basix Admin
Get Free and Premium Vue.js Bootstrap v4 Admin Dashboard Templates
Stars: ✭ 138 (-15.85%)
Mutual labels:  vue-cli
Spring Boot Vue Bank
我,请始皇[打钱]是一个前后端分离的工具人系统,项目采用 SpringBoot+Go+Vue 开发,项目加入常见的企业级应用所涉及到的技术点,例如 Redis、RabbitMQ 等(主要是多用用工具多踩踩坑)。
Stars: ✭ 157 (-4.27%)
Mutual labels:  vue-cli
Spring Boot Vuejs
Example project showing how to build a Spring Boot App providing a GUI with Vue.js
Stars: ✭ 1,818 (+1008.54%)
Mutual labels:  vue-cli
Gradex
An online tool to generate gradients background from given image so you can preview and download it.
Stars: ✭ 145 (-11.59%)
Mutual labels:  vue-cli
Github Ranking
🔍GitHub不同语言热门项目排行,Vue.js做页面展示
Stars: ✭ 160 (-2.44%)
Mutual labels:  vue-cli
Vuetify Todo Pwa
✔️ A simple Todo PWA built with Vue CLI 3 + Vuex + Vuetify.
Stars: ✭ 160 (-2.44%)
Mutual labels:  vue-cli
Gpk admin
✨ GeekPark Content Management System
Stars: ✭ 150 (-8.54%)
Mutual labels:  vue-cli

Use Vite Today

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

logo

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

the plugin's generator will write some main.html for corresponding main.{js,ts}, since vite need html file for dev-server entry file

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: {
      ///**
      // * deprecated since v0.2.2. we can auto-resolve alias from vue.config.js
      // * @ is setted by the plugin, you can set others used in your projects, like @components
      // * Record<string, string>
      // * @default {}
      // */
      // alias: {
      //   '@components': path.resolve(__dirname, './src/components'),
      // },
      /**
       * Plugin[]
       * @default []
       */
      plugins: [], // other vite plugins list, will be merge into this plugin\'s underlying vite.config.ts
      /**
       * you can enable jsx support by setting { jsx: true }
       * @see https://github.com/underfin/vite-plugin-vue2#options
       * @default {}
       */
      vitePluginVue2Options: {},
      /**
       * Vite UserConfig.optimizeDeps options
       * @default {}
       */
      optimizeDeps: {}
    }
  },
}

Underlying principle

Compatibility

  • NO EXTRA files, code and dependencies injected
    • injected corresponding main.html
      • SPA: projectRoot/main.html
      • MPA: projectRoot/src/pages/*/main.html(s)
    • 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...)
  • 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

Milestones

  • ✅ 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 (you can use other instead by config, e.g. REACT_APP_)
    • ✅ 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
  • ✅ 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) )
    • ❌ '~some-module' syntax for Import CSS (maybe #2185)
    • ✅ require.context compatibility
    • ✅ module.hot compatibility

Examples

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

Troubleshooting

Vite Build Support

  • Currently only support vite dev for development, you should still use yarn build(vue-cli-service build)
  • But you can use BUILD=true MODERN=true yarn vite to invoke vite build(no legacy and use esbuild minify, not recommended, please use yarn build instead)

How to completely migrate to vite in the future

  • if this plugin help you fix error and use vite successfully, it is not too hard to migrate, compared to directly migrate from vue-cli or others
  • safely replace all VUE_APP_ to VITE_ code (e.g. .env.*)
  • safely replace all process.env.VUE_APP_ to import.meta.env.VITE_ in client-side code.
  • safely copy ./node_modules/vite-plugin-vue-cli/config/index.ts to $projectRoot/vite.config.ts and install corresponding vite-plugin list by it
  • add npm scripts dev: vite & build: vite build, remove other vue-cli scripts, like serve
  • migrate all require.context to import.meta.glob/globEager
  • remove all webpack plugins/vue-cli plugins and migrate all vue.config.js setted chainWebpack/configureWebpack to corresponding vite plugin or options
  • deps & devDeps cleanup
  • other cleanup and tests

Some module response 404 not found

  • if not compiler errors, maybe you import vue file without .vue ext, added it and it is required for vite and recommended for vue-cli (and required in vue-cli 5.x)

Custom Style missing fonts

/* theme color */
$--color-primary: teal;

/* icon font path, required */
$--font-path: '~element-plus/lib/theme-chalk/fonts'; // changed to 'path/to/node_modules/element-plus/lib/theme-chalk/fonts;' (TOOD: can vite support it ?)

@import "~element-plus/packages/theme-chalk/src/index"; // remove '~', both css-loader and vite support it

JSX support

  • see options above, vitePluginVue2Options: { jsx: true }
  • you may also see that React is not defined, it is you use jsx without set vitePluginVue2Options: { jsx: true }

Vue3 support

  • currently only support Vue2.x, since Vue3.x you can use vite directly

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].