refactor(core): cleanup code with side-effects which was preventing tree-shaking (#30580)
PR Close #30580
This commit is contained in:
@ -12,6 +12,9 @@ import {EventEmitter} from '../event_emitter';
|
||||
import {flatten} from '../util/array_utils';
|
||||
import {getSymbolIterator} from '../util/symbol';
|
||||
|
||||
function symbolIterator<T>(this: QueryList<T>): Iterator<T> {
|
||||
return ((this as any as{_results: Array<T>})._results as any)[getSymbolIterator()]();
|
||||
}
|
||||
|
||||
/**
|
||||
* An unmodifiable list of items that Angular keeps up to date when the state
|
||||
@ -50,6 +53,16 @@ export class QueryList<T>/* implements Iterable<T> */ {
|
||||
// TODO(issue/24571): remove '!'.
|
||||
readonly last !: T;
|
||||
|
||||
constructor() {
|
||||
// This function should be declared on the prototype, but doing so there will cause the class
|
||||
// declaration to have side-effects and become not tree-shakable. For this reason we do it in
|
||||
// the constructor.
|
||||
// [getSymbolIterator()](): Iterator<T> { ... }
|
||||
const symbol = getSymbolIterator();
|
||||
const proto = QueryList.prototype as any;
|
||||
if (!proto[symbol]) proto[symbol] = symbolIterator;
|
||||
}
|
||||
|
||||
/**
|
||||
* See
|
||||
* [Array.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
|
||||
@ -99,8 +112,6 @@ export class QueryList<T>/* implements Iterable<T> */ {
|
||||
*/
|
||||
toArray(): T[] { return this._results.slice(); }
|
||||
|
||||
[getSymbolIterator()](): Iterator<T> { return (this._results as any)[getSymbolIterator()](); }
|
||||
|
||||
toString(): string { return this._results.toString(); }
|
||||
|
||||
/**
|
||||
|
@ -15,20 +15,16 @@ declare var global: any /** TODO #9100 */;
|
||||
// Not yet available in TypeScript: https://github.com/Microsoft/TypeScript/pull/29332
|
||||
declare var globalThis: any /** TODO #9100 */;
|
||||
|
||||
function getGlobal(): any {
|
||||
const __globalThis = typeof globalThis !== 'undefined' && globalThis;
|
||||
const __window = typeof window !== 'undefined' && window;
|
||||
const __self = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' &&
|
||||
self instanceof WorkerGlobalScope && self;
|
||||
const __global = typeof global !== 'undefined' && global;
|
||||
const __globalThis = typeof globalThis !== 'undefined' && globalThis;
|
||||
const __window = typeof window !== 'undefined' && window;
|
||||
const __self = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' &&
|
||||
self instanceof WorkerGlobalScope && self;
|
||||
const __global = typeof global !== 'undefined' && global;
|
||||
|
||||
// Always use __globalThis if available, which is the spec-defined global variable across all
|
||||
// environments, then fallback to __global first, because in Node tests both __global and
|
||||
// __window may be defined and _global should be __global in that case.
|
||||
return __globalThis || __global || __window || __self;
|
||||
}
|
||||
|
||||
const _global = getGlobal();
|
||||
// Always use __globalThis if available, which is the spec-defined global variable across all
|
||||
// environments, then fallback to __global first, because in Node tests both __global and
|
||||
// __window may be defined and _global should be __global in that case.
|
||||
const _global = __globalThis || __global || __window || __self;
|
||||
|
||||
/**
|
||||
* Attention: whenever providing a new value, be sure to add an
|
||||
|
@ -19,9 +19,12 @@ if (typeof ngI18nClosureMode === 'undefined') {
|
||||
// These property accesses can be ignored because ngI18nClosureMode will be set to false
|
||||
// when optimizing code and the whole if statement will be dropped.
|
||||
// Make sure to refer to ngI18nClosureMode as ['ngI18nClosureMode'] for closure.
|
||||
// tslint:disable-next-line:no-toplevel-property-access
|
||||
global['ngI18nClosureMode'] =
|
||||
// TODO(FW-1250): validate that this actually, you know, works.
|
||||
// tslint:disable-next-line:no-toplevel-property-access
|
||||
typeof goog !== 'undefined' && typeof goog.getMsg === 'function';
|
||||
// NOTE: we need to have it in IIFE so that the tree-shaker is happy.
|
||||
(function() {
|
||||
// tslint:disable-next-line:no-toplevel-property-access
|
||||
global['ngI18nClosureMode'] =
|
||||
// TODO(FW-1250): validate that this actually, you know, works.
|
||||
// tslint:disable-next-line:no-toplevel-property-access
|
||||
typeof goog !== 'undefined' && typeof goog.getMsg === 'function';
|
||||
})();
|
||||
}
|
||||
|
Reference in New Issue
Block a user