123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- import path from 'node:path'
- import { execSync } from 'node:child_process'
- import dayjs from 'dayjs'
- import { defineConfig, loadEnv } from 'vite'
- import Uni from '@dcloudio/vite-plugin-uni'
- import UniPages from '@uni-helper/vite-plugin-uni-pages'
- import UniLayouts from '@uni-helper/vite-plugin-uni-layouts'
- import UniPlatform from '@uni-helper/vite-plugin-uni-platform'
- import UniManifest from '@uni-helper/vite-plugin-uni-manifest'
- import UnoCSS from 'unocss/vite'
- import AutoImport from 'unplugin-auto-import/vite'
- import { visualizer } from 'rollup-plugin-visualizer'
- import ViteRestart from 'vite-plugin-restart'
- export default ({ command, mode }) => {
-
-
- console.log('command, mode -> ', command, mode)
-
-
-
-
-
-
-
- const { UNI_PLATFORM } = process.env
- console.log('UNI_PLATFORM -> ', UNI_PLATFORM)
- 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',
- plugins: [
- UniPages({
- exclude: ['**/components/**/**.*'],
- routeBlockLang: 'json5',
-
-
-
- dts: 'src/types/uni-pages.d.ts',
- }),
- UniLayouts(),
- UniPlatform(),
- UniManifest(),
-
- Uni(),
- {
-
-
-
- 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'],
- eslintrc: { enabled: true },
- vueTemplate: true,
- ignore: ['h'],
- }),
- ViteRestart({
-
- restart: ['vite.config.js'],
- }),
-
- UNI_PLATFORM === 'h5' && {
- name: 'html-transform',
- transformIndexHtml(html) {
- return html.replace('%BUILD_TIME%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
- },
- },
-
- UNI_PLATFORM === 'h5' &&
- mode === 'production' &&
- visualizer({
- filename: './node_modules/.cache/visualizer/stats.html',
- open: true,
- gzipSize: true,
- brotliSize: true,
- }),
- ],
- define: {
- __UNI_PLATFORM__: JSON.stringify(UNI_PLATFORM),
- __VITE_APP_PROXY__: JSON.stringify(VITE_APP_PROXY),
- },
- css: {
- postcss: {
- plugins: [
-
-
-
-
- ],
- },
- },
- 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),
-
- 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: VITE_SHOW_SOURCEMAP === 'true',
- target: 'es6',
-
- minify: mode === 'development' ? false : 'terser',
- terserOptions: {
- compress: {
- drop_console: VITE_DELETE_CONSOLE === 'true',
- drop_debugger: true,
- },
- },
- },
- })
- }
|