-
-
Save LayZeeDK/2cd1ff1fc605af6f578a39311b3aba99 to your computer and use it in GitHub Desktop.
import { | |
Type, | |
ɵNG_COMP_DEF, | |
ɵNG_DIR_DEF, | |
ɵNG_MOD_DEF, | |
ɵNG_PIPE_DEF, | |
} from '@angular/core'; | |
function isIvy(): boolean { | |
const ng: any = ((self || global || window) as any).ng; | |
return ng === undefined | |
|| ng.getComponent !== undefined | |
|| ng.applyChanges !== undefined; | |
} | |
function isIvyComponent(componentType: Type<any>): boolean { | |
return (componentType as any)[ɵNG_COMP_DEF] !== undefined; | |
} | |
function isIvyDirective(directiveType: Type<any>): boolean { | |
return (directiveType as any)[ɵNG_DIR_DEF] !== undefined; | |
} | |
function isIvyModule(moduleType: Type<any>): boolean { | |
return (moduleType as any)[ɵNG_MOD_DEF] !== undefined; | |
} | |
function isIvyPipe(pipeType: Type<any>): boolean { | |
return (pipeType as any)[ɵNG_PIPE_DEF] !== undefined; | |
} |
You also may include:
function isIvy(): boolean {
const ng: any = ((self || global || window) as any).ng;
return ng === undefined || ng.probe === undefined;
}
"ng unavailable?" = Ivy.
"ng available with probe property?" = View Engine.
Thanks for the suggestions, Michael. You left out the header of the table though 😄
Updated. Sry for the mistake!
Also here the tests I created, just in case it helps. :)
Functions:
export function getGlobalThis() {
return (self || global || window) as any;
}
export function isIvy(): boolean {
const ng: any = getGlobalThis().ng;
// Is the global ng object is unavailable?
// ng === undefined in Ivy production mode
// View Engine has the ng object both in development mode and production mode.
return (
ng === undefined ||
// in case we are in dev mode in ivy
// `probe` property is available on ng object we use View Engine.
ng.probe === undefined
);
}
Tests:
describe('isIvy', () => {
describe('in ViewEngine Angular 8 + 9', () => {
it('should return false if ng is defined with probe', () => {
getGlobalThis().ng = { probe: true };
expect(isIvy()).toBe(false);
});
});
describe('in Ivy Angular 9', () => {
it('should return true if ng is undefined', () => {
getGlobalThis().ng = undefined;
expect(isIvy()).toBe(true);
});
it('should return true if ng.probe is available', () => {
getGlobalThis().ng = { probe: undefined };
expect(isIvy()).toBe(true);
});
});
});
if ng.probe is set
Should be rephrased 😊
ng.markDirty changed name to ng.applyChanges in v9.
Changed to 'is available'
But the description doesn't match what's tested. It's backwards. If probe is defined, we're in View Engine.
Maybe it's too late for me, but isn't this
it(' isIvy should return false if ng is defined with probe', () => {
getGlobalThis().ng = { probe: true };
expect(isIvy()).toBe(false);
});
correct as
If probe is defined, we're in View Engine.
and
it('should return true if ng is undefined', () => {
getGlobalThis().ng = undefined;
expect(isIvy()).toBe(true);
});
and
it('should return true if ng.probe is available', () => {
getGlobalThis().ng = { probe: undefined };
expect(isIvy()).toBe(true);
});
});
is also correct as ng is undefined or ng is defined but probe is undefined?
"should return true if ng.probe is undefined"
See, it was too late. haha.
Thanks! Updated it.
You may want to include your table:
coreTokens
&probe
__annotations__
(array)coreTokens
&probe
coreTokens
&probe
coreTokens
&probe
ngPipeDef
(object) &decorators
(array)ngPipeDef
(object)getComponent
&markDirty
ɵpipe
(object)ɵpipe
(object)And info on how to use the table:
For Angular versions 8 and 9
window.ng
is an object with propertyprobe
.ngPipeDef
(version 8) orɵpipe
(version 9), both objects.window.ngDevMode
is an object.window.ngDevMode
isundefined
orfalse
.As file to the above gist