palc-newyear2024/vite.config.js
2025-05-20 18:28:02 +08:00

145 lines
4.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from 'node:url'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from 'unplugin-vue-components/resolvers';
import postcsspxtoviewport8plugin from 'postcss-px-to-viewport-8-plugin';
import viteCompression from 'vite-plugin-compression';
import { createHtmlPlugin } from 'vite-plugin-html'
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
console.log('mode:', mode);
const env = loadEnv(mode, process.cwd(), '')
return {
define: {
// enable hydration mismatch details in production build
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: mode == 'prd' ? 'false' : 'true',
},
plugins: [
vue(),
// 多页面配置插件
createHtmlPlugin({
minify: true,
pages: [
{
filename: 'index',
entry: '/src/page/Home/main.js',
template: 'index.html',
injectOptions: {
data: {
title: '打卡理想假期',
},
}
},
]
}),
// 按需自动引入vant组件
Components({
resolvers: [VantResolver()],
}),
AutoImport({
resolvers: [VantResolver()],
imports: ['vue', 'vue-router',]
}),
// 开启gzip压缩
viteCompression({
verbose: true, // 默认即可
disable: false, //开启压缩(不禁用),默认即可
deleteOriginFile: false, //删除源文件
threshold: 10240, //压缩前最小文件大小:10k
algorithm: 'gzip', //压缩算法
ext: '.gz', //文件类型
})
],
server: { // ← ← ← ← ← ←
host: '0.0.0.0' // ← 新增内容 ←
},
// 别名配置
resolve: {
alias: {
// '@': resolve(__dirname, './src'),
'@': fileURLToPath(new URL('./src', import.meta.url))
},
extensions: ['.js', '.vue', '.json'] // 引入对应的文件时可以忽略其后缀
},
// css处理器配置
css: {
postcss: {
plugins: [
postcsspxtoviewport8plugin({
unitToConvert: 'px',
viewportWidth: file => {
let num = 750;
//van是375
if (file.indexOf('van') > 0) {
num = 375;
}
return num;
},
unitPrecision: 5, // 单位转换后保留的精度
propList: ['*'], // 能转化为vw的属性列表
viewportUnit: 'vw', // 希望使用的视口单位
fontViewportUnit: 'vw', // 字体使用的视口单位
selectorBlackList: [ 'pc-content'], // 需要忽略的CSS选择器不会转为视口单位使用原有的px等单位。
minPixelValue: 1, // 设置最小的转换数值如果为1的话只有大于1的值会被转换
mediaQuery: true, // 媒体查询里的单位是否需要转换单位
replace: true, // 是否直接更换属性值,而不添加备用属性
exclude: [], // 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
include: [], // 如果设置了include那将只有匹配到的文件才会被转换
landscape: false, // 是否添加根据 landscapeWidth 生成的媒体查询条件 @media (orientation: landscape)
landscapeUnit: 'vw', // 横屏时使用的单位
landscapeWidth: 750, // 横屏时使用的视口宽度
}),
],
},
preprocessorOptions: {
scss: {
javascriptEnabled: true,
additionalData: '@import "@/styles/global.scss";',
},
},
},
// 公共基础路径构建生产环境时自动载入cdn路径
base: './',
// 打包配置
build: {
// outDir: 'dist',
assetsPublicPath: './',
assetsDir: 'static',
minify: 'terser',
chunkSizeWarningLimit: 512,
terserOptions: {
compress: {
//生产环境时移除打印日志
// drop_console: mode == 'prd' ? true : false,
// drop_debugger: mode == 'prd' ? true : false,
},
},
rollupOptions: {
input: {},
output: {
// node_modules下引用的插件采用分包策略名称不改变应对浏览器缓存策略
"manualChunks": (id) => {
if (id.includes("node_modules")) {
return id.toString().split('node_modules/')[1].split('/')[0].toString()
}
},
// 为输出文件加Hash值
chunkFileNames: 'static/js/[name]-[hash].js',
entryFileNames: 'static/js/[name]-[hash].js',
assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
}
}
}
}
})