Chromium Code Reviews| 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:args/args.dart' show ArgParser, ArgResults; | 5 import 'package:args/args.dart' show ArgParser, ArgResults; |
| 6 import 'package:path/path.dart' as path; | 6 import 'package:path/path.dart' as path; |
| 7 | 7 |
| 8 import '../js_ast/js_ast.dart'; | 8 import '../js_ast/js_ast.dart'; |
| 9 import 'js_names.dart'; | 9 import 'js_names.dart'; |
| 10 | 10 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 statements.add(node.exported.toStatement()); | 135 statements.add(node.exported.toStatement()); |
| 136 } | 136 } |
| 137 | 137 |
| 138 visitStatement(Statement node) { | 138 visitStatement(Statement node) { |
| 139 statements.add(node); | 139 statements.add(node); |
| 140 } | 140 } |
| 141 } | 141 } |
| 142 | 142 |
| 143 /// Generates modules for with our legacy `dart_library.js` loading mechanism. | 143 /// Generates modules for with our legacy `dart_library.js` loading mechanism. |
| 144 // TODO(jmesserly): remove this and replace with something that interoperates. | 144 // TODO(jmesserly): remove this and replace with something that interoperates. |
| 145 class LegacyModuleBuilder extends _ModuleBuilder { | 145 class LegacyModuleBuilder extends _ModuleBuilder { |
|
Jennifer Messerly
2017/02/15 20:08:49
Should we rename this, DeferredModuleBuilder, and
| |
| 146 Program build(Program module) { | 146 Program build(Program module) { |
| 147 // Collect imports/exports/statements. | 147 // Collect imports/exports/statements. |
| 148 visitProgram(module); | 148 visitProgram(module); |
| 149 | 149 |
| 150 // Build import parameters. | 150 // Build import parameters. |
| 151 var exportsVar = new TemporaryId('exports'); | 151 var exportsVar = new TemporaryId('exports'); |
| 152 var parameters = <TemporaryId>[exportsVar]; | 152 var parameters = <TemporaryId>[exportsVar]; |
| 153 var importNames = <Expression>[]; | 153 var importNames = <Expression>[]; |
| 154 var importStatements = <Statement>[]; | 154 var importStatements = <Statement>[]; |
| 155 for (var import in imports) { | 155 for (var import in imports) { |
| 156 importNames.add(import.from); | 156 importNames.add(import.from); |
| 157 // TODO(jmesserly): we could use destructuring here. | 157 // TODO(jmesserly): we could use destructuring here. |
| 158 var moduleVar = | 158 var moduleVar = |
| 159 new TemporaryId(pathToJSIdentifier(import.from.valueWithoutQuotes)); | 159 new TemporaryId(pathToJSIdentifier(import.from.valueWithoutQuotes)); |
| 160 parameters.add(moduleVar); | 160 parameters.add(moduleVar); |
| 161 for (var importName in import.namedImports) { | 161 for (var importName in import.namedImports) { |
| 162 assert(!importName.isStar); // import * not supported in legacy modules. | 162 assert(!importName.isStar); // import * not supported in legacy modules. |
| 163 var asName = importName.asName ?? importName.name; | 163 var asName = importName.asName ?? importName.name; |
| 164 importStatements.add(js.statement( | 164 var fromName = importName.name.name; |
| 165 'const # = #.#', [asName, moduleVar, importName.name.name])); | 165 // Load non-SDK modules on demand (i.e., deferred). |
| 166 if (import.from.valueWithoutQuotes != dartSdkModule) { | |
| 167 importStatements.add(js.statement( | |
| 168 'let # = dart_library.defer(#, #, function (mod, lib) {' | |
|
Jennifer Messerly
2017/02/15 20:08:49
Does this work for a JavaScript module we import?
vsm
2017/02/15 20:32:58
If that JS module uses our legacy modules as well,
| |
| 169 ' # = mod;' | |
| 170 ' # = lib;' | |
| 171 '});', | |
| 172 [asName, moduleVar, js.string(fromName), moduleVar, asName])); | |
| 173 } else { | |
| 174 importStatements.add(js.statement( | |
| 175 'const # = #.#', [asName, moduleVar, importName.name.name])); | |
| 176 } | |
| 166 } | 177 } |
| 167 } | 178 } |
| 168 statements.insertAll(0, importStatements); | 179 statements.insertAll(0, importStatements); |
| 169 | 180 |
| 170 if (exports.isNotEmpty) { | 181 if (exports.isNotEmpty) { |
| 171 statements.add(js.comment('Exports:')); | 182 statements.add(js.comment('Exports:')); |
| 172 // TODO(jmesserly): make these immutable in JS? | 183 // TODO(jmesserly): make these immutable in JS? |
| 173 for (var export in exports) { | 184 for (var export in exports) { |
| 174 var names = export.exportedNames; | 185 var names = export.exportedNames; |
| 175 assert(names != null); // export * not supported in legacy modules. | 186 assert(names != null); // export * not supported in legacy modules. |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 // Ensure the identifier first character is not numeric and that the whole | 345 // Ensure the identifier first character is not numeric and that the whole |
| 335 // identifier is not a keyword. | 346 // identifier is not a keyword. |
| 336 if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) { | 347 if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) { |
| 337 return '\$$result'; | 348 return '\$$result'; |
| 338 } | 349 } |
| 339 return result; | 350 return result; |
| 340 } | 351 } |
| 341 | 352 |
| 342 // Invalid characters for identifiers, which would need to be escaped. | 353 // Invalid characters for identifiers, which would need to be escaped. |
| 343 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); | 354 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); |
| OLD | NEW |