-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
121 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,136 @@ | ||
import { defineConfig } from 'vite' | ||
import path from 'path' | ||
import fs from 'fs' | ||
import vue from '@vitejs/plugin-vue' | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig((configEnv) => { | ||
return { | ||
plugins: [vue()], | ||
build: { | ||
rollupOptions: { | ||
input: getHtmlEntryFiles('src'), | ||
output: { | ||
manualChunks(id) { | ||
// console.log('Processing:', id); | ||
if (id.includes('node_modules')) { | ||
if (id.includes('lodash')) { | ||
return 'lodash'; | ||
} | ||
if (id.includes('element-plus')) { | ||
return 'element-plus'; | ||
} | ||
if (id.includes('highlight.js')) { | ||
return 'highlightjs'; | ||
} | ||
return 'vendor' | ||
} | ||
if (id.includes('src/components')) { | ||
return 'components' | ||
} | ||
} | ||
} | ||
}, | ||
emptyOutDir: true, | ||
sourcemap: configEnv.mode === 'development', | ||
minify: 'terser', | ||
terserOptions: { | ||
compress: { | ||
drop_console: configEnv.mode !== 'development', // Remove console logs | ||
drop_debugger: configEnv.mode !== 'development' // Remove debugger statements | ||
} | ||
} | ||
}, | ||
resolve: { | ||
alias: { | ||
'vue': 'vue/dist/vue.esm-bundler.js', | ||
'@': path.resolve(__dirname, 'src') | ||
} | ||
} | ||
} | ||
}) | ||
import { defineConfig } from "vite"; | ||
import path from "path"; | ||
import fs from "fs"; | ||
import vue from "@vitejs/plugin-vue"; | ||
|
||
function getHtmlEntryFiles(srcDir) { | ||
const entry = {} | ||
const entry = {}; | ||
|
||
function traverseDir(currentDir) { | ||
const files = fs.readdirSync(currentDir) | ||
const files = fs.readdirSync(currentDir); | ||
|
||
files.forEach((file) => { | ||
const filePath = path.join(currentDir, file) | ||
const isDirectory = fs.statSync(filePath).isDirectory() | ||
const filePath = path.join(currentDir, file); | ||
const isDirectory = fs.statSync(filePath).isDirectory(); | ||
|
||
if (isDirectory) { | ||
// If it's a directory, recursively traverse it | ||
traverseDir(filePath) | ||
} else if (path.extname(file) === '.html') { | ||
traverseDir(filePath); | ||
} else if (path.extname(file) === ".html") { | ||
// If it's an HTML file, add it to the entry object | ||
const name = path.relative(srcDir, filePath).replace(/\..*$/, '') | ||
entry[name] = filePath | ||
const name = path.relative(srcDir, filePath).replace(/\..*$/, ""); | ||
entry[name] = filePath; | ||
} | ||
}) | ||
}); | ||
} | ||
|
||
traverseDir(srcDir) | ||
traverseDir(srcDir); | ||
|
||
return entry | ||
return entry; | ||
} | ||
|
||
// 开发环境打包配置 | ||
const DEV_CONFIG = { | ||
plugins: [vue()], | ||
optimizeDeps: { | ||
noTreeShake: true, | ||
disabled: false, | ||
}, | ||
build: { | ||
treeshake: false, | ||
emptyOutDir: false, | ||
reportCompressedSize: false, | ||
chunkSizeWarningLimit: 5000, | ||
rollupOptions: { | ||
treeshake: false, | ||
maxParallelFileOps: 3, | ||
input: getHtmlEntryFiles("src"), | ||
output: { | ||
manualChunks(id) { | ||
if (id.includes("node_modules")) { | ||
if (id.includes("lodash")) { | ||
return "lodash"; | ||
} | ||
if (id.includes("element-plus")) { | ||
return "element-plus"; | ||
} | ||
if (id.includes("highlight.js")) { | ||
return "highlightjs"; | ||
} | ||
return "vendor"; | ||
} | ||
if (id.includes("src/components")) { | ||
return "components"; | ||
} | ||
}, | ||
}, | ||
}, | ||
modulePreload: false, | ||
cssCodeSplit: false, | ||
sourcemap: false, | ||
minify: false, | ||
brotliSize: false, | ||
watch: { | ||
ignored: ["node_modules/**", "dist/**"], | ||
}, | ||
}, | ||
resolve: { | ||
alias: { | ||
vue: "vue/dist/vue.esm-bundler.js", | ||
"@": path.resolve(__dirname, "src"), | ||
}, | ||
// 优化模块解析 | ||
dedupe: ["vue"], | ||
}, | ||
}; | ||
|
||
// 生产环境打包配置 | ||
const PROD_CONFIG = { | ||
plugins: [vue()], | ||
build: { | ||
rollupOptions: { | ||
input: getHtmlEntryFiles("src"), | ||
output: { | ||
manualChunks(id) { | ||
if (id.includes("node_modules")) { | ||
if (id.includes("lodash")) { | ||
return "lodash"; | ||
} | ||
if (id.includes("element-plus")) { | ||
return "element-plus"; | ||
} | ||
if (id.includes("highlight.js")) { | ||
return "highlightjs"; | ||
} | ||
return "vendor"; | ||
} | ||
if (id.includes("src/components")) { | ||
return "components"; | ||
} | ||
}, | ||
}, | ||
}, | ||
emptyOutDir: true, | ||
sourcemap: false, | ||
minify: "terser", | ||
terserOptions: { | ||
compress: { | ||
drop_console: true, // Remove console logs | ||
drop_debugger: true, // Remove debugger statements | ||
}, | ||
}, | ||
}, | ||
resolve: { | ||
alias: { | ||
vue: "vue/dist/vue.esm-bundler.js", | ||
"@": path.resolve(__dirname, "src"), | ||
}, | ||
}, | ||
}; | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig((configEnv) => { | ||
const isDev = configEnv.mode === "development"; | ||
// 将生产和开发 配置区分,生产注重代码质量和包体积,开发注重编译速度 | ||
return isDev ? DEV_CONFIG : PROD_CONFIG; | ||
}); |