| 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 library dev_compiler.src.codegen.module_builder; | 5 library dev_compiler.src.codegen.module_builder; |
| 6 | 6 |
| 7 import 'package:path/path.dart' show relative; | 7 import 'package:path/path.dart' show relative; |
| 8 | 8 |
| 9 import '../js/js_ast.dart' as JS; | 9 import '../js/js_ast.dart' as JS; |
| 10 import '../js/js_ast.dart' show js; | 10 import '../js/js_ast.dart' show js; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 // Lazy declarations may reference exports. | 125 // Lazy declarations may reference exports. |
| 126 js.statement("const # = {};", [exportsVar]) | 126 js.statement("const # = {};", [exportsVar]) |
| 127 ]; | 127 ]; |
| 128 | 128 |
| 129 // TODO(jmesserly): it would be great to run the renamer on the body, | 129 // TODO(jmesserly): it would be great to run the renamer on the body, |
| 130 // then figure out if we really need each of these parameters. | 130 // then figure out if we really need each of these parameters. |
| 131 // See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34 | 131 // See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34 |
| 132 for (var i in _imports) { | 132 for (var i in _imports) { |
| 133 var moduleName = js.string(_relativeModuleName(i.name, from: jsPath)); | 133 var moduleName = js.string(_relativeModuleName(i.name, from: jsPath)); |
| 134 // TODO(ochafik): laziness, late binding, etc, to support Closure... | 134 // TODO(ochafik): laziness, late binding, etc, to support Closure... |
| 135 moduleStatements.add(new JS.ImportDeclaration( | 135 moduleStatements.add( |
| 136 defaultBinding: i.libVar, from: moduleName)); | 136 new JS.ImportDeclaration(defaultBinding: i.libVar, from: moduleName)); |
| 137 } | 137 } |
| 138 | 138 |
| 139 moduleStatements.addAll(_flattenBlocks(moduleItems)); | 139 moduleStatements.addAll(_flattenBlocks(moduleItems)); |
| 140 | 140 |
| 141 if (_exports.isNotEmpty) { | 141 if (_exports.isNotEmpty) { |
| 142 moduleStatements.add(js.comment('Exports:')); | 142 moduleStatements.add(js.comment('Exports:')); |
| 143 // TODO(jmesserly): make these immutable in JS? | 143 // TODO(jmesserly): make these immutable in JS? |
| 144 for (var name in _exports) { | 144 for (var name in _exports) { |
| 145 moduleStatements | 145 moduleStatements |
| 146 .add(js.statement('#.# = #;', [exportsVar, name, name])); | 146 .add(js.statement('#.# = #;', [exportsVar, name, name])); |
| 147 } | 147 } |
| 148 moduleStatements | 148 moduleStatements |
| 149 .add(new JS.ExportDeclaration(exportsVar, isDefault: true)); | 149 .add(new JS.ExportDeclaration(exportsVar, isDefault: true)); |
| 150 } | 150 } |
| 151 // TODO(ochafik): What to do of jsModuleValue? | 151 // TODO(ochafik): What to do of jsModuleValue? |
| 152 return new JS.Program(moduleStatements); | 152 return new JS.Program(moduleStatements); |
| 153 } | 153 } |
| 154 } | 154 } |
| 155 | 155 |
| 156 /// Flattens blocks in [stats] to a single list of module items. | 156 /// Flattens blocks in [stats] to a single list of module items. |
| 157 /// Note that in general, blocks should not be flattened, because it can | 157 /// Note that in general, blocks should not be flattened, because it can |
| 158 /// mess up with block-level scoping (let, const). | 158 /// mess up with block-level scoping (let, const). |
| 159 // TODO(ochafik): Remove this / find better pattern (adding statements as they | 159 // TODO(ochafik): Remove this / find better pattern (adding statements as they |
| 160 // are generated from [JSCodegenVisitor], instead of composing them with | 160 // are generated from [JSCodegenVisitor], instead of composing them with |
| 161 // [_statements]). | 161 // [_statements]). |
| 162 Iterable<JS.ModuleItem> _flattenBlocks(List<JS.ModuleItem> stats) => | 162 Iterable<JS.ModuleItem> _flattenBlocks(List<JS.ModuleItem> stats) => |
| 163 stats.expand((item) => item is JS.Block | 163 stats.expand( |
| 164 ? _flattenBlocks(item.statements) : [item]); | 164 (item) => item is JS.Block ? _flattenBlocks(item.statements) : [item]); |
| OLD | NEW |