X Tutup
Skip to content

Commit 5a04ffe

Browse files
committed
refactor(Directive): drop moduleId
moduleId is only used by components to resolve urls. Directives have no templates and do not need moduleId. Closes angular#5873
1 parent 1c779d8 commit 5a04ffe

File tree

9 files changed

+37
-47
lines changed

9 files changed

+37
-47
lines changed

modules/angular2/src/compiler/runtime_metadata.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ export class RuntimeMetadataResolver {
3232
var meta = this._cache.get(directiveType);
3333
if (isBlank(meta)) {
3434
var dirMeta = this._directiveResolver.resolve(directiveType);
35-
var moduleUrl = calcModuleUrl(directiveType, dirMeta);
35+
var moduleUrl = null;
3636
var templateMeta = null;
3737
var changeDetectionStrategy = null;
3838

3939
if (dirMeta instanceof md.ComponentMetadata) {
4040
var cmpMeta = <md.ComponentMetadata>dirMeta;
41+
moduleUrl = calcModuleUrl(directiveType, cmpMeta);
4142
var viewMeta = this._viewResolver.resolve(directiveType);
4243
templateMeta = new cpl.CompileTemplateMetadata({
4344
encapsulation: viewMeta.encapsulation,
@@ -107,8 +108,8 @@ function isValidDirective(value: Type): boolean {
107108
return isPresent(value) && (value instanceof Type);
108109
}
109110

110-
function calcModuleUrl(type: Type, dirMeta: md.DirectiveMetadata): string {
111-
var moduleId = dirMeta.moduleId;
111+
function calcModuleUrl(type: Type, cmpMetadata: md.ComponentMetadata): string {
112+
var moduleId = cmpMetadata.moduleId;
112113
if (isPresent(moduleId)) {
113114
var scheme = getUrlScheme(moduleId);
114115
return isPresent(scheme) && scheme.length > 0 ? moduleId :

modules/angular2/src/core/linker/directive_resolver.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ export class DirectiveResolver {
132132
outputs: mergedOutputs,
133133
host: mergedHost,
134134
exportAs: dm.exportAs,
135-
moduleId: dm.moduleId,
136135
queries: mergedQueries,
137136
providers: dm.providers
138137
});

modules/angular2/src/core/metadata.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class Directive extends DirectiveMetadata {
2727
List bindings,
2828
List providers,
2929
String exportAs,
30-
String moduleId,
3130
Map<String, dynamic> queries})
3231
: super(
3332
selector: selector,
@@ -39,7 +38,6 @@ class Directive extends DirectiveMetadata {
3938
bindings: bindings,
4039
providers: providers,
4140
exportAs: exportAs,
42-
moduleId: moduleId,
4341
queries: queries);
4442
}
4543

modules/angular2/src/core/metadata.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ export interface DirectiveFactory {
146146
bindings?: any[],
147147
providers?: any[],
148148
exportAs?: string,
149-
moduleId?: string,
150149
queries?: {[key: string]: any}
151150
}): DirectiveDecorator;
152151
new (obj: {
@@ -159,7 +158,6 @@ export interface DirectiveFactory {
159158
bindings?: any[],
160159
providers?: any[],
161160
exportAs?: string,
162-
moduleId?: string,
163161
queries?: {[key: string]: any}
164162
}): DirectiveMetadata;
165163
}

modules/angular2/src/core/metadata/directives.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -696,26 +696,6 @@ export class DirectiveMetadata extends InjectableMetadata {
696696
*/
697697
exportAs: string;
698698

699-
/**
700-
* The module id of the module that contains the directive.
701-
* Needed to be able to resolve relative urls for templates and styles.
702-
* In Dart, this can be determined automatically and does not need to be set.
703-
* In CommonJS, this can always be set to `module.id`.
704-
*
705-
* ## Simple Example
706-
*
707-
* ```
708-
* @Directive({
709-
* selector: 'someDir',
710-
* moduleId: module.id
711-
* })
712-
* class SomeDir {
713-
* }
714-
*
715-
* ```
716-
*/
717-
moduleId: string;
718-
719699
// TODO: add an example after ContentChildren and ViewChildren are in master
720700
/**
721701
* Configures the queries that will be injected into the directive.
@@ -752,7 +732,7 @@ export class DirectiveMetadata extends InjectableMetadata {
752732
queries: {[key: string]: any};
753733

754734
constructor({selector, inputs, outputs, properties, events, host, bindings, providers, exportAs,
755-
moduleId, queries}: {
735+
queries}: {
756736
selector?: string,
757737
inputs?: string[],
758738
outputs?: string[],
@@ -762,7 +742,6 @@ export class DirectiveMetadata extends InjectableMetadata {
762742
providers?: any[],
763743
/** @deprecated */ bindings?: any[],
764744
exportAs?: string,
765-
moduleId?: string,
766745
queries?: {[key: string]: any}
767746
} = {}) {
768747
super();
@@ -773,7 +752,6 @@ export class DirectiveMetadata extends InjectableMetadata {
773752
this._events = events;
774753
this.host = host;
775754
this.exportAs = exportAs;
776-
this.moduleId = moduleId;
777755
this.queries = queries;
778756
this._providers = providers;
779757
this._bindings = bindings;
@@ -865,6 +843,26 @@ export class ComponentMetadata extends DirectiveMetadata {
865843
private _viewProviders: any[];
866844
private _viewBindings: any[];
867845

846+
/**
847+
* The module id of the module that contains the component.
848+
* Needed to be able to resolve relative urls for templates and styles.
849+
* In Dart, this can be determined automatically and does not need to be set.
850+
* In CommonJS, this can always be set to `module.id`.
851+
*
852+
* ## Simple Example
853+
*
854+
* ```
855+
* @Directive({
856+
* selector: 'someDir',
857+
* moduleId: module.id
858+
* })
859+
* class SomeDir {
860+
* }
861+
*
862+
* ```
863+
*/
864+
moduleId: string;
865+
868866
templateUrl: string;
869867

870868
template: string;
@@ -913,7 +911,6 @@ export class ComponentMetadata extends DirectiveMetadata {
913911
events: events,
914912
host: host,
915913
exportAs: exportAs,
916-
moduleId: moduleId,
917914
bindings: bindings,
918915
providers: providers,
919916
queries: queries
@@ -929,6 +926,7 @@ export class ComponentMetadata extends DirectiveMetadata {
929926
this.directives = directives;
930927
this.pipes = pipes;
931928
this.encapsulation = encapsulation;
929+
this.moduleId = moduleId;
932930
}
933931
}
934932

modules/angular2/src/mock/directive_resolver_mock.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export class MockDirectiveResolver extends DirectiveResolver {
4747
host: dm.host,
4848
providers: providers,
4949
exportAs: dm.exportAs,
50-
moduleId: dm.moduleId,
5150
queries: dm.queries
5251
});
5352
}

modules/angular2/test/compiler/runtime_metadata_spec.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ export function main() {
7070

7171
it('should use the moduleUrl from the reflector if none is given',
7272
inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => {
73-
var value: string = resolver.getMetadata(DirectiveWithoutModuleId).type.moduleUrl;
73+
var value: string = resolver.getMetadata(ComponentWithoutModuleId).type.moduleUrl;
7474
var expectedEndValue =
7575
IS_DART ? 'base/dist/dart/angular2/test/compiler/runtime_metadata_spec.dart' : './';
76-
expect((<any>value).endsWith(expectedEndValue)).toBe(true);
76+
expect(value.endsWith(expectedEndValue)).toBe(true);
7777
}));
7878
});
7979

@@ -82,7 +82,7 @@ export function main() {
8282
it('should return the directive metadatas',
8383
inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => {
8484
expect(resolver.getViewDirectivesMetadata(ComponentWithEverything))
85-
.toEqual([resolver.getMetadata(DirectiveWithoutModuleId)]);
85+
.toEqual([resolver.getMetadata(SomeDirective)]);
8686
}));
8787

8888
describe("platform directives", () => {
@@ -91,25 +91,24 @@ export function main() {
9191
it('should include platform directives when available',
9292
inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => {
9393
expect(resolver.getViewDirectivesMetadata(ComponentWithEverything))
94-
.toEqual([
95-
resolver.getMetadata(ADirective),
96-
resolver.getMetadata(DirectiveWithoutModuleId)
97-
]);
94+
.toEqual([resolver.getMetadata(ADirective), resolver.getMetadata(SomeDirective)]);
9895
}));
9996
});
10097
});
10198

10299
});
103100
}
104101

105-
106-
107102
@Directive({selector: 'a-directive'})
108103
class ADirective {
109104
}
110105

111106
@Directive({selector: 'someSelector'})
112-
class DirectiveWithoutModuleId {
107+
class SomeDirective {
108+
}
109+
110+
@Component({selector: 'someComponent', template: ''})
111+
class ComponentWithoutModuleId {
113112
}
114113

115114
@Component({
@@ -131,7 +130,7 @@ class DirectiveWithoutModuleId {
131130
encapsulation: ViewEncapsulation.Emulated,
132131
styles: ['someStyle'],
133132
styleUrls: ['someStyleUrl'],
134-
directives: [DirectiveWithoutModuleId]
133+
directives: [SomeDirective]
135134
})
136135
class ComponentWithEverything implements OnChanges,
137136
OnInit, DoCheck, OnDestroy, AfterContentInit, AfterContentChecked, AfterViewInit,

modules/angular2/test/compiler/template_compiler_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ class CompWithEmbeddedTemplate {
321321
}
322322

323323

324-
@Directive({selector: 'plain', moduleId: THIS_MODULE_ID})
324+
@Directive({selector: 'plain'})
325325
@View({template: ''})
326326
class NonComponent {
327327
}

modules/angular2/test/public_api_spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,6 @@ var NG_CORE = [
923923
'Directive.events',
924924
'Directive.exportAs',
925925
'Directive.host',
926-
'Directive.moduleId',
927926
'Directive.inputs',
928927
'Directive.properties',
929928
'Directive.queries',
@@ -935,7 +934,6 @@ var NG_CORE = [
935934
'DirectiveMetadata.events',
936935
'DirectiveMetadata.exportAs',
937936
'DirectiveMetadata.host',
938-
'DirectiveMetadata.moduleId',
939937
'DirectiveMetadata.inputs',
940938
'DirectiveMetadata.properties',
941939
'DirectiveMetadata.queries',

0 commit comments

Comments
 (0)
X Tutup