我的webpack生成了一个大的main.js文件(1.7mb),其中包含一个小于20-30个文件的小项目,每个文件少于100行。所需的依赖项数量很少(React,Fluxible),我正在使用我能理解的每个优化插件:
module.exports = {
output: {
path: './build',
publicPath: '/public/',
filename: '[name].js'
},
debug: false,
devtool: 'eval',
target: 'web',
entry: [
'bootstrap-sass!./bootstrap-sass.config.js',
'./client.js',
],
stats: {
colors: true,
reasons: false
},
resolve: {
extensions: ['', '.js'],
alias: {
'styles': __dirname + '/src/styles',
'components': __dirname + '/src/scripts/components',
'actions': __dirname + '/src/scripts/actions',
'stores': __dirname + '/src/scripts/stores',
'constants': __dirname + '/src/scripts/constants',
'mixins': __dirname + '/src/scripts/mixins',
'configs': __dirname + '/src/scripts/configs',
'utils': __dirname + '/src/scripts/utils'
}
},
module: {
loaders: [
{ test: /\.css$/, loader: 'style!css' },
{ test: /\.js$/, exclude: /node_modules/, loader: require.resolve('babel-loader') },
{ test: /\.json$/, loader: 'json-loader'},
{ test: /\.(png|svg|jpg)$/, loader: 'url-loader?limit=8192' },
{ test: /\.(ttf|eot|svg|woff|woff(2))(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url?name=/[name].[ext]"},
{ test: /\.scss$/,
loader: ExtractTextPlugin.extract('style-loader',
'css!sass?outputStyle=expanded&' +
"includePaths[]=" +
(path.resolve(__dirname, "./node_modules"))
)
}
]
},
plugins: [
new webpack.NoErrorsPlugin(),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"windows.jQuery": "jquery"
}),
new ExtractTextPlugin("[name].css", {allChunks: true}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.AggressiveMergingPlugin()
],
};
我做错了什么或在哪里可以进一步改善文件的大小?
答案 0 :(得分:14)
我只是通过这里的事情(devtools:' source-map')将我的反应从2.1mb降低到160kb,使用uglifyjs和默认设置(没有gzip它最终是关于670KB)。
它可能 很棒,但至少它不再疯狂了。
对于后代,这是我的webpack配置:
// webpack.config.js
var webpack = require('webpack');
module.exports = {
devtool: 'source-map',
entry: [
'webpack-dev-server/client?http://127.0.0.1:2992',
'webpack/hot/only-dev-server',
'./js/main'
],
output: {
path: './out/',
filename: 'main.js',
chunkFilename: '[name]-[chunkhash].js',
publicPath: 'http://127.0.0.1:2992/out/'
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loaders: ['react-hot', 'babel?optional=runtime&stage=0&plugins=typecheck']
}
]
},
progress: true,
resolve: {
modulesDirectories: [
'js',
'node_modules'
],
extensions: ['', '.json', '.js']
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new webpack.DefinePlugin({
'process.env': {
// This has effect on the react lib size
'NODE_ENV': JSON.stringify('production'),
}
}),
new webpack.optimize.UglifyJsPlugin()
]
};
答案 1 :(得分:9)
你应该至少设置
plugins: [
new webpack.DefinePlugin({
'process.env': {
// This has effect on the react lib size
'NODE_ENV': JSON.stringify('production'),
}
}),
...
],
这对React有很大帮助。
此外,在生产环境中将devtool
设置为source-map
更为可取。有关详细信息,请参阅official documentation。
您可以尝试使用the analyse tool检查输出。要获得JSON,它希望您需要执行webpack --json > stats.json
之类的操作,然后将stats.json
传递给该工具。这可能会给你一些见解。
答案 2 :(得分:7)
您还可以查看Webpack Bundle Analyzer。
它将捆绑内容表示为方便的交互式可缩放树形图。