随着web应用功能愈来愈复杂,模块打包后体积愈来愈大,这样会带来两个问题:html
webpack支持三种代码分割方式分别是:vue
对应的vue中也有三种对应的解决方式:react
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
export default new Router({
mode: 'history',
routes: [
{
path: '/vue-async',
name: 'vueAsync',
component: resolve => require(['@/components/VueAsync'], resolve)
}
]
})
复制代码
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
export default new Router({
mode: 'history',
routes: [
{
path: '/require-ensure'
name: 'RequireEnsure',
component: r => require.ensure([], () => r(require('@/components/RequireEnsure'), 'ensure'))
}
]
})
复制代码
import Vue from 'vue'
import Router from 'vue-router'
const EsImport = () => import('@/components/EsImport')
Vue.use(Router)
export default new Router({
mode: 'history',
routes: [
{
path: '/es-import',
name: 'EsImport',
component: EsImport
}
]
})
复制代码
第三种方式比较简单,并且代码组织也比较明显,因此也更常被使用jquery
CommonsChunkPlugin 插件,是一个可选的用于创建一个独立文件(又称做 chunk)的功能,这个文件包括多个入口 chunk 的公共模块。简单来讲CommonsChunkPlugin主要是用来提取第三方库和公共模块,避免首屏加载的bundle文件或者按需加载的bundle文件体积过大,从而致使加载时间过长,着实是优化的一把利器。webpack
使用 CommonsChunkPlugin,设置 minChunks 为 Infinity 可用于打包此类代码,但缺点也是比较明显的:git
const path = require('path')
const webpack = require('webpack')
const AssetsPlugin = require('assets-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
module.exports = {
entry: {
libs: [
'vue/dist/vue.esm.js',
'vue-router',
'vuex',
'element-ui'
]
},
output: {
path: path.resolve(__dirname, '../static'),
filename: '[name].[chunkhash:7].js',
library: '[name]_library'
},
plugins: [
new webpack.DllPlugin({
path: path.resolve(__dirname, '../static/[name]-manifest.json'),
name: '[name]_library',
context: __dirname
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
new AssetsPlugin({
filename: 'bundle-config.json',
path: './static'
}),
new CleanWebpackPlugin(['static'], {
root: path.join(__dirname, '../'),
verbose: true,
dry: false
})
]
}
复制代码
const path = require('path')
const webpack = require('webpack')
const webpackDll = require('./webpack.dll.conf')
const chalk = require('chalk')
const ora = require('ora')
const rm = require('rimraf')
const spinner = ora({
color: 'green',
text: 'Dll生产中'
})
spinner.start()
rm(path.resolve(__dirname, '../static'), err => {
if(err) throw err
webpack(webpackDll, (err, status) => {
spinner.stop()
if(err) throw err
process.stdout.write(status.toString({
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false
}) + '\n\n')
})
console.log(chalk.cyan('dll success'))
})
复制代码
// 添加plugins
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('../static/libs-manifest.json')
})
]
复制代码
const bundleConfig = require("../static/bundle-config.json")//调入生成的的路径json
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true,
libJsName: bundleConfig.libs.js
}),
复制代码
const bundleConfig = require("../static/bundle-config.json")//调入生成的的路径json
new HtmlWebpackPlugin({
...省略号...
libJsName: bundleConfig.libs.js
...省略号...
})
复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>vue-multipage</title>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
<script src="./static/<%= htmlWebpackPlugin.options.libJsName %>"></script>
</body>
</html>
复制代码
参考文章:github