@@ -20,9 +20,9 @@ import 'package:barback/barback.dart';
2020/// The returned value wraps the [NgDepsModel] at `assetId` as well as these
2121/// created objects.
2222Future <CompileDataResults > createCompileData (
23- AssetReader reader, AssetId assetId) async {
23+ AssetReader reader, AssetId assetId, List < String > ambientDirectives ) async {
2424 return logElapsedAsync (() async {
25- final creator = await _CompileDataCreator .create (reader, assetId);
25+ final creator = await _CompileDataCreator .create (reader, assetId, ambientDirectives );
2626 return creator != null ? creator.createCompileData () : null ;
2727 }, operationName: 'createCompileData' , assetId: assetId);
2828}
@@ -41,17 +41,18 @@ class _CompileDataCreator {
4141 final AssetReader reader;
4242 final AssetId entryPoint;
4343 final NgMeta ngMeta;
44+ final List <String > ambientDirectives;
4445
45- _CompileDataCreator (this .reader, this .entryPoint, this .ngMeta);
46+ _CompileDataCreator (this .reader, this .entryPoint, this .ngMeta, this .ambientDirectives );
4647
4748 static Future <_CompileDataCreator > create (
48- AssetReader reader, AssetId assetId) async {
49+ AssetReader reader, AssetId assetId, List < String > ambientDirectives ) async {
4950 if (! (await reader.hasInput (assetId))) return null ;
5051 final json = await reader.readAsString (assetId);
5152 if (json == null || json.isEmpty) return null ;
5253
5354 final ngMeta = new NgMeta .fromJson (JSON .decode (json));
54- return new _CompileDataCreator (reader, assetId, ngMeta);
55+ return new _CompileDataCreator (reader, assetId, ngMeta, ambientDirectives );
5556 }
5657
5758 NgDepsModel get ngDeps => ngMeta.ngDeps;
@@ -64,13 +65,16 @@ class _CompileDataCreator {
6465 final compileData =
6566 < ReflectionInfoModel , NormalizedComponentWithViewDirectives > {};
6667 final ngMetaMap = await _extractNgMeta ();
68+ final ambientDirectives = await _readAmbientDirectives ();
6769
6870 for (var reflectable in ngDeps.reflectables) {
6971 if (ngMeta.types.containsKey (reflectable.name)) {
7072 final compileDirectiveMetadata = ngMeta.types[reflectable.name];
7173 if (compileDirectiveMetadata.template != null ) {
7274 final compileDatum = new NormalizedComponentWithViewDirectives (
7375 compileDirectiveMetadata, < CompileDirectiveMetadata > []);
76+ compileDatum.directives.addAll (ambientDirectives);
77+
7478 for (var dep in reflectable.directives) {
7579 if (! ngMetaMap.containsKey (dep.prefix)) {
7680 logger.warning (
@@ -99,6 +103,50 @@ class _CompileDataCreator {
99103 return new CompileDataResults ._(ngMeta, compileData);
100104 }
101105
106+ Future <List <CompileDirectiveMetadata >> _readAmbientDirectives () async {
107+ if (ambientDirectives == null ) return const [];
108+
109+ final res = [];
110+ for (var ad in ambientDirectives) {
111+ final parts = ad.split ("#" );
112+ if (parts.length != 2 ) {
113+ logger.warning ('The ambient directives configuration option '
114+ 'must be in the following format: "URI#TOKEN"' );
115+ return const [];
116+ }
117+ res.addAll (await _readAmbientDirectivesFromUri (parts[0 ], parts[1 ]));
118+ }
119+ return res;
120+ }
121+
122+ Future <List <CompileDirectiveMetadata >> _readAmbientDirectivesFromUri (String uri, String token) async {
123+ final metaAssetId = fromUri (toMetaExtension (uri));
124+ if (await reader.hasInput (metaAssetId)) {
125+ try {
126+ var jsonString = await reader.readAsString (metaAssetId);
127+ if (jsonString != null && jsonString.isNotEmpty) {
128+ var newMetadata = new NgMeta .fromJson (JSON .decode (jsonString));
129+
130+ if (newMetadata.types.containsKey (token)) {
131+ return [newMetadata.types[token]];
132+
133+ } else if (newMetadata.aliases.containsKey (token)) {
134+ return newMetadata.flatten (token);
135+
136+ } else {
137+ logger.warning ('Could not resolve ambient directive ${token } in ${uri }' ,
138+ asset: metaAssetId);
139+ }
140+
141+ }
142+ } catch (ex, stackTrace) {
143+ logger.warning ('Failed to decode: $ex , $stackTrace ' ,
144+ asset: metaAssetId);
145+ }
146+ }
147+ return [];
148+ }
149+
102150 /// Creates a map from import prefix to the asset: uris of all `.dart`
103151 /// libraries visible from `entryPoint` , excluding `dart:` and `.ng_deps.dart`
104152 /// files it imports. Unprefixed imports have the empty string as their key.
0 commit comments