【问题标题】:Using gulp and wiredep, socket.io is not added to index.html (even though it is in bower.json)使用 gulp 和wiredep,socket.io 不会添加到 index.html(即使它在 bower.json 中)
【发布时间】:2015-04-17 19:18:35
【问题描述】:

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

当我运行我的 gulp 文件(使用wiredep)时,两个角度脚本已成功添加到我的 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"
  }

【问题讨论】:

    标签: javascript html socket.io gulp wiredep


    【解决方案1】:

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

    您可以通过将其serveClient 选项设置为true 来为套接字服务器提供客户端脚本,并将其直接插入到您的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

    【讨论】:

    【解决方案2】:

    有时供应商会在他们的bower.json 文件中排除可选的main 属性,我相信wiredep 使用它来编译源文件数组。检查bower.json 文件 bower_components/socket.io/ 文件夹中,看看他们是否已包含该文件。如果没有,也许您可​​以向 socket.io 发出拉取请求,或者至少提出一个问题?

    【讨论】:

      猜你喜欢
      • 2015-09-04
      • 2014-03-07
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多