-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vite-plugin-svg-icons插件的svg打包出来的js模块占用很大,我怎么把它变小或者异步加载 #89
Comments
单独打包 |
怎么单独打包呀 |
|
在这个插件里如何实现“每个 svg 做成单个的图标组件,然后按需引入“呀?还有个问题就是打包会特别慢 |
自定义拆包。首先是路由懒加载,如home.vue和about.vue,那么只有点击home.vue才会请求home.js的文件,只有点击about.vue才会请求about.js的文件。这个是打包自带的。 |
不行 做了路由懒加载,但是所有的包都会被引入到入口包内 |
试试在vite.config.js中配置,可以把svg单独打包,不绑定在主包中 build: {
rollupOptions: {
treeshake: true,
output: {
// 根据不同的js库 拆分包,减少index.js包体积
manualChunks(id) {
if(id.includes('svg-icons-register')){
return "svg-icons"
}
},
},
}
}, |
有个问题,如果 svg 越来越多,这个 svg-icons.js 也会越来越大 |
高频使用的话,还不如单个引入,不要拼接,长远看,还是使用或者自建支持按需引入的图标库靠谱。 rollupOptions: {
output: {
// https://rollupjs.org/configuration-options/#output-manualchunks
manualChunks(id: string) {
if (id.indexOf('node_modules') !== -1) {
const basic = id.toString().split('node_modules/')[1]
const sub1 = basic.split('/')[0]
if (sub1 !== '.pnpm') {
return sub1.toString()
}
const name2 = basic.split('/')[1]
return name2.split('@')[name2[0] === '@' ? 1 : 0].toString()
}
},
entryFileNames() {
return 'assets/app.[hash].js'
},
assetFileNames(chunk: any) {
const chunkName = chunk.name || ''
// svg
if (/.svg$/.test(chunkName)) {
return 'assets/svg/[name].[hash].[ext]'
}
return 'assets/others/[name].[hash].[ext]'
}
},
} |
有毒啊 |
No description provided.
The text was updated successfully, but these errors were encountered: