我正在尝试为react / redux项目创建自己的webpack配置。配置似乎工作得很好,但是捆绑包大小很大(在开发模式下,我知道如何在生产模式下减少它)
我的package.json
看起来像
"dependencies": {
"react": "15.4.2",
"react-dom": "15.4.2",
"react-hot-loader": "3.0.0-beta.6"
},
"devDependencies": {
"babel-core": "6.24.0",
"babel-loader": "6.4.1",
"babel-preset-react": "6.23.0",
"commitizen": "2.9.6",
"cz-conventional-changelog": "2.0.0",
"html-webpack-plugin": "2.28.0",
"webpack": "2.3.1",
"webpack-bundle-analyzer": "^2.3.1",
"webpack-dev-server": "2.4.2"
}
我的webpack配置看起来像
const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const HtmlWebpackPlugin = require('html-webpack-plugin');
const project = require('./project.config')
const __DEV__ = project.globals.__DEV__;
const __PROD__ = project.globals.__PROD__;
const __TEST__ = project.globals.__TEST__;
const APP_ENTRIES = [project.paths.client('index.js')];
if (__DEV__) {
APP_ENTRIES.unshift(
'react-hot-loader/patch',
`webpack-dev-server/client?http://${project.server_host}:${project.server_port}`,
'webpack/hot/only-dev-server'
)
}
const config = {
devtool: project.compiler_devtool,
entry: APP_ENTRIES,
output: {
path: project.paths.dist(),
filename: `[name].[${project.compiler_hash_type}].js`,
publicPath: project.compiler_public_path,
},
resolve: {
modules: [
project.paths.client(),
'node_modules',
],
},
module: {
rules: [{
test: /\.js?$/,
loader: 'babel-loader',
exclude: /node_modules/,
}],
},
plugins: [
new BundleAnalyzerPlugin(),
new HtmlWebpackPlugin({
template: project.paths.client('index.html'),
hash: false,
filename: 'index.html',
inject: 'body',
}),
],
node: {
fs: 'empty',
net: 'empty',
tls: 'empty'
}
}
if (__DEV__) {
config.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin());
}
module.exports = config
现在,当我运行webpack-dev-server
正如您所看到的,我的捆绑包大于1.3 Mb,但我只使用了一些库。
我尝试使用webpack-bundle-analyser
找出我的捆绑包为何如此之大。结果如下
似乎react
和readct-dom
是最大的图书馆。
有没有办法在开发模式下缩小捆绑包的大小?我做错了吗?
PS:我在compress = true
选项中设置webpack-dev-server
,但它没有减少捆绑包的大小。
以下是有关我如何使用webpack-dev-server
bin / start.js (我使用node bin/start.js
运行)
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const config = require('../config/webpack.config');
const project = require('../config/project.config');
function runDevServer() {
const devServer = new WebpackDevServer(webpack(config), {
compress: true,
hot: true,
publicPath: project.compiler_public_path,
stats: project.compiler_stats,
watchOptions: {
ignored: /node_modules/
},
});
// Launch WebpackDevServer.
devServer.listen(project.server_port, project.server_host, (err) => {
if (err) {
console.log('Webpack dev server encountered an error', err);
return reject(err);
}
console.log(`Listening at ${project.server_host}:${project.server_port}`);
});
}
runDevServer()
src / index.js (我的应用程序。Home
只是一个返回的组件" Hello world")
import React from 'react';
import ReactDOM from 'react-dom';
import { AppContainer } from 'react-hot-loader';
import Home from './routes';
const render = () => {
try {
ReactDOM.render(
<AppContainer>
<Home />
</AppContainer>,
document.getElementById('root')
);
} catch (err) {
console.error(err)
}
};
if (module.hot) {
module.hot.accept('./routes', () => render());
}
render()
答案 0 :(得分:3)
在开发过程中使用大型捆绑包是正常的,1.3MB仍然很小,通常应该快速加载。由于您使用的是webpack-dev-server,因此每次更改后都应该重新加载。如果你使用compress = true
,你的包大小将是相同的,除了当客户端要求它时它将被gzip压缩(你应该在网络选项卡中看到gzip)。我建议你不要尝试减少开发包的大小,因为它很难调试,刷新也慢。如果你真的想减小尺寸,你可以只修改和缩小库代码。这是一种非常常见的方法,您基本上将每个库都放在一个名为vendor.js
的文件中,您将缩小,uglify等。在另一个文件中bundle.js
,您将获得所有代码。我写道。这将显着减少总尺寸。但你必须加载2个js文件。你也可以为webpack 2激活树摇动。她是关于tree shaking的更多信息。您可以参考this answer了解如何单独捆绑供应商脚本,并阅读有关code splitting here的更多信息。但请记住,dev开发包的大小总是更大,因为它在调试时非常方便。