使用gulp和wiredep,socket.io未被添加到index.html中(尽管它在bower.json中)。

4

我在我的bower.json文件中有angular、angular-ui-router和socket.io。

当我运行我的gulp文件(使用wiredep)时,这两个angular脚本成功地添加到了我的index.html文件中,但是socket.io脚本没有被添加——我无法找出原因。谢谢任何帮助。

//命令行

[21:56:06] Using gulpfile ~/dev/projects/ecommerceVidChat/gulpfile.js
[21:56:06] Starting 'default'...
[21:56:06] Starting 'bower-dependencies'...
[21:56:06] Finished 'bower-dependencies' after 6.24 ms
[21:56:06] Finished 'default' after 7.24 ms

//bower.json

  "dependencies": {
    "angular": "~1.3.13",
    "socket.io": "~1.3.4",
    "angular-ui-router": "~0.2.13"
  }

// gulpfile.js

var gulp = require('gulp'),
    wiredep = require('wiredep').stream;

gulp.task('default', function() {
  gulp.start('bower-dependencies')
});

gulp.task('bower-dependencies', function () {  
  gulp.src('./build/index.html') 
    .pipe(wiredep({
      directory: './build/bower_components',
      bowerJson: require('./bower.json'),
    }))
    .pipe(gulp.dest('./build/'));
});

//index.html

<!-- bower:js -->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
<!-- endbower -->

//package.json

"devDependencies": {
    "gulp": "^3.8.11"
  }
2个回答

10

Socket.io本身不支持bower,请记住它是服务器而不是客户端。

您可以通过将其serveClient选项设置为true来使用socket服务器提供客户端脚本,并通过以下方式直接插入到您的index中:

 <script src="socket.io/socket.io.js"></script>

或者安装在bower中被引用但具有另一个名称的客户端脚本:

bower install -save socket.io-client

如果此软件包没有 main 属性,则您必须在主要的 bower.json 中覆盖它:

"overrides": {
  "socket.io-client": {
    "main": "socket.io.js"
  }
}

这样,wiredep 将自动将它注入到你的 index.html 中。


对我而言,在使用yeoman并编辑bower.json文件时,适用于main的值是dist/socket.io.js。解决方案:https://github.com/socketio/socket.io-client/issues/666 - Touch
2
"main": "dist/socket.io.js"将能够正确地与wiredep配合使用。 - nodox

1
有时候供应商会在其文件中排除可选的main属性,我相信wiredep会使用它来编译源文件数组。检查文件夹中的文件,看他们是否包括该属性。如果没有,或许你可以向socket.io发起一个pull request或至少提出一个问题?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接