diff --git a/modules_dart/transform/lib/src/transform/common/options.dart b/modules_dart/transform/lib/src/transform/common/options.dart index 2186216310..1b67fcf459 100644 --- a/modules_dart/transform/lib/src/transform/common/options.dart +++ b/modules_dart/transform/lib/src/transform/common/options.dart @@ -11,6 +11,7 @@ const FORMAT_CODE_PARAM = 'format_code'; const REFLECT_PROPERTIES_AS_ATTRIBUTES = 'reflect_properties_as_attributes'; const PLATFORM_DIRECTIVES = 'platform_directives'; const PLATFORM_PIPES = 'platform_pipes'; +const RESOLVED_IDENTIFIERS = 'resolved_identifiers'; const INIT_REFLECTOR_PARAM = 'init_reflector'; const INLINE_VIEWS_PARAM = 'inline_views'; const MIRROR_MODE_PARAM = 'mirror_mode'; @@ -54,6 +55,9 @@ class TransformerOptions { /// angular2/lib/src/common/pipes.dart#COMMON_PIPES final List platformPipes; + /// A map of identifier/asset pairs used when resolving identifiers. + final Map resolvedIdentifiers; + /// Whether to format generated code. /// Code that is only modified will never be formatted because doing so may /// invalidate the source maps generated by `dart2js` and/or other tools. @@ -98,6 +102,7 @@ class TransformerOptions { this.lazyTransformers, this.platformDirectives, this.platformPipes, + this.resolvedIdentifiers, this.reflectPropertiesAsAttributes}); factory TransformerOptions(List entryPoints, @@ -111,6 +116,7 @@ class TransformerOptions { bool reflectPropertiesAsAttributes: false, List platformDirectives, List platformPipes, + Map resolvedIdentifiers, bool lazyTransformers: false, bool formatCode: false}) { var annotationMatcher = new AnnotationMatcher() @@ -125,6 +131,7 @@ class TransformerOptions { reflectPropertiesAsAttributes: reflectPropertiesAsAttributes, platformDirectives: platformDirectives, platformPipes: platformPipes, + resolvedIdentifiers: resolvedIdentifiers, inlineViews: inlineViews, lazyTransformers: lazyTransformers, formatCode: formatCode); diff --git a/modules_dart/transform/lib/src/transform/common/options_reader.dart b/modules_dart/transform/lib/src/transform/common/options_reader.dart index 9123917b38..36dab07bb1 100644 --- a/modules_dart/transform/lib/src/transform/common/options_reader.dart +++ b/modules_dart/transform/lib/src/transform/common/options_reader.dart @@ -5,7 +5,7 @@ import 'annotation_matcher.dart'; import 'mirror_mode.dart'; import 'options.dart'; -TransformerOptions parseBarbackSettings(BarbackSettings settings) { + TransformerOptions parseBarbackSettings(BarbackSettings settings) { var config = settings.configuration; var entryPoints = _readStringList(config, ENTRY_POINT_PARAM); var initReflector = @@ -14,6 +14,7 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) { _readBool(config, REFLECT_PROPERTIES_AS_ATTRIBUTES, defaultValue: false); var platformDirectives = _readStringList(config, PLATFORM_DIRECTIVES); var platformPipes = _readStringList(config, PLATFORM_PIPES); + var resolvedIdentifiers = config[RESOLVED_IDENTIFIERS]; var formatCode = _readBool(config, FORMAT_CODE_PARAM, defaultValue: false); String mirrorModeVal = config.containsKey(MIRROR_MODE_PARAM) ? config[MIRROR_MODE_PARAM] : ''; @@ -38,6 +39,7 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) { reflectPropertiesAsAttributes: reflectPropertiesAsAttributes, platformDirectives: platformDirectives, platformPipes: platformPipes, + resolvedIdentifiers: resolvedIdentifiers, inlineViews: _readBool(config, INLINE_VIEWS_PARAM, defaultValue: false), lazyTransformers: _readBool(config, LAZY_TRANSFORMERS, defaultValue: false), diff --git a/modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart b/modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart index 8a52c3097b..785bbe5abf 100644 --- a/modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart +++ b/modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart @@ -29,10 +29,12 @@ Future createCompileData( AssetReader reader, AssetId assetId, List platformDirectives, - List platformPipes) async { + List platformPipes, + Map resolvedIdentifiers + ) async { return logElapsedAsync(() async { final creator = await _CompileDataCreator.create( - reader, assetId, platformDirectives, platformPipes); + reader, assetId, platformDirectives, platformPipes, resolvedIdentifiers); return creator != null ? creator.createCompileData() : null; }, operationName: 'createCompileData', assetId: assetId); } @@ -53,19 +55,20 @@ class _CompileDataCreator { final NgMeta ngMeta; final List platformDirectives; final List platformPipes; + final Map resolvedIdentifiers; _CompileDataCreator(this.reader, this.entryPoint, this.ngMeta, - this.platformDirectives, this.platformPipes); + this.platformDirectives, this.platformPipes, this.resolvedIdentifiers); static Future<_CompileDataCreator> create(AssetReader reader, AssetId assetId, - List platformDirectives, List platformPipes) async { + List platformDirectives, List platformPipes, Map resolvedIdentifiers) async { if (!(await reader.hasInput(assetId))) return null; final json = await reader.readAsString(assetId); if (json == null || json.isEmpty) return null; final ngMeta = new NgMeta.fromJson(JSON.decode(json)); return new _CompileDataCreator( - reader, assetId, ngMeta, platformDirectives, platformPipes); + reader, assetId, ngMeta, platformDirectives, platformPipes, resolvedIdentifiers); } NgDepsModel get ngDeps => ngMeta.ngDeps; @@ -215,24 +218,8 @@ class _CompileDataCreator { } else if (_isPrimitive(id.name)) { return id; - // TODO: move the following if statements into transformer configuration - } else if (id.name == "Window") { - return new CompileIdentifierMetadata(name: "Window", moduleUrl: 'dart:html'); - - } else if (id.name == "Clock") { - return new CompileIdentifierMetadata(name: "Clock", moduleUrl: 'asset:quiver/time/clock.dart'); - - } else if (id.name == "Profiler") { - return new CompileIdentifierMetadata(name: "Profiler", moduleUrl: 'asset:perf_api/perf_api.dart'); - - } else if (id.name == "Campaign") { - return new CompileIdentifierMetadata(name: "Campaign", moduleUrl: 'unspecified'); - - } else if (id.name == "PreloadData") { - return new CompileIdentifierMetadata(name: "PreloadData", moduleUrl: 'unspecified'); - - } else if (id.name == "FiberMarket") { - return new CompileIdentifierMetadata(name: "FiberMarket", moduleUrl: 'unspecified'); + } else if (resolvedIdentifiers != null && resolvedIdentifiers.containsKey(id.name)) { + return new CompileIdentifierMetadata(name: id.name, moduleUrl: resolvedIdentifiers[id.name]); } else { log.warning( diff --git a/modules_dart/transform/lib/src/transform/template_compiler/generator.dart b/modules_dart/transform/lib/src/transform/template_compiler/generator.dart index e36cae442d..460cf73d25 100644 --- a/modules_dart/transform/lib/src/transform/template_compiler/generator.dart +++ b/modules_dart/transform/lib/src/transform/template_compiler/generator.dart @@ -38,9 +38,11 @@ Future processTemplates(AssetReader reader, AssetId assetId, {bool genChangeDetectionDebugInfo: false, bool reflectPropertiesAsAttributes: false, List platformDirectives, - List platformPipes}) async { + List platformPipes, + Map resolvedIdentifiers + }) async { var viewDefResults = await createCompileData( - reader, assetId, platformDirectives, platformPipes); + reader, assetId, platformDirectives, platformPipes, resolvedIdentifiers); if (viewDefResults == null) return null; final compileTypeMetadatas = viewDefResults.ngMeta.identifiers.values; if (compileTypeMetadatas.isNotEmpty) { diff --git a/modules_dart/transform/lib/src/transform/template_compiler/transformer.dart b/modules_dart/transform/lib/src/transform/template_compiler/transformer.dart index 3418e2928f..298dc68c22 100644 --- a/modules_dart/transform/lib/src/transform/template_compiler/transformer.dart +++ b/modules_dart/transform/lib/src/transform/template_compiler/transformer.dart @@ -47,7 +47,9 @@ class TemplateCompiler extends Transformer implements LazyTransformer { genChangeDetectionDebugInfo: options.genChangeDetectionDebugInfo, reflectPropertiesAsAttributes: options.reflectPropertiesAsAttributes, platformDirectives: options.platformDirectives, - platformPipes: options.platformPipes); + platformPipes: options.platformPipes, + resolvedIdentifiers: options.resolvedIdentifiers + ); var ngDepsCode = _emptyNgDepsContents; if (outputs != null) { if (outputs.ngDeps != null) { diff --git a/modules_dart/transform/test/transform/template_compiler/all_tests.dart b/modules_dart/transform/test/transform/template_compiler/all_tests.dart index 27427e2dd6..99e21befa8 100644 --- a/modules_dart/transform/test/transform/template_compiler/all_tests.dart +++ b/modules_dart/transform/test/transform/template_compiler/all_tests.dart @@ -83,12 +83,13 @@ void allTests() { }); Future process(AssetId assetId, - {List platformDirectives, List platformPipes}) { + {List platformDirectives, List platformPipes, Map resolvedIdentifiers}) { logger = new RecordingLogger(); return zone.exec( () => processTemplates(reader, assetId, platformDirectives: platformDirectives, - platformPipes: platformPipes), + platformPipes: platformPipes, + resolvedIdentifiers: resolvedIdentifiers), log: logger); } @@ -142,7 +143,7 @@ void allTests() { updateReader(); - final viewDefResults = await createCompileData(reader, fooAssetId, [], []); + final viewDefResults = await createCompileData(reader, fooAssetId, [], [], {}); final cmp = viewDefResults.viewDefinitions.values.first.component; expect(cmp.providers.length).toEqual(1); @@ -165,7 +166,7 @@ void allTests() { updateReader(); - final viewDefResults = await createCompileData(reader, fooAssetId, [], []); + final viewDefResults = await createCompileData(reader, fooAssetId, [], [], {}); final cmp = viewDefResults.viewDefinitions.values.first.component; expect(cmp.providers.length).toEqual(1); @@ -193,7 +194,7 @@ void allTests() { updateReader(); - final viewDefResults = await createCompileData(reader, fooAssetId, [], []); + final viewDefResults = await createCompileData(reader, fooAssetId, [], [], {}); final cmp = viewDefResults.viewDefinitions.values.first.component; expect(cmp.providers.length).toEqual(1); @@ -219,7 +220,7 @@ void allTests() { updateReader(); - final viewDefResults = await createCompileData(reader, fooAssetId, [], []); + final viewDefResults = await createCompileData(reader, fooAssetId, [], [], {}); final cmp = viewDefResults.viewDefinitions.values.first.component; expect(cmp.providers.length).toEqual(1); @@ -248,7 +249,7 @@ void allTests() { updateReader(); - final viewDefResults = await createCompileData(reader, fooAssetId, [], []); + final viewDefResults = await createCompileData(reader, fooAssetId, [], [], {}); final cmp = viewDefResults.viewDefinitions.values.first.component; expect(cmp.providers.length).toEqual(1); @@ -587,6 +588,31 @@ void allTests() { ..toContain("import 'bar.dart'") ..toContain(barPipeMeta.name); }); + + it('should fallback to the list of resolved identifiers.', () async { + barNgMeta.identifiers['Service2'] = new CompileTypeMetadata(name: 'Service2', moduleUrl: 'moduleUrl'); + + fooComponentMeta.template = new CompileTemplateMetadata(template: "import 'bar.dart';"); + fooComponentMeta.providers = [new CompileProviderMetadata(token: new CompileIdentifierMetadata(name: 'Service1'), useClass: + new CompileTypeMetadata(name: 'Service2'))]; + + final viewAnnotation = new AnnotationModel()..name = 'View'..isView = true; + final reflectable = fooNgMeta.ngDeps.reflectables.first; + reflectable.annotations.add(viewAnnotation); + fooNgMeta.ngDeps.imports.add(new ImportModel()..uri = 'package:a/bar.dart'); + + updateReader(); + + final viewDefResults = await createCompileData(reader, fooAssetId, [], [], {"Service1": "someModuleUrl", "Service2": "someModuleUrl"}); + final cmp = viewDefResults.viewDefinitions.values.first.component; + + expect(cmp.providers.length).toEqual(1); + + expect(cmp.providers[0].token.name).toEqual("Service1"); + expect(cmp.providers[0].token.moduleUrl).toEqual("someModuleUrl"); + expect(cmp.providers[0].useClass.name).toEqual("Service2"); + expect(cmp.providers[0].useClass.moduleUrl).toEqual("moduleUrl"); + }); } String _generatedCode(Outputs outputs) {