Chromium Code Reviews| Index: lib/src/transformer/entry_point_transformer.dart |
| diff --git a/lib/src/transformer/entry_point_transformer.dart b/lib/src/transformer/entry_point_transformer.dart |
| index cb7dd0a7ef9ba0d0b53005d30f87f3753e9660b0..d7ecad0744b3503570000731acbc839f22c7689c 100644 |
| --- a/lib/src/transformer/entry_point_transformer.dart |
| +++ b/lib/src/transformer/entry_point_transformer.dart |
| @@ -11,7 +11,6 @@ import 'package:barback/barback.dart' show Asset, AssetId, Transform, |
| import 'package:code_transformers/resolver.dart' show Resolver, |
| ResolverTransformer, Resolvers, isPossibleDartEntryId; |
| import 'package:logging/logging.dart' show Logger; |
| -import 'package:path/path.dart' as path; |
| import 'utils.dart'; |
| @@ -46,51 +45,79 @@ class EntryPointTransformer extends Transformer with ResolverTransformer { |
| .map((lib) { |
| // look for initializer library |
| var libAssetId = resolver.getSourceAssetId(lib); |
| - var initializerAssetId = libAssetId.addExtension(INITIALIZER_SUFFIX); |
| - return transform |
| - .getInput(initializerAssetId) |
| - .catchError((e) => null, |
| - test: (e) => e is AssetNotFoundException); |
| + var dartInitializerAssetId = |
| + libAssetId.addExtension(DART_INITIALIZER_SUFFIX); |
| + var jsInitializerAssetId = |
| + libAssetId.addExtension(JS_INITIALIZER_SUFFIX); |
| + return [ |
| + transform |
| + .getInput(dartInitializerAssetId) |
| + .catchError((e) => null, |
| + test: (e) => e is AssetNotFoundException), |
| + transform |
| + .getInput(jsInitializerAssetId) |
| + .catchError((e) => null, |
| + test: (e) => e is AssetNotFoundException), |
| + ]; |
| + }); |
| + |
| + var dartInitializerFutures = initializerFutures.map((l) => l[0]); |
| + var jsInitializerFutures = initializerFutures.map((l) => l[1]); |
| + |
| + var dartImports = new StringBuffer('\n'); |
| + var dartInitializerCalls = new StringBuffer(); |
| + |
| + var dartFuture = Future.wait(dartInitializerFutures). |
| + then((initializerAssets) { |
| + 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
|
| + var id = asset.id; |
| + var importUri = getImportUri(id, input.id); |
| + if (importUri == null) continue; |
| + var prefix = assetIdToPrefix(id); |
| + dartImports.writeln("import '$importUri' as $prefix;"); |
| + 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.
|
| + } |
| }) |
| - .where((f) => f != null); |
| - return Future.wait(initializerFutures).then((initializerAssets) { |
| - var imports = new StringBuffer('\n'); |
| - var calls = new StringBuffer(); |
| - for (Asset asset in initializerAssets.where((a) => a != null)) { |
| - var id = asset.id; |
| - var importUri = getImportUri(id, input.id); |
| - if (importUri == null) continue; |
| - var prefix = assetIdToPrefix(id); |
| - imports.writeln("import '$importUri' as $prefix;"); |
| - calls.writeln(" $prefix.initializeJavaScriptLibrary();"); |
| - } |
| - |
| - var initMethod = 'initializeJavaScript() {\n$calls}\n'; |
| - var insertImportOffset = getInsertImportOffset(library); |
| - var initMethodOffset = library.source.contents.data.length; |
| - transaction.edit(insertImportOffset, insertImportOffset, '$imports'); |
| - transaction.edit(initMethodOffset, initMethodOffset, initMethod); |
| - var printer = transaction.commit(); |
| - printer.build(input.id.path); |
| - transform.addOutput(new Asset.fromString(input.id, printer.text)); |
| - }); |
| + .then((_) { |
| + var initMethod = 'initializeJavaScript() {\n$dartInitializerCalls}\n'; |
| + var insertImportOffset = getInsertImportOffset(library); |
| + var initMethodOffset = library.source.contents.data.length; |
| + transaction.edit(insertImportOffset, insertImportOffset, '$dartImports'); |
| + transaction.edit(initMethodOffset, initMethodOffset, initMethod); |
| + var printer = transaction.commit(); |
| + printer.build(input.id.path); |
| + transform.addOutput(new Asset.fromString(input.id, printer.text)); |
| + }); |
| + |
| + var jsInitializerScript = new StringBuffer(''' |
| + |
| +window.dart = window.dart || {}; |
| + |
| +window.dart.Object = function DartObject() { |
| + throw "not allowed"; |
| +}; |
| + |
| +window.dart.Object._wrapDartObject = function(dartObject) { |
| + var o = Object.create(window.dart.Object.prototype); |
| + o.__dart_object__ = dartObject; |
| + return o; |
| +}; |
| + |
| +'''); |
| + |
| + var jsFuture = |
| + Future.wait(jsInitializerFutures) |
| + .then((assets) => Future.wait(assets |
| + .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.
|
| + .map((Asset a) => a.readAsString()))) |
| + .then((initializerSources) { |
| + for (String source in initializerSources) { |
| + jsInitializerScript.writeln(source); |
| + } |
| + }).then((_) { |
| + var jsInitializerId = input.id.addExtension('_initialize.js'); |
| + transform.addOutput(new Asset.fromString(jsInitializerId, jsInitializerScript.toString())); |
| + }); |
| + return Future.wait([dartFuture, jsFuture]); |
| } |
| } |
| - |
| -final illegalIdRegex = new RegExp(r'[^a-zA-Z0-9_]'); |
| - |
| -String assetIdToPrefix(AssetId id) => |
| - '_js__${id.package}__${id.path.replaceAll(illegalIdRegex, '_')}'; |
| - |
| -// TODO(justinfagnani): put this in code_transformers ? |
| -String getImportUri(AssetId importId, AssetId from) { |
| - if (importId.path.startsWith('lib/')) { |
| - // we support package imports |
| - return "package:${importId.package}/${importId.path.substring(4)}"; |
| - } else if (importId.package == from.package) { |
| - // we can support relative imports |
| - return path.relative(importId.path, from: path.dirname(from.path)); |
| - } |
| - // cannot import |
| - return null; |
| -} |