Skip to content

Commit 3763bf3

Browse files
Merge pull request #48 from ChadKillingsworth/trim-deps
Reduce Dependencies
2 parents 967c2ec + d07ec8a commit 3763bf3

File tree

5 files changed

+112
-19
lines changed

5 files changed

+112
-19
lines changed

lib/grunt/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
'use strict';
2828

2929
module.exports = function(grunt, extraArguments) {
30-
var chalk = require('gulp-util').colors;
31-
var vinylfs = require('vinyl-fs');
30+
var chalk = require('chalk');
31+
var VinylStream = require('./vinyl-stream');
3232
var fs = require('fs');
3333
var path = require('path');
3434
var gulpCompiler = require('../gulp')({extraArguments: extraArguments});
@@ -71,8 +71,8 @@ module.exports = function(grunt, extraArguments) {
7171
// Source files were provided by grunt. Read these
7272
// in to a stream of vinyl files and pipe them through
7373
// the compiler task
74-
stream = vinylfs.src(files, {base: process.cwd()})
75-
.pipe(gulpCompiler(options, gulpCompilerOptions))
74+
stream = new VinylStream(files, {base: process.cwd()})
75+
.pipe(gulpCompiler(options, gulpCompilerOptions));
7676
} else {
7777
// No source files were provided. Assume the options specify
7878
// --js flags and invoke the compiler without any grunt inputs.

lib/grunt/vinyl-stream.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2016 The Closure Compiler Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* @fileoverview Class to convert an array of file paths to
19+
* as stream of Vinyl files.
20+
*
21+
* @author Chad Killingsworth ([email protected])
22+
*/
23+
'use strict';
24+
25+
var fs = require('fs');
26+
var path = require('path');
27+
var Readable = require('stream').Readable;
28+
var File = require('vinyl');
29+
30+
function VinylStream(files, opts) {
31+
Readable.call(this, {objectMode: true});
32+
this._base = path.resolve(opts.base || process.cwd());
33+
this._files = files.slice();
34+
this.resume();
35+
}
36+
VinylStream.prototype = Object.create(Readable.prototype);
37+
38+
VinylStream.prototype._read = function() {
39+
if (this._files.length === 0) {
40+
this.emit('end');
41+
return;
42+
}
43+
this.readFile();
44+
};
45+
46+
VinylStream.prototype.readFile = function() {
47+
if (this._files.length === 0) {
48+
return;
49+
}
50+
var filepath = this._files.shift();
51+
var fullpath = path.resolve(this._base, filepath);
52+
fs.readFile(fullpath, (function (fullpath, err, data) {
53+
if (err) {
54+
this.emit('error', err);
55+
return;
56+
}
57+
58+
var file = new File({
59+
base: this._base,
60+
path: fullpath,
61+
contents: data
62+
});
63+
64+
if (!this.push(file)) {
65+
return;
66+
} else {
67+
this.readFile();
68+
}
69+
}).bind(this, fullpath));
70+
};
71+
72+
module.exports = VinylStream;

lib/gulp/index.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
'use strict';
3030

31-
3231
/**
3332
* @param {Object<string,string>} initOptions
3433
* @return {function(Object<string,string>|Array<string>):Object}
@@ -37,16 +36,40 @@ module.exports = function(initOptions) {
3736
var filesToJson = require('./concat-to-json');
3837
var jsonToVinyl = require('./json-to-vinyl');
3938
var Compiler = require('../node/closure-compiler');
40-
var gutil = require('gulp-util');
41-
var PluginError = gutil.PluginError;
4239
var stream = require('stream');
4340
/** @const */
4441
var PLUGIN_NAME = 'gulp-google-closure-compiler';
4542

4643
var extraCommandArguments = initOptions ? initOptions.extraArguments : undefined;
4744
var applySourceMap = require('vinyl-sourcemaps-apply');
4845
var path = require('path');
46+
var chalk = require('chalk');
47+
var File = require('vinyl');
48+
49+
/** @constructor */
50+
function CustomError(plugin, msg) {
51+
var superError = Error.call(this) || this;
52+
Error.captureStackTrace(superError, this.constructor);
53+
superError.name = 'Error';
54+
superError.message = msg;
55+
return superError;
56+
}
57+
CustomError.prototype = Object.create(Error.prototype);
58+
CustomError.prototype.name = 'Error';
59+
60+
var PluginError;
61+
try {
62+
PluginError = require('gulp-util').PluginError;
63+
} catch(e) {
64+
PluginError = CustomError;
65+
}
4966

67+
var gulpLog;
68+
try {
69+
gulpLog = require('gulp-util').log;
70+
} catch(e) {
71+
gulpLog = console;
72+
}
5073

5174
function CompilationStream(compilationOptions, pluginOptions) {
5275
stream.Transform.call(this, {objectMode: true});
@@ -55,7 +78,7 @@ module.exports = function(initOptions) {
5578

5679
this.compilatonOptions_ = compilationOptions;
5780
this.streamMode_ = pluginOptions.streamMode || 'BOTH';
58-
this.logger_ = pluginOptions.logger || gutil.log;
81+
this.logger_ = pluginOptions.logger || gulpLog;
5982
this.PLUGIN_NAME_ = pluginOptions.pluginName || PLUGIN_NAME;
6083

6184
this.fileList_ = [];
@@ -68,11 +91,11 @@ module.exports = function(initOptions) {
6891
this._streamInputRequired = false;
6992
process.nextTick((function() {
7093
var stdInStream = new stream.Readable({ read: function() {
71-
return new gutil.File();
94+
return new File();
7295
}});
73-
stdInStream.pipe(this);
74-
stdInStream.push(null);
75-
}).bind(this));
96+
stdInStream.pipe(this);
97+
stdInStream.push(null);
98+
}).bind(this));
7699
return this;
77100
};
78101

@@ -151,7 +174,7 @@ module.exports = function(initOptions) {
151174

152175
// standard error will contain compilation warnings, log those
153176
if (stdErrData.trim().length > 0) {
154-
logger(gutil.colors.yellow(this.PLUGIN_NAME_) + ': ' + stdErrData);
177+
logger(chalk.yellow(this.PLUGIN_NAME_) + ': ' + stdErrData);
155178
}
156179

157180
// non-zero exit means a compilation error

lib/gulp/json-to-vinyl.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323

2424
'use strict';
2525

26-
var gutil = require('gulp-util');
27-
var File = gutil.File;
26+
var File = require('vinyl');
2827
var path = require('path');
2928

3029
/**

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "google-closure-compiler",
3-
"version": "20161024.1.0",
3+
"version": "20161024.2.0",
44
"description": "Check, compile, optimize and compress Javascript with Closure-Compiler",
55
"repository": {
66
"type": "git",
@@ -37,8 +37,7 @@
3737
"homepage": "https://developers.google.com/closure/compiler/",
3838
"dependencies": {
3939
"chalk": "^1.0.0",
40-
"gulp-util": "^3.0.7",
41-
"vinyl-fs": "^2.2.1",
40+
"vinyl": "^1.2.0",
4241
"vinyl-sourcemaps-apply": "^0.2.0"
4342
},
4443
"devDependencies": {
@@ -58,6 +57,6 @@
5857
"pretest": "./build_compiler.js"
5958
},
6059
"engines": {
61-
"node" : ">=0.12.0"
60+
"node": ">=0.12.0"
6261
}
6362
}

0 commit comments

Comments
 (0)