This commit is contained in:
oneao committed 2025-09-29 17:33:48 +08:00
1 parent 9954a33f89
commit aa10a8c1f7
80 files changed
+14576 -732

No files matched your search

+55 -28
View File
@@ -7,6 +7,7 @@ import UniLayouts from '@uni-helper/vite-plugin-uni-layouts'
import UniPlatform from '@uni-helper/vite-plugin-uni-platform'
import Optimization from '@uni-ku/bundle-optimizer'
import legacy from '@vitejs/plugin-legacy'
import { visualizer } from 'rollup-plugin-visualizer'
import AutoImport from 'unplugin-auto-import/vite'
import { defineConfig, loadEnv } from 'vite'
@@ -15,7 +16,6 @@ import openDevTools from './scripts/open-dev-tools'
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
const { UNI_PLATFORM } = process.env
console.log('UNI_PLATFORM -> ', UNI_PLATFORM) // 得到 mp-weixin, h5, app 等
const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
const {
@@ -24,31 +24,41 @@ export default defineConfig(({ command, mode }) => {
VITE_APP_PUBLIC_BASE,
VITE_APP_PROXY_ENABLE,
} = env
console.log('环境变量 env -> ', env)
if (process.env.NODE_ENV === 'development') {
console.log('当前是开发模式')
}
else {
console.log('当前是生产/打包模式')
}
return defineConfig({
envDir: './env', // 自定义env目录
base: VITE_APP_PUBLIC_BASE,
plugins: [
legacy({
targets: ['defaults', 'Chrome >= 56'],
additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
renderLegacyChunks: true,
polyfills: [
'es.symbol',
'es.array.filter',
'es.promise',
'es.promise.finally',
'es.object.assign',
'es.map',
'es.set',
'es.array.for-each',
'es.object.define-properties',
'es.object.define-property',
'es.object.get-own-property-descriptor',
'es.object.get-own-property-descriptors',
'es.object.keys',
'es.object.to-string',
'web.dom-collections.for-each',
'esnext.global-this',
'esnext.string.match-all',
'es.array.iterator',
'es.string.includes',
'es.string.starts-with',
'es.object.values',
],
}),
UniLayouts(),
UniPlatform(),
// Optimization 插件需要 page.json 文件,故应在 UniPages 插件之后执行
Optimization({
enable: {
'optimization': true,
'async-import': true,
'async-component': true,
},
dts: {
base: 'src/types',
},
logger: false,
}),
// UniXXX 需要在 Uni 之前引入
{
// 临时解决 dcloudio 官方的 @dcloudio/uni-mp-compiler 出现的编译 BUG
@@ -68,6 +78,18 @@ export default defineConfig(({ command, mode }) => {
dirs: ['src/hooks'], // 自动导入 hooks
vueTemplate: true, // default false
}),
// Optimization 插件需要 page.json 文件,故应在 UniPages 插件之后执行
Optimization({
enable: {
'optimization': true,
'async-import': true,
'async-component': true,
},
dts: {
base: 'src/types',
},
logger: false,
}),
// 打包分析插件,h5 + 生产环境才弹出
UNI_PLATFORM === 'h5'
&& mode === 'production'
@@ -87,7 +109,7 @@ export default defineConfig(({ command, mode }) => {
}),
Uni(),
// 自动打开开发者工具插件 (必须修改 .env 文件中的 VITE_WX_APPID)
openDevTools(),
// openDevTools(),
],
define: {
__UNI_PLATFORM__: JSON.stringify(UNI_PLATFORM),
@@ -114,16 +136,21 @@ export default defineConfig(({ command, mode }) => {
hmr: true,
port: Number.parseInt(VITE_APP_PORT, 10),
},
esbuild: {
drop: VITE_DELETE_CONSOLE === 'true' ? ['console', 'debugger'] : ['debugger'],
},
// esbuild: {
// drop: VITE_DELETE_CONSOLE === 'true' ? ['console', 'debugger'] : ['debugger'],
// },
build: {
sourcemap: false,
// 方便非h5端调试
// sourcemap: VITE_SHOW_SOURCEMAP === 'true', // 默认是false
target: 'es6',
// 开发环境不用压缩
minify: mode === 'development' ? false : 'esbuild',
target: 'es6', // legacy 插件会处理 polyfill
minify: mode === 'development' ? false : 'terser', // 使用 terser 保证 ES6 语法降级
terserOptions: {
compress: {
drop_console: VITE_DELETE_CONSOLE === 'true',
drop_debugger: true,
},
},
},
optimizeDeps: {
exclude: ['sard-uniapp'],