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 var block = | |
vsm
2016/04/18 13:38:25
Not entirely sure why I seem to need this. Withou
Jennifer Messerly
2016/04/18 20:52:18
Yeah seems like it's skipping the top-level. That'
vsm
2016/04/19 21:28:29
Added a TODO and filed a bug for now.
| |
143 js.statement("(function() { 'use strict'; #; })()", [statements]); | |
144 | |
145 return new Program([block]); | |
139 } | 146 } |
140 } | 147 } |
141 | 148 |
142 /// Escape [name] to make it into a valid identifier. | 149 /// Escape [name] to make it into a valid identifier. |
143 String pathToJSIdentifier(String name) { | 150 String pathToJSIdentifier(String name) { |
144 name = path.basenameWithoutExtension(name); | 151 name = path.basenameWithoutExtension(name); |
145 if (name.length == 0) return r'$'; | 152 if (name.length == 0) return r'$'; |
146 | 153 |
147 // Escape any invalid characters | 154 // Escape any invalid characters |
148 StringBuffer buffer = null; | 155 StringBuffer buffer = null; |
(...skipping 12 matching lines...) Expand all Loading... | |
161 // Ensure the identifier first character is not numeric and that the whole | 168 // Ensure the identifier first character is not numeric and that the whole |
162 // identifier is not a keyword. | 169 // identifier is not a keyword. |
163 if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) { | 170 if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) { |
164 return '\$$result'; | 171 return '\$$result'; |
165 } | 172 } |
166 return result; | 173 return result; |
167 } | 174 } |
168 | 175 |
169 // Invalid characters for identifiers, which would need to be escaped. | 176 // Invalid characters for identifiers, which would need to be escaped. |
170 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); | 177 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); |
OLD | NEW |