[英]Setting up code splitting in Webpack and React.js
I'm trying to set up code splitting / chunking in my app - by route, using require.ensure
. 我正在尝试在我的应用程序中设置代码拆分/分块 - 通过路由,使用require.ensure
。 So here's my route: 所以这是我的路线:
<Route path="profile"
getComponent={(location, cb) =>
{require.ensure(
[],
(require) => { cb(null, require('attendee/containers/Profile/').default) },
'attendee')}} />
Here are relevant lines from my webpack config: 以下是我的webpack配置中的相关行:
const PATHS = {
app: path.join(__dirname, '../src'),
build: path.join(__dirname, '../dist'),
};
const common = {
entry: [
PATHS.app,
],
output: {
path: PATHS.build,
publicPath: PATHS.build + '/',
filename: '[name].js',
chunkFilename: '[name].js',
sourceMapFilename: '[name].js.map'
},
target: 'web',
devtool: 'cheap-module-eval-source-map',
entry: [
'bootstrap-loader',
'webpack-hot-middleware/client',
'./src/index',
],
output: {
publicPath: '/dist/',
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"development"',
},
__DEVELOPMENT__: true,
}),
new ExtractTextPlugin('main.css'),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.ProvidePlugin({
jQuery: 'jquery',
}),
],
When I navigate to the page in the route, I see in the logs that the required chunk does get downloaded. 当我导航到路径中的页面时,我在日志中看到所需的块已下载。 The page however does not load. 但是页面没有加载。
And I see the following stack trace in the console: 我在控制台中看到以下堆栈跟踪:
Uncaught TypeError: Cannot read property 'call' of undefined
t @ main.js:10
(anonymous function) @ main.js:44637
window.webpackJsonp @ main.js:40
(anonymous function) @ attendee.js:1
The line it complains about is this: 它抱怨的界限是这样的:
return e[n].call(o.exports, o, o.exports, t)
The second line ((anonymous function) @ main.js:44637) is essentially this: 第二行((匿名函数)@main.js:44637)基本上是这样的:
require('attendee/containers/Profile/').default
Note, if I do console.log(require('./attendee/containers/Profile/').default)
, I get a function as an output, so I'm not sure why it is undefined. 注意,如果我执行console.log(require('./attendee/containers/Profile/').default)
,我会得到一个函数作为输出,所以我不确定为什么它是未定义的。 And of course when I do that the code works, but than there's no chunking any more. 当然,当我这样做时,代码可以工作,但是不再有任何分块。
So I'm doing something wrong with the require
. 所以我做一些错误的require
。 Any idea what it is? 知道它是什么?
BTW I'm using hash history in this project - could this be the culprit? 顺便说一下,我在这个项目中使用哈希历史 - 这可能是罪魁祸首吗?
Update: 更新:
Also tried the bundle-loader as in this answer . 还尝试了在这个答案中的bundle-loader。 Same result. 结果相同。
you are almost there! 你快到了! Try this: you need to specific the array of module dependencies ahead of time in the first argument of require.ensure
instead of []
explicitly set it to ['attendee/containers/Profile']
试试这个:你需要在require.ensure
的第一个参数中提前特定模块依赖项数组而不是[]
显式设置为['attendee/containers/Profile']
(location, cb) => {
require.ensure(['attendee/containers/Profile'], (require) => {
cb(null, require('attendee/containers/Profile').default)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.