如何让 gulp-vulcanize 忽略 socket.io.js?

How to get gulp-vulcanize to ignore socket.io.js?

我的一个 .html 文件导入 /socket.io/socket.io.js,我想硫化这个文件但忽略导入 socket.io

的脚本标签

我写了以下gulp任务:

// vulcanize HTML files
var vulcanizeHtmlSrc = 'views/**/*.html',
    vulcanizeHtmlDst = 'build/views';
gulp.task('vulcanize', function() {
gulp.src(vulcanizeHtmlSrc)
    .pipe(vulcanize({
        excludes: ['//fonts.googleapis.com/*', '/socket.io/socket.io.js'],
        stripExcludes: false
    }))
    .pipe(gulp.dest(vulcanizeHtmlDst));    
});

我仍然收到以下错误:

ERROR finding /socket.io/socket.io.js

我做错了什么?

// vulcanize HTML files
var vulcanizeHtmlSrc = 'views/**/*.html',
vulcanizeHtmlDst = 'build/views';
gulp.task('vulcanize', function() {
gulp.src(vulcanizeHtmlSrc)
    .pipe(vulcanize({
        excludes: ['//fonts.googleapis.com/*',
            './bower_components/polymer/polymer.html'
        ],
        stripExcludes: false,
        inlineScripts: true,
        inlineCss: true,
        implicitStrip: true,
        stripComments: true
    }))
    // pipe to injectString to add script tags that cause an issue with vulcanize
    // e.g. <script src="/socket.io/socket.io.js">
    // Error caused if the script is added in index.html itself
    // ERROR finding /socket.io/socket.io.js
    .pipe(injectString.before('<script class="usesSocket.io">', '<script src="/socket.io/socket.io.js"></script>\n'))
    .pipe(gulp.dest(vulcanizeHtmlDst));
});

只需在需要socket.io.jsscript标签中添加一个class,然后在硫化后使用gulp-inject-string模块插入socket.io.js。这有点hacky。无论哪种方式 vulcanize 仍然会导致很多错误,我建议人们避免使用 Polymer(如果正在开发的应用程序已投入生产),直到它完全稳定并且有更好的文档。

如果您希望在原始源中保留 socket.io 脚本,您也可以删除这些脚本,然后按照@Torcellite 的建议将脚本注入 vulcanise。我使用开始和结束注释在 HTML.

中标出此块

HTML

<!-- gulp:remove -->
<script src="/socket.io/socket.io.js"></script>
<!-- gulp:endremove -->

gulp.task

  // 1 - remove server scripts for vulcanization
  var start_comment = "gulp:remove",
      end_comment = "gulp:endremove",
      pattern = new RegExp("(\<!--\s" + start_comment + "\s--\>)(.*\n)*(\<!--\s" + end_comment + "\s--\>)", "g");
  .pipe(require('gulp-tap')(function(file) {
      file.contents = new Buffer(String(file.contents).replace(pattern, ""));
  }))
  // 2 - pipe vulcanize...
  // 3 - pipe injectString back in...