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

Commit ebc9973

Browse files
Update gulp plugin to use modern logging and errors
Remove deprecated gulp usage
1 parent e3d5862 commit ebc9973

File tree

1 file changed

+33
-32
lines changed
  • packages/google-closure-compiler/lib/gulp

1 file changed

+33
-32
lines changed

packages/google-closure-compiler/lib/gulp/index.js

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
*/
2828

2929
import stream from 'node:stream';
30-
import {createRequire} from 'node:module';
3130
import chalk from 'chalk';
3231
import File from 'vinyl';
3332
import applySourceMap from 'vinyl-sourcemaps-apply';
@@ -37,21 +36,27 @@ import jsonToVinyl from './json-to-vinyl.js';
3736
import Compiler from '../node/index.js';
3837
import {getNativeImagePath, getFirstSupportedPlatform} from '../utils.js';
3938

40-
const require = createRequire(import.meta.url);
4139
const PLUGIN_NAME = 'gulp-google-closure-compiler';
4240

43-
let gulpLog;
44-
try {
45-
gulpLog = require('gulp-util').log;
46-
} catch(e) {
47-
gulpLog = console;
48-
}
41+
const getLogger = async () => {
42+
try {
43+
const { default: fancyLog } = await import('fancy-log');
44+
return fancyLog;
45+
} catch {}
46+
47+
try {
48+
const { default: gulpUtil } = await import('gulp-util');
49+
return gulpUtil.log;
50+
} catch {}
51+
52+
return console;
53+
};
4954

5055
/**
5156
* Rethrow an error with a custom message.
5257
* @see https://stackoverflow.com/a/42755876/1211524
5358
*/
54-
class CustomError extends Error {
59+
class PluginError extends Error {
5560
constructor(plugin, message) {
5661
if (message instanceof Error) {
5762
super(`Error in ${plugin}`, {cause: message});
@@ -61,20 +66,12 @@ class CustomError extends Error {
6166
}
6267
}
6368

64-
const PluginError = (() => {
65-
try {
66-
return require('gulp-util').PluginError;
67-
} catch {
68-
return CustomError;
69-
}
70-
})();
71-
7269
class CompilationStream extends stream.Transform {
7370
constructor(compilationOptions, pluginOptions = {}) {
7471
super({objectMode: true});
7572
this.compilationOptions_ = compilationOptions;
7673
this.streamMode_ = pluginOptions.streamMode || 'BOTH';
77-
this.logger_ = pluginOptions.logger || gulpLog;
74+
this.logger_ = pluginOptions.logger;
7875
this.PLUGIN_NAME_ = pluginOptions.pluginName || PLUGIN_NAME;
7976
this.extraCommandArgs_ = pluginOptions.extraCommandArguments || [];
8077

@@ -154,10 +151,9 @@ class CompilationStream extends stream.Transform {
154151
stdErrData += data;
155152
});
156153
// Error events occur when there was a problem spawning the compiler process
157-
compilerProcess.on('error', async (err) => {
158-
this.emit('error', new PluginError(this.PLUGIN_NAME_,
154+
compilerProcess.on('error', (err) => {
155+
cb( new PluginError(this.PLUGIN_NAME_,
159156
`Process spawn error. Is java in the path?\n${err.message}`));
160-
cb();
161157
});
162158
compilerProcess.stdin.on('error', (err) => {
163159
stdErrData += `Error writing to stdin of the compiler. ${err.message}`;
@@ -187,32 +183,37 @@ class CompilationStream extends stream.Transform {
187183
let outputFiles = [];
188184
if (stdOutData.trim().length > 0) {
189185
if (code !== 0) {
190-
this.emit('error', new PluginError(this.PLUGIN_NAME_, `Compiler error.\n${stdOutData}\n${stdErrData}`));
191-
cb();
186+
cb(new PluginError(this.PLUGIN_NAME_, `Compiler error.\n${stdOutData}\n${stdErrData}`));
192187
return;
193188
}
194189

195190
// stdOutData = stdOutData.substr(stdOutData.indexOf('{'));
196191
try {
197192
outputFiles = JSON.parse(stdOutData);
198-
} catch (e) {
199-
this.emit('error', new PluginError(this.PLUGIN_NAME_, 'Error parsing json encoded files'));
200-
cb();
193+
} catch {
194+
cb(new PluginError(this.PLUGIN_NAME_, 'Error parsing json encoded files'));
201195
return;
202196
}
203197
}
204198

199+
if (!this.logger_) {
200+
this.logger_ = await getLogger();
201+
}
205202
this._compilationComplete(code, outputFiles, stdErrData);
206-
cb();
207203
} catch (err) {
208-
this.emit('error', new PluginError(this.PLUGIN_NAME_, err, { showStack: true }));
209-
cb();
204+
cb(new PluginError(this.PLUGIN_NAME_, err));
205+
return;
210206
}
207+
cb();
211208
}
212209

213210
/**
214211
* @param {number} exitCode
215-
* @param {string} compiledJs
212+
* @param {Array<!{
213+
* path: string,
214+
* src: string,
215+
* sourceMap: (string|undefined)
216+
* }>} compiledJs
216217
* @param {string} errors
217218
* @private
218219
*/
@@ -224,8 +225,8 @@ class CompilationStream extends stream.Transform {
224225
}
225226

226227
// non-zero exit means a compilation error
227-
if (exitCode !== 0) {
228-
this.emit('error', new PluginError(this.PLUGIN_NAME_, 'Compilation errors occurred'));
228+
if (exitCode !== 0 || true) {
229+
throw new Error('Compilation errors occurred');
229230
}
230231

231232
// If present, standard output will be a string of JSON encoded files.

0 commit comments

Comments
 (0)