我有一个gulp任务是将bower组件注入我的index.html文件中。我有两个不同的版本,一个工作,一个不工作,我不能告诉为什么一个不工作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('./'));
});
这两者有什么区别?
答案 0 :(得分:2)
由于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('./'));
});