socket.io-client在运行grunt构建时中断

时间:2013-10-03 17:32:20

标签: socket.io requirejs gruntjs yeoman

我有一个Yeoman webapp项目,我使用bower添加了socket.io-client

当我使用grunt server运行webapp时,一切正常。但是当我使用grunt build构建它时,我收到以下错误:

Uncaught TypeError: Cannot call method 'push' of undefined

通过在Gruntfile.jsgenerateSourceMaps: true)中启用源地图,我设法在 socket.io.js 中找到了错误的来源:

/**
 * Add the transport to your public io.transports array.
 *
 * @api private
 */

io.transports.push('websocket');

运行io.transports后,grunt build成为未定义的原因是什么?

更新

可能值得告诉我使用RequireJS并且它的配置如下:

require.config({
    paths: {
        jquery: '../bower_components/jquery/jquery',
        // ...
        // socket.io: Try the node server first
        'socket.io': ['/socket.io/socket.io', '../bower_components/socket.io-client/dist/socket.io'],
    },
    shim: {
        // Export io object: https://gist.github.com/guerrerocarlos/3651490
        'socket.io': {
            exports: 'io'
        }
    }
});

require(['jquery', 'socket.io'], function ($, io) {
    'use strict';
    // ...
});

1 个答案:

答案 0 :(得分:0)

您应该只在路径中定义socket.io-client:

paths: {
    'jquery': '../bower_components/jquery/jquery',
    'socket.io': '../bower_components/socket.io-client/dist/socket.io'
},

另外你不应该设置io参数(它是全局设置的)(或使用像sio这样的其他东西)

require(['jquery', 'socket.io'], function ($) {
    'use strict';
    // ...
});