155 lines
5.0 KiB
TypeScript
155 lines
5.0 KiB
TypeScript
import path from 'node:path'
|
|
import process from 'node:process'
|
|
import Uni from '@uni-helper/plugin-uni'
|
|
|
|
import Components from '@uni-helper/vite-plugin-uni-components'
|
|
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'
|
|
import openDevTools from './scripts/open-dev-tools'
|
|
|
|
// https://vitejs.dev/config/
|
|
export default defineConfig(({ command, mode }) => {
|
|
const { UNI_PLATFORM } = process.env
|
|
|
|
const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
|
|
const {
|
|
VITE_APP_PORT,
|
|
VITE_DELETE_CONSOLE,
|
|
VITE_APP_PUBLIC_BASE,
|
|
VITE_APP_PROXY_ENABLE,
|
|
} = env
|
|
|
|
return defineConfig({
|
|
envDir: './env', // 自定义env目录
|
|
plugins: [
|
|
legacy({
|
|
targets: ['defaults', 'ie >= 11', 'chrome 52'], // 需要兼容的目标列表,可以设置多个
|
|
additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
|
|
renderLegacyChunks: true,
|
|
polyfills: [
|
|
'es.symbol',
|
|
'es.array.filter',
|
|
'es.promise',
|
|
'es.promise.finally',
|
|
'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',
|
|
],
|
|
}),
|
|
UniLayouts(),
|
|
UniPlatform(),
|
|
|
|
// UniXXX 需要在 Uni 之前引入
|
|
{
|
|
// 临时解决 dcloudio 官方的 @dcloudio/uni-mp-compiler 出现的编译 BUG
|
|
// 参考 github issue: https://github.com/dcloudio/uni-app/issues/4952
|
|
// 自定义插件禁用 vite:vue 插件的 devToolsEnabled,强制编译 vue 模板时 inline 为 true
|
|
name: 'fix-vite-plugin-vue',
|
|
configResolved(config) {
|
|
const plugin = config.plugins.find(p => p.name === 'vite:vue')
|
|
if (plugin && plugin.api && plugin.api.options) {
|
|
plugin.api.options.devToolsEnabled = false
|
|
}
|
|
},
|
|
},
|
|
AutoImport({
|
|
imports: ['vue', 'uni-app'],
|
|
dts: 'src/types/auto-import.d.ts',
|
|
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'
|
|
&& visualizer({
|
|
filename: './node_modules/.cache/visualizer/stats.html',
|
|
open: true,
|
|
gzipSize: true,
|
|
brotliSize: true,
|
|
}),
|
|
// 只有在 app 平台时才启用 copyNativeRes 插件
|
|
// UNI_PLATFORM === 'app' && copyNativeRes(),
|
|
Components({
|
|
extensions: ['vue'],
|
|
deep: true, // 是否递归扫描子目录,
|
|
directoryAsNamespace: false, // 是否把目录名作为命名空间前缀,true 时组件名为 目录名+组件名,
|
|
dts: 'src/types/components.d.ts', // 自动生成的组件类型声明文件路径(用于 TypeScript 支持)
|
|
}),
|
|
Uni(),
|
|
// 自动打开开发者工具插件 (必须修改 .env 文件中的 VITE_WX_APPID)
|
|
// openDevTools(),
|
|
],
|
|
define: {
|
|
__UNI_PLATFORM__: JSON.stringify(UNI_PLATFORM),
|
|
__VITE_APP_PROXY__: JSON.stringify(VITE_APP_PROXY_ENABLE),
|
|
},
|
|
css: {
|
|
postcss: {
|
|
plugins: [
|
|
// autoprefixer({
|
|
// // 指定目标浏览器
|
|
// overrideBrowserslist: ['> 1%', 'last 2 versions'],
|
|
// }),
|
|
],
|
|
},
|
|
},
|
|
resolve: {
|
|
alias: {
|
|
'@': path.join(process.cwd(), './src'),
|
|
'@img': path.join(process.cwd(), './src/static/images'),
|
|
},
|
|
},
|
|
server: {
|
|
host: '0.0.0.0',
|
|
hmr: true,
|
|
port: Number.parseInt(VITE_APP_PORT, 10),
|
|
},
|
|
// esbuild: {
|
|
// drop: VITE_DELETE_CONSOLE === 'true' ? ['console', 'debugger'] : ['debugger'],
|
|
// },
|
|
build: {
|
|
sourcemap: false,
|
|
// 方便非h5端调试
|
|
// sourcemap: VITE_SHOW_SOURCEMAP === 'true', // 默认是false
|
|
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'],
|
|
},
|
|
})
|
|
})
|