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

Commit a5fd957

Browse files
Merge pull request #26 from ChadKillingsworth/fix-no-gulp-src
Add a .src method to the gulp plugin to support usage without gulp.src
2 parents 35bb7c2 + 2b1b6b4 commit a5fd957

File tree

4 files changed

+38
-55
lines changed

4 files changed

+38
-55
lines changed

README.md

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ gulp.task('js-compile', function () {
219219
output_wrapper: '(function(){\n%output%\n}).call(this)',
220220
js_output_file: 'output.min.js'
221221
})
222+
.src() // needed to force the plugin to run without gulp.src
222223
.pipe(gulp.dest('./dist/js'));
223224
});
224225
```
@@ -241,33 +242,11 @@ gulp.task('js-compile', function () {
241242
'--js_output_file', 'out.js',
242243
'--debug'
243244
])
245+
.src() // needed to force the plugin to run without gulp.src
244246
.pipe(gulp.dest('./dist/js'));
245247
});
246248
```
247249

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-
271250
### Gulp Sourcemaps
272251
The gulp plugin supports gulp sourcemaps.
273252

lib/grunt/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ module.exports = function(grunt, extraArguments) {
3636
var gulpCompilerOptions = {
3737
streamMode: 'IN',
3838
logger: grunt.log,
39-
pluginName: 'grunt-google-closure-compiler'
39+
pluginName: 'grunt-google-closure-compiler',
40+
requireStreamInput: false
4041
};
4142

4243
/**

lib/gulp/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = function(initOptions) {
4242
var stream = require('stream');
4343
/** @const */
4444
var PLUGIN_NAME = 'gulp-google-closure-compiler';
45-
var streamInputRequired = initOptions ? !!initOptions.requireStreamInput : false;
45+
4646
var extraCommandArguments = initOptions ? initOptions.extraArguments : undefined;
4747
var SourceMapGenerator = require('source-map').SourceMapGenerator;
4848
var SourceMapConsumer = require('source-map').SourceMapConsumer;
@@ -61,10 +61,23 @@ module.exports = function(initOptions) {
6161
this.PLUGIN_NAME_ = pluginOptions.pluginName || PLUGIN_NAME;
6262

6363
this.fileList_ = [];
64-
64+
this._streamInputRequired = pluginOptions.requireStreamInput !== false;
6565
}
6666
CompilationStream.prototype = Object.create(stream.Transform.prototype);
6767

68+
// Buffer the files into an array
69+
CompilationStream.prototype.src = function() {
70+
this._streamInputRequired = false;
71+
process.nextTick((function() {
72+
var stdInStream = new stream.Readable({ read: function() {
73+
return new gutil.File();
74+
}});
75+
stdInStream.pipe(this);
76+
stdInStream.push(null);
77+
}).bind(this));
78+
return this;
79+
};
80+
6881
// Buffer the files into an array
6982
CompilationStream.prototype._transform = function(file, enc, cb) {
7083
// ignore empty files
@@ -80,7 +93,6 @@ module.exports = function(initOptions) {
8093
}
8194

8295
this.fileList_.push(file);
83-
8496
cb();
8597
};
8698

@@ -91,7 +103,7 @@ module.exports = function(initOptions) {
91103
jsonFiles = filesToJson(this.fileList_);
92104
} else {
93105
// If files in the stream were required, no compilation needed here.
94-
if (streamInputRequired) {
106+
if (this._streamInputRequired) {
95107
this.emit('end');
96108
cb();
97109
return;

test/gulp.js

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,12 @@ describe('gulp-google-closure-compiler', function() {
220220
compilation_level: 'SIMPLE',
221221
warning_level: 'VERBOSE'
222222
})
223+
.src()
223224
.pipe(assert.length(1))
224225
.pipe(assert.first(function(f) {
225-
f.contents.toString().should.eql('function log(a){console.log(a)}log("one.js");log("two.js");\n');
226+
f.contents.toString().should.eql(fixturesCompiled);
226227
}))
227228
.pipe(assert.end(done));
228-
229-
stream.end();
230229
});
231230

232231
it('should include js options before gulp.src files', function(done) {
@@ -255,46 +254,38 @@ describe('gulp-google-closure-compiler', function() {
255254
'--compilation_level=SIMPLE',
256255
'--warning_level=VERBOSE'
257256
])
257+
.src()
258258
.pipe(assert.length(1))
259259
.pipe(assert.first(function(f) {
260260
f.contents.toString().should.eql(fixturesCompiled);
261261
}))
262262
.pipe(assert.end(done));
263-
264-
stream.end();
265263
});
266264

267-
it('should compile gulp.src files when stream input is required', function(done) {
268-
this.timeout(30000);
269-
this.slow(10000);
270-
271-
var streamRequiredCompiler = compilerPackage.gulp({
272-
requireStreamInput: true
273-
});
274-
275-
gulp.src(__dirname + '/fixtures/**.js')
276-
.pipe(streamRequiredCompiler({
265+
it('should generate no output without gulp.src files', function(done) {
266+
gulp.src([])
267+
.pipe(closureCompiler({
277268
compilation_level: 'SIMPLE',
278269
warning_level: 'VERBOSE'
279270
}))
280-
.pipe(assert.length(1))
281-
.pipe(assert.first(function(f) {
282-
f.contents.toString().should.eql(fixturesCompiled);
283-
}))
271+
.pipe(assert.length(0))
284272
.pipe(assert.end(done));
285273
});
286274

287-
it('should generate no output without gulp.src files when stream input is required', function(done) {
288-
var streamRequiredCompiler = compilerPackage.gulp({
289-
requireStreamInput: true
290-
});
275+
it('should compile without gulp.src files when .src() is called', function(done) {
276+
this.timeout(30000);
277+
this.slow(10000);
291278

292-
gulp.src([])
293-
.pipe(streamRequiredCompiler({
279+
closureCompiler({
294280
compilation_level: 'SIMPLE',
295-
warning_level: 'VERBOSE'
281+
warning_level: 'VERBOSE',
282+
js: __dirname + '/fixtures/**.js'
283+
})
284+
.src()
285+
.pipe(assert.length(1))
286+
.pipe(assert.first(function(f) {
287+
f.contents.toString().should.eql(fixturesCompiled);
296288
}))
297-
.pipe(assert.length(0))
298289
.pipe(assert.end(done));
299290
});
300291

0 commit comments

Comments
 (0)