我正在使用Webpack构建客户端+服务器Node.js / Express应用程序。我正在使用多编译器功能来构建app.js(服务器)和main.js(客户端)。如果我自己构建app.js,Express运行正常。如果我使用多编译器功能来构建两者,则由于此错误而失败:
user@mac:~/project$ node build/app.js
/project/build/app.js:132611
module.exports = typeof self == 'object' ? self.FormData : window.FormData;
^
ReferenceError: window is not defined
at Object.<anonymous> (/project/build/app.js:132611:61)
at __webpack_require__ (/project/build/app.js:20:30)
at Object.<anonymous> (/project/build/app.js:114314:17)
at __webpack_require__ (/project/build/app.js:20:30)
at Object.hasOwn (/project/build/app.js:112065:20)
at __webpack_require__ (/project/build/app.js:20:30)
at Object.VERSION (/project/build/app.js:24863:17)
at __webpack_require__ (/project/build/app.js:20:30)
at Object.<anonymous> (/project/build/app.js:59:11)
at __webpack_require__ (/project/build/app.js:20:30)
我的基础是我的webpack配置:https://github.com/villeristi/vue.js-starter-template
你可以在这里找到我修改过的webpack文件:
基本webpack配置:http://pastebin.com/zeEEbNrf
Dev webpack config:http://pastebin.com/p5isXwSd(将上面的内容导入为webpackBase)
当我对自己和多编译器构建的构建的app.js文件进行区分时,代码大不相同,所以我不确定是什么导致了这个问题。
我能做什么,所以我可以同时构建app.js / main.js,并将它与Express一起使用?
当我自己构建app.js时,我只是注释掉了line 38 here。我尝试交换第38/39行以查看订单是否重要,现在我的webpack构建失败了:
> webpack --progress --colors --watch --inline --config webpack/development.js
69% 1332/1350 builHash: 194ece3ebdbb1574a343
Version: webpack 1.13.3
Time: 10287ms
Asset Size Chunks Chunk Names
app.js 9.62 MB 0 [emitted] app
[0] multi app 28 bytes {0} [built]
+ 1209 hidden modules
WARNING in ./~/express/lib/view.js
Critical dependencies:
78:29-56 the request of a dependency is an expression
@ ./~/express/lib/view.js 78:29-56
Hash: 849be51dfde2a0e34a2b
Version: webpack 1.13.3
Time: 15130ms
Asset Size Chunks Chunk Names
ed19359cb473909255e502db33bd7f63.png 1.51 MB [emitted]
e1ea82cb1c39656b925012efe60f22ea.png 11.5 kB [emitted]
main.849be51dfde2a0e34a2b.js 13.1 MB 0 [emitted] main
index.html 531 bytes [emitted]
[0] multi main 28 bytes {0} [built]
+ 1374 hidden modules
ERROR in ./~/sntp/lib/index.js
Module not found: Error: Cannot resolve module 'dgram' in /project/node_modules/sntp/lib
@ ./~/sntp/lib/index.js 3:12-28
ERROR in ./~/sntp/lib/index.js
Module not found: Error: Cannot resolve module 'dns' in /project/node_modules/sntp/lib
@ ./~/sntp/lib/index.js 4:10-24
Child html-webpack-plugin for "index.html":
+ 1 hidden modules
也许我误解了webpack多编译器的工作原理。我只想让每个入口点完全独立构建。我想通过使用这样的数组,我可以省去在两个终端中运行webpack --watch frontend.dev.js
webpack --watch backend.dev.js
的痛苦。