Skip to content
This repository was archived by the owner on Sep 19, 2025. It is now read-only.

Commit 35e21cb

Browse files
Merge pull request #4 from brendankenny/master
add option to skip compilation if no files in gulp stream
2 parents 0ef48c1 + 4c5bdf0 commit 35e21cb

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,29 @@ gulp.task('js-compile', function () {
245245
});
246246
```
247247

248+
### Requiring gulp.src files for compilation
249+
By default, the gulp plugin will run the compiler even if no input is streamed in via `gulp.src`.
250+
To disable this behavior, users can specify a { requireStreamInput: true } argument to skip
251+
compilation if no files are streamed in. This allows interoperation with gulp plugins that signal
252+
through empty streams.
253+
254+
```js
255+
var closureCompiler = require('google-closure-compiler').gulp({
256+
requireStreamInput: true
257+
});
258+
var newer = require('gulp-newer');
259+
260+
gulp.task('js-compile', function () {
261+
return gulp.src('./src/js/**/*.js', {base: './'})
262+
.pipe(newer('./dist/output.min.js'))
263+
.pipe(closureCompiler({
264+
compilation_level: 'ADVANCED',
265+
js_output_file: 'output.min.js'
266+
}))
267+
.pipe(gulp.dest('./dist'));
268+
});
269+
```
270+
248271
### Gulp Sourcemaps
249272
The gulp plugin supports gulp sourcemaps.
250273

lib/gulp/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030

3131

3232
/**
33+
* @param {Object<string,string>} initOptions
3334
* @return {function(Object<string,string>|Array<string>):Object}
3435
*/
35-
module.exports = function() {
36+
module.exports = function(initOptions) {
3637
var filesToJson = require('./concat-to-json');
3738
var jsonToVinyl = require('./json-to-vinyl');
3839
var Compiler = require('../node/closure-compiler');
@@ -41,6 +42,7 @@ module.exports = function() {
4142
var through = require('through2');
4243
/** @const */
4344
var PLUGIN_NAME = 'gulp-google-closure-compiler';
45+
var streamInputRequired = initOptions ? !!initOptions.requireStreamInput : false;
4446

4547
return function (compilationOptions, pluginOptions) {
4648
pluginOptions = pluginOptions || {};
@@ -76,6 +78,13 @@ module.exports = function() {
7678
// Input files are present. Convert them to a JSON encoded string
7779
stdInData = filesToJson(fileList);
7880
} else {
81+
// If files in the stream were required, no compilation needed here.
82+
if (streamInputRequired) {
83+
this.emit('end');
84+
cb();
85+
return;
86+
}
87+
7988
// The compiler will always expect something on standard-in. So pass it an empty
8089
// list if no files were piped into this plugin.
8190
stdInData = "[]";

test/gulp.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,40 @@ describe('gulp-google-closure-compiler', function() {
260260

261261
stream.end();
262262
});
263+
264+
it('should compile gulp.src files when stream input is required', function(done) {
265+
this.timeout(30000);
266+
this.slow(10000);
267+
268+
var streamRequiredCompiler = compilerPackage.gulp({
269+
requireStreamInput: true
270+
});
271+
272+
gulp.src(__dirname + '/fixtures/**.js')
273+
.pipe(streamRequiredCompiler({
274+
compilation_level: 'SIMPLE',
275+
warning_level: 'VERBOSE'
276+
}))
277+
.pipe(assert.length(1))
278+
.pipe(assert.first(function(f) {
279+
f.contents.toString().should.eql('function log(a){console.log(a)}log("one.js");log("two.js");\n');
280+
}))
281+
.pipe(assert.end(done));
282+
});
283+
284+
it('should generate no output without gulp.src files when stream input is required', function(done) {
285+
var streamRequiredCompiler = compilerPackage.gulp({
286+
requireStreamInput: true
287+
});
288+
289+
gulp.src([])
290+
.pipe(streamRequiredCompiler({
291+
compilation_level: 'SIMPLE',
292+
warning_level: 'VERBOSE'
293+
}))
294+
.pipe(assert.length(0))
295+
.pipe(assert.end(done));
296+
});
263297
});
264298

265299
describe('in streaming mode', function() {

0 commit comments

Comments
 (0)