| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 | 4 |
| 5 import 'package:path/path.dart' as path; | 5 import 'package:path/path.dart' as path; |
| 6 | 6 |
| 7 import '../js_ast/js_ast.dart'; | 7 import '../js_ast/js_ast.dart'; |
| 8 import 'js_names.dart'; | 8 import 'js_names.dart'; |
| 9 | 9 |
| 10 /// Base class for compiling ES6 modules into various ES5 module patterns. | 10 /// Base class for compiling ES6 modules into various ES5 module patterns. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 "Imports", new ArrayInitializer(importNames, multiline: true)), | 97 "Imports", new ArrayInitializer(importNames, multiline: true)), |
| 98 resultModule | 98 resultModule |
| 99 ]); | 99 ]); |
| 100 return new Program(<ModuleItem>[moduleDef]); | 100 return new Program(<ModuleItem>[moduleDef]); |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 | 103 |
| 104 /// Generates node modules. | 104 /// Generates node modules. |
| 105 class NodeModuleBuilder extends _ModuleBuilder { | 105 class NodeModuleBuilder extends _ModuleBuilder { |
| 106 Program build(Program module) { | 106 Program build(Program module) { |
| 107 var importStatements = [js.statement("'use strict';"),]; | 107 var importStatements = []; |
| 108 |
| 109 // Collect imports/exports/statements. |
| 110 visitProgram(module); |
| 108 | 111 |
| 109 for (var import in imports) { | 112 for (var import in imports) { |
| 110 // TODO(jmesserly): we could use destructuring once Atom supports it. | 113 // TODO(jmesserly): we could use destructuring once Atom supports it. |
| 111 var moduleVar = | 114 var moduleVar = |
| 112 new TemporaryId(pathToJSIdentifier(import.from.valueWithoutQuotes)); | 115 new TemporaryId(pathToJSIdentifier(import.from.valueWithoutQuotes)); |
| 113 importStatements | 116 importStatements |
| 114 .add(js.statement('const # = require(#);', [moduleVar, import.from])); | 117 .add(js.statement('const # = require(#);', [moduleVar, import.from])); |
| 115 | 118 |
| 116 // TODO(jmesserly): optimize for the common case of a single import. | 119 // TODO(jmesserly): optimize for the common case of a single import. |
| 117 for (var importName in import.namedImports) { | 120 for (var importName in import.namedImports) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 128 statements.add(js.comment('Exports:')); | 131 statements.add(js.comment('Exports:')); |
| 129 for (var export in exports) { | 132 for (var export in exports) { |
| 130 var names = export.exportedNames; | 133 var names = export.exportedNames; |
| 131 assert(names != null); // export * not supported in legacy modules. | 134 assert(names != null); // export * not supported in legacy modules. |
| 132 for (var name in names) { | 135 for (var name in names) { |
| 133 statements | 136 statements |
| 134 .add(js.statement('#.# = #;', [exportsVar, name.name, name])); | 137 .add(js.statement('#.# = #;', [exportsVar, name.name, name])); |
| 135 } | 138 } |
| 136 } | 139 } |
| 137 } | 140 } |
| 138 return new Program(statements); | 141 |
| 142 // TODO(vsm): See https://github.com/dart-lang/dev_compiler/issues/512 |
| 143 // This extra level of indirection should be unnecessary. |
| 144 var block = |
| 145 js.statement("(function() { 'use strict'; #; })()", [statements]); |
| 146 |
| 147 return new Program([block]); |
| 139 } | 148 } |
| 140 } | 149 } |
| 141 | 150 |
| 142 /// Escape [name] to make it into a valid identifier. | 151 /// Escape [name] to make it into a valid identifier. |
| 143 String pathToJSIdentifier(String name) { | 152 String pathToJSIdentifier(String name) { |
| 144 name = path.basenameWithoutExtension(name); | 153 name = path.basenameWithoutExtension(name); |
| 145 if (name.length == 0) return r'$'; | 154 if (name.length == 0) return r'$'; |
| 146 | 155 |
| 147 // Escape any invalid characters | 156 // Escape any invalid characters |
| 148 StringBuffer buffer = null; | 157 StringBuffer buffer = null; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 161 // Ensure the identifier first character is not numeric and that the whole | 170 // Ensure the identifier first character is not numeric and that the whole |
| 162 // identifier is not a keyword. | 171 // identifier is not a keyword. |
| 163 if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) { | 172 if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) { |
| 164 return '\$$result'; | 173 return '\$$result'; |
| 165 } | 174 } |
| 166 return result; | 175 return result; |
| 167 } | 176 } |
| 168 | 177 |
| 169 // Invalid characters for identifiers, which would need to be escaped. | 178 // Invalid characters for identifiers, which would need to be escaped. |
| 170 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); | 179 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); |
| OLD | NEW |