我有一个任务来将Bower组件注入到index.html文件中。我有2种不同的版本,一种有效,一种无效,我不能说出为什么一个无效b / c对我来说看起来完全一样。

不起作用

var gulp = require('gulp');
var inject = require('gulp-inject');
var mainBowerFiles = require('main-bower-files');

/**
 * Injects all the bower dependencies into index.html
 */
gulp.task('inject-bower-files', function() {
  return
    gulp.src('./index.html')
      .pipe(inject(gulp.src(mainBowerFiles(), {read: false})))
      .pipe(gulp.dest('./'));
});


作品

var gulp = require('gulp');
var inject = require('gulp-inject');
var mainBowerFiles = require('main-bower-files');

/**
 * Injects all the bower dependencies into index.html
 */
gulp.task('inject-bower-files', function() {

  var target = gulp.src('./index.html');
  var sources = gulp.src(mainBowerFiles(), {read: false});

  return target.pipe(inject(sources))
    .pipe(gulp.dest('./'));
});


两者有什么区别?

最佳答案

您的第一个示例由于JavaScript的automatic semicolon insertion而失败。

相当于

var gulp = require('gulp');
var inject = require('gulp-inject');
var mainBowerFiles = require('main-bower-files');

/**
 * Injects all the bower dependencies into index.html
 */
gulp.task('inject-bower-files', function() {
  return; // <-- semicolon inserted here, nothing below this runs
    gulp.src('./index.html')
      .pipe(inject(gulp.src(mainBowerFiles(), {read: false})))
      .pipe(gulp.dest('./'));
});


要使其正常运行,请将其更改为

var gulp = require('gulp');
var inject = require('gulp-inject');
var mainBowerFiles = require('main-bower-files');

/**
 * Injects all the bower dependencies into index.html
 */
gulp.task('inject-bower-files', function() {
  // return is no longer on a line by itself
  return gulp.src('./index.html')
      .pipe(inject(gulp.src(mainBowerFiles(), {read: false})))
      .pipe(gulp.dest('./'));
});

关于javascript - Gulp注入(inject)不以一种方式工作,而以另一种方式工作-没有区别吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34210882/

10-12 15:47