vite + vue3 + TS搭建项目

和 Webpack相比,Vite 具有以下特点:快速的冷启动,不需要等待打包即时的热模块更新真正的按需编译,不用等待整个项目编译完成由于完全跳过了打包这个概念,Vite的出现大大的撼动了Webpack的地位,且真正做到了服务器随起随用。

使用vite快速搭建

  • 安装vite
npm i -g vite
  • 搭建vite项目
npm init vite client
  • 安装依赖
npm install / npm i
  • cd 到项目里 ,启动项目
vite / vite --host
  • 启动成功后打开如下
图片[1]|vite + vue3 + TS搭建项目|子归云

vite.config.ts 配置

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  loadEnv(mode, process.cwd())
  return {
    base: './', //打包后的相对路径
    define: {
      'process.env': process.env //public环境
    },
    server: {
      host: true,
      port: 8080, //vite项目启动时自定义端口

      open: true,
      proxy: {
        // 正则表达式写法
        '^/api': {
          target: 'http://192.168.3.151:3000/api', // 后端服务实际地址
          changeOrigin: true, //开启代理
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },
    plugins: [vue()],
    resolve: {
      //别名
      alias: {
        '@': resolve(__dirname, './src'),
        components: resolve(__dirname, './src/components'),
        api: resolve(__dirname, './src/api'),
        utils: resolve(__dirname, './src/utils'),
        store: resolve(__dirname, './src/store')
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          charset: false,
          additionalData: '@use "@/assets/style/global.scss" as *;'
        }
      }
    },
    build: {
      assetsDir: 'static', //打包后的公共文件夹名
      target: 'es2015',
      cssTarget: ['chrome52'],
      chunkSizeWarningLimit: 5000,
      terserOptions: {}
    }
  }
})
© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容