Skip to content

Commit 586af0a

Browse files
fix: catch error when loading minimizers (#639)
1 parent 15ccc60 commit 586af0a

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const {
6969

7070
/**
7171
* @typedef {object} MinimizedResult
72-
* @property {string} code code
72+
* @property {string=} code code
7373
* @property {RawSourceMap=} map source map
7474
* @property {Array<Error | string>=} errors errors
7575
* @property {Array<Error | string>=} warnings warnings

src/utils.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,13 @@ async function terserMinify(
243243
// toplevel: terserOptions.toplevel
244244
});
245245

246-
const { minify } = require("terser");
246+
let minify;
247+
248+
try {
249+
({ minify } = require("terser"));
250+
} catch (err) {
251+
return { errors: [/** @type {Error} */ (err)] };
252+
}
247253

248254
// Copy `terser` options
249255
const terserOptions = buildTerserOptions(minimizerOptions);
@@ -487,7 +493,13 @@ async function uglifyJsMinify(
487493
};
488494
};
489495

490-
const { minify } = require("uglify-js");
496+
let minify;
497+
498+
try {
499+
({ minify } = require("uglify-js"));
500+
} catch (err) {
501+
return { errors: [/** @type {Error} */ (err)] };
502+
}
491503

492504
// Copy `uglify-js` options
493505
const uglifyJsOptions = buildUglifyJsOptions(minimizerOptions);
@@ -578,7 +590,13 @@ async function swcMinify(input, sourceMap, minimizerOptions) {
578590
sourceMap: undefined,
579591
});
580592

581-
const swc = require("@swc/core");
593+
let swc;
594+
595+
try {
596+
swc = require("@swc/core");
597+
} catch (err) {
598+
return { errors: [/** @type {Error} */ (err)] };
599+
}
582600

583601
// Copy `swc` options
584602
const swcOptions = buildSwcOptions(minimizerOptions);
@@ -673,7 +691,13 @@ async function esbuildMinify(input, sourceMap, minimizerOptions) {
673691
};
674692
};
675693

676-
const esbuild = require("esbuild");
694+
let esbuild;
695+
696+
try {
697+
esbuild = require("esbuild");
698+
} catch (err) {
699+
return { errors: [/** @type {Error} */ (err)] };
700+
}
677701

678702
// Copy `esbuild` options
679703
const esbuildOptions = buildEsbuildOptions(minimizerOptions);

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ type MinimizedResult = {
183183
/**
184184
* code
185185
*/
186-
code: string;
186+
code?: string | undefined;
187187
/**
188188
* source map
189189
*/

0 commit comments

Comments
 (0)