OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:convert' show JSON; |
| 6 import 'dart:io'; |
| 7 import 'package:args/command_runner.dart'; |
| 8 import 'package:analyzer/src/generated/source.dart' show Source; |
| 9 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
| 10 show InSummarySource; |
| 11 import 'compiler.dart' |
| 12 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
| 13 import '../analyzer/context.dart' show AnalyzerOptions; |
| 14 import 'package:path/path.dart' as path; |
| 15 |
| 16 /// The command for invoking the modular compiler. |
| 17 class CompileCommand extends Command { |
| 18 get name => 'compile'; |
| 19 get description => 'Compile a set of Dart files into a JavaScript module.'; |
| 20 |
| 21 CompileCommand() { |
| 22 argParser.addOption('out', abbr: 'o', help: 'Output file (required)'); |
| 23 CompilerOptions.addArguments(argParser); |
| 24 AnalyzerOptions.addArguments(argParser); |
| 25 } |
| 26 |
| 27 @override |
| 28 void run() { |
| 29 var compilerOptions = new CompilerOptions.fromArguments(argResults); |
| 30 var compiler = |
| 31 new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults)); |
| 32 |
| 33 var outPath = argResults['out']; |
| 34 if (outPath == null) { |
| 35 usageException('Please include the output file location. For example:\n' |
| 36 ' -o PATH/TO/OUTPUT_FILE.js'); |
| 37 } |
| 38 var unit = new BuildUnit(path.basenameWithoutExtension(outPath), |
| 39 argResults.rest, _moduleForLibrary); |
| 40 |
| 41 JSModuleFile module = compiler.compile(unit, compilerOptions); |
| 42 module.errors.forEach(print); |
| 43 |
| 44 if (!module.isValid) throw new CompileErrorException(); |
| 45 |
| 46 // Write JS file, as well as source map and summary (if requested). |
| 47 new File(outPath).writeAsStringSync(module.code); |
| 48 if (module.sourceMap != null) { |
| 49 var mapPath = outPath + '.map'; |
| 50 new File(mapPath) |
| 51 .writeAsStringSync(JSON.encode(module.placeSourceMap(mapPath))); |
| 52 } |
| 53 if (module.summaryBytes != null) { |
| 54 var summaryPath = path.withoutExtension(outPath) + '.sum'; |
| 55 new File(summaryPath).writeAsBytesSync(module.summaryBytes); |
| 56 } |
| 57 } |
| 58 |
| 59 String _moduleForLibrary(Source source) { |
| 60 if (source is InSummarySource) { |
| 61 return path.basenameWithoutExtension(source.summaryPath); |
| 62 } |
| 63 |
| 64 throw usageException( |
| 65 'Imported file "${source.uri}" was not found as a summary or source ' |
| 66 'file. Please pass in either the summary or the source file ' |
| 67 'for this import.'); |
| 68 } |
| 69 } |
| 70 |
| 71 /// Thrown when the input source code has errors. |
| 72 class CompileErrorException implements Exception { |
| 73 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
| 74 } |
OLD | NEW |