050:vue项目webpack打包,大文件分成几个小文件的方法
专栏目标
在vue和element UI联合技术栈的操控下,本专栏提供行之有效的源代码示例和信息点介绍,做到灵活运用。
(1)提供vue2的一些基本操作:安装、引用,模板使用,computed,watch,生命周期(beforeCreate,created,beforeMount,mounted, beforeUpdate,updated, beforeDestroy,destroyed,activated,deactivated,errorCaptured,components,)、 $root , $parent , $children , $slots , $refs , props, $emit , eventbus ,provide / inject, Vue.observable, $listeners, $attrs, $nextTick , v-for, v-if, v-else,v-else-if,v-on,v-pre,v-cloak,v-once,v-model, v-html, v-text, keep-alive,slot-scope, filters, v-bind,.stop, .native, directives,mixin,render,国际化,Vue Router等
(2)提供element UI的经典操作:安装,引用,国际化,el-row,el-col,el-button,el-link,el-radio,el-checkbox ,el-input,el-select, el-cascader, el-input-number, el-switch,el-slider, el-time-picker, el-date-picker, el-upload, el-rate, el-color-picker, el-transfer, el-form, el-table, el-tree, el-pagination,el-badge,el-avatar,el-skeleton, el-empty, el-descriptions, el-result, el-statistic, el-alert, v-loading, $message, $alert, $prompt, $confirm , $notify, el-breadcrumb, el-page-header,el-tabs ,el-dropdown,el-steps,el-dialog, el-tooltip, el-popover, el-popconfirm, el-card, el-carousel, el-collapse, el-timeline, el-divider, el-calendar, el-image, el-backtop,v-infinite-scroll, el-drawer等
应用场景
vue项目中,不做处理,通常首页文件比较大,影像加载速度。解决办法是通过webpack将大文件分解为小文件,具体的一个vue.config.js文件参考。
安装插件
npm install --save-dev webpack-bundle-analyze
vue.config.js 配置
onst BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = {
publicPath: './',
assetsDir: 'static',
productionSourceMap: false,
devServer: {
proxy: {
'/api':{
target:'http://xxx.xxx.xxx.xxx:1234',
changeOrigin:true,
pathRewrite:{
'^/api':'/api'
}
}
}
},
chainWebpack: config => {
if (process.env.NODE_ENV == 'production') {
// 移除 prefetch 插件
config.plugins.delete('prefetch');
}
},
configureWebpack: (config) => {
const res = {
plugins: []
};
if (process.env.NODE_ENV == 'production') {
res.plugins.push(new BundleAnalyzerPlugin()),
res.optimization = {
splitChunks: {
// chunks: 'async',
// minSize: 300000,
maxSize: 650000,
// maxAsyncRequests: 5,
// maxInitialRequests: 3,
cacheGroups: {
vendors: {
name: `chunk-vendors`,
test: /[\\/]node_modules[\\/]/,
chunks: 'initial',
priority: 2,
reuseExistingChunk: true,
enforce: true
},
common: {
name: `chunk-common`,
chunks: 'initial',
minChunks: 2,
maxInitialRequests: 5,
minSize: 0,
priority: 1,
reuseExistingChunk: true,
enforce: true
},
elementui: {
test:/[\\/]node_modules[\\/]element-ui[\\/]/, //匹配要拆分的模块
name: 'chunk-elementui',
chunks: 'all',
priority: 3,
reuseExistingChunk: true,
enforce: true
},
echarts: {
test: /[\\/]node_modules[\\/](vue-)?echarts[\\/]/,
name: 'chunk-echarts',
chunks: 'all',
priority: 4,
reuseExistingChunk: true,
enforce: true
},
xlsx: {
test: /[\\/]node_modules[\\/]xlsx[\\/]/,
name: 'chunk-xlsx',
chunks: 'all',
priority: 3,
reuseExistingChunk: true,
enforce: true
}
}
}
}
}
return {
...res
}
},
// css相关配置
css: {
extract: true, // 是否使用css分离插件 ExtractTextPlugin
sourceMap: false, // 开启 CSS source maps?
loaderOptions: {
css: {
}, // 这里的选项会传递给 css-loader
}, // css预设器配置项 详见https://cli.vuejs.org/zh/config/#css-loaderoptions
modules: false // 启用 CSS modules for all css / pre-processor files.
},
}
webpack-bundle-analyzer 是什么?
webpack-bundle-analyzer 是 webpack 的插件,需要配合 webpack 和 webpack-cli 一起使用。这个插件可以读取输出文件夹(通常是 dist)中的 stats.json 文件,把该文件可视化展现,生成代码分析报告,可以直观地分析打包出的文件有哪些,及它们的大小、占比情况、各文件 Gzipped 后的大小、模块包含关系、依赖项等,对应做出优化,从而帮助提升代码质量和网站性能。
相关API
https://www.npmjs.com/package/webpack-bundle-analyzer
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!