102 lines
3.2 KiB
JavaScript
102 lines
3.2 KiB
JavaScript
import {
|
||
join
|
||
} from 'path'
|
||
import {
|
||
defineConfig
|
||
} from 'vite';
|
||
import vue from '@vitejs/plugin-vue';
|
||
import {
|
||
VuetifyResolver
|
||
} from 'unplugin-vue-components/resolvers';
|
||
import Components from 'unplugin-vue-components/vite';
|
||
import AutoImport from 'unplugin-auto-import/vite';
|
||
import viteCompression from 'vite-plugin-compression'
|
||
import { visualizer } from 'rollup-plugin-visualizer' //查看项目的依赖
|
||
import { createHtmlPlugin } from 'vite-plugin-html'
|
||
|
||
|
||
export default defineConfig(({ command }) => {
|
||
return {
|
||
base: command === 'build' ? './' : './',
|
||
plugins: [
|
||
vue(),
|
||
AutoImport({
|
||
// 自动导入 Vue 相关函数,如:ref, reactive, toRef 等
|
||
imports: ['vue']
|
||
}),
|
||
visualizer({
|
||
open: false
|
||
}),
|
||
// 将下面的添加到plugin下
|
||
createHtmlPlugin({
|
||
minify: true,
|
||
inject: {
|
||
data: {
|
||
title: '健康宣教',
|
||
}
|
||
}
|
||
}),
|
||
Components({
|
||
resolvers: [VuetifyResolver()],
|
||
include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
|
||
dirs: ['src/components', 'src/views'],
|
||
}),
|
||
],
|
||
|
||
build: {
|
||
assetsInlineLimit: 4096, // 图片转 base64 编码的阈值
|
||
minify: 'terser',
|
||
plugins: [
|
||
viteCompression({
|
||
threshold: 1024000 // 对大于 1mb 的文件进行压缩
|
||
}),
|
||
],
|
||
// rollup 配置
|
||
rollupOptions: {
|
||
output: {
|
||
chunkFileNames: 'static/js/[name]-[hash].js', // 引入文件名的名称
|
||
entryFileNames: 'static/js/[name]-[hash].js', // 包的入口文件名称
|
||
assetFileNames: 'static/[ext]/[name]-[hash].[ext]', // 资源文件像 字体,图片等
|
||
manualChunks(id) {
|
||
// 如果不同模块使用的插件基本相同那就尽可能打包在同一个文件中,减少http请求,如果不同模块使用不同插件明显,那就分成不同模块打包。这是一个矛盾体。
|
||
// 这里使用的是最小化拆分包。如果是前者可以直接选择返回'vendor'。
|
||
if (id.includes('node_modules')) {
|
||
return id.toString().split('node_modules/')[1].split('/')[0].toString(); //让打开那个页面,加载那个页面的js ,让之间的关联足够小
|
||
// return 'vendor' 如果不同模块使用的插件基本相同那就尽可能打包在同一个文件中,减少http请求;
|
||
}
|
||
}
|
||
}
|
||
},
|
||
terserOptions: {
|
||
compress: {
|
||
//生产环境时移除console
|
||
drop_console: true,
|
||
drop_debugger: true,
|
||
},
|
||
},
|
||
},
|
||
resolve: {
|
||
alias: {
|
||
'@': join(__dirname, 'src'),
|
||
}
|
||
},
|
||
server: {
|
||
host: true,
|
||
port: 1798,
|
||
//secure: false,
|
||
proxy: {
|
||
// '/api': {
|
||
// target: 'https://vue3.go-admin.dev',
|
||
// changeOrigin: true, //开启跨域
|
||
// rewrite: (path) => path.replace(/^\/api/, '')
|
||
// },
|
||
'/book': {
|
||
target: 'https://twx.igandan.org',
|
||
changeOrigin: true, //开启跨域
|
||
rewrite: (path) => path.replace(/^\/book/, '')
|
||
}
|
||
}
|
||
},
|
||
publicDir: '/public'
|
||
}
|
||
}); |