我試圖編譯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