diff --git a/packages/zone.js/lib/common/error-rewrite.ts b/packages/zone.js/lib/common/error-rewrite.ts index 31d3587743..56a5f87c75 100644 --- a/packages/zone.js/lib/common/error-rewrite.ts +++ b/packages/zone.js/lib/common/error-rewrite.ts @@ -331,8 +331,9 @@ Zone.__load_patch('Error', (global: any, Zone: ZoneType, api: _ZonePrivate) => { // we need to detect all zone related frames, it will // exceed default stackTraceLimit, so we set it to // larger number here, and restore it after detect finish. - const originalStackTraceLimit = Error.stackTraceLimit; - Error.stackTraceLimit = 100; + // We cast through any so we don't need to depend on nodejs typings. + const originalStackTraceLimit = (Error as any).stackTraceLimit; + (Error as any).stackTraceLimit = 100; // we schedule event/micro/macro task, and invoke them // when onSchedule, so we can get all stack traces for // all kinds of tasks with one error thrown. @@ -374,5 +375,5 @@ Zone.__load_patch('Error', (global: any, Zone: ZoneType, api: _ZonePrivate) => { }); }); - Error.stackTraceLimit = originalStackTraceLimit; + (Error as any).stackTraceLimit = originalStackTraceLimit; }); diff --git a/packages/zone.js/lib/zone-spec/long-stack-trace.ts b/packages/zone.js/lib/zone-spec/long-stack-trace.ts index d89e42e51a..4b024edfc2 100644 --- a/packages/zone.js/lib/zone-spec/long-stack-trace.ts +++ b/packages/zone.js/lib/zone-spec/long-stack-trace.ts @@ -78,6 +78,16 @@ function renderLongStackTrace(frames: LongStackTrace[], stack?: string): string return longTrace.join(NEWLINE); } +// if Error.stackTraceLimit is 0, means stack trace +// is disabled, so we don't need to generate long stack trace +// this will improve performance in some test(some test will +// set stackTraceLimit to 0, https://github.com/angular/zone.js/issues/698 +function stackTracesEnabled(): boolean { + // Cast through any since this property only exists on Error in the nodejs + // typings. + return (Error as any).stackTraceLimit > 0; +} + type LongStackTraceZoneSpec = ZoneSpec & {longStackTraceLimit: number}; (Zone as any)['longStackTraceZoneSpec'] = { @@ -99,11 +109,7 @@ type LongStackTraceZoneSpec = ZoneSpec & {longStackTraceLimit: number}; onScheduleTask: function( parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): any { - if (Error.stackTraceLimit > 0) { - // if Error.stackTraceLimit is 0, means stack trace - // is disabled, so we don't need to generate long stack trace - // this will improve performance in some test(some test will - // set stackTraceLimit to 0, https://github.com/angular/zone.js/issues/698 + if (stackTracesEnabled()) { const currentTask = Zone.currentTask; let trace = currentTask && currentTask.data && (currentTask.data as any)[creationTrace] || []; trace = [new LongStackTrace()].concat(trace); @@ -127,11 +133,7 @@ type LongStackTraceZoneSpec = ZoneSpec & {longStackTraceLimit: number}; onHandleError: function( parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any): boolean { - if (Error.stackTraceLimit > 0) { - // if Error.stackTraceLimit is 0, means stack trace - // is disabled, so we don't need to generate long stack trace - // this will improve performance in some test(some test will - // set stackTraceLimit to 0, https://github.com/angular/zone.js/issues/698 + if (stackTracesEnabled()) { const parentTask = Zone.currentTask || error.task; if (error instanceof Error && parentTask) { const longStack = @@ -154,7 +156,7 @@ function captureStackTraces(stackTraces: string[][], count: number): void { } function computeIgnoreFrames() { - if (Error.stackTraceLimit <= 0) { + if (!stackTracesEnabled()) { return; } const frames: string[][] = []; diff --git a/packages/zone.js/lib/zone.ts b/packages/zone.js/lib/zone.ts index bddc4d5a9a..57a98604fe 100644 --- a/packages/zone.js/lib/zone.ts +++ b/packages/zone.js/lib/zone.ts @@ -675,6 +675,12 @@ type AmbientZone = Zone; /** @internal */ type AmbientZoneDelegate = ZoneDelegate; +// CommonJS / Node have global context exposed as "global" variable. +// This code should run in a Browser, so we don't want to include the whole node.d.ts +// typings for this compilation unit. +// We'll just fake the global "global" var for now. +declare var global: NodeJS.Global; + const Zone: ZoneType = (function(global: any) { const performance: {mark(name: string): void; measure(name: string, label: string): void;} = global['performance'];