xyyh-hhj/vite.config.js

142 lines
4.8 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 { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';
import vitePluginLvdeploy from 'vite-plugin-lvdeploy' //自动化部署
import deployConfig from './deploy.config'; //自动化部署配置文件
import viteCompression from 'vite-plugin-compression';
import { createHtmlPlugin } from 'vite-plugin-html'
import postcsspxtoviewport8plugin from 'postcss-px-to-viewport-8-plugin';
import pxtoviewportCofig from './config/config'
const ENV_DIR = resolve(__dirname, './config')
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
console.log('mode:', mode);
const env = loadEnv(mode, process.cwd(), '')
return {
// envDir: ENV_DIR, //更改环境变量目录
plugins: [
vue(),
// 多页面配置插件
createHtmlPlugin({
minify: true,
pages: [
{
filename: 'index',
entry: '/src/page/Home/main.js',
template: 'index.html',
injectOptions: {
data: {
title: '兴福龙 护航记',
},
}
},
]
}),
// 按需引入vant组件
Components({
resolvers: [VantResolver()],
}),
// 自动化部署
vitePluginLvdeploy(deployConfig),
AutoImport({
imports: ['vue', 'vue-router',]
}),
// 开启gzip压缩
viteCompression({
verbose: true, // 默认即可
disable: false, //开启压缩(不禁用),默认即可
deleteOriginFile: false, //删除源文件
threshold: 10240, //压缩前最小文件大小
algorithm: 'gzip', //压缩算法
ext: '.gz', //文件类型
})
],
server: { // ← ← ← ← ← ←
host: '0.0.0.0' // ← 新增内容 ←
},
// 别名配置
resolve: {
alias: {
'@': resolve(__dirname, './src'),
},
extensions: ['.js', '.vue', '.json'] // 引入对应的文件时可以忽略其后缀
},
// sass全局配置
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: ['ignore-'], // 需要忽略的CSS选择器不会转为视口单位使用原有的px等单位。
minPixelValue: 1, // 设置最小的转换数值如果为1的话只有大于1的值会被转换
mediaQuery: true, // 媒体查询里的单位是否需要转换单位
replace: true, // 是否直接更换属性值,而不添加备用属性
exclude: [], // 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
include: [], // 如果设置了include那将只有匹配到的文件才会被转换
landscape: false, // 是否添加根据 landscapeWidth 生成的媒体查询条件 @media (orientation: landscape)
landscapeUnit: 'vw', // 横屏时使用的单位
landscapeWidth: 1628, // 横屏时使用的视口宽度
}),
],
},
preprocessorOptions: {
scss: {
javascriptEnabled: true,
additionalData: '@import "./src/styles/global.scss";',
},
},
},
// 公共基础路径构建生产环境时自动载入cdn路径
base: mode == 'production' ? env.VITE_CDN : './',
// 打包配置
build: {
assetsPublicPath: './',
assetsDir: 'static',
minify: 'terser',
chunkSizeWarningLimit: 1500,
terserOptions: {
compress: {
//生产环境时移除console.log()
drop_console: false,
drop_debugger: true,
},
},
rollupOptions: {
input: {},
output: {
// node_modules下引用的插件采用分包策略名称不改变应对浏览器缓存策略
"manualChunks": (id) => {
if (id.includes("node_modules")) {
return "vendor"
}
},
// 为输出文件加Hash值
chunkFileNames: 'static/js/[name]-[hash].js',
entryFileNames: 'static/js/[name]-[hash].js',
assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
}
}
}
}
})