转战掘金第一帖,原连接 zhihu,GitHub地址dllPlugin_vux_multipagejavascript
技术框架:Android hybrid + vue H5(多页)css
问题: 项目参照网上多页vue的配置自定义入口和打包。但问题就是多页致使项目开发编译和打包速度很慢,有一部分缘由来自设备太垃圾。html
体会一下官网的说法vue
版本java
"vue": "2.5.13",
"vue-loader": "^13.3.0",
"vux": "^2.9.2",
"vux-loader": "^1.1.30",
"webpack": "^3.6.0",
复制代码
安装插件node
assets-webpack-plugin
clean-webpack-plugin
复制代码
因为各类vue-cli版本区别,可能插件有出入,少了就安装就好webpack
package.json 配置 启动命令ios
"scripts": {
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
"dll": "node build/buildDll.js"
},
复制代码
buildDll.js 模仿build.jsgit
const ora = require('ora')
const rm = require('rimraf')
const path = require('path');
const chalk = require('chalk')
const webpack = require('webpack');
const dllConfig = require('./webpack.dll.config.js');
const spinner = ora({
color: 'green',
text: 'Dll生产中...'
})
spinner.start()
rm(path.resolve(__dirname, '../dll'), err => {
if (err) throw err
webpack(dllConfig, function (err, stats) {
spinner.stop()
if (err) throw err
process.stdout.write(stats.toString({
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false
}) + '\n\n')
console.log(chalk.cyan(' dll succeed !.\n'))
})
});
复制代码
webpack.dll.config.jsgithub
const path = require('path');
const webpack = require('webpack'); //调用webpack内置DllPlugin插件
const ExtractTextPlugin = require('extract-text-webpack-plugin'); // 提取css
const AssetsPlugin = require('assets-webpack-plugin'); // 生成文件名,配合HtmlWebpackPlugin增长打包后dll的缓存
const CleanWebpackPlugin = require('clean-webpack-plugin'); //清空文件夹
const vueLoaderConfig = require('./vue-loader.conf');
const vuxLoader = require('vux-loader');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; // 统计
const dllConfig = {
entry: {
// libs: ['vconsole/dist/vconsole.min.js', 'axios', 'qs']
libs: ['sockjs-client/dist/sockjs.js', 'better-scroll/dist/bscroll.esm.js', 'vconsole/dist/vconsole.min.js', 'axios', 'qs', 'vux/index.js']
},
output: {
path: path.resolve(__dirname, '../dll'),
filename: '[name].[chunkhash:7].js',
library: '[name]_library'
},
resolve: {
extensions: ['.js', '.vue', '.json', '.scss'],
},
plugins: [
new webpack.DllPlugin({
path: path.resolve(__dirname, '../dll/[name]-mainfest.json'),
name: '[name]_library',
context: __dirname // 执行的上下文环境,对以后DllReferencePlugin有用
}),
new ExtractTextPlugin('[name].[contenthash:7].css'),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
}),
new AssetsPlugin({
filename: 'bundle-config.json',
path: './dll'
}),
new CleanWebpackPlugin(['dll'], {
root: path.join(__dirname, '../'), // 绝对路径
verbose: true, // 是否显示到控制台
dry: false // 不删除全部
}),
new BundleAnalyzerPlugin(), // 使用统计
],
module: {
rules: [{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
}, {
test: /\.js$/,
loader: 'babel-loader',
include: path.resolve(__dirname, './node_modules/vux'),
// exclude: path.resolve(__dirname, './node_modules/vux'),
}, {
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
options: {
minimize: true //启用压缩
}
}]
})
}, {
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
query: {
limit: 10000,
name: 'img/[name].[hash:7].[ext]'
}
}, {
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
query: {
limit: 10000,
name: 'fonts/[name].[hash:7].[ext]'
}
}]
},
};
module.exports = vuxLoader.merge(dllConfig, {
plugins: [
'vux-ui', // v-cart/mixin.js 须要这个插件
// 'duplicate-style',
] // 数组方式插件配置
});
复制代码
修改webpack.dev.conf.js
配置 CopyWebpackPlugin,DllReferencePlugin
// ...
plugins: [
// ...
new CopyWebpackPlugin([
/* ... */
/* dll使用 */
{
from: path.resolve(__dirname, '../dll'), // dll设置,改成dll
to: config.dev.assetsSubDirectory,
ignore: ['.*']
}
]),
new vConsolePlugin({
filter: [], // 须要过滤的入口文件
enable: true // 发布代码前记得改回 false
}),
/* mhs dll使用, 连接dll */
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('../dll/libs-mainfest.json') // 指向生成的manifest.json
})
]
// ...
复制代码
在配置HtmlWebpackPlugin 的文件中配置路径
// ...
try {
bundleConfig = require('../dll/bundle-config.json');
} catch(err) {
bundleConfig = '';
}
// ...
return new HtmlWebpackPlugin({ // 返回一个HtmlWebpackPlugin配置
filename: `${fileName}.html`,
template: 'template.html',
title,
minify,
inject: true,
chunks,
chunksSortMode: 'dependency',
vhost: dev ? '' : '../../' + config.build.virtualHost, // 资源放置的目录
libJsName: dev && bundleConfig ? '/' + bundleConfig.libs.js : '', // 只须要注意这里,这样变量能够在index.html中使用
});
复制代码
模板html(一般是index.html)添加
<script type="text/javascript" src="<%=htmlWebpackPlugin.options.libJsName %>"></script>
复制代码
运行
首先打dll包
npm run dll
复制代码
运行
npm run start
复制代码