[英]Execute webpack compiled bundle in NodeJS
I want to implement server-side rendering for my ReactJS app. 我想为我的ReactJS应用程序实现服务器端渲染。 I use react-router
. 我使用react-router
。 I take routes.js
as webpack entry point and compile with output.libraryTarget = "commonjs2"
option. 我将routes.js
作为webpack的入口点,并使用output.libraryTarget = "commonjs2"
选项进行编译。 Then I require the result of compilation in NodeJS script to make rendering. 然后,我需要在NodeJS脚本中进行编译的结果来进行渲染。 But I've got en error. 但是我有一个错误。 Webpack wrap modules in following code: Webpack将模块包装在以下代码中:
/* 277 */
/***/ function(module, exports, __webpack_require__) {
/* WEBPACK VAR INJECTION */(function(Buffer, global) {
if (global.foo) {
/* ... */
}
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(277).Buffer, (function() { return this; }())))
/***/ }
When NodeJS tries to execute (function() { return this; }())
its return undefined
. 当NodeJS尝试执行(function() { return this; }())
其返回undefined
。 In browser it will return window
. 在浏览器中,它将返回window
。 Why webpack use such wrap code? 为什么webpack使用这样的包装代码? How to make this code works in NodeJS? 如何使此代码在NodeJS中工作?
I use node-clone as external lib. 我使用节点克隆作为外部库。 It don't use any other libs as dependency. 它不使用任何其他库作为依赖项。 But webpack in its bundle makes buffer as a dependency for this lib. 但是捆绑包中的webpack将缓冲区作为此lib的依赖项。 And inside the buffer
code I've got en error Cannot read property 'TYPED_ARRAY_SUPPORT' of undefined
. 在buffer
代码中,我得到了错误Cannot read property 'TYPED_ARRAY_SUPPORT' of undefined
。 It happens because in nodeJS (function() { return this; }())
return undefined
. 发生这种情况是因为在nodeJS (function() { return this; }())
返回undefined
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.