Index: web/web_command.dart |
diff --git a/web/web_command.dart b/web/web_command.dart |
index 238257ae6e4f7bb240892ff31c915331e2195b65..1c01d9953d228dc88b20c15ab6f8263ad20750ce 100644 |
--- a/web/web_command.dart |
+++ b/web/web_command.dart |
@@ -1,6 +1,10 @@ |
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
+@JS() |
+library dev_compiler.web.web_command; |
+ |
+import 'dart:async'; |
import 'dart:html' show HttpRequest; |
import 'dart:convert' show BASE64; |
@@ -18,6 +22,12 @@ import 'package:analyzer/src/generated/sdk.dart' |
import 'package:analyzer/src/generated/source.dart' |
show DartUriResolver, Source, SourceFactory, UriKind; |
import 'package:analyzer/src/summary/idl.dart' show PackageBundle; |
+import 'package:analyzer/src/summary/package_bundle_reader.dart' |
+ show |
+ SummaryDataStore, |
+ InSummaryPackageUriResolver, |
+ InputPackagesResultProvider, |
+ InSummarySource; |
import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; |
import 'package:args/command_runner.dart'; |
@@ -26,49 +36,71 @@ import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions; |
import 'package:dev_compiler/src/compiler/compiler.dart' |
show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
+import 'package:js/js.dart'; |
+ |
typedef void MessageHandler(Object message); |
-typedef String CompileFn(String dart); |
-typedef void OnLoadFn(CompileFn compile); |
/// The command for invoking the modular compiler. |
class WebCompileCommand extends Command { |
get name => 'compile'; |
get description => 'Compile a set of Dart files into a JavaScript module.'; |
final MessageHandler messageHandler; |
- final OnLoadFn onload; |
- WebCompileCommand(this.onload, {MessageHandler messageHandler}) |
+ WebCompileCommand({MessageHandler messageHandler}) |
: this.messageHandler = messageHandler ?? print { |
CompilerOptions.addArguments(argParser); |
AnalyzerOptions.addArguments(argParser); |
} |
@override |
- void run() { |
- var request = new HttpRequest(); |
- |
- request.onReadyStateChange.listen((_) { |
- if (request.readyState == HttpRequest.DONE && |
- (request.status == 200 || request.status == 0)) { |
- var response = request.responseText; |
- var sdkBytes = BASE64.decode(response); |
- var result = setUpCompile(sdkBytes); |
- onload(result); |
- } |
- }); |
- |
- request.open('get', 'dart_sdk.sum'); |
- request.send(); |
+ Function run() { |
+ return requestSummaries; |
+ } |
+ |
+ void requestSummaries(String sdkUrl, List<String> summaryUrls, |
+ Function onCompileReady, Function onError) { |
+ HttpRequest.request(sdkUrl).then((sdkRequest) { |
+ var sdkResponse = sdkRequest.responseText; |
+ var sdkBytes = BASE64.decode(sdkResponse); |
+ |
+ // Map summary URLs to HttpRequests. |
+ var summaryRequests = summaryUrls |
+ .map((summary) => new Future(() => HttpRequest.request(summary))); |
+ |
+ Future.wait(summaryRequests).then((summaryResponses) { |
+ // Map summary responses to summary bytes. |
+ var summaryBytes = []; |
+ for (var response in summaryResponses) { |
+ summaryBytes.add(BASE64.decode(response.responseText)); |
+ } |
+ |
+ var compileFn = setUpCompile(sdkBytes, summaryBytes, summaryUrls); |
+ onCompileReady(compileFn); |
+ }).catchError((error) => onError('Summaries failed to load: $error')); |
+ }).catchError( |
+ (error) => onError('Dart sdk summaries failed to load: $error')); |
} |
- CompileFn setUpCompile(List<int> sdkBytes) { |
+ Function setUpCompile(List<int> sdkBytes, List<List<int>> summaryBytes, |
+ List<String> summaryUrls) { |
var resourceProvider = new MemoryResourceProvider(); |
+ var resourceUriResolver = new ResourceUriResolver(resourceProvider); |
+ |
var packageBundle = new PackageBundle.fromBuffer(sdkBytes); |
var webDartSdk = new SummaryBasedDartSdk.fromBundle( |
true, packageBundle, resourceProvider); |
- |
var sdkResolver = new DartUriResolver(webDartSdk); |
- var fileResolvers = [new ResourceUriResolver(resourceProvider)]; |
+ |
+ var summaryDataStore = new SummaryDataStore([]); |
+ for (var i = 0; i < summaryBytes.length; i++) { |
+ var bytes = summaryBytes[i]; |
+ var url = summaryUrls[i]; |
+ var summaryBundle = new PackageBundle.fromBuffer(bytes); |
+ summaryDataStore.addBundle(url, summaryBundle); |
+ } |
+ var summaryResolver = new InSummaryPackageUriResolver(summaryDataStore); |
+ |
+ var fileResolvers = [summaryResolver, resourceUriResolver]; |
var compiler = new ModuleCompiler( |
new AnalyzerOptions(dartSdkPath: '/dart-sdk'), |
@@ -76,17 +108,17 @@ class WebCompileCommand extends Command { |
fileResolvers: fileResolvers, |
resourceProvider: resourceProvider); |
- var compilerOptions = new CompilerOptions.fromArguments(argResults); |
+ compiler.context.resultProvider = |
+ new InputPackagesResultProvider(compiler.context, summaryDataStore); |
- var number = 0; |
+ var compilerOptions = new CompilerOptions.fromArguments(argResults); |
- return (String dart) { |
+ var compileFn = (String dart, int number) { |
// Create a new virtual File that contains the given Dart source. |
- number++; |
- resourceProvider.newFile("/expression$number.dart", dart); |
+ resourceProvider.newFile("/expression${number}.dart", dart); |
- var unit = |
- new BuildUnit("", "", ["file:///expression$number.dart"], null); |
+ var unit = new BuildUnit("expression${number}", "", |
+ ["file:///expression${number}.dart"], _moduleForLibrary); |
JSModuleFile module = compiler.compile(unit, compilerOptions); |
module.errors.forEach(messageHandler); |
@@ -94,7 +126,17 @@ class WebCompileCommand extends Command { |
if (!module.isValid) throw new CompileErrorException(); |
return module.code; |
}; |
+ |
+ return allowInterop(compileFn); |
+ } |
+} |
+ |
+// Given path, determine corresponding dart library. |
+String _moduleForLibrary(source) { |
+ if (source is InSummarySource) { |
+ return source.summaryPath.substring(1).replaceAll('.api.ds', ''); |
} |
+ return source.toString().substring(1).replaceAll('.dart', ''); |
} |
/// Thrown when the input source code has errors. |