我试图编译react(.jsx),coffeescript(.coffee)和vanilla javascript( .js)文件使用gulp将所有生成的.js文件打包到一个文件app.js中,该文件被加载到我的index.html页面中。我正在为每种编译类型生成一个流,并使用merge-stream将3个馈线流的内容收集到一个流中,我将它传递给gulp-concat来创建app.js.gulp-concat抛出异常'file.isNull(),index.js:39 TypeError:file.isNull不是函数
我从gulp-concat,index.js,第39行得到一个异常,让我知道'文件'不是函数。这里是我的整个gulpfile.js,对gulp-concat的引用接近本节的底部。
var browserify = require('browserify');
var coffee = require('gulp-coffee');
var concat = require('gulp-concat');
var gulp = require('gulp');
var gutil = require('gulp-util');
var mergeStream = require('merge-stream');
var reactify = require('reactify');
var sass = require('gulp-sass');
var source = require('vinyl-source-stream');
gulp.task('javascript', function(){
// convert .jsx files to .js, collecting them in a stream
var b = browserify();
b.transform(reactify); // use the reactify transform
b.add('./jsx-transforms.js');
jsxStream = b.bundle();
if (gutil.isStream(jsxStream)) {
gutil.log("jsxStream is a stream");
} else {gulp-concatgulp
gutil.log("jsxStream is not a stream");
}
merged = mergeStream(jsxStream);
if (gutil.isStream(merged)) {
gutil.log("merged is a stream");
} else {
gutil.log("merged is not a stream");
}
// collect all .js files in a stream
jsStream = gulp.src(['./client/**/*.js','./common/**/*.js']);
if (gutil.isStream(jsStream)) {
gutil.log("jsStream is a stream");
} else {
gutil.log("jsStream is not a stream");
}
merged.add(jsStream);
// compile all .coffee file to .js, collect in a stream
coffeeStream = gulp.src(['./client/**/*.coffee','./common/**/*.coffee'])
.pipe(coffee({bare: true}).on('error', gutil.log));
if (gutil.isStream(coffeeStream)) {
gutil.log("coffeeStream is a stream");
} else {
gutil.log("coffeeStream is not a stream");
}
merged.add(coffeeStream);
// concatenate all of the .js files into ./build/app.js
merged
.pipe(concat('app.js'))
.pipe(gulp.dest('./build'));
});
gulp.task('styles', function() {
gulp.src('./client/assets/stylesheets/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(concat('app.css'))
.pipe(gulp.dest('./build'));
});
gulp.task('default', ['javascript', 'styles']);
我之前使用过gulp-concat,但之前从未遇到过这个问题。
非常感谢关于缓冲区和流的澄清。在过去的几天里,我读了许多关于吞咽和浏览的内容,但是错过了这个区别。再次,谢谢。 – explainer
@explainer直到我自己开发了一些插件之前,我从未完全理解Gulp流。如果你计划经常使用Gulp,你可能想尝试编写一个你自己的插件。它会让你的Gulp生活的其余部分容易。 – McMath