199 lines
6.6 KiB
TypeScript
199 lines
6.6 KiB
TypeScript
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import Uni from '@dcloudio/vite-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'
|
|
/**
|
|
* 分包优化、模块异步跨包调用、组件异步跨包引用
|
|
* @see https://github.com/uni-ku/bundle-optimizer
|
|
*/
|
|
import Optimization from '@uni-ku/bundle-optimizer'
|
|
import basicSsl from '@vitejs/plugin-basic-ssl'
|
|
import legacy from '@vitejs/plugin-legacy'
|
|
import dayjs from 'dayjs'
|
|
import { visualizer } from 'rollup-plugin-visualizer'
|
|
import AutoImport from 'unplugin-auto-import/vite'
|
|
import { defineConfig, loadEnv } from 'vite'
|
|
import ViteRestart from 'vite-plugin-restart'
|
|
import { copyNativeRes } from './vite-plugins/copyNativeRes'
|
|
// https://vitejs.dev/config/
|
|
export default async ({ command, mode }) => {
|
|
const UnoCSS = (await import('unocss/vite')).default
|
|
|
|
const { UNI_PLATFORM } = process.env
|
|
|
|
const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
|
|
const {
|
|
VITE_APP_PORT,
|
|
VITE_SERVER_BASEURL,
|
|
VITE_DELETE_CONSOLE,
|
|
VITE_SHOW_SOURCEMAP,
|
|
VITE_APP_PROXY,
|
|
VITE_APP_PROXY_PREFIX,
|
|
} = env
|
|
console.log('环境变量 env -> ', env)
|
|
|
|
return defineConfig({
|
|
envDir: './env', // 自定义env目录
|
|
|
|
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',
|
|
],
|
|
}),
|
|
UniPages({
|
|
exclude: ['**/components/**/**.*'],
|
|
routeBlockLang: 'json5', // 虽然设了默认值,但是vue文件还是要加上 lang="json5", 这样才能很好地格式化
|
|
// homePage 通过 vue 文件的 route-block 的type="home"来设定
|
|
// pages 目录为 src/pages,分包目录不能配置在pages目录下
|
|
subPackages: ['src/pages-sub'], // 是个数组,可以配置多个,但是不能为pages里面的目录
|
|
dts: 'src/types/uni-pages.d.ts',
|
|
}),
|
|
UniLayouts(),
|
|
UniPlatform(),
|
|
UniManifest(),
|
|
// 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
|
|
}
|
|
},
|
|
},
|
|
UnoCSS(),
|
|
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,
|
|
}),
|
|
|
|
ViteRestart({
|
|
// 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
|
|
restart: ['vite.config.js'],
|
|
}),
|
|
// h5环境增加 BUILD_TIME 和 BUILD_BRANCH
|
|
UNI_PLATFORM === 'h5' && {
|
|
name: 'html-transform',
|
|
transformIndexHtml(html) {
|
|
return html.replace('%BUILD_TIME%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
|
|
},
|
|
},
|
|
// 打包分析插件,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(),
|
|
],
|
|
define: {
|
|
__UNI_PLATFORM__: JSON.stringify(UNI_PLATFORM),
|
|
__VITE_APP_PROXY__: JSON.stringify(VITE_APP_PROXY),
|
|
},
|
|
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',
|
|
https: {
|
|
key: fs.readFileSync('D:/Devtool/localhost+3-key.pem'),
|
|
cert: fs.readFileSync('D:/Devtool/localhost+3.pem'),
|
|
},
|
|
hmr: true,
|
|
port: Number.parseInt(VITE_APP_PORT, 10),
|
|
// 仅 H5 端生效,其他端不生效(其他端走build,不走devServer)
|
|
proxy: JSON.parse(VITE_APP_PROXY)
|
|
? {
|
|
[VITE_APP_PROXY_PREFIX]: {
|
|
target: VITE_SERVER_BASEURL,
|
|
changeOrigin: true,
|
|
rewrite: path => path.replace(new RegExp(`^${VITE_APP_PROXY_PREFIX}`), ''),
|
|
},
|
|
}
|
|
: undefined,
|
|
},
|
|
build: {
|
|
sourcemap: false,
|
|
// 方便非h5端调试
|
|
// sourcemap: VITE_SHOW_SOURCEMAP === 'true', // 默认是false
|
|
target: 'es6',
|
|
// 开发环境不用压缩
|
|
minify: mode === 'development' ? false : 'terser',
|
|
terserOptions: {
|
|
compress: {
|
|
drop_console: VITE_DELETE_CONSOLE === 'true',
|
|
drop_debugger: true,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|