Webpack-如何识别某些包并将其从呈现中排除

Webpack - how can I identify and exclude some package from rendering?

本文关键字:排除 何识别 识别 Webpack-      更新时间:2023-09-26

我使用react stdio服务器在服务器端呈现react.js。

Webpack渲染的javascript文件有以下几行:

 /***/
 /* 49 */
 /***/ function(module, exports, __webpack_require__)
 »·/* WEBPACK VAR INJECTION */(function(global) {/* global window */             
 »·'use strict';                                                                 
 »·module.exports = __webpack_require__(50)(global || window || this);           
 »·/* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }())))  
 /***/ },                                                                        
 /* 50 */                                                                        
 /***/ function(module, exports) { 

问题是react-stdio不喜欢window变量:react-stdio返回给我ReferenceError: window is not defined。所以,问题:

  1. 我如何理解哪个依赖项被标记为#49
  2. 如何从生成中删除此window变量

尝试将target: 'node'添加到您的webpack.config中。根据webpack文档:

目标:"node"编译以在类似node.js的环境中使用(use require to加载块(

module.exports = {
  entry: './src/main.js',
  target: 'node',
  output: {
    path: path.join(__dirname, 'build'),
    filename: 'backend.js'
  }
}

如果这不起作用,请尝试本教程-使用Webpack的后端应用程序(示例取自此教程(。