From 78a0193105c05a333d510239ba6a605846b27691 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Tue, 11 Apr 2017 08:17:28 -0700 Subject: [PATCH] fix(closure): make root.ts work with closure 1) don't throw at top-level scope. Closure compiler does not allow this if a goog.module statement is present in the file. We need this modification to use RxJS with angular/tsickle. Addresses https://github.com/angular/tsickle/issues/420 2) refactor the conditional logic for finding the root object See https://github.com/alexeagle/closure-compiler-angular-bundling/issues/15 Closure seems to statically reduce the existing code and eliminates the conditional, making it always throw. --- src/util/root.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/util/root.ts b/src/util/root.ts index e5b4d1c3b1..079faefcc7 100644 --- a/src/util/root.ts +++ b/src/util/root.ts @@ -12,12 +12,18 @@ declare module NodeJS { * self: browser in WebWorker * global: Node.js/other */ -export const root: any = ( - typeof window == 'object' && window.window === window && window - || typeof self == 'object' && self.self === self && self - || typeof global == 'object' && global.global === global && global -); - -if (!root) { - throw new Error('RxJS could not find any global context (window, self, global)'); +export let root: any; +if (typeof window == 'object' && window.window === window) { + root = window; +} else if (typeof self == 'object' && self.self === self) { + root = self; +} else if (typeof global == 'object' && global.global === global) { + root = global; +} else { + // Workaround Closure Compiler restriction: The body of a goog.module cannot use throw. + // This is needed when used with angular/tsickle which inserts a goog.module statement. + // Wrap in IIFE + (function () { + throw new Error('RxJS could not find any global context (window, self, global)'); + })(); }