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: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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 js.string(module.name, "'"), | 98 js.string(module.name, "'"), |
| 99 new LiteralNull(), | 99 new LiteralNull(), |
| 100 js.commentExpression( | 100 js.commentExpression( |
| 101 "Imports", new ArrayInitializer(importNames, multiline: true)), | 101 "Imports", new ArrayInitializer(importNames, multiline: true)), |
| 102 resultModule | 102 resultModule |
| 103 ]); | 103 ]); |
| 104 return new Program(<ModuleItem>[moduleDef]); | 104 return new Program(<ModuleItem>[moduleDef]); |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 /// Generates node modules. | 108 /// Generates commonjs modules (used by node.js). |
|
nweiz
2016/08/16 22:14:54
Nit: "CommonJS" and "Node.js"
Jennifer Messerly
2016/08/24 22:39:51
Done.
| |
| 109 class NodeModuleBuilder extends _ModuleBuilder { | 109 class CommonJSModuleBuilder extends _ModuleBuilder { |
| 110 Program build(Program module) { | 110 Program build(Program module) { |
| 111 var importStatements = <Statement>[]; | 111 var importStatements = <Statement>[]; |
| 112 | 112 |
| 113 // Collect imports/exports/statements. | 113 // Collect imports/exports/statements. |
| 114 visitProgram(module); | 114 visitProgram(module); |
| 115 | 115 |
| 116 for (var import in imports) { | 116 for (var import in imports) { |
| 117 // TODO(jmesserly): we could use destructuring once Atom supports it. | 117 // TODO(jmesserly): we could use destructuring once Atom supports it. |
| 118 var moduleVar = | 118 var moduleVar = |
| 119 new TemporaryId(pathToJSIdentifier(import.from.valueWithoutQuotes)); | 119 new TemporaryId(pathToJSIdentifier(import.from.valueWithoutQuotes)); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 145 | 145 |
| 146 // TODO(vsm): See https://github.com/dart-lang/dev_compiler/issues/512 | 146 // TODO(vsm): See https://github.com/dart-lang/dev_compiler/issues/512 |
| 147 // This extra level of indirection should be unnecessary. | 147 // This extra level of indirection should be unnecessary. |
| 148 var block = | 148 var block = |
| 149 js.statement("(function() { 'use strict'; #; })()", [statements]); | 149 js.statement("(function() { 'use strict'; #; })()", [statements]); |
| 150 | 150 |
| 151 return new Program([block]); | 151 return new Program([block]); |
| 152 } | 152 } |
| 153 } | 153 } |
| 154 | 154 |
| 155 /// Generates requirejs/AMD modules (popular for use in browser). | |
| 156 class RequireJSModuleBuilder extends _ModuleBuilder { | |
| 157 Program build(Program module) { | |
| 158 var importStatements = <Statement>[]; | |
| 159 | |
| 160 // Collect imports/exports/statements. | |
| 161 visitProgram(module); | |
| 162 | |
| 163 var dependencies = <LiteralString>[]; | |
| 164 var fnParams = <Parameter>[]; | |
| 165 for (var import in imports) { | |
| 166 // TODO(jmesserly): we could use destructuring once Atom supports it. | |
|
vsm
2016/08/16 21:23:23
nit: atom / electron would use common, not require
Jennifer Messerly
2016/08/16 21:29:28
thanks, I will clarify that comment. It's more "TO
| |
| 167 var moduleVar = | |
| 168 new TemporaryId(pathToJSIdentifier(import.from.valueWithoutQuotes)); | |
| 169 fnParams.add(moduleVar); | |
| 170 dependencies.add(import.from); | |
| 171 | |
| 172 // TODO(jmesserly): optimize for the common case of a single import. | |
| 173 for (var importName in import.namedImports) { | |
| 174 assert(!importName.isStar); // import * not supported yet. | |
|
nweiz
2016/08/16 22:14:54
Not supported by what?
Jennifer Messerly
2016/08/24 22:39:51
clarified comment. the compiler doesn't emit `impo
| |
| 175 var asName = importName.asName ?? importName.name; | |
| 176 importStatements.add(js.statement( | |
| 177 'const # = #.#', [asName, moduleVar, importName.name.name])); | |
| 178 } | |
| 179 } | |
| 180 statements.insertAll(0, importStatements); | |
| 181 | |
| 182 if (exports.isNotEmpty) { | |
| 183 var exportedProps = <Property>[]; | |
| 184 for (var export in exports) { | |
| 185 var names = export.exportedNames; | |
| 186 assert(names != null); // export * not supported in legacy modules. | |
|
nweiz
2016/08/16 22:14:54
This isn't a legacy module, right?
Jennifer Messerly
2016/08/24 22:39:51
clarified comment. The compiler doesn't emit `expo
| |
| 187 for (var name in names) { | |
| 188 exportedProps.add(new Property(js.string(name.name), name)); | |
| 189 } | |
| 190 } | |
| 191 statements.add(js.comment('Exports:')); | |
| 192 statements.add( | |
| 193 new Return(new ObjectInitializer(exportedProps, multiline: true))); | |
| 194 } | |
| 195 | |
| 196 var block = js.statement("define(#, function(#) { 'use strict'; #; });", | |
| 197 [new ArrayInitializer(dependencies), fnParams, statements]); | |
| 198 | |
| 199 return new Program([block]); | |
| 200 } | |
| 201 } | |
| 202 | |
| 155 /// Escape [name] to make it into a valid identifier. | 203 /// Escape [name] to make it into a valid identifier. |
| 156 String pathToJSIdentifier(String name) { | 204 String pathToJSIdentifier(String name) { |
| 157 return toJSIdentifier(path.basenameWithoutExtension(name)); | 205 return toJSIdentifier(path.basenameWithoutExtension(name)); |
| 158 } | 206 } |
| 159 | 207 |
| 160 /// Escape [name] to make it into a valid identifier. | 208 /// Escape [name] to make it into a valid identifier. |
| 161 String toJSIdentifier(String name) { | 209 String toJSIdentifier(String name) { |
| 162 if (name.length == 0) return r'$'; | 210 if (name.length == 0) return r'$'; |
| 163 | 211 |
| 164 // Escape any invalid characters | 212 // Escape any invalid characters |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 178 // Ensure the identifier first character is not numeric and that the whole | 226 // Ensure the identifier first character is not numeric and that the whole |
| 179 // identifier is not a keyword. | 227 // identifier is not a keyword. |
| 180 if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) { | 228 if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) { |
| 181 return '\$$result'; | 229 return '\$$result'; |
| 182 } | 230 } |
| 183 return result; | 231 return result; |
| 184 } | 232 } |
| 185 | 233 |
| 186 // Invalid characters for identifiers, which would need to be escaped. | 234 // Invalid characters for identifiers, which would need to be escaped. |
| 187 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); | 235 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); |
| OLD | NEW |