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/js_ast.dart' as JS; | 7 import '../js/js_ast.dart' as JS; |
8 import '../js/js_ast.dart' show js; | 8 import '../js/js_ast.dart' show js; |
9 import '../options.dart' show ModuleFormat; | 9 import '../options.dart' show ModuleFormat; |
10 | 10 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 | 147 |
148 moduleStatements.addAll(_flattenBlocks(moduleItems)); | 148 moduleStatements.addAll(_flattenBlocks(moduleItems)); |
149 | 149 |
150 if (_exports.isNotEmpty) { | 150 if (_exports.isNotEmpty) { |
151 moduleStatements.add(js.comment('Exports:')); | 151 moduleStatements.add(js.comment('Exports:')); |
152 // TODO(jmesserly): make these immutable in JS? | 152 // TODO(jmesserly): make these immutable in JS? |
153 _exports.forEach((name, exportName) { | 153 _exports.forEach((name, exportName) { |
154 moduleStatements | 154 moduleStatements |
155 .add(js.statement('#.# = #;', [exportsVar, exportName, name])); | 155 .add(js.statement('#.# = #;', [exportsVar, exportName, name])); |
156 }); | 156 }); |
157 moduleStatements | |
158 .add(new JS.ExportDeclaration(exportsVar, isDefault: true)); | |
159 } | 157 } |
| 158 moduleStatements |
| 159 .add(new JS.ExportDeclaration(exportsVar, isDefault: true)); |
160 // TODO(ochafik): What to do with jsModuleValue? | 160 // TODO(ochafik): What to do with jsModuleValue? |
161 return new JS.Program(moduleStatements); | 161 return new JS.Program(moduleStatements); |
162 } | 162 } |
163 } | 163 } |
164 | 164 |
165 /// Generates node modules. | 165 /// Generates node modules. |
166 class NodeModuleBuilder extends ModuleBuilder { | 166 class NodeModuleBuilder extends ModuleBuilder { |
167 NodeModuleBuilder() : super._(); | 167 NodeModuleBuilder() : super._(); |
168 | 168 |
169 JS.Program build(String jsPath, String jsModuleValue, | 169 JS.Program build(String jsPath, String jsModuleValue, |
(...skipping 26 matching lines...) Expand all Loading... |
196 | 196 |
197 /// Flattens blocks in [stats] to a single list of module items. | 197 /// Flattens blocks in [stats] to a single list of module items. |
198 /// Note that in general, blocks should not be flattened, because it can | 198 /// Note that in general, blocks should not be flattened, because it can |
199 /// mess up with block-level scoping (let, const). | 199 /// mess up with block-level scoping (let, const). |
200 // TODO(ochafik): Remove this / find better pattern (adding statements as they | 200 // TODO(ochafik): Remove this / find better pattern (adding statements as they |
201 // are generated from [JSCodegenVisitor], instead of composing them with | 201 // are generated from [JSCodegenVisitor], instead of composing them with |
202 // [_statements]). | 202 // [_statements]). |
203 Iterable<JS.ModuleItem> _flattenBlocks(List<JS.ModuleItem> stats) => | 203 Iterable<JS.ModuleItem> _flattenBlocks(List<JS.ModuleItem> stats) => |
204 stats.expand( | 204 stats.expand( |
205 (item) => item is JS.Block ? _flattenBlocks(item.statements) : [item]); | 205 (item) => item is JS.Block ? _flattenBlocks(item.statements) : [item]); |
OLD | NEW |