X Tutup
Skip to content

Commit 76247b7

Browse files
committed
refactor(compiler): use the new compiler everywhere
Closes angular#3605 BREAKING CHANGE: - we don't mark an element as bound any more if it only contains text bindings E.g. <div>{{hello}}</div> This changes the indices when using `DebugElement.componentViewChildren` / `DebugElement.children`. - `@Directive.compileChildren` was removed, `ng-non-bindable` is now builtin and not a directive any more - angular no more adds the `ng-binding` class to elements with bindings - directives are now ordered as they are listed in the View.directives regarding change detection. Previously they had an undefined order. - the `Renderer` interface has new methods `createProtoView` and `registerComponentTemplate`. See `DomRenderer` for default implementations. - reprojection with `ng-content` is now all or nothing per `ng-content` element - angular2 transformer can't be used in tests that modify directive metadata. Use `angular2/src/transform/inliner_for_test` transformer instead.
1 parent 30ca043 commit 76247b7

File tree

124 files changed

+2014
-3452
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+2014
-3452
lines changed

modules/angular2/src/compiler/change_definition_factory.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ function createChangeDefinitions(pvVisitors: ProtoViewVisitor[], componentType:
189189
genConfig: ChangeDetectorGenConfig): ChangeDetectorDefinition[] {
190190
var pvVariableNames = _collectNestedProtoViewsVariableNames(pvVisitors);
191191
return pvVisitors.map(pvVisitor => {
192-
var viewType = pvVisitor.viewIndex === 0 ? 'component' : 'embedded';
193-
var id = _protoViewId(componentType, pvVisitor.viewIndex, viewType);
192+
var id = `${componentType.name}_${pvVisitor.viewIndex}`;
194193
return new ChangeDetectorDefinition(
195194
id, pvVisitor.strategy, pvVariableNames[pvVisitor.viewIndex], pvVisitor.bindingRecords,
196195
pvVisitor.eventRecords, pvVisitor.directiveRecords, genConfig);
@@ -207,9 +206,3 @@ function _collectNestedProtoViewsVariableNames(pvVisitors: ProtoViewVisitor[]):
207206
});
208207
return nestedPvVariableNames;
209208
}
210-
211-
212-
function _protoViewId(hostComponentType: CompileTypeMetadata, pvIndex: number, viewType: string):
213-
string {
214-
return `${hostComponentType.name}_${viewType}_${pvIndex}`;
215-
}

modules/angular2/src/compiler/change_detector_compiler.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ import {
1616

1717
import {TemplateAst} from './template_ast';
1818
import {Codegen} from 'angular2/src/transform/template_compiler/change_detector_codegen';
19-
import {IS_DART} from './util';
19+
import {IS_DART, MODULE_SUFFIX} from './util';
2020
import {Injectable} from 'angular2/src/core/di';
2121

2222
const ABSTRACT_CHANGE_DETECTOR = "AbstractChangeDetector";
2323
const UTIL = "ChangeDetectionUtil";
2424

25-
var ABSTRACT_CHANGE_DETECTOR_MODULE =
26-
moduleRef('angular2/src/core/change_detection/abstract_change_detector');
27-
var UTIL_MODULE = moduleRef('angular2/src/core/change_detection/change_detection_util');
28-
var PREGEN_PROTO_CHANGE_DETECTOR_MODULE =
29-
moduleRef('angular2/src/core/change_detection/pregen_proto_change_detector');
25+
var ABSTRACT_CHANGE_DETECTOR_MODULE = moduleRef(
26+
`package:angular2/src/core/change_detection/abstract_change_detector${MODULE_SUFFIX}`);
27+
var UTIL_MODULE =
28+
moduleRef(`package:angular2/src/core/change_detection/change_detection_util${MODULE_SUFFIX}`);
29+
var PREGEN_PROTO_CHANGE_DETECTOR_MODULE = moduleRef(
30+
`package:angular2/src/core/change_detection/pregen_proto_change_detector${MODULE_SUFFIX}`);
3031

3132
@Injectable()
3233
export class ChangeDetectionCompiler {
@@ -54,24 +55,31 @@ export class ChangeDetectionCompiler {
5455
var changeDetectorDefinitions =
5556
createChangeDetectorDefinitions(componentType, strategy, this._genConfig, parsedTemplate);
5657
var factories = [];
58+
var index = 0;
5759
var sourceParts = changeDetectorDefinitions.map(definition => {
5860
var codegen: any;
61+
var sourcePart;
5962
// TODO(tbosch): move the 2 code generators to the same place, one with .dart and one with .ts
6063
// suffix
6164
// and have the same API for calling them!
6265
if (IS_DART) {
6366
codegen = new Codegen(PREGEN_PROTO_CHANGE_DETECTOR_MODULE);
6467
var className = definition.id;
65-
codegen.generate(componentType.name, className, definition);
68+
var typeRef = (index === 0 && componentType.isHost) ?
69+
'dynamic' :
70+
`${moduleRef(componentType.moduleUrl)}${componentType.name}`;
71+
codegen.generate(typeRef, className, definition);
6672
factories.push(`(dispatcher) => new ${className}(dispatcher)`);
67-
return codegen.toString();
73+
sourcePart = codegen.toString();
6874
} else {
6975
codegen = new ChangeDetectorJITGenerator(
7076
definition, `${UTIL_MODULE}${UTIL}`,
7177
`${ABSTRACT_CHANGE_DETECTOR_MODULE}${ABSTRACT_CHANGE_DETECTOR}`);
7278
factories.push(`function(dispatcher) { return new ${codegen.typeName}(dispatcher); }`);
73-
return codegen.generateSource();
79+
sourcePart = codegen.generateSource();
7480
}
81+
index++;
82+
return sourcePart;
7583
});
7684
return new SourceExpressions(sourceParts, factories);
7785
}

modules/angular2/src/compiler/command_compiler.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ import {
3737
shimContentAttributeExpr,
3838
shimHostAttributeExpr
3939
} from './style_compiler';
40-
import {escapeSingleQuoteString} from './util';
40+
import {escapeSingleQuoteString, MODULE_SUFFIX} from './util';
4141
import {Injectable} from 'angular2/src/core/di';
4242

43-
export var TEMPLATE_COMMANDS_MODULE_REF = moduleRef('angular2/src/core/compiler/template_commands');
43+
export var TEMPLATE_COMMANDS_MODULE_REF =
44+
moduleRef(`package:angular2/src/core/compiler/template_commands${MODULE_SUFFIX}`);
4445

4546
const IMPLICIT_TEMPLATE_VAR = '\$implicit';
4647

@@ -353,6 +354,6 @@ function codeGenArray(data: any[]): string {
353354

354355
function codeGenDirectivesArray(directives: CompileDirectiveMetadata[]): string {
355356
var expressions = directives.map(
356-
directiveType => `${moduleRef(directiveType.type.moduleId)}${directiveType.type.name}`);
357+
directiveType => `${moduleRef(directiveType.type.moduleUrl)}${directiveType.type.name}`);
357358
return `[${expressions.join(',')}]`;
358359
}

modules/angular2/src/compiler/compiler.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import {StyleCompiler} from 'angular2/src/compiler/style_compiler';
1717
import {CommandCompiler} from 'angular2/src/compiler/command_compiler';
1818
import {TemplateCompiler} from 'angular2/src/compiler/template_compiler';
1919
import {ChangeDetectorGenConfig} from 'angular2/src/core/change_detection/change_detection';
20+
import {Compiler} from 'angular2/src/core/compiler/compiler';
21+
import {RuntimeCompiler} from 'angular2/src/compiler/runtime_compiler';
22+
import {ElementSchemaRegistry} from 'angular2/src/core/render/dom/schema/element_schema_registry';
23+
import {
24+
DomElementSchemaRegistry
25+
} from 'angular2/src/core/render/dom/schema/dom_element_schema_registry';
2026

2127
export function compilerBindings(): Array<Type | Binding | any[]> {
2228
return [
@@ -31,5 +37,9 @@ export function compilerBindings(): Array<Type | Binding | any[]> {
3137
.toValue(
3238
new ChangeDetectorGenConfig(assertionsEnabled(), assertionsEnabled(), false, true)),
3339
TemplateCompiler,
40+
RuntimeCompiler,
41+
bind(Compiler).toAlias(RuntimeCompiler),
42+
DomElementSchemaRegistry,
43+
bind(ElementSchemaRegistry).toAlias(DomElementSchemaRegistry)
3444
];
3545
}

modules/angular2/src/compiler/directive_metadata.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,27 @@ var HOST_REG_EXP = /^(?:(?:\[([^\]]+)\])|(?:\(([^\)]+)\)))$/g;
2424
export class CompileTypeMetadata {
2525
runtime: Type;
2626
name: string;
27-
moduleId: string;
28-
constructor({runtime, name, moduleId}: {runtime?: Type, name?: string, moduleId?: string} = {}) {
27+
moduleUrl: string;
28+
isHost: boolean;
29+
constructor({runtime, name, moduleUrl, isHost}:
30+
{runtime?: Type, name?: string, moduleUrl?: string, isHost?: boolean} = {}) {
2931
this.runtime = runtime;
3032
this.name = name;
31-
this.moduleId = moduleId;
33+
this.moduleUrl = moduleUrl;
34+
this.isHost = normalizeBool(isHost);
3235
}
3336

3437
static fromJson(data: StringMap<string, any>): CompileTypeMetadata {
35-
return new CompileTypeMetadata({name: data['name'], moduleId: data['moduleId']});
38+
return new CompileTypeMetadata(
39+
{name: data['name'], moduleUrl: data['moduleUrl'], isHost: data['isHost']});
3640
}
3741

3842
toJson(): StringMap<string, any> {
3943
return {
4044
// Note: Runtime type can't be serialized...
4145
'name': this.name,
42-
'moduleId': this.moduleId
46+
'moduleUrl': this.moduleUrl,
47+
'isHost': this.isHost
4348
};
4449
}
4550
}
@@ -248,8 +253,12 @@ export function createHostComponentMeta(componentType: CompileTypeMetadata,
248253
componentSelector: string): CompileDirectiveMetadata {
249254
var template = CssSelector.parse(componentSelector)[0].getMatchingElementTemplate();
250255
return CompileDirectiveMetadata.create({
251-
type: new CompileTypeMetadata(
252-
{runtime: Object, name: `Host${componentType.name}`, moduleId: componentType.moduleId}),
256+
type: new CompileTypeMetadata({
257+
runtime: Object,
258+
name: `Host${componentType.name}`,
259+
moduleUrl: componentType.moduleUrl,
260+
isHost: true
261+
}),
253262
template: new CompileTemplateMetadata(
254263
{template: template, templateUrl: '', styles: [], styleUrls: [], ngContentSelectors: []}),
255264
changeDetection: ChangeDetectionStrategy.Default,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {Compiler, internalCreateProtoView} from 'angular2/src/core/compiler/compiler';
2+
import {ProtoViewRef} from 'angular2/src/core/compiler/view_ref';
3+
import {ProtoViewFactory} from 'angular2/src/core/compiler/proto_view_factory';
4+
import {TemplateCompiler} from './template_compiler';
5+
6+
import {Injectable} from 'angular2/src/core/di';
7+
import {Type} from 'angular2/src/core/facade/lang';
8+
import {Promise, PromiseWrapper} from 'angular2/src/core/facade/async';
9+
10+
@Injectable()
11+
export class RuntimeCompiler extends Compiler {
12+
/**
13+
* @private
14+
*/
15+
constructor(_protoViewFactory: ProtoViewFactory, private _templateCompiler: TemplateCompiler) {
16+
super(_protoViewFactory);
17+
}
18+
19+
compileInHost(componentType: Type): Promise<ProtoViewRef> {
20+
return this._templateCompiler.compileHostComponentRuntime(componentType)
21+
.then(compiledHostTemplate => internalCreateProtoView(this, compiledHostTemplate));
22+
}
23+
24+
clearCache() {
25+
super.clearCache();
26+
this._templateCompiler.clearCache();
27+
}
28+
}

modules/angular2/src/compiler/runtime_metadata.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {hasLifecycleHook} from 'angular2/src/core/compiler/directive_lifecycle_r
1818
import {LifecycleHooks, LIFECYCLE_HOOKS_VALUES} from 'angular2/src/core/compiler/interfaces';
1919
import {reflector} from 'angular2/src/core/reflection/reflection';
2020
import {Injectable} from 'angular2/src/core/di';
21+
import {MODULE_SUFFIX} from './util';
2122

2223
// group 1: "property" from "[property]"
2324
// group 2: "event" from "(event)"
@@ -33,7 +34,7 @@ export class RuntimeMetadataResolver {
3334
var meta = this._cache.get(directiveType);
3435
if (isBlank(meta)) {
3536
var directiveAnnotation = this._directiveResolver.resolve(directiveType);
36-
var moduleId = calcModuleId(directiveType, directiveAnnotation);
37+
var moduleUrl = calcModuleUrl(directiveType, directiveAnnotation);
3738
var templateMeta = null;
3839
var changeDetectionStrategy = null;
3940

@@ -55,7 +56,7 @@ export class RuntimeMetadataResolver {
5556
isComponent: isPresent(templateMeta),
5657
dynamicLoadable: true,
5758
type: new cpl.CompileTypeMetadata(
58-
{name: stringify(directiveType), moduleId: moduleId, runtime: directiveType}),
59+
{name: stringify(directiveType), moduleUrl: moduleUrl, runtime: directiveType}),
5960
template: templateMeta,
6061
changeDetection: changeDetectionStrategy,
6162
inputs: directiveAnnotation.inputs,
@@ -111,10 +112,10 @@ function isValidDirective(value: Type): boolean {
111112
return isPresent(value) && (value instanceof Type);
112113
}
113114

114-
function calcModuleId(type: Type, directiveAnnotation: dirAnn.DirectiveMetadata): string {
115+
function calcModuleUrl(type: Type, directiveAnnotation: dirAnn.DirectiveMetadata): string {
115116
if (isPresent(directiveAnnotation.moduleId)) {
116-
return directiveAnnotation.moduleId;
117+
return `package:${directiveAnnotation.moduleId}${MODULE_SUFFIX}`;
117118
} else {
118-
return reflector.moduleId(type);
119+
return reflector.importUri(type);
119120
}
120121
}

modules/angular2/src/compiler/source_module.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@ import {StringWrapper, isBlank} from 'angular2/src/core/facade/lang';
22

33
var MODULE_REGEXP = /#MODULE\[([^\]]*)\]/g;
44

5-
export function moduleRef(moduleId): string {
6-
return `#MODULE[${moduleId}]`;
5+
export function moduleRef(moduleUrl): string {
6+
return `#MODULE[${moduleUrl}]`;
77
}
88

99
export class SourceModule {
10-
constructor(public moduleId: string, public sourceWithModuleRefs: string) {}
10+
constructor(public moduleUrl: string, public sourceWithModuleRefs: string) {}
1111

1212
getSourceWithImports(): SourceWithImports {
1313
var moduleAliases = {};
1414
var imports: string[][] = [];
1515
var newSource =
1616
StringWrapper.replaceAllMapped(this.sourceWithModuleRefs, MODULE_REGEXP, (match) => {
17-
var moduleId = match[1];
18-
var alias = moduleAliases[moduleId];
17+
var moduleUrl = match[1];
18+
var alias = moduleAliases[moduleUrl];
1919
if (isBlank(alias)) {
20-
if (moduleId == this.moduleId) {
20+
if (moduleUrl == this.moduleUrl) {
2121
alias = '';
2222
} else {
2323
alias = `import${imports.length}`;
24-
imports.push([moduleId, alias]);
24+
imports.push([moduleUrl, alias]);
2525
}
26-
moduleAliases[moduleId] = alias;
26+
moduleAliases[moduleUrl] = alias;
2727
}
2828
return alias.length > 0 ? `${alias}.` : '';
2929
});

modules/angular2/src/compiler/style_compiler.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
codeGenMapArray,
1515
codeGenReplaceAll,
1616
codeGenExportVariable,
17-
codeGenToString
17+
codeGenToString,
18+
MODULE_SUFFIX
1819
} from './util';
1920
import {Injectable} from 'angular2/src/core/di';
2021

@@ -56,13 +57,15 @@ export class StyleCompiler {
5657
return this._styleCodeGen(template.styles, template.styleUrls, shim, suffix);
5758
}
5859

59-
compileStylesheetCodeGen(moduleId: string, cssText: string): SourceModule[] {
60-
var styleWithImports = resolveStyleUrls(this._urlResolver, moduleId, cssText);
60+
compileStylesheetCodeGen(stylesheetUrl: string, cssText: string): SourceModule[] {
61+
var styleWithImports = resolveStyleUrls(this._urlResolver, stylesheetUrl, cssText);
6162
return [
62-
this._styleModule(moduleId, false, this._styleCodeGen([styleWithImports.style],
63-
styleWithImports.styleUrls, false, '')),
64-
this._styleModule(moduleId, true, this._styleCodeGen([styleWithImports.style],
65-
styleWithImports.styleUrls, true, ''))
63+
this._styleModule(
64+
stylesheetUrl, false,
65+
this._styleCodeGen([styleWithImports.style], styleWithImports.styleUrls, false, '')),
66+
this._styleModule(
67+
stylesheetUrl, true,
68+
this._styleCodeGen([styleWithImports.style], styleWithImports.styleUrls, true, ''))
6669
];
6770
}
6871

@@ -96,28 +99,28 @@ export class StyleCompiler {
9699
expressionSource +=
97100
`[${plainStyles.map( plainStyle => escapeSingleQuoteString(this._shimIfNeeded(plainStyle, shim)) ).join(',')}]`;
98101
for (var i = 0; i < absUrls.length; i++) {
99-
var moduleId = this._shimModuleIdIfNeeded(absUrls[i], shim);
100-
expressionSource += codeGenConcatArray(`${moduleRef(moduleId)}STYLES`);
102+
var moduleUrl = this._createModuleUrl(absUrls[i], shim);
103+
expressionSource += codeGenConcatArray(`${moduleRef(moduleUrl)}STYLES`);
101104
}
102105
expressionSource += `)${suffix}`;
103106
return new SourceExpression([], expressionSource);
104107
}
105108

106-
private _styleModule(moduleId: string, shim: boolean,
109+
private _styleModule(stylesheetUrl: string, shim: boolean,
107110
expression: SourceExpression): SourceModule {
108111
var moduleSource = `
109112
${expression.declarations.join('\n')}
110113
${codeGenExportVariable('STYLES')}${expression.expression};
111114
`;
112-
return new SourceModule(this._shimModuleIdIfNeeded(moduleId, shim), moduleSource);
115+
return new SourceModule(this._createModuleUrl(stylesheetUrl, shim), moduleSource);
113116
}
114117

115118
private _shimIfNeeded(style: string, shim: boolean): string {
116119
return shim ? this._shadowCss.shimCssText(style, CONTENT_ATTR, HOST_ATTR) : style;
117120
}
118121

119-
private _shimModuleIdIfNeeded(moduleId: string, shim: boolean): string {
120-
return shim ? `${moduleId}.shim` : moduleId;
122+
private _createModuleUrl(stylesheetUrl: string, shim: boolean): string {
123+
return shim ? `${stylesheetUrl}.shim${MODULE_SUFFIX}` : `${stylesheetUrl}${MODULE_SUFFIX}`;
121124
}
122125
}
123126

0 commit comments

Comments
 (0)
X Tutup