Skip to content

Commit

Permalink
Merge pull request #55 from broccolijs/lazy-stacks
Browse files Browse the repository at this point in the history
Avoid forcing eager calculation of error stacks
  • Loading branch information
rwjblue authored Mar 11, 2021
2 parents 3b59b0b + d28facf commit 341d445
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Plugin implements TransformNode {
private _needsCache: boolean;
private _volatile: boolean;
private _trackInputChanges: boolean;
private _instantiationStack: string;
private _instantiationError: Error;
private _readCompatError?: Error;
private _readCompat?: ReadCompat | false;
private rebuild?: () => void;
Expand All @@ -80,9 +80,10 @@ class Plugin implements TransformNode {
cachePath?: string;

constructor(inputNodes: InputNode[], options: PluginOptions = {}) {
// Remember current call stack (minus "Error" line)
const errorStack = '' + new Error().stack;
this._instantiationStack = errorStack.replace(/[^\n]*\n/, '');
// capture an instantiation error so that we can lazily access the stack to
// let folks know where a plugin was instantiated from if there is a build
// error
this._instantiationError = new Error();

if (options.name != null) {
this._name = options.name;
Expand Down Expand Up @@ -188,12 +189,20 @@ class Plugin implements TransformNode {
);
}

const instantiationError = this._instantiationError;

const nodeInfo: TransformNodeInfo = {
nodeType: 'transform',
inputNodes: this._inputNodes,
setup: this._setup.bind(this),
getCallbackObject: this.getCallbackObject.bind(this), // .build, indirectly
instantiationStack: this._instantiationStack,

get instantiationStack() {
// Remember current call stack (minus "Error" line)
const errorStack = '' + instantiationError.stack;
return errorStack.replace(/[^\n]*\n/, '');
},

name: this._name,
annotation: this._annotation,
persistentOutput: this._persistentOutput,
Expand Down

0 comments on commit 341d445

Please sign in to comment.