feat(vite): 调整构建输出路径并增强 manualChunks 分包逻辑
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import type { ManualChunkMeta, PreRenderedAsset } from 'rollup';
|
||||
import { fileURLToPath, URL } from 'node:url';
|
||||
import { createViteProxy } from 'utils4u/vite';
|
||||
import { defineConfig, loadEnv } from 'vite';
|
||||
@@ -5,7 +6,7 @@ import { optimizeDeps } from './vite.config.optimizeDeps';
|
||||
import { Plugins } from './vite.config.plugins';
|
||||
|
||||
// https://vite.dev/config/
|
||||
export default defineConfig(({ command, mode }) => {
|
||||
export default defineConfig(async ({ command, mode }) => {
|
||||
const isBuild = command === 'build';
|
||||
const env = loadEnv(mode, process.cwd());
|
||||
|
||||
@@ -21,29 +22,82 @@ export default defineConfig(({ command, mode }) => {
|
||||
if (warning.code === 'EVAL' && warning.id?.includes('node_modules/protobufjs')) return;
|
||||
warn(warning);
|
||||
}, */
|
||||
|
||||
output: {
|
||||
// Keep hashed file names predictable across entry, chunk, and asset outputs.
|
||||
entryFileNames: 'assets/[name].[hash].js',
|
||||
chunkFileNames: 'assets/[name].[hash].js',
|
||||
entryFileNames: '_entry/[name].[hash].js',
|
||||
chunkFileNames: '_chunk/[name].[hash].js',
|
||||
// https://cn.rollupjs.org/configuration-options/#output-assetfilenames
|
||||
assetFileNames: (assetInfo) => {
|
||||
if (assetInfo.names.length > 1) {
|
||||
console.warn('Multiple names for asset:', assetInfo);
|
||||
assetFileNames(chunkInfo: PreRenderedAsset) {
|
||||
const names = chunkInfo.names;
|
||||
|
||||
if (names.length !== 1) {
|
||||
console.error('Multiple names for asset:', chunkInfo);
|
||||
process.exit(1);
|
||||
}
|
||||
const assetName =
|
||||
assetInfo.names.find(Boolean) ?? assetInfo.originalFileNames.find(Boolean) ?? '';
|
||||
|
||||
const assetName = names[0];
|
||||
const ext = assetName.split('.').pop()?.toLowerCase();
|
||||
if (ext && /png|jpe?g|gif|svg|webp|avif/.test(ext)) {
|
||||
return 'assets/images/[name].[hash][extname]';
|
||||
return 'chunks/images/[name].[hash][extname]';
|
||||
}
|
||||
if (ext && /woff2?|ttf|otf/.test(ext)) {
|
||||
return 'assets/fonts/[name].[hash][extname]';
|
||||
return 'chunks/fonts/[name].[hash][extname]';
|
||||
}
|
||||
if (ext === 'css') {
|
||||
return 'assets/css/[name].[hash][extname]';
|
||||
return 'chunks/css/[name].[hash][extname]';
|
||||
}
|
||||
return '_chunks/[name].[hash][extname]';
|
||||
},
|
||||
|
||||
// https://www.npmjs.com/package/utils4u/v/2.19.2?activeTab=code
|
||||
manualChunks: (id: string, meta: ManualChunkMeta) => {
|
||||
if (id.includes('node_modules')) {
|
||||
// 处理 pnpm 的特殊路径结构
|
||||
let packageName;
|
||||
if (id.includes('.pnpm')) {
|
||||
// pnpm 路径: .pnpm/naive-ui@2.43.1_vue@3.5.22/node_modules/naive-ui/...
|
||||
const pnpmMatch = id.match(/\.pnpm\/(.+?)@/);
|
||||
if (pnpmMatch) {
|
||||
packageName = pnpmMatch[1];
|
||||
}
|
||||
} else {
|
||||
// 普通路径: node_modules/lodash/...
|
||||
const normalMatch = id.match(/node_modules\/(@[^/]+\/[^/]+|[^/]+)\//);
|
||||
if (normalMatch) {
|
||||
packageName = normalMatch[1];
|
||||
}
|
||||
}
|
||||
|
||||
if (packageName) {
|
||||
// 根据包名分组
|
||||
if (packageName.includes('naive-ui')) {
|
||||
return 'naive-ui';
|
||||
}
|
||||
if (packageName.includes('lodash')) {
|
||||
return 'lodash';
|
||||
}
|
||||
if (packageName.includes('@juggle+resize-observer')) {
|
||||
return 'resize-observer';
|
||||
}
|
||||
if (packageName.includes('date-fns')) {
|
||||
return 'date-fns';
|
||||
}
|
||||
if (
|
||||
['primelocale', 'primevue', '@primeuix'].some((name) =>
|
||||
packageName!.includes(name),
|
||||
)
|
||||
) {
|
||||
return 'primevue';
|
||||
}
|
||||
// console.log('packageName :>> ', packageName);
|
||||
// console.log('id :>> ', id);
|
||||
if (['vue', 'vue-router', 'pinia', 'vue-demi'].includes(packageName)) {
|
||||
return 'vue-vendor';
|
||||
}
|
||||
|
||||
// return 'vendor';
|
||||
}
|
||||
}
|
||||
return 'assets/[name].[hash][extname]';
|
||||
},
|
||||
// // Split key dependency groups to improve long-term caching.
|
||||
// manualChunks: (id) => {
|
||||
|
||||
Reference in New Issue
Block a user