123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import { defineConfig,loadEnv } from 'vite'
- import react from '@vitejs/plugin-react-swc'
- import monacoEditorPlugin from 'vite-plugin-monaco-editor';
- // https://vitejs.dev/config/
- export default defineConfig(({mode})=> {
- const env = loadEnv(mode, process.cwd(), '')
- // 如果你用的ts,请使用 let define: { [key: string]: string } = {};
- let define = {};
- Object.keys(env).forEach(key => {
- define[`import.meta.env.${key}`] = JSON.stringify(env[key])
- })
- return {
- base: './',
- plugins: [
- react(),
- {
- name: 'less',
- apply: 'build',
- loaders: ['less-loader']
- },
- monacoEditorPlugin({
- languageWorkers: ['editorWorkerService']
- })
- ],
- build: {
- outDir: 'dist', // 打包文件 默认dist
- minify: 'terser',
- chunkSizeWarningLimit: 2000, // 文件大小,默认500kb,生成的一个或多个文件的大小超过该值时,Vite 会发出警告提示
- // 打包清除console和debugger
- terserOptions: {
- compress: {
- drop_console: true,
- drop_debugger: true
- }
- },
- // rollupOptions: {
- // output: {
- // // 最小化拆分包
- // manualChunks(id) {
- // if (id.includes('node_modules')) {
- // return id.toString().split('node_modules/')[1].split('/')[0].toString()
- // }
- // },
- // // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
- // entryFileNames: 'js/[name].[hash].js',
- // // 用于命名代码拆分时创建的共享块的输出命名
- // // chunkFileNames: 'js/[name].[hash].js',
- // // 用于输出静态资源的命名,[ext]表示文件扩展名
- // assetFileNames: '[ext]/[name].[hash].[ext]',
- // // 拆分js到模块文件夹
- // chunkFileNames: (chunkInfo) => {
- // const facadeModuleId = chunkInfo.facadeModuleId ? chunkInfo.facadeModuleId.split('/') : [];
- // const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]';
- // return `js/${fileName}/[name].[hash].js`;
- // },
- // }
- // }
- },
- server: {
- host: '0.0.0.0',// 监听所有地址
- port: 5173, // 默认端口
- https: false, // 是否开启 https
- open: false, // 项目启动时是否打开浏览器
- cors: true, // 为开发服务器配置 CORS。默认启用并允许任何源,传递一个 选项对象 来调整行为或设为 false 表示禁用。
- base: '/', // 用于代理 Vite 作为子文件夹时使用。
- headers: {}, // 指定服务端响应的headers信息
- strictPort: false, // 设为 true 时若端口已被占用则会直接退出,而不是尝试下一个可用端口。
- proxy: {
- // 当有 /api开头的地址是,代理到target地址
- '^/api': {
- target: 'http://192.168.2.39:9080/', //目标源,目标服务器,真实请求地址
- changeOrigin: true, //支持跨域
- rewrite: (path) => path.replace(/^\/api/, "/api"), //重写真实路径,替换/api
- }
- },
- },
- }
- })
|