Skip to content

Commit

Permalink
fix: defer loading of pretty-error to improve startup time
Browse files Browse the repository at this point in the history
  • Loading branch information
Knagis committed Apr 14, 2023
1 parent d5ce5a8 commit 064f300
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions lib/errors.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// @ts-nocheck
'use strict';
const PrettyError = require('pretty-error');
const prettyError = new PrettyError();
prettyError.withoutColors();
prettyError.skipPackage('html-plugin-evaluation');
prettyError.skipNodeFiles();
prettyError.skip(function (traceLine) {
return traceLine.path === 'html-plugin-evaluation';
});

let prettyError;

function getPrettyError () {
if (!prettyError) {
// lazily require to improve startup time since pretty-error is rather heavy package
const PrettyError = require('pretty-error');
prettyError = new PrettyError();
prettyError.withoutColors();
prettyError.skipPackage('html-plugin-evaluation');
prettyError.skipNodeFiles();
prettyError.skip(function (traceLine) {
return traceLine.path === 'html-plugin-evaluation';
});
}
return prettyError;
}

module.exports = function (err, context) {
return {
Expand All @@ -19,7 +28,7 @@ module.exports = function (err, context) {
},
toString: function () {
try {
return prettyError.render(err).replace(/webpack:\/\/\/\./g, context);
return getPrettyError().render(err).replace(/webpack:\/\/\/\./g, context);
} catch (e) {
// This can sometimes fail. We don't know why, but returning the
// original error is better than returning the error thrown by
Expand Down

0 comments on commit 064f300

Please sign in to comment.