Skip to content

Commit ce9300d

Browse files
jialudevhiveer
andauthored
config: optimize packaging speed: (#806)
* config: optimize packaging speed: * config: updated based on code review * Refactor: update vite config options order --------- Co-authored-by: Hiveer <[email protected]>
1 parent 676a801 commit ce9300d

File tree

1 file changed

+117
-62
lines changed

1 file changed

+117
-62
lines changed

frontend/vite.config.js

Lines changed: 117 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,132 @@
1-
import { defineConfig } from 'vite'
2-
import path from 'path'
3-
import fs from 'fs'
4-
import vue from '@vitejs/plugin-vue'
5-
6-
// https://vitejs.dev/config/
7-
export default defineConfig((configEnv) => {
8-
return {
9-
plugins: [vue()],
10-
build: {
11-
rollupOptions: {
12-
input: getHtmlEntryFiles('src'),
13-
output: {
14-
manualChunks(id) {
15-
// console.log('Processing:', id);
16-
if (id.includes('node_modules')) {
17-
if (id.includes('lodash')) {
18-
return 'lodash';
19-
}
20-
if (id.includes('element-plus')) {
21-
return 'element-plus';
22-
}
23-
if (id.includes('highlight.js')) {
24-
return 'highlightjs';
25-
}
26-
return 'vendor'
27-
}
28-
if (id.includes('src/components')) {
29-
return 'components'
30-
}
31-
}
32-
}
33-
},
34-
emptyOutDir: true,
35-
sourcemap: configEnv.mode === 'development',
36-
minify: 'terser',
37-
terserOptions: {
38-
compress: {
39-
drop_console: configEnv.mode !== 'development', // Remove console logs
40-
drop_debugger: configEnv.mode !== 'development' // Remove debugger statements
41-
}
42-
}
43-
},
44-
resolve: {
45-
alias: {
46-
'vue': 'vue/dist/vue.esm-bundler.js',
47-
'@': path.resolve(__dirname, 'src')
48-
}
49-
}
50-
}
51-
})
1+
import { defineConfig } from "vite";
2+
import path from "path";
3+
import fs from "fs";
4+
import vue from "@vitejs/plugin-vue";
525

536
function getHtmlEntryFiles(srcDir) {
54-
const entry = {}
7+
const entry = {};
558

569
function traverseDir(currentDir) {
57-
const files = fs.readdirSync(currentDir)
10+
const files = fs.readdirSync(currentDir);
5811

5912
files.forEach((file) => {
60-
const filePath = path.join(currentDir, file)
61-
const isDirectory = fs.statSync(filePath).isDirectory()
13+
const filePath = path.join(currentDir, file);
14+
const isDirectory = fs.statSync(filePath).isDirectory();
6215

6316
if (isDirectory) {
6417
// If it's a directory, recursively traverse it
65-
traverseDir(filePath)
66-
} else if (path.extname(file) === '.html') {
18+
traverseDir(filePath);
19+
} else if (path.extname(file) === ".html") {
6720
// If it's an HTML file, add it to the entry object
68-
const name = path.relative(srcDir, filePath).replace(/\..*$/, '')
69-
entry[name] = filePath
21+
const name = path.relative(srcDir, filePath).replace(/\..*$/, "");
22+
entry[name] = filePath;
7023
}
71-
})
24+
});
7225
}
7326

74-
traverseDir(srcDir)
27+
traverseDir(srcDir);
7528

76-
return entry
29+
return entry;
7730
}
31+
32+
// 开发环境打包配置
33+
const DEV_CONFIG = {
34+
plugins: [vue()],
35+
optimizeDeps: {
36+
disabled: true,
37+
},
38+
build: {
39+
rollupOptions: {
40+
treeshake: false,
41+
input: getHtmlEntryFiles("src"),
42+
output: {
43+
manualChunks(id) {
44+
if (id.includes("node_modules")) {
45+
if (id.includes("lodash")) {
46+
return "lodash";
47+
}
48+
if (id.includes("element-plus")) {
49+
return "element-plus";
50+
}
51+
if (id.includes("highlight.js")) {
52+
return "highlightjs";
53+
}
54+
return "vendor";
55+
}
56+
if (id.includes("src/components")) {
57+
return "components";
58+
}
59+
},
60+
},
61+
},
62+
emptyOutDir: false,
63+
reportCompressedSize: false,
64+
chunkSizeWarningLimit: 5000,
65+
modulePreload: false,
66+
cssCodeSplit: false,
67+
sourcemap: true,
68+
minify: false,
69+
watch: {
70+
exclude: "node_modules/**",
71+
},
72+
},
73+
resolve: {
74+
alias: {
75+
vue: "vue/dist/vue.esm-bundler.js",
76+
"@": path.resolve(__dirname, "src"),
77+
},
78+
// 优化模块解析
79+
dedupe: ["vue"],
80+
},
81+
};
82+
83+
// 生产环境打包配置
84+
const PROD_CONFIG = {
85+
plugins: [vue()],
86+
build: {
87+
rollupOptions: {
88+
input: getHtmlEntryFiles("src"),
89+
output: {
90+
manualChunks(id) {
91+
if (id.includes("node_modules")) {
92+
if (id.includes("lodash")) {
93+
return "lodash";
94+
}
95+
if (id.includes("element-plus")) {
96+
return "element-plus";
97+
}
98+
if (id.includes("highlight.js")) {
99+
return "highlightjs";
100+
}
101+
return "vendor";
102+
}
103+
if (id.includes("src/components")) {
104+
return "components";
105+
}
106+
},
107+
},
108+
},
109+
emptyOutDir: true,
110+
minify: "terser",
111+
terserOptions: {
112+
compress: {
113+
drop_console: true, // Remove console logs
114+
drop_debugger: true, // Remove debugger statements
115+
},
116+
},
117+
},
118+
resolve: {
119+
alias: {
120+
vue: "vue/dist/vue.esm-bundler.js",
121+
"@": path.resolve(__dirname, "src"),
122+
},
123+
},
124+
};
125+
126+
// https://vitejs.dev/config/
127+
export default defineConfig((configEnv) => {
128+
const isDev = configEnv.mode === "development";
129+
// Production: optimize for code quality and bundle size
130+
// Development: optimize for build speed
131+
return isDev ? DEV_CONFIG : PROD_CONFIG;
132+
});

0 commit comments

Comments
 (0)