2727 */
2828
2929import stream from 'node:stream' ;
30- import { createRequire } from 'node:module' ;
3130import chalk from 'chalk' ;
3231import File from 'vinyl' ;
3332import applySourceMap from 'vinyl-sourcemaps-apply' ;
@@ -37,21 +36,27 @@ import jsonToVinyl from './json-to-vinyl.js';
3736import Compiler from '../node/index.js' ;
3837import { getNativeImagePath , getFirstSupportedPlatform } from '../utils.js' ;
3938
40- const require = createRequire ( import . meta. url ) ;
4139const 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-
7269class 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
@@ -196,23 +193,30 @@ class CompilationStream extends stream.Transform {
196193 try {
197194 outputFiles = JSON . parse ( stdOutData ) ;
198195 } catch ( e ) {
199- this . emit ( 'error' , new PluginError ( this . PLUGIN_NAME_ , 'Error parsing json encoded files' ) ) ;
196+ const composedError = new Error ( 'Error parsing json encoded files' , { cause : e } ) ;
197+ this . emit ( 'error' , new PluginError ( this . PLUGIN_NAME_ , composedError ) ) ;
200198 cb ( ) ;
201199 return ;
202200 }
203201 }
204202
203+ if ( ! this . logger_ ) {
204+ this . logger_ = await getLogger ( ) ;
205+ }
205206 this . _compilationComplete ( code , outputFiles , stdErrData ) ;
206- cb ( ) ;
207207 } catch ( err ) {
208- this . emit ( 'error' , new PluginError ( this . PLUGIN_NAME_ , err , { showStack : true } ) ) ;
209- cb ( ) ;
208+ this . emit ( 'error' , new PluginError ( this . PLUGIN_NAME_ , err ) ) ;
210209 }
210+ cb ( ) ;
211211 }
212212
213213 /**
214214 * @param {number } exitCode
215- * @param {string } compiledJs
215+ * @param {Array<!{
216+ * path: string,
217+ * src: string,
218+ * sourceMap: (string|undefined)
219+ * }>} compiledJs
216220 * @param {string } errors
217221 * @private
218222 */
0 commit comments