From 4cca00717692ab8b68a1c7dd985a2821a28ec5db Mon Sep 17 00:00:00 2001 From: vben Date: Fri, 4 Dec 2020 23:35:58 +0800 Subject: [PATCH] test(code-split): code split optimization --- CHANGELOG.zh_CN.md | 1 + build/vite/optimizer.ts | 20 ++++++++++++++++++++ vite.config.ts | 23 ++++++++++++++++++----- 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 build/vite/optimizer.ts diff --git a/CHANGELOG.zh_CN.md b/CHANGELOG.zh_CN.md index 71279d66..54f79d3b 100644 --- a/CHANGELOG.zh_CN.md +++ b/CHANGELOG.zh_CN.md @@ -10,6 +10,7 @@ - 还原 antdv 默认 loading,重构 `Loading` 组件,增加`useLoading`和`v-loading`指令。并增加示例 - i18n 支持 vscode `i18n-ally`插件 - 新增多级路由缓存示例 +- 打包代码拆分(试验) ### ⚡ Performance Improvements diff --git a/build/vite/optimizer.ts b/build/vite/optimizer.ts new file mode 100644 index 00000000..ca3425ca --- /dev/null +++ b/build/vite/optimizer.ts @@ -0,0 +1,20 @@ +import type { GetManualChunk, GetManualChunkApi } from 'rollup'; + +// +const vendorLibs: { match: string[]; output: string }[] = [ + { + match: ['xlsx'], + output: 'xlsx', + }, +]; + +// @ts-ignore +export const configManualChunk: GetManualChunk = (id: string, api: GetManualChunkApi) => { + if (/[\\/]node_modules[\\/]/.test(id)) { + const matchItem = vendorLibs.find((item) => { + const reg = new RegExp(`[\\/]node_modules[\\/]_?(${item.match.join('|')})(.*)`, 'ig'); + return reg.test(id); + }); + return matchItem ? matchItem.output : null; + } +}; diff --git a/vite.config.ts b/vite.config.ts index 9fe937e6..57d67ce9 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,6 +3,7 @@ import { resolve } from 'path'; import { modifyVars } from './build/config/lessModifyVars'; import { createProxy } from './build/vite/proxy'; +import { configManualChunk } from './build/vite/optimizer'; import globbyTransform from './build/vite/plugin/transform/globby'; import dynamicImportTransform from './build/vite/plugin/transform/dynamic-import'; @@ -53,9 +54,11 @@ const viteConfig: UserConfig = { // terser options terserOptions: { compress: { + keep_infinity: true, drop_console: VITE_DROP_CONSOLE, }, }, + define: { __VERSION__: pkg.version, // setting vue-i18-next @@ -64,12 +67,14 @@ const viteConfig: UserConfig = { __VUE_I18N_FULL_INSTALL__: false, __INTLIFY_PROD_DEVTOOLS__: false, }, + cssPreprocessOptions: { less: { modifyVars: modifyVars, javascriptEnabled: true, }, }, + // The package will be recompiled using rollup, and the new package compiled into the esm module specification will be put into node_modules/.vite_opt_cache optimizeDeps: { include: [ @@ -80,11 +85,6 @@ const viteConfig: UserConfig = { ], }, - proxy: createProxy(VITE_PROXY), - plugins: createVitePlugins(viteEnv), - rollupInputOptions: { - plugins: createRollupPlugin(), - }, transforms: [ globbyTransform({ resolvers: resolvers, @@ -94,6 +94,19 @@ const viteConfig: UserConfig = { }), dynamicImportTransform(VITE_DYNAMIC_IMPORT), ], + + proxy: createProxy(VITE_PROXY), + + plugins: createVitePlugins(viteEnv), + + rollupInputOptions: { + plugins: createRollupPlugin(), + }, + + rollupOutputOptions: { + compact: true, + manualChunks: configManualChunk, + }, }; export default viteConfig;