Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: defer loading of pretty-error to improve startup time #1789

Merged
merged 3 commits into from
Apr 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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