我的路线是用 jsx 映射的。我使用 webpack 来打包东西,我想根据路由将输出的 js 文件分成块。
我试过 require.ensure 但 webpack 没有拆分任何东西。最后它只生成一个捆绑文件。我不确定我在这里做错了什么。我不想维护路线所在的 2 个地方。理想情况下,webpack 使用我已经定义的路由。
export const renderRoutes = () => (
<Provider store={store}>
<Router history={history}>
<Route path='en' component={AppContainer}>
<Route path='cart' getComponent={(location, cb) => {
require.ensure([], (require) => {
cb(null, require('./Cart/CartContainer'));
});
}}/>
<Route path='checkout'>
<Route path='shipping_address' component={ShippingAddressFormContainer} />
<Route path='delivery_options' component={DeliveryOptionFormContainer} />
<Route path='payment' component={PaymentContainer} />
<Route path='review_order' component={ReviewOrderContainer} />
<Route path='confirmation' component={ConfirmationContainer} />
</Route>
</Route>
</Router>
</Provider>
);
render(
renderRoutes(),
document.getElementById('react-root')
);
咕噜文件配置:
dev: {
entry: [
'./<%= paths.src %>/javascripts/react/containers/Root'
],
output: {
path: path.join(__dirname, '<%= paths.dist %>/javascripts'),
filename: 'bundle.js',
chunkFilename: '[id].chunk.js',
publicPath: '/en/'
},
devtool: 'cheap-module-source-map',
plugins: [
new webpack.optimize.CommonsChunkPlugin('bundle.js'),
new webpack.optimize.OccurrenceOrderPlugin(), // Chunk ids by occurrence count. Ids that are used often get lower (shorter) ids.
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('development')
}
})
],
module: {
preLoaders: [
{
test: /\.json$/,
loader: 'json'
},
],
loaders: [
{
test: /\.js$/,
loaders: ['babel'],
exclude: /node_modules/,
include: __dirname
}
]
}
},
开发任务输出
Asset Size Chunks Chunk Names
bundle.js 2.76 MB 0, 1, 0 [emitted] bundle.js, main
bundle.js.map 3.17 MB 0, 1, 0 [emitted] bundle.js, main
我阅读了一些教程,但似乎这种情况并不常见。