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:html' show HttpRequest; | 8 import 'dart:html' show HttpRequest; |
9 import 'dart:convert' show BASE64; | 9 import 'dart:convert' show BASE64; |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions; | 27 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions; |
28 import 'package:dev_compiler/src/compiler/compiler.dart' | 28 import 'package:dev_compiler/src/compiler/compiler.dart' |
29 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; | 29 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
30 | 30 |
31 import 'package:dev_compiler/src/compiler/module_builder.dart'; | 31 import 'package:dev_compiler/src/compiler/module_builder.dart'; |
32 import 'package:js/js.dart'; | 32 import 'package:js/js.dart'; |
33 | 33 |
34 typedef void MessageHandler(Object message); | 34 typedef void MessageHandler(Object message); |
35 | 35 |
| 36 @JS() |
| 37 @anonymous |
| 38 class CompileResult { |
| 39 external factory CompileResult( |
| 40 {String code, List<String> errors, bool isValid}); |
| 41 } |
| 42 |
| 43 typedef CompileResult CompileModule( |
| 44 String code, String libraryName, String fileName); |
| 45 |
36 /// The command for invoking the modular compiler. | 46 /// The command for invoking the modular compiler. |
37 class WebCompileCommand extends Command { | 47 class WebCompileCommand extends Command { |
38 get name => 'compile'; | 48 get name => 'compile'; |
39 | 49 |
40 get description => 'Compile a set of Dart files into a JavaScript module.'; | 50 get description => 'Compile a set of Dart files into a JavaScript module.'; |
41 final MessageHandler messageHandler; | 51 final MessageHandler messageHandler; |
42 | 52 |
43 WebCompileCommand({MessageHandler messageHandler}) | 53 WebCompileCommand({MessageHandler messageHandler}) |
44 : this.messageHandler = messageHandler ?? print { | 54 : this.messageHandler = messageHandler ?? print { |
45 CompilerOptions.addArguments(argParser); | 55 CompilerOptions.addArguments(argParser); |
(...skipping 22 matching lines...) Expand all Loading... |
68 summaryBytes.add(BASE64.decode(response.responseText)); | 78 summaryBytes.add(BASE64.decode(response.responseText)); |
69 } | 79 } |
70 | 80 |
71 var compileFn = setUpCompile(sdkBytes, summaryBytes, summaryUrls); | 81 var compileFn = setUpCompile(sdkBytes, summaryBytes, summaryUrls); |
72 onCompileReady(compileFn); | 82 onCompileReady(compileFn); |
73 }).catchError((error) => onError('Summaries failed to load: $error')); | 83 }).catchError((error) => onError('Summaries failed to load: $error')); |
74 }).catchError( | 84 }).catchError( |
75 (error) => onError('Dart sdk summaries failed to load: $error')); | 85 (error) => onError('Dart sdk summaries failed to load: $error')); |
76 } | 86 } |
77 | 87 |
78 Function setUpCompile(List<int> sdkBytes, List<List<int>> summaryBytes, | 88 CompileModule setUpCompile(List<int> sdkBytes, List<List<int>> summaryBytes, |
79 List<String> summaryUrls) { | 89 List<String> summaryUrls) { |
80 var resourceProvider = new MemoryResourceProvider(); | 90 var resourceProvider = new MemoryResourceProvider(); |
81 var resourceUriResolver = new ResourceUriResolver(resourceProvider); | 91 var resourceUriResolver = new ResourceUriResolver(resourceProvider); |
82 | 92 |
83 var packageBundle = new PackageBundle.fromBuffer(sdkBytes); | 93 var packageBundle = new PackageBundle.fromBuffer(sdkBytes); |
84 var webDartSdk = new SummaryBasedDartSdk.fromBundle( | 94 var webDartSdk = new SummaryBasedDartSdk.fromBundle( |
85 true, packageBundle, resourceProvider); | 95 true, packageBundle, resourceProvider); |
86 var sdkResolver = new DartUriResolver(webDartSdk); | 96 var sdkResolver = new DartUriResolver(webDartSdk); |
87 | 97 |
88 var summaryDataStore = new SummaryDataStore([]); | 98 var summaryDataStore = new SummaryDataStore([]); |
(...skipping 11 matching lines...) Expand all Loading... |
100 new AnalyzerOptions(dartSdkPath: '/dart-sdk'), | 110 new AnalyzerOptions(dartSdkPath: '/dart-sdk'), |
101 sdkResolver: sdkResolver, | 111 sdkResolver: sdkResolver, |
102 fileResolvers: fileResolvers, | 112 fileResolvers: fileResolvers, |
103 resourceProvider: resourceProvider); | 113 resourceProvider: resourceProvider); |
104 | 114 |
105 (compiler.context as AnalysisContextImpl).resultProvider = | 115 (compiler.context as AnalysisContextImpl).resultProvider = |
106 new InputPackagesResultProvider(compiler.context, summaryDataStore); | 116 new InputPackagesResultProvider(compiler.context, summaryDataStore); |
107 | 117 |
108 var compilerOptions = new CompilerOptions.fromArguments(argResults); | 118 var compilerOptions = new CompilerOptions.fromArguments(argResults); |
109 | 119 |
110 var compileFn = (String dart, int number) { | 120 CompileModule compileFn = |
| 121 (String sourceCode, String libraryName, String fileName) { |
111 // Create a new virtual File that contains the given Dart source. | 122 // Create a new virtual File that contains the given Dart source. |
112 resourceProvider.newFile("/expression${number}.dart", dart); | 123 resourceProvider.newFile("/$fileName", sourceCode); |
113 | 124 |
114 var unit = new BuildUnit("expression${number}", "", | 125 var unit = new BuildUnit( |
115 ["file:///expression${number}.dart"], _moduleForLibrary); | 126 libraryName, "", ["file:///$fileName"], _moduleForLibrary); |
116 | 127 |
117 JSModuleFile module = compiler.compile(unit, compilerOptions); | 128 JSModuleFile module = compiler.compile(unit, compilerOptions); |
118 module.errors.forEach(messageHandler); | |
119 | 129 |
120 if (!module.isValid) throw new CompileErrorException(); | 130 var moduleCode = module.isValid |
| 131 ? module |
| 132 .getCode(ModuleFormat.legacy, unit.name, unit.name + '.map') |
| 133 .code |
| 134 : ''; |
121 | 135 |
122 var code = | 136 return new CompileResult( |
123 module.getCode(ModuleFormat.amd, unit.name, unit.name + '.map'); | 137 code: moduleCode, isValid: module.isValid, errors: module.errors); |
124 return code.code; | |
125 }; | 138 }; |
126 | 139 |
127 return allowInterop(compileFn); | 140 return allowInterop(compileFn); |
128 } | 141 } |
129 } | 142 } |
130 | 143 |
131 // Given path, determine corresponding dart library. | 144 // Given path, determine corresponding dart library. |
132 String _moduleForLibrary(source) { | 145 String _moduleForLibrary(source) { |
133 if (source is InSummarySource) { | 146 if (source is InSummarySource) { |
134 return source.summaryPath.substring(1).replaceAll('.api.ds', ''); | 147 return source.summaryPath.substring(1).replaceAll('.api.ds', ''); |
135 } | 148 } |
136 return source.toString().substring(1).replaceAll('.dart', ''); | 149 return source.toString().substring(1).replaceAll('.dart', ''); |
137 } | 150 } |
138 | 151 |
139 /// Thrown when the input source code has errors. | 152 /// Thrown when the input source code has errors. |
140 class CompileErrorException implements Exception { | 153 class CompileErrorException implements Exception { |
141 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 154 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
142 } | 155 } |
OLD | NEW |