Skip to content
Permalink
Browse files

perf(core): Remove decorator DSL which depends on Reflect

BREAKING CHANGE

It is no longer possible to declare classes in this format.

```
Component({...}).
Class({
  constructor: function() {...}
})
```

This format would only work with JIT and with ES5. This mode doesn’t
allow build tools like Webpack to process and optimize the code, which
results in prohibitively large bundles. We are removing this API
because we are trying to ensure that everyone is on the fast path by
default, and it is not possible to get on the fast path using the ES5
DSL. The replacement is to use TypeScript and `@decorator` format.

```
@component({...})
class {
  constructor() {...}
}
```
  • Loading branch information...
mhevery authored and hansl committed Aug 8, 2017
1 parent 679608d commit cac130eff9b9cb608f2308ae40c42c9cd1850c4d
@@ -13,7 +13,7 @@
*/
export * from './metadata';
export * from './version';
export {Class, ClassDefinition, TypeDecorator} from './util/decorators';
export {TypeDecorator} from './util/decorators';
export * from './di';
export {createPlatform, assertPlatform, destroyPlatform, getPlatform, PlatformRef, ApplicationRef, enableProdMode, isDevMode, createPlatformFactory, NgProbeToken} from './application_ref';
export {APP_ID, PACKAGE_ROOT_URL, PLATFORM_INITIALIZER, PLATFORM_ID, APP_BOOTSTRAP_LISTENER} from './application_tokens';
@@ -277,6 +277,7 @@ export abstract class ReflectiveInjector implements Injector {
}

export class ReflectiveInjector_ implements ReflectiveInjector {
private static INJECTOR_KEY = ReflectiveKey.get(Injector);
/** @internal */
_constructionCounter: number = 0;
/** @internal */
@@ -389,7 +390,7 @@ export class ReflectiveInjector_ implements ReflectiveInjector {
}

private _getByKey(key: ReflectiveKey, visibility: Self|SkipSelf|null, notFoundValue: any): any {
if (key === INJECTOR_KEY) {
if (key === ReflectiveInjector_.INJECTOR_KEY) {
return this;
}

@@ -463,8 +464,6 @@ export class ReflectiveInjector_ implements ReflectiveInjector {
toString(): string { return this.displayName; }
}

const INJECTOR_KEY = ReflectiveKey.get(Injector);

function _mapProviders(injector: ReflectiveInjector_, fn: Function): any[] {
const res: any[] = new Array(injector._providers.length);
for (let i = 0; i < injector._providers.length; ++i) {
@@ -73,18 +73,6 @@ export interface AttributeDecorator {
*
* {@example core/ts/metadata/metadata.ts region='attributeFactory'}
*
* ### Example as ES5 DSL
*
* ```
* var MyComponent = ng
* .Component({...})
* .Class({
* constructor: [new ng.Attribute('title'), function(title) {
* ...
* }]
* })
* ```
*
* ### Example as ES5 annotation
*
* ```
@@ -13,8 +13,20 @@ export interface PlatformReflectionCapabilities {
isReflectionEnabled(): boolean;
factory(type: Type<any>): Function;
hasLifecycleHook(type: any, lcProperty: string): boolean;

/**
* Return a list of annotations/types for constructor parameters
*/
parameters(type: Type<any>): any[][];

/**
* Return a list of annotations declared on the class
*/
annotations(type: Type<any>): any[];

/**
* Return a object literal which describes the annotations on Class fields/properties.
*/
propMetadata(typeOrFunc: Type<any>): {[key: string]: any[]};
getter(name: string): GetterFn;
setter(name: string): SetterFn;
@@ -8,9 +8,12 @@

import {Type, isType} from '../type';
import {global, stringify} from '../util';
import {ANNOTATIONS, PARAMETERS, PROP_METADATA} from '../util/decorators';

import {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
import {GetterFn, MethodFn, SetterFn} from './types';


/**
* Attention: This regex has to hold even if the code is minified!
*/
@@ -85,12 +88,11 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
}

// API for metadata created by invoking the decorators.
if (this._reflect != null && this._reflect.getOwnMetadata != null) {
const paramAnnotations = this._reflect.getOwnMetadata('parameters', type);
const paramTypes = this._reflect.getOwnMetadata('design:paramtypes', type);
if (paramTypes || paramAnnotations) {
return this._zipTypesAndAnnotations(paramTypes, paramAnnotations);
}
const paramAnnotations = type.hasOwnProperty(PARAMETERS) && (type as any)[PARAMETERS];
const paramTypes = this._reflect && this._reflect.getOwnMetadata &&
this._reflect.getOwnMetadata('design:paramtypes', type);
if (paramTypes || paramAnnotations) {
return this._zipTypesAndAnnotations(paramTypes, paramAnnotations);
}

// If a class has no decorators, at least create metadata
@@ -130,8 +132,8 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
}

// API for metadata created by invoking the decorators.
if (this._reflect && this._reflect.getOwnMetadata) {
return this._reflect.getOwnMetadata('annotations', typeOrFunc);
if (typeOrFunc.hasOwnProperty(ANNOTATIONS)) {
return (typeOrFunc as any)[ANNOTATIONS];
}
return null;
}
@@ -169,8 +171,8 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
}

// API for metadata created by invoking the decorators.
if (this._reflect && this._reflect.getOwnMetadata) {
return this._reflect.getOwnMetadata('propMetadata', typeOrFunc);
if (typeOrFunc.hasOwnProperty(PROP_METADATA)) {
return (typeOrFunc as any)[PROP_METADATA];
}
return null;
}
Oops, something went wrong.

0 comments on commit cac130e

Please sign in to comment.
You can’t perform that action at this time.