ana*_*han 3 javascript reactjs webpack
我如何改善webpack构建时间.目前我正在打包150个文件.大约需要15秒(这是太多的时间).在"优化大块资产"期间,大部分时间都被吃掉,大约需要10秒钟.任何方式将此降低到最多3-4秒.
或者我如何在webpack中禁用优化步骤.我没有明确使用任何配置来缩小/丑化.
这是我目前使用的配置::
module.exports = {
context: __dirname,
entry: './js/main.js',
target: 'web',
module: {
loaders: [
{ test: /text!/, loader: 'raw-loader'},
{ test: /backbone/, loader: 'imports-loader?this=>window' },
{ test: /marionette/, loader: 'imports-loader?this=>window' },
{ test: /sprintf/, loader: 'script-loader' },
{ test: /\.css$/, loader: "style!css" },
{ test: /\.scss$/, loader: 'style!css!sass' },
{ test: /\.js$/, loader: 'jsx-loader?harmony' }
]
},
resolveLoader: {
root: path.join(__dirname, 'node_modules'),
alias: {
'text': 'raw'
}
},
output: {
filename: 'bundle.js',
library: 'require',
libraryTarget: 'this'
},
resolve: {
alias: alias,
root: path.join(__dirname, 'node_modules'),
extensions: ['', '.js', '.jsx']
},
externals: {
jquery: 'jQuery'
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
'root.jQuery': 'jquery',
'Backbone': 'backbone',
'_': 'underscore',
'Marionette': 'marionette',
'sprintf': 'sprintf'
})
],
devtool: 'source-map'
};
Run Code Online (Sandbox Code Playgroud)
我在这里先向您的帮助表示感谢.
您可以对构建进行一些优化.
首先,实际上,您正在node_modules通过jsx加载器解析您的所有文件.你想要排除它们.
loaders: [{
test: /\.js$/,
loader: 'jsx-loader?harmony',
exclude: /node_modules/, // <---
}]
Run Code Online (Sandbox Code Playgroud)
其次,所有供应商文件(在开发期间不会更改)都会在每次更改文件时进行编译.这不是很有效,您应该使用CommonsChunkPlugin将供应商文件与应用程序代码分开.
实质上,你必须添加:
config.entry = {
app: './js/main.js',
vendor: ['react', 'moment', 'react-router', /* etc. all the "big" libs */],
};
config.output = {
filename: '[name].js', /* will create app.js & vendor.js */
};
config.plugins = [
/* ... */
new webpack.optimize.CommonsChunkPlugin(
/* chunkName= */"vendor",
/* filename= */"vendor.bundle.js"
),
];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2361 次 |
| 最近记录: |