OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 @JS() | 4 @JS() |
5 library dev_compiler.web.web_command; | 5 library dev_compiler.web.web_command; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:html' show HttpRequest; | 9 import 'dart:html' show HttpRequest; |
| 10 import 'dart:typed_data'; |
10 | 11 |
11 import 'package:analyzer/dart/element/element.dart' | 12 import 'package:analyzer/dart/element/element.dart' |
12 show | 13 show |
13 LibraryElement, | 14 LibraryElement, |
14 ImportElement, | 15 ImportElement, |
15 ShowElementCombinator, | 16 ShowElementCombinator, |
16 HideElementCombinator; | 17 HideElementCombinator; |
17 import 'package:analyzer/file_system/file_system.dart' show ResourceUriResolver; | 18 import 'package:analyzer/file_system/file_system.dart' show ResourceUriResolver; |
18 import 'package:analyzer/file_system/memory_file_system.dart' | 19 import 'package:analyzer/file_system/memory_file_system.dart' |
19 show MemoryResourceProvider; | 20 show MemoryResourceProvider; |
(...skipping 10 matching lines...) Expand all Loading... |
30 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; | 31 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
31 | 32 |
32 import 'package:dev_compiler/src/compiler/module_builder.dart'; | 33 import 'package:dev_compiler/src/compiler/module_builder.dart'; |
33 import 'package:js/js.dart'; | 34 import 'package:js/js.dart'; |
34 import 'package:path/path.dart' as path; | 35 import 'package:path/path.dart' as path; |
35 | 36 |
36 typedef void MessageHandler(Object message); | 37 typedef void MessageHandler(Object message); |
37 | 38 |
38 @JS() | 39 @JS() |
39 @anonymous | 40 @anonymous |
| 41 class JSIterator<V> {} |
| 42 |
| 43 @JS('Map') |
| 44 class JSMap<K, V> { |
| 45 external V get(K v); |
| 46 external set(K k, V v); |
| 47 external JSIterator<K> keys(); |
| 48 external JSIterator<V> values(); |
| 49 } |
| 50 |
| 51 @JS('Array.from') |
| 52 external List<V> iteratorToList<V>(JSIterator<V> iterator); |
| 53 |
| 54 @JS() |
| 55 @anonymous |
40 class CompileResult { | 56 class CompileResult { |
41 external factory CompileResult( | 57 external factory CompileResult( |
42 {String code, List<String> errors, bool isValid}); | 58 {String code, List<String> errors, bool isValid}); |
43 } | 59 } |
44 | 60 |
45 typedef CompileModule(String imports, String body, String libraryName, | 61 typedef CompileModule(String imports, String body, String libraryName, |
46 String existingLibrary, String fileName); | 62 String existingLibrary, String fileName); |
47 | 63 |
48 /// The command for invoking the modular compiler. | 64 /// The command for invoking the modular compiler. |
49 class WebCompileCommand extends Command { | 65 class WebCompileCommand extends Command { |
50 get name => 'compile'; | 66 get name => 'compile'; |
51 | 67 |
52 get description => 'Compile a set of Dart files into a JavaScript module.'; | 68 get description => 'Compile a set of Dart files into a JavaScript module.'; |
53 final MessageHandler messageHandler; | 69 final MessageHandler messageHandler; |
54 | 70 |
55 WebCompileCommand({MessageHandler messageHandler}) | 71 WebCompileCommand({MessageHandler messageHandler}) |
56 : this.messageHandler = messageHandler ?? print { | 72 : this.messageHandler = messageHandler ?? print { |
57 CompilerOptions.addArguments(argParser); | 73 CompilerOptions.addArguments(argParser); |
58 AnalyzerOptions.addArguments(argParser); | 74 AnalyzerOptions.addArguments(argParser); |
59 } | 75 } |
60 | 76 |
61 @override | 77 @override |
62 Function run() { | 78 Function run() { |
63 return requestSummaries; | 79 return requestSummaries; |
64 } | 80 } |
65 | 81 |
66 void requestSummaries(String summaryRoot, String sdkUrl, | 82 Future<Null> requestSummaries(String sdkUrl, JSMap<String, String> summaryMap, |
67 List<String> summaryUrls, Function onCompileReady, Function onError) { | 83 Function onCompileReady, Function onError) async { |
68 HttpRequest | 84 var sdkRequest; |
69 .request(sdkUrl, | 85 try { |
70 responseType: "arraybuffer", mimeType: "application/octet-stream") | 86 sdkRequest = await HttpRequest.request(sdkUrl, |
71 .then((sdkRequest) { | 87 responseType: "arraybuffer", mimeType: "application/octet-stream"); |
72 var sdkBytes = sdkRequest.response.asUint8List(); | 88 } catch (error) { |
| 89 onError('Dart sdk summaries failed to load: $error. url: $sdkUrl'); |
| 90 return null; |
| 91 } |
73 | 92 |
74 // Map summary URLs to HttpRequests. | 93 var sdkBytes = (sdkRequest.response as ByteBuffer).asUint8List(); |
75 var summaryRequests = summaryUrls.map((summary) => new Future(() => | |
76 HttpRequest.request(summaryRoot + summary, | |
77 responseType: "arraybuffer", | |
78 mimeType: "application/octet-stream"))); | |
79 | 94 |
80 Future.wait(summaryRequests).then((summaryResponses) { | 95 // Map summary URLs to HttpRequests. |
81 // Map summary responses to summary bytes. | 96 var summaryRequests = iteratorToList(summaryMap.values()) |
82 var summaryBytes = <List<int>>[]; | 97 .map((String summaryUrl) => HttpRequest.request(summaryUrl, |
83 for (var response in summaryResponses) { | 98 responseType: "arraybuffer", mimeType: "application/octet-stream")) |
84 summaryBytes.add(response.response.asUint8List()); | 99 .toList(); |
85 } | 100 try { |
86 | 101 var summaryResponses = await Future.wait(summaryRequests); |
87 onCompileReady(setUpCompile(sdkBytes, summaryBytes, summaryUrls)); | 102 // Map summary responses to summary bytes. |
88 }).catchError((error) => onError('Summaries failed to load: $error')); | 103 List<List<int>> summaryBytes = summaryResponses |
89 }).catchError((error) => | 104 .map((response) => (response.response as ByteBuffer).asUint8List()) |
90 onError('Dart sdk summaries failed to load: $error. url: $sdkUrl')); | 105 .toList(); |
| 106 onCompileReady(setUpCompile( |
| 107 sdkBytes, summaryBytes, iteratorToList(summaryMap.keys()))); |
| 108 } catch (error) { |
| 109 onError('Summaries failed to load: $error'); |
| 110 } |
91 } | 111 } |
92 | 112 |
93 List<Function> setUpCompile(List<int> sdkBytes, List<List<int>> summaryBytes, | 113 List<Function> setUpCompile(List<int> sdkBytes, List<List<int>> summaryBytes, |
94 List<String> summaryUrls) { | 114 List<String> moduleIds) { |
95 var dartSdkSummaryPath = '/dart-sdk/lib/_internal/web_sdk.sum'; | 115 var dartSdkSummaryPath = '/dart-sdk/lib/_internal/web_sdk.sum'; |
96 | 116 |
97 var resourceProvider = new MemoryResourceProvider() | 117 var resourceProvider = new MemoryResourceProvider() |
98 ..newFileWithBytes(dartSdkSummaryPath, sdkBytes); | 118 ..newFileWithBytes(dartSdkSummaryPath, sdkBytes); |
99 | 119 |
100 var resourceUriResolver = new ResourceUriResolver(resourceProvider); | 120 var resourceUriResolver = new ResourceUriResolver(resourceProvider); |
101 | 121 |
102 var options = new AnalyzerOptions.basic( | 122 var options = new AnalyzerOptions.basic( |
103 dartSdkPath: '/dart-sdk', dartSdkSummaryPath: dartSdkSummaryPath); | 123 dartSdkPath: '/dart-sdk', dartSdkSummaryPath: dartSdkSummaryPath); |
104 | 124 |
105 var summaryDataStore = new SummaryDataStore(options.summaryPaths, | 125 var summaryDataStore = new SummaryDataStore(options.summaryPaths, |
106 resourceProvider: resourceProvider, recordDependencyInfo: true); | 126 resourceProvider: resourceProvider, recordDependencyInfo: true); |
107 for (var i = 0; i < summaryBytes.length; i++) { | 127 for (var i = 0; i < summaryBytes.length; i++) { |
108 var bytes = summaryBytes[i]; | 128 var bytes = summaryBytes[i]; |
109 var url = '/' + summaryUrls[i]; | 129 |
| 130 // Packages with no dart source files will have empty invalid summaries. |
| 131 if (bytes.length == 0) continue; |
| 132 |
| 133 var url = '/${moduleIds[i]}.api.ds'; |
110 var summaryBundle = new PackageBundle.fromBuffer(bytes); | 134 var summaryBundle = new PackageBundle.fromBuffer(bytes); |
111 summaryDataStore.addBundle(url, summaryBundle); | 135 summaryDataStore.addBundle(url, summaryBundle); |
112 } | 136 } |
113 var summaryResolver = | 137 var summaryResolver = |
114 new InSummaryUriResolver(resourceProvider, summaryDataStore); | 138 new InSummaryUriResolver(resourceProvider, summaryDataStore); |
115 | 139 |
116 var fileResolvers = [summaryResolver, resourceUriResolver]; | 140 var fileResolvers = [summaryResolver, resourceUriResolver]; |
117 | 141 |
118 var compiler = new ModuleCompiler(options, | 142 var compiler = new ModuleCompiler(options, |
119 analysisRoot: '/web-compile-root', | 143 analysisRoot: '/web-compile-root', |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 if (source is InSummarySource) { | 300 if (source is InSummarySource) { |
277 return source.summaryPath.substring(1).replaceAll('.api.ds', ''); | 301 return source.summaryPath.substring(1).replaceAll('.api.ds', ''); |
278 } | 302 } |
279 return source.toString().substring(1).replaceAll('.dart', ''); | 303 return source.toString().substring(1).replaceAll('.dart', ''); |
280 } | 304 } |
281 | 305 |
282 /// Thrown when the input source code has errors. | 306 /// Thrown when the input source code has errors. |
283 class CompileErrorException implements Exception { | 307 class CompileErrorException implements Exception { |
284 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 308 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
285 } | 309 } |
OLD | NEW |