webpack - Webpack 供应商库未拆分为单独的包
问题描述
我正在尝试使用autodll-webpack-plugin ( v0.4.2
) 将我的供应商和应用程序包分开。这个包是webpack的 DllPlugin 和add-asset-html-webpack-plugin 的顶级插件,用于自动排序index.html
.
这个插件应该做的是分离供应商库和应用程序代码。我可以使用webpack中的 CommonsChunkPlugin 来做到这一点,但是这样每次重新编译时都会重新生成包。与生成供应商捆绑包一次并且仅在更改库时重新编译它相比效率较低。
问题
我让这个插件“工作”(它正在输出一个vendor.bundle.js
)。这里唯一的问题是,当我app.bundle.js
使用webpack-bundle-analyzer ( v2.13.1
) 检查时。我可以看到其中的所有 node_modules 也都vendor.bundle.js
加载到了这个包中,因此插件无法正常工作。
版本
我在用:
- 网页包
v4.11.0
- 通天塔装载机
v7.1.4
- 通天塔核心
v6.26.3
- autodll-webpack-插件
v0.4.2
项目结构
我的项目具有以下文档结构:
App
-- build //Here are the output bundles located
-- node_modules
-- public
-- index.html
-- src // App code
-- webpack
-- webpack.common.js
-- webpack.dev.js
-- webpack.prod.js
-- package.json
我的 webpack.common.js(此代码与开发和产品构建共享)
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const AutoDllPlugin = require('autodll-webpack-plugin');
module.exports = {
entry: {
app: [
'babel-polyfill',
'./src/index.js',
],
},
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, '../build'),
publicPath: '', // Is referenced by the autodll-webpack-plugin
},
module: {
rules: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
plugins: ['react-hot-loader/babel'],
cacheDirectory: true,
presets: ['env', 'react'],
},
}
],
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html'
}),
new AutoDllPlugin({
inject: true, // will inject the DLL bundle to index.html
debug: false,
filename: '[name]_[hash].js',
context: path.join(__dirname, '..'),
path: '',
entry: {
vendor: [
'react',
'react-dom'
]
},
}),
],
};
根据上下文键的文档应该autodll-webpack-plugin
用于分离。所以我认为这就是问题所在。
文档描述您应该引用它所在的文件夹webpack.config.js
,但我有 3 个,我需要引用哪一个?我的文件夹被调用webpack
,而不是config
你在文档中看到的,在..
这里也正确吗?
解决方案
所以最后我没有让DLL设置工作。在阅读了更多内容后,我意识到autodll-webpack-plugin的创建者建议改用hard-source-webpack-plugin,因为 webpack 将来可能会用作默认选项。
在阅读了更多内容后,我还意识到不建议在生产中使用 DLL 插件,因为无论如何您都必须重新编译它(开发构建添加了东西)。因此,您应该将hard-source-webpack-plugin
用于开发构建,将SplitChunksPlugin用于生产。
我让这两个工作很容易:
Webpack.dev.js
const merge = require('webpack-merge');
const webpack = require('webpack');
const path = require('path');
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'development',
devtool: 'eval-source-map',
devServer: {
hot: true,
contentBase: path.resolve(__dirname, 'build'),
historyApiFallback: true, // Allow refreshing of the page
},
plugins: [
// Enable hot reloading
new webpack.HotModuleReplacementPlugin(),
// Enable caching
new HardSourceWebpackPlugin({
cacheDirectory: '.cache/hard-source/[confighash]',
configHash: function(webpackConfig) {
return require('node-object-hash')({ sort: false }).hash(webpackConfig);
},
environmentHash: {
root: process.cwd(),
directories: [],
files: ['package-lock.json'],
},
info: {
mode: 'none',
level: 'warn',
},
}),
],
});
webpack.prod.js
const merge = require('webpack-merge');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'production',
plugins: [
// Clean the build folders
new CleanWebpackPlugin(['build'], {
root: process.cwd(), // Otherwise the 'webpack' folder is used
}),
//Make vendor bundle size visible
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: 'stats/prod-report.html',
}),
],
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
name: 'vendor',
chunks: 'all',
test: /[\\/]node_modules[\\/]/,
},
},
},
minimizer: [
// Optimize minimization
new UglifyJsPlugin({
cache: true,
parallel: true,
uglifyOptions: {
ecma: 6,
mangle: true,
toplevel: true,
},
}),
],
},
});
我希望这对任何人都有帮助。
推荐阅读
- swift - 如何将 swift 变量放入另一个视图
- javascript - REACT.JS - 选择/选项未按应有方式显示
- sql - 错误:无法在 PL/SQL 中使用选择创建过程
- r - 删除 r 中所有条目均为 0 的列
- python - 如果值相等,则将连续的子列表组合在一起,然后提取具有属性 = 'R' 的值
- html - 如何使用 Font Awesome 5 使按钮遵循其内容的形状?
- python - 迭代 JSON 并附加到字典?
- php - 每次我想获取 MySQL-QUERY (PDO) 的 COUNT 时都会出现错误消息
- sql-server - 如何在 SSMS 单元格中输入文本“NULL”(带引号)?
- django - 部署到 Heroku 时无法通过 Django 静态文件