|
@@ -9,7 +9,7 @@ import { CodeInspectorPlugin } from 'code-inspector-plugin';
|
|
|
import fs from 'fs';
|
|
import fs from 'fs';
|
|
|
import { visualizer } from 'rollup-plugin-visualizer';
|
|
import { visualizer } from 'rollup-plugin-visualizer';
|
|
|
import { webUpdateNotice } from '@plugin-web-update-notification/vite';
|
|
import { webUpdateNotice } from '@plugin-web-update-notification/vite';
|
|
|
-import vitePluginsAutoI18n, { EmptyTranslator, YoudaoTranslator } from 'vite-auto-i18n-plugin';
|
|
|
|
|
|
|
+import vitePluginsAutoI18n, { EmptyTranslator } from 'vite-auto-i18n-plugin';
|
|
|
const pathResolve = (dir: string) => {
|
|
const pathResolve = (dir: string) => {
|
|
|
return resolve(__dirname, '.', dir);
|
|
return resolve(__dirname, '.', dir);
|
|
|
};
|
|
};
|
|
@@ -22,10 +22,15 @@ const alias: Record<string, string> = {
|
|
|
|
|
|
|
|
const viteConfig = defineConfig((mode: ConfigEnv) => {
|
|
const viteConfig = defineConfig((mode: ConfigEnv) => {
|
|
|
const env = loadEnv(mode.mode, process.cwd());
|
|
const env = loadEnv(mode.mode, process.cwd());
|
|
|
|
|
+ /** dev 代理目标:始终本机后端。勿用浏览器用的公网 VITE_API_URL,避免绕一圈且易配错 */
|
|
|
|
|
+ const apiProxyTarget = env.VITE_PROXY_TARGET || 'http://127.0.0.1:5005';
|
|
|
|
|
+ const devPort = Number(env.VITE_PORT) || 8888;
|
|
|
|
|
+ /** 浏览器访问用公网 IP/域名;勿写入 server.hmr.host,否则 WS 会 bind 该地址,云主机 EIP 常不在网卡上 → EADDRNOTAVAIL */
|
|
|
|
|
+ const devPublicHost = String(env.VITE_DEV_PUBLIC_HOST || '').trim();
|
|
|
fs.writeFileSync('./public/config.js', `window.__env__ = ${JSON.stringify(env, null, 2)} `);
|
|
fs.writeFileSync('./public/config.js', `window.__env__ = ${JSON.stringify(env, null, 2)} `);
|
|
|
return {
|
|
return {
|
|
|
plugins: [
|
|
plugins: [
|
|
|
- visualizer({ open: false }), // 开启可视化分析页面
|
|
|
|
|
|
|
+ visualizer({ open: false }),
|
|
|
CodeInspectorPlugin({
|
|
CodeInspectorPlugin({
|
|
|
bundler: 'vite',
|
|
bundler: 'vite',
|
|
|
hotKeys: ['shiftKey'],
|
|
hotKeys: ['shiftKey'],
|
|
@@ -45,25 +50,21 @@ const viteConfig = defineConfig((mode: ConfigEnv) => {
|
|
|
},
|
|
},
|
|
|
}),
|
|
}),
|
|
|
vueSetupExtend(),
|
|
vueSetupExtend(),
|
|
|
- compression({
|
|
|
|
|
- deleteOriginalAssets: false, // 是否删除源文件
|
|
|
|
|
- threshold: 5120, // 对大于 5KB 文件进行 gzip 压缩,单位Bytes
|
|
|
|
|
- skipIfLargerOrEqual: true, // 如果压缩后的文件大小等于或大于原始文件,则跳过压缩
|
|
|
|
|
- // algorithm: 'gzip', // 压缩算法,可选[‘gzip’,‘brotliCompress’,‘deflate’,‘deflateRaw’]
|
|
|
|
|
- // exclude: [/\.(br)$/, /\.(gz)$/], // 排除指定文件
|
|
|
|
|
- }),
|
|
|
|
|
|
|
+ ...(mode.command === 'build'
|
|
|
|
|
+ ? [
|
|
|
|
|
+ compression({
|
|
|
|
|
+ deleteOriginalAssets: false,
|
|
|
|
|
+ threshold: 5120,
|
|
|
|
|
+ skipIfLargerOrEqual: true,
|
|
|
|
|
+ }),
|
|
|
|
|
+ ]
|
|
|
|
|
+ : []),
|
|
|
JSON.parse(env.VITE_OPEN_CDN) ? buildConfig.cdn() : null,
|
|
JSON.parse(env.VITE_OPEN_CDN) ? buildConfig.cdn() : null,
|
|
|
- // 使用说明 https://github.com/auto-i18n/auto-i18n-translation-plugins
|
|
|
|
|
vitePluginsAutoI18n({
|
|
vitePluginsAutoI18n({
|
|
|
- // 是否触发翻译
|
|
|
|
|
enabled: false,
|
|
enabled: false,
|
|
|
- originLang: 'zh-cn', //源语言,翻译以此语言为基础
|
|
|
|
|
- targetLangList: ['zh-hk', 'zh-tw', 'en', 'it'], // 目标语言列表,支持配置多个语言
|
|
|
|
|
- translator: new EmptyTranslator(), // 只生成Web\lang\index.json文件
|
|
|
|
|
- // translator: new YoudaoTranslator({ // 有道实时翻译
|
|
|
|
|
- // appId: '你申请的appId',
|
|
|
|
|
- // appKey: '你申请的appKey'
|
|
|
|
|
- // })
|
|
|
|
|
|
|
+ originLang: 'zh-cn',
|
|
|
|
|
+ targetLangList: ['zh-hk', 'zh-tw', 'en', 'it'],
|
|
|
|
|
+ translator: new EmptyTranslator(),
|
|
|
}),
|
|
}),
|
|
|
],
|
|
],
|
|
|
root: process.cwd(),
|
|
root: process.cwd(),
|
|
@@ -74,14 +75,38 @@ const viteConfig = defineConfig((mode: ConfigEnv) => {
|
|
|
host: '0.0.0.0',
|
|
host: '0.0.0.0',
|
|
|
port: env.VITE_PORT as unknown as number,
|
|
port: env.VITE_PORT as unknown as number,
|
|
|
open: JSON.parse(env.VITE_OPEN),
|
|
open: JSON.parse(env.VITE_OPEN),
|
|
|
- hmr: true,
|
|
|
|
|
|
|
+ ...(mode.command === 'serve' && devPublicHost
|
|
|
|
|
+ ? { origin: `http://${devPublicHost}:${devPort}` }
|
|
|
|
|
+ : {}),
|
|
|
|
|
+ // VITE_HMR=false 可关热更新。公网访问时不要给 hmr.host 填 EIP(见 devPublicHost 注释)
|
|
|
|
|
+ // 勿设 hmr.port === server.port:Vite 8 会先把 WS 占住该端口,HTTP 再监听会冲突并退到 8889,浏览器仍访问 8888 会 426
|
|
|
|
|
+ hmr: env.VITE_HMR === 'false' ? false : true,
|
|
|
|
|
+ // 文件监视过多时 inotify 会 ENOSPC;设 VITE_WATCH_POLLING=true 可改用轮询(略耗 CPU)
|
|
|
|
|
+ watch:
|
|
|
|
|
+ env.VITE_WATCH_POLLING === 'true'
|
|
|
|
|
+ ? { usePolling: true, interval: 1500 }
|
|
|
|
|
+ : undefined,
|
|
|
proxy: {
|
|
proxy: {
|
|
|
- '^/api': {
|
|
|
|
|
- target: env.VITE_API_URL,
|
|
|
|
|
|
|
+ // 勿对 /api 开 ws:true,在部分环境下会导致普通 GET 挂起无响应
|
|
|
|
|
+ '/api': {
|
|
|
|
|
+ target: apiProxyTarget,
|
|
|
|
|
+ changeOrigin: true,
|
|
|
|
|
+ },
|
|
|
|
|
+ '/upload': {
|
|
|
|
|
+ target: apiProxyTarget,
|
|
|
|
|
+ changeOrigin: true,
|
|
|
|
|
+ },
|
|
|
|
|
+ '/Upload': {
|
|
|
|
|
+ target: apiProxyTarget,
|
|
|
|
|
+ changeOrigin: true,
|
|
|
|
|
+ },
|
|
|
|
|
+ '/hubs': {
|
|
|
|
|
+ target: apiProxyTarget,
|
|
|
changeOrigin: true,
|
|
changeOrigin: true,
|
|
|
|
|
+ ws: true,
|
|
|
},
|
|
},
|
|
|
- '^/[Uu]pload': {
|
|
|
|
|
- target: env.VITE_API_URL,
|
|
|
|
|
|
|
+ '/schedule': {
|
|
|
|
|
+ target: apiProxyTarget,
|
|
|
changeOrigin: true,
|
|
changeOrigin: true,
|
|
|
},
|
|
},
|
|
|
},
|
|
},
|
|
@@ -92,23 +117,26 @@ const viteConfig = defineConfig((mode: ConfigEnv) => {
|
|
|
assetsInlineLimit: 5000, // 小于此阈值的导入或引用资源将内联为 base64 编码
|
|
assetsInlineLimit: 5000, // 小于此阈值的导入或引用资源将内联为 base64 编码
|
|
|
sourcemap: false, // 构建后是否生成 source map 文件
|
|
sourcemap: false, // 构建后是否生成 source map 文件
|
|
|
extractComments: false, // 移除注释
|
|
extractComments: false, // 移除注释
|
|
|
- minify: 'terser', // 启用后 terserOptions 配置才有效
|
|
|
|
|
- terserOptions: {
|
|
|
|
|
- compress: {
|
|
|
|
|
- drop_console: true, // 生产环境时移除console
|
|
|
|
|
- drop_debugger: true,
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ // esbuild 压缩内存占用远低于 terser;maxParallelFileOps 降低并发,便于小内存环境完成构建
|
|
|
|
|
+ minify: 'esbuild',
|
|
|
|
|
+ esbuild: {
|
|
|
|
|
+ drop: mode.mode === 'production' ? ['console', 'debugger'] : [],
|
|
|
},
|
|
},
|
|
|
rollupOptions: {
|
|
rollupOptions: {
|
|
|
output: {
|
|
output: {
|
|
|
chunkFileNames: 'assets/js/[name]-[hash].js', // 引入文件名的名称
|
|
chunkFileNames: 'assets/js/[name]-[hash].js', // 引入文件名的名称
|
|
|
entryFileNames: 'assets/js/[name]-[hash].js', // 包的入口文件名称
|
|
entryFileNames: 'assets/js/[name]-[hash].js', // 包的入口文件名称
|
|
|
assetFileNames: 'assets/[ext]/[name]-[hash].[ext]', // 资源文件像 字体,图片等
|
|
assetFileNames: 'assets/[ext]/[name]-[hash].[ext]', // 资源文件像 字体,图片等
|
|
|
- manualChunks(id) {
|
|
|
|
|
- if (id.includes('node_modules')) {
|
|
|
|
|
- return id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups!.moduleName ?? 'vender';
|
|
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ // 小内存环境构建时可去掉 manualChunks,降低 Rollup 分析内存(大机器可恢复分包策略)
|
|
|
|
|
+ ...(process.env.VITE_LOW_MEM_BUILD === '1'
|
|
|
|
|
+ ? {}
|
|
|
|
|
+ : {
|
|
|
|
|
+ manualChunks(id: string) {
|
|
|
|
|
+ if (id.includes('node_modules')) {
|
|
|
|
|
+ return id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups!.moduleName ?? 'vender';
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ }),
|
|
|
},
|
|
},
|
|
|
...(JSON.parse(env.VITE_OPEN_CDN) ? { external: buildConfig.external } : {}),
|
|
...(JSON.parse(env.VITE_OPEN_CDN) ? { external: buildConfig.external } : {}),
|
|
|
},
|
|
},
|