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

Side by Side Diff: lib/src/codegen/module_builder.dart

Issue 1612083002: Initial --modules=es6 support (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 4 years, 11 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 unified diff | Download patch
« no previous file with comments | « lib/src/codegen/js_codegen.dart ('k') | lib/src/js/nodes.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library dev_compiler.src.codegen.js_codegen;
6
7 import '../js/js_ast.dart' as JS;
8 import '../js/js_ast.dart' show js;
9 import '../options.dart' show ModuleFormat;
10
11 final _builderCtors = {
12 ModuleFormat.es6: new ES6ModuleBuilder#,
13 ModuleFormat.dart: new DartModuleBuilder#
14 };
15
16 abstract class ModuleBuilder {
17 final String _jsPath;
18 final String _jsModuleValue;
19 final JS.Identifier _exportsVar;
20 final List<String> _exports = <String>[];
21 final List<_ModuleImport> _imports = <_ModuleImport>[];
22
23 ModuleBuilder._(this._jsPath, this._jsModuleValue, this._exportsVar);
24
25 factory ModuleBuilder(String jsPath, String _jsModuleValue, JS.Identifier _exp ortsVar, ModuleFormat format) {
26 var ctor = _builderCtors[format];
27 if (ctor == null) throw new ArgumentError("Unsupported format: $format");
28 return ctor(jsPath, _jsModuleValue, _exportsVar);
29 }
30
31 void addExport(String name) {
32 _exports.add(name);
33 }
34 void addImport(String name, JS.Identifier libVar, {bool isLazy: false}) {
35 _imports.add(new _ModuleImport(name, libVar, isLazy));
36 }
37
38 List<JS.ModuleItem> build(List<JS.Statement> moduleItems);
39 }
40
41 class _ModuleImport {
42 final String name;
43 final JS.Identifier libVar;
44 final bool isLazy;
45 _ModuleImport(this.name, this.libVar, this.isLazy);
46 }
47
48 /// Generates modules for with DDC's `dart_library.js` loading mechanism.
49 class DartModuleBuilder extends ModuleBuilder {
50
51 DartModuleBuilder(jsPath, _jsModuleValue, _exportsVar)
52 : super._(jsPath, _jsModuleValue, _exportsVar);
53
54 List<JS.ModuleItem> build(List<JS.Statement> moduleItems) {
55 // TODO(jmesserly): it would be great to run the renamer on the body,
56 // then figure out if we really need each of these parameters.
57 // See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34
58 var params = [_exportsVar];
59 var lazyParams = [];
60
61 var imports = <JS.Expression>[];
62 var lazyImports = <JS.Expression>[];
63 var moduleStatements = <JS.Statement>[];
64
65 for (var i in _imports) {
66 (i.isLazy ? lazyImports : imports).add(js.string(i.name, "'"));
67 (i.isLazy ? lazyParams : params).add(i.libVar);
68 }
69 params.addAll(lazyParams);
70
71 moduleStatements.addAll(_flattenBlocks(moduleItems));
72
73 if (_exports.isNotEmpty) {
74 moduleStatements.add(js.comment('Exports:'));
75 // TODO(jmesserly): make these immutable in JS?
76 for (var name in _exports) {
77 moduleStatements.add(js.statement('#.# = #;', [_exportsVar, name, name]) );
78 }
79 }
80
81 var module =
82 js.call("function(#) { 'use strict'; #; }", [params, moduleStatements]);
83
84 var moduleDef = js.statement("dart_library.library(#, #, #, #, #)", [
85 js.string(_jsPath, "'"),
86 _jsModuleValue ?? new JS.LiteralNull(),
87 js.commentExpression(
88 "Imports", new JS.ArrayInitializer(imports, multiline: true)),
89 js.commentExpression("Lazy imports",
90 new JS.ArrayInitializer(lazyImports, multiline: true)),
91 module
92 ]);
93 return <JS.ModuleItem>[moduleDef];
94 }
95 }
96
97 /// Generates ES6 modules.
98 ///
99 /// TODO(ochafik): Break strong dep cycles to accommodate the Closure Compiler.
100 class ES6ModuleBuilder extends ModuleBuilder {
101
102 ES6ModuleBuilder(jsPath, _jsModuleValue, _exportsVar)
103 : super._(jsPath, _jsModuleValue, _exportsVar);
104
105 List<JS.ModuleItem> build(List<JS.Statement> moduleItems) {
106 // TODO(jmesserly): it would be great to run the renamer on the body,
107 // then figure out if we really need each of these parameters.
108 // See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34
109 var moduleStatements = <JS.ModuleItem>[];
110
111 moduleStatements.add(js.statement("'use strict';"));
112 // Lazy declarations may reference exports.
113 moduleStatements.add(js.statement("const # = {};", [_exportsVar]));
114
115 for (var i in _imports) {
116 // TODO(ochafik): laziness, late binding, etc, to support Closure...
117 moduleStatements.add(new JS.ImportDeclaration(
118 defaultBinding: i.libVar,
119 from: js.string(i.name)));
120 }
121
122 moduleStatements.addAll(_flattenBlocks(moduleItems));
123
124 if (_exports.isNotEmpty) {
125 moduleStatements.add(js.comment('Exports:'));
126 // TODO(jmesserly): make these immutable in JS?
127 for (var name in _exports) {
128 moduleStatements.add(js.statement('#.# = #;', [_exportsVar, name, name]) );
129 }
130 moduleStatements.add(new JS.ExportDeclaration(_exportsVar, isDefault: true ));
131 }
132 // TODO(ochafik): What to do of _jsModuleValue?
133 return moduleStatements;
134 }
135 }
136
137 Iterable<JS.ModuleItem> _flattenBlocks(List<JS.ModuleItem> stats) =>
138 stats.expand((item) => item is JS.Block ? item.statements : [item]);
OLDNEW
« no previous file with comments | « lib/src/codegen/js_codegen.dart ('k') | lib/src/js/nodes.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698