Skip to main content

基础使用

基础使用

官方地址

安装

  • npm
$ npm create vite@latest
  • yarn
$ yarn create vite
  • pnpm
$ pnpm create vite

模板

# npm 6.x
npm create vite@latest my-vue-app --template vue-ts

# npm 7+, extra double-dash is needed:
npm create vite@latest my-vue-app -- --template vue-ts

# yarn
yarn create vite my-vue-app --template vue-ts

# pnpm
pnpm create vite my-vue-app -- --template vue-ts

常用模板:

vanillavanilla-tsvuevue-tsreactreact-tspreactpreact-tslitlit-tssveltesvelte-ts

使用技巧

自动加载大量模块文件

const modules = import.meta.glob('../views/*.vue');

插件

自动引入组件

自动引入api

配置文件

文件路径:vite.config.ts

  • ts模板
import type { UserConfig, ConfigEnv } from 'vite';
export default ({ command, mode }: ConfigEnv): UserConfig => {

})
  • 常用配置
// 本配置提取自 Ant design vue3
{
base: VITE_PUBLIC_PATH,
root,
resolve: {
alias: [ // 设置路径别名
{
find: 'vue-i18n',
replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
},
// /@/xxxx => src/xxxx
{
find: /\/@\//,
replacement: pathResolve('src') + '/',
},
// /#/xxxx => types/xxxx
{
find: /\/#\//,
replacement: pathResolve('types') + '/',
},
],
},

server: {
https: true,
// Listening on all local IPs
host: true,
port: VITE_PORT,
// Load proxy configuration from .env
proxy: createProxy(VITE_PROXY),
},

esbuild: {
pure: VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : [],
},

build: {
target: 'es2015',
cssTarget: 'chrome80',
outDir: OUTPUT_DIR,
// minify: 'terser',
/**
* 当 minify=“minify:'terser'” 解开注释
* Uncomment when minify="minify:'terser'"
*/
// terserOptions: {
// compress: {
// keep_infinity: true,
// drop_console: VITE_DROP_CONSOLE,
// },
// },
// Turning off brotliSize display can slightly reduce packaging time
brotliSize: false,
chunkSizeWarningLimit: 2000,
},

define: {
// setting vue-i18-next
// Suppress warning
__INTLIFY_PROD_DEVTOOLS__: false,
__APP_INFO__: JSON.stringify(__APP_INFO__),
},

css: {
preprocessorOptions: {
less: {
modifyVars: generateModifyVars(),
javascriptEnabled: true,
},
},
},

// The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
plugins: createVitePlugins(viteEnv, isBuild),

optimizeDeps: {
// @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
include: [
'@vue/runtime-core',
'@vue/shared',
'@iconify/iconify',
'ant-design-vue/es/locale/zh_CN',
'ant-design-vue/es/locale/en_US',
],
},
}