Skip to content

Commit

Permalink
fix(closure): make root.ts work with closure (#2546)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexeagle authored and benlesh committed Apr 12, 2017
1 parent dc2e7f1 commit 0ecf55d
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/util/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)');
})();
}

1 comment on commit 0ecf55d

@fmorriso
Copy link

@fmorriso fmorriso commented on 0ecf55d May 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of 2017-05-09, this change in rxjs 5.3.2 breaks Angular 4.1.1 + Angular-CLI 1.0.2 with the following error during a simple ng version call:
RxJS could not find any global context (window, self, global)
Error: RxJS could not find any global context (window, self, global)
at C:\projects\APM-Routed\node_modules@angular\cli\node_modules\rxjs\util\root.js:15:11
at Object. (C:\projects\APM-Routed\node_modules@angular\cli\node_modules\rxjs\util\root.js:16:3)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object. (C:\projects\APM-Routed\node_modules@angular\cli\node_modules\rxjs\Observable.js:2:14)

I look forward to the respective teams (Angular, Angular-CLI, rxjs) working quickly to resolve this issue.

Please sign in to comment.