Skip to content

Commit 0ecf55d

Browse files
alexeaglebenlesh
authored andcommitted
fix(closure): make root.ts work with closure (#2546)
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 angular/tsickle#420 2) refactor the conditional logic for finding the root object See alexeagle/closure-compiler-angular-bundling#15 Closure seems to statically reduce the existing code and eliminates the conditional, making it always throw.
1 parent dc2e7f1 commit 0ecf55d

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/util/root.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ declare module NodeJS {
1212
* self: browser in WebWorker
1313
* global: Node.js/other
1414
*/
15-
export const root: any = (
16-
typeof window == 'object' && window.window === window && window
17-
|| typeof self == 'object' && self.self === self && self
18-
|| typeof global == 'object' && global.global === global && global
19-
);
20-
21-
if (!root) {
22-
throw new Error('RxJS could not find any global context (window, self, global)');
15+
export let root: any;
16+
if (typeof window == 'object' && window.window === window) {
17+
root = window;
18+
} else if (typeof self == 'object' && self.self === self) {
19+
root = self;
20+
} else if (typeof global == 'object' && global.global === global) {
21+
root = global;
22+
} else {
23+
// Workaround Closure Compiler restriction: The body of a goog.module cannot use throw.
24+
// This is needed when used with angular/tsickle which inserts a goog.module statement.
25+
// Wrap in IIFE
26+
(function () {
27+
throw new Error('RxJS could not find any global context (window, self, global)');
28+
})();
2329
}

0 commit comments

Comments
 (0)