OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library js.transformer.entry_point_transformer; | 5 library js.transformer.entry_point_transformer; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:barback/barback.dart' show Asset, AssetId, Transform, | 9 import 'package:barback/barback.dart' show Asset, AssetId, Transform, |
10 Transformer, AssetNotFoundException; | 10 Transformer, AssetNotFoundException; |
11 import 'package:code_transformers/resolver.dart' show Resolver, | 11 import 'package:code_transformers/resolver.dart' show Resolver, |
12 ResolverTransformer, Resolvers, isPossibleDartEntryId; | 12 ResolverTransformer, Resolvers, isPossibleDartEntryId; |
13 import 'package:logging/logging.dart' show Logger; | 13 import 'package:logging/logging.dart' show Logger; |
14 import 'package:path/path.dart' as path; | |
15 | 14 |
16 import 'utils.dart'; | 15 import 'utils.dart'; |
17 | 16 |
18 final _logger = new Logger('js.transformer.interface_transformer'); | 17 final _logger = new Logger('js.transformer.interface_transformer'); |
19 | 18 |
20 class EntryPointTransformer extends Transformer with ResolverTransformer { | 19 class EntryPointTransformer extends Transformer with ResolverTransformer { |
21 @override | 20 @override |
22 final Resolvers resolvers; | 21 final Resolvers resolvers; |
23 | 22 |
24 EntryPointTransformer(this.resolvers); | 23 EntryPointTransformer(this.resolvers); |
(...skipping 14 matching lines...) Expand all Loading... | |
39 } | 38 } |
40 | 39 |
41 var transaction = resolver.createTextEditTransaction(library); | 40 var transaction = resolver.createTextEditTransaction(library); |
42 | 41 |
43 var initializerFutures = resolver.libraries | 42 var initializerFutures = resolver.libraries |
44 .where((lib) => | 43 .where((lib) => |
45 lib != jsLibrary && lib.visibleLibraries.contains(jsLibrary)) | 44 lib != jsLibrary && lib.visibleLibraries.contains(jsLibrary)) |
46 .map((lib) { | 45 .map((lib) { |
47 // look for initializer library | 46 // look for initializer library |
48 var libAssetId = resolver.getSourceAssetId(lib); | 47 var libAssetId = resolver.getSourceAssetId(lib); |
49 var initializerAssetId = libAssetId.addExtension(INITIALIZER_SUFFIX); | 48 var dartInitializerAssetId = |
50 return transform | 49 libAssetId.addExtension(DART_INITIALIZER_SUFFIX); |
51 .getInput(initializerAssetId) | 50 var jsInitializerAssetId = |
52 .catchError((e) => null, | 51 libAssetId.addExtension(JS_INITIALIZER_SUFFIX); |
53 test: (e) => e is AssetNotFoundException); | 52 return [ |
53 transform | |
54 .getInput(dartInitializerAssetId) | |
55 .catchError((e) => null, | |
56 test: (e) => e is AssetNotFoundException), | |
57 transform | |
58 .getInput(jsInitializerAssetId) | |
59 .catchError((e) => null, | |
60 test: (e) => e is AssetNotFoundException), | |
61 ]; | |
62 }); | |
63 | |
64 var dartInitializerFutures = initializerFutures.map((l) => l[0]); | |
65 var jsInitializerFutures = initializerFutures.map((l) => l[1]); | |
66 | |
67 var dartImports = new StringBuffer('\n'); | |
68 var dartInitializerCalls = new StringBuffer(); | |
69 | |
70 var dartFuture = Future.wait(dartInitializerFutures). | |
71 then((initializerAssets) { | |
72 for (Asset asset in initializerAssets.where((a) => a != null)) { | |
Siggi Cherem (dart-lang)
2014/09/19 22:47:10
I didn't realize this last time, it might be simpl
justinfagnani
2014/09/22 21:50:21
I like this style, it reads well to me.
Siggi Cherem (dart-lang)
2014/09/22 22:42:58
we'll agree to disagree :)
It feels noisy to me
| |
73 var id = asset.id; | |
74 var importUri = getImportUri(id, input.id); | |
75 if (importUri == null) continue; | |
76 var prefix = assetIdToPrefix(id); | |
77 dartImports.writeln("import '$importUri' as $prefix;"); | |
78 dartInitializerCalls.writeln(" $prefix.initializeJavaScriptLibrary( );"); | |
Siggi Cherem (dart-lang)
2014/09/19 22:47:09
nit: 80 col (below too)
justinfagnani
2014/09/22 21:50:21
Done.
| |
79 } | |
54 }) | 80 }) |
55 .where((f) => f != null); | 81 .then((_) { |
56 return Future.wait(initializerFutures).then((initializerAssets) { | 82 var initMethod = 'initializeJavaScript() {\n$dartInitializerCalls}\n'; |
57 var imports = new StringBuffer('\n'); | 83 var insertImportOffset = getInsertImportOffset(library); |
58 var calls = new StringBuffer(); | 84 var initMethodOffset = library.source.contents.data.length; |
59 for (Asset asset in initializerAssets.where((a) => a != null)) { | 85 transaction.edit(insertImportOffset, insertImportOffset, '$dartImports '); |
60 var id = asset.id; | 86 transaction.edit(initMethodOffset, initMethodOffset, initMethod); |
61 var importUri = getImportUri(id, input.id); | 87 var printer = transaction.commit(); |
62 if (importUri == null) continue; | 88 printer.build(input.id.path); |
63 var prefix = assetIdToPrefix(id); | 89 transform.addOutput(new Asset.fromString(input.id, printer.text)); |
64 imports.writeln("import '$importUri' as $prefix;"); | 90 }); |
65 calls.writeln(" $prefix.initializeJavaScriptLibrary();"); | |
66 } | |
67 | 91 |
68 var initMethod = 'initializeJavaScript() {\n$calls}\n'; | 92 var jsInitializerScript = new StringBuffer(''' |
69 var insertImportOffset = getInsertImportOffset(library); | 93 |
70 var initMethodOffset = library.source.contents.data.length; | 94 window.dart = window.dart || {}; |
71 transaction.edit(insertImportOffset, insertImportOffset, '$imports'); | 95 |
72 transaction.edit(initMethodOffset, initMethodOffset, initMethod); | 96 window.dart.Object = function DartObject() { |
73 var printer = transaction.commit(); | 97 throw "not allowed"; |
74 printer.build(input.id.path); | 98 }; |
75 transform.addOutput(new Asset.fromString(input.id, printer.text)); | 99 |
76 }); | 100 window.dart.Object._wrapDartObject = function(dartObject) { |
101 var o = Object.create(window.dart.Object.prototype); | |
102 o.__dart_object__ = dartObject; | |
103 return o; | |
104 }; | |
105 | |
106 '''); | |
107 | |
108 var jsFuture = | |
109 Future.wait(jsInitializerFutures) | |
110 .then((assets) => Future.wait(assets | |
111 .where((Asset a) => a != null) | |
Siggi Cherem (dart-lang)
2014/09/19 22:47:10
nit: remove the type here?
justinfagnani
2014/09/22 21:50:20
Done.
| |
112 .map((Asset a) => a.readAsString()))) | |
113 .then((initializerSources) { | |
114 for (String source in initializerSources) { | |
115 jsInitializerScript.writeln(source); | |
116 } | |
117 }).then((_) { | |
118 var jsInitializerId = input.id.addExtension('_initialize.js'); | |
119 transform.addOutput(new Asset.fromString(jsInitializerId, jsInitialize rScript.toString())); | |
120 }); | |
121 return Future.wait([dartFuture, jsFuture]); | |
77 } | 122 } |
78 } | 123 } |
79 | |
80 final illegalIdRegex = new RegExp(r'[^a-zA-Z0-9_]'); | |
81 | |
82 String assetIdToPrefix(AssetId id) => | |
83 '_js__${id.package}__${id.path.replaceAll(illegalIdRegex, '_')}'; | |
84 | |
85 // TODO(justinfagnani): put this in code_transformers ? | |
86 String getImportUri(AssetId importId, AssetId from) { | |
87 if (importId.path.startsWith('lib/')) { | |
88 // we support package imports | |
89 return "package:${importId.package}/${importId.path.substring(4)}"; | |
90 } else if (importId.package == from.package) { | |
91 // we can support relative imports | |
92 return path.relative(importId.path, from: path.dirname(from.path)); | |
93 } | |
94 // cannot import | |
95 return null; | |
96 } | |
OLD | NEW |