Socket.io: Server.prototype.serveClient 不适用于 webpack

创建于 2017-07-29  ·  4评论  ·  资料来源: socketio/socket.io

  • [x] 报告错误

当前行为

该函数使用相对路径来检查 socket.io-client。 但是,如果我使用 webpack 捆绑使用 webpack 的 nodejs 应用程序。 这将成为 node_modules 中 socket.io-client 的相对路径。

然后运行节点会返回一个错误:

错误:找不到模块“。”

重现步骤(如果当前行为是错误)

注意:获得快速答案的最佳方法是提供一个失败的测试用例,例如通过分叉以下小提琴

包中的编译代码变为:

Server.prototype.serveClient = function(v){
  if (!arguments.length) return this._serveClient;
  this._serveClient = v;
  var resolvePath = function(file){
    var filepath = path.resolve(__dirname, './../../', file);
    if (exists(filepath)) {
      return filepath;
    }
    return /*require.resolve*/(!(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }()));
  };
  if (v && !clientSource) {
    clientSource = read(resolvePath( 'socket.io-client/dist/socket.io.js'), 'utf-8');
    try {
      clientSourceMap = read(resolvePath( 'socket.io-client/dist/socket.io.js.map'), 'utf-8');
    } catch(err) {
      debug('could not load sourcemap file');
    }
  }
  return this;
};

预期行为

该包应该可以与 webpack 一起编译并顺利运行。

设置

  • 操作系统:Windows
  • 浏览器:不适用
  • socket.io 版本:
    "webpack": "^3.3.0"
    "socket.io": "^2.0.3",
    “socket.io-client”:“^2.0.3”

    其他信息(例如堆栈跟踪、相关问题、如何修复的建议)

最有用的评论

尝试禁用 socket.io 的 _serving client files_ 壮举。

const io = require('socket.io')(httpServer, { serveClient: false })

不理想,但至少捆绑有效。

所有4条评论

同样的问题

尝试禁用 socket.io 的 _serving client files_ 壮举。

const io = require('socket.io')(httpServer, { serveClient: false })

不理想,但至少捆绑有效。

我认为这是同样的错误,我通过使用 webpack 进入运行时

Error: Cannot find module 'socket.io-client/dist/socket.io.js'

我不使用socket.io self,但它是androidjs的依赖

如果我在后续补丁中使用raw-loader ,它会起作用

diff --git a/node_modules/socket.io/lib/index.js b/node_modules/socket.io/lib/index.js
index 5287e4e..9ee4577
--- a/node_modules/socket.io/lib/index.js
+++ b/node_modules/socket.io/lib/index.js
@@ -113,9 +113,9 @@ Server.prototype.serveClient = function(v){
     return require.resolve(file);
   };
   if (v && !clientSource) {
-    clientSource = read(resolvePath( 'socket.io-client/dist/socket.io.js'), 'utf-8');
+    clientSource = require( 'raw-loader!socket.io-client/dist/socket.io.js');
     try {
-      clientSourceMap = read(resolvePath( 'socket.io-client/dist/socket.io.js.map'), 'utf-8');
+      clientSourceMap = require( 'raw-loader!socket.io-client/dist/socket.io.js.map');
     } catch(err) {
       debug('could not load sourcemap file');
     }

谢谢这对我有用。

此页面是否有帮助?
0 / 5 - 0 等级