Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(633)

Unified Diff: lib/src/compiler/module_builder.dart

Issue 2249233002: fix #626, add AMD module format and make it default (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: fix sunflower Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/src/compiler/module_builder.dart
diff --git a/lib/src/compiler/module_builder.dart b/lib/src/compiler/module_builder.dart
index 6e72217f6d65df982eb17ce7384ef565e94f6e54..ef607040c83a449db2ad5a43605e953de0150658 100644
--- a/lib/src/compiler/module_builder.dart
+++ b/lib/src/compiler/module_builder.dart
@@ -105,8 +105,8 @@ class LegacyModuleBuilder extends _ModuleBuilder {
}
}
-/// Generates node modules.
-class NodeModuleBuilder extends _ModuleBuilder {
+/// 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.
+class CommonJSModuleBuilder extends _ModuleBuilder {
Program build(Program module) {
var importStatements = <Statement>[];
@@ -152,6 +152,54 @@ class NodeModuleBuilder extends _ModuleBuilder {
}
}
+/// Generates requirejs/AMD modules (popular for use in browser).
+class RequireJSModuleBuilder extends _ModuleBuilder {
+ Program build(Program module) {
+ var importStatements = <Statement>[];
+
+ // Collect imports/exports/statements.
+ visitProgram(module);
+
+ var dependencies = <LiteralString>[];
+ var fnParams = <Parameter>[];
+ for (var import in imports) {
+ // 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
+ var moduleVar =
+ new TemporaryId(pathToJSIdentifier(import.from.valueWithoutQuotes));
+ fnParams.add(moduleVar);
+ dependencies.add(import.from);
+
+ // TODO(jmesserly): optimize for the common case of a single import.
+ for (var importName in import.namedImports) {
+ 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
+ var asName = importName.asName ?? importName.name;
+ importStatements.add(js.statement(
+ 'const # = #.#', [asName, moduleVar, importName.name.name]));
+ }
+ }
+ statements.insertAll(0, importStatements);
+
+ if (exports.isNotEmpty) {
+ var exportedProps = <Property>[];
+ for (var export in exports) {
+ var names = export.exportedNames;
+ 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
+ for (var name in names) {
+ exportedProps.add(new Property(js.string(name.name), name));
+ }
+ }
+ statements.add(js.comment('Exports:'));
+ statements.add(
+ new Return(new ObjectInitializer(exportedProps, multiline: true)));
+ }
+
+ var block = js.statement("define(#, function(#) { 'use strict'; #; });",
+ [new ArrayInitializer(dependencies), fnParams, statements]);
+
+ return new Program([block]);
+ }
+}
+
/// Escape [name] to make it into a valid identifier.
String pathToJSIdentifier(String name) {
return toJSIdentifier(path.basenameWithoutExtension(name));

Powered by Google App Engine
This is Rietveld 408576698