3

这是我到目前为止所尝试的:

gulp.task('watch-index-html', function () {
    gulp.watch(files, function (file) {
        return gulp.src('index/index.html')
            .pipe(inject(gulp.src(['index/base3.html']), {
                starttag: '<!-- inject:base3:html -->'
            }))
            .pipe(print(function (file) {
                return "Processing " + file;
             }))
            .pipe(gulp.dest('./'));
    });
});

这是发生的事情:

前:

<body>
    abcd
    <!-- inject:base3:html -->
    <!-- endinject -->
    efgh

后:

<body>
    abcd
<!-- inject:base3:html -->
<link rel="import" href="/index/base3.html">
<!-- endinject -->
    efgh

我期望看到的是文件的实际内容,而不是行:

<link rel="import" href="/index/base3.html">

谁能告诉我是否可以使用“插入文件内容”gulp-inject或者是否还有其他我应该使用的东西。请注意,过去我尝试使用gulp-mustache,但在插入字符时遇到了问题(65279)。我认为这与以下问题有关,但我从来没有让小胡子为我工作:

如何避免在php中回显字符65279?(这个问题也与 Javascript xmlhttp.responseText (ajax) 有关)

我希望有人可以用gulp-inject或用gulp-mustache或任何其他方式给我一些建议,以将文件的内容插入另一个文件。

4

1 回答 1

6

使用自定义转换函数注入文件的内容:

gulp.task('watch-index-html', function () {
  gulp.watch(files, function (file) {
    return gulp.src('index/index.html')
      .pipe(inject(gulp.src(['index/base3.html']), {
         starttag: '<!-- inject:base3:html -->',
         transform: function(filepath, file) {
           return file.contents.toString();
         }
      }))
      .pipe(print(function (file) {
        return "Processing " + file;
      }))
     .pipe(gulp.dest('./'));
  });
});
于 2016-05-06T07:28:09.193 回答