Skip to content

Commit 4eec9f5

Browse files
author
Olivier Chafik
committed
Resolve local paths in es6 modules (+ refactor ModuleBuilder)
1 parent adf6842 commit 4eec9f5

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

pkg/dev_compiler/lib/src/codegen/js_codegen.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
179179
id.setQualified(!_loader.isLoaded(element));
180180
}
181181

182-
var moduleBuilder = new ModuleBuilder(
183-
compiler.getModuleName(currentLibrary.source.uri),
184-
_jsModuleValue,
185-
_exportsVar,
186-
options.moduleFormat);
182+
var moduleBuilder = new ModuleBuilder(options.moduleFormat);
187183

188184
_exports.forEach(moduleBuilder.addExport);
189185

@@ -208,7 +204,11 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
208204
// var jsBin = compiler.options.runnerOptions.v8Binary;
209205
// String scriptTag = null;
210206
// if (library.library.scriptTag != null) scriptTag = '/usr/bin/env $jsBin';
211-
return moduleBuilder.build(items);
207+
return moduleBuilder.build(
208+
compiler.getModuleName(currentLibrary.source.uri),
209+
_jsModuleValue,
210+
_exportsVar,
211+
items);
212212
}
213213

214214
void _emitModuleItem(AstNode node) {

pkg/dev_compiler/lib/src/codegen/module_builder.dart

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44

55
library dev_compiler.src.codegen.module_builder;
66

7+
import 'package:path/path.dart' show relative;
8+
79
import '../js/js_ast.dart' as JS;
810
import '../js/js_ast.dart' show js;
911
import '../options.dart' show ModuleFormat;
1012

1113
/// Helper that builds JS modules in a given [ModuleFormat].
1214
abstract class ModuleBuilder {
13-
final String _jsPath;
14-
final String _jsModuleValue;
15-
final JS.Identifier _exportsVar;
1615
final List<String> _exports = <String>[];
1716
final List<_ModuleImport> _imports = <_ModuleImport>[];
1817

19-
ModuleBuilder._(this._jsPath, this._jsModuleValue, this._exportsVar);
18+
ModuleBuilder._();
2019

2120
/// Returns a [format]-specific [ModuleBuilder].
2221
/// - [jsPath] is the path of the module being built.
@@ -25,13 +24,12 @@ abstract class ModuleBuilder {
2524
/// library directive). It is null in any other case.
2625
/// - [exportsVar] is the name of the object on which items are exported. Lazy
2726
/// variables and constants are assumed to be declared on this instance.
28-
factory ModuleBuilder(String jsPath, String jsModuleValue,
29-
JS.Identifier exportsVar, ModuleFormat format) {
27+
factory ModuleBuilder(ModuleFormat format) {
3028
switch (format) {
3129
case ModuleFormat.legacy:
32-
return new LegacyModuleBuilder(jsPath, jsModuleValue, exportsVar);
30+
return new LegacyModuleBuilder();
3331
case ModuleFormat.es6:
34-
return new ES6ModuleBuilder(jsPath, jsModuleValue, exportsVar);
32+
return new ES6ModuleBuilder();
3533
}
3634
}
3735

@@ -48,7 +46,8 @@ abstract class ModuleBuilder {
4846
}
4947

5048
/// Builds a program out of menu items.
51-
JS.Program build(List<JS.ModuleItem> moduleItems);
49+
JS.Program build(String jsPath, String jsModuleValue,
50+
JS.Identifier exportsVar, Iterable<JS.ModuleItem> moduleItems);
5251
}
5352

5453
class _ModuleImport {
@@ -63,14 +62,14 @@ class _ModuleImport {
6362

6463
/// Generates modules for with DDC's `dart_library.js` loading mechanism.
6564
class LegacyModuleBuilder extends ModuleBuilder {
66-
LegacyModuleBuilder(jsPath, _jsModuleValue, _exportsVar)
67-
: super._(jsPath, _jsModuleValue, _exportsVar);
65+
LegacyModuleBuilder() : super._();
6866

69-
JS.Program build(List<JS.ModuleItem> moduleItems) {
67+
JS.Program build(String jsPath, String jsModuleValue,
68+
JS.Identifier exportsVar, List<JS.ModuleItem> moduleItems) {
7069
// TODO(jmesserly): it would be great to run the renamer on the body,
7170
// then figure out if we really need each of these parameters.
7271
// See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34
73-
var params = [_exportsVar];
72+
var params = [exportsVar];
7473
var lazyParams = [];
7574

7675
var imports = <JS.Expression>[];
@@ -90,16 +89,16 @@ class LegacyModuleBuilder extends ModuleBuilder {
9089
// TODO(jmesserly): make these immutable in JS?
9190
for (var name in _exports) {
9291
moduleStatements
93-
.add(js.statement('#.# = #;', [_exportsVar, name, name]));
92+
.add(js.statement('#.# = #;', [exportsVar, name, name]));
9493
}
9594
}
9695

9796
var module =
9897
js.call("function(#) { 'use strict'; #; }", [params, moduleStatements]);
9998

10099
var moduleDef = js.statement("dart_library.library(#, #, #, #, #)", [
101-
js.string(_jsPath, "'"),
102-
_jsModuleValue ?? new JS.LiteralNull(),
100+
js.string(jsPath, "'"),
101+
jsModuleValue ?? new JS.LiteralNull(),
103102
js.commentExpression(
104103
"Imports", new JS.ArrayInitializer(imports, multiline: true)),
105104
js.commentExpression("Lazy imports",
@@ -110,25 +109,31 @@ class LegacyModuleBuilder extends ModuleBuilder {
110109
}
111110
}
112111

112+
String _relativeModuleName(String moduleName, {String from}) {
113+
var relativeName = relative('/' + moduleName, from: '/' + from + '/..');
114+
return relativeName.startsWith('.') ? relativeName : './$relativeName';
115+
}
116+
113117
/// Generates ES6 modules.
114118
// TODO(ochafik): Break strong dep cycles to accommodate the Closure Compiler.
115119
class ES6ModuleBuilder extends ModuleBuilder {
116-
ES6ModuleBuilder(jsPath, _jsModuleValue, _exportsVar)
117-
: super._(jsPath, _jsModuleValue, _exportsVar);
120+
ES6ModuleBuilder() : super._();
118121

119-
JS.Program build(List<JS.ModuleItem> moduleItems) {
122+
JS.Program build(String jsPath, String jsModuleValue,
123+
JS.Identifier exportsVar, Iterable<JS.ModuleItem> moduleItems) {
120124
var moduleStatements = <JS.ModuleItem>[
121125
// Lazy declarations may reference exports.
122-
js.statement("const # = {};", [_exportsVar])
126+
js.statement("const # = {};", [exportsVar])
123127
];
124128

125129
// TODO(jmesserly): it would be great to run the renamer on the body,
126130
// then figure out if we really need each of these parameters.
127131
// See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34
128132
for (var i in _imports) {
133+
var moduleName = js.string(_relativeModuleName(i.name, from: jsPath));
129134
// TODO(ochafik): laziness, late binding, etc, to support Closure...
130135
moduleStatements.add(new JS.ImportDeclaration(
131-
defaultBinding: i.libVar, from: js.string(i.name)));
136+
defaultBinding: i.libVar, from: moduleName));
132137
}
133138

134139
moduleStatements.addAll(_flattenBlocks(moduleItems));
@@ -138,12 +143,12 @@ class ES6ModuleBuilder extends ModuleBuilder {
138143
// TODO(jmesserly): make these immutable in JS?
139144
for (var name in _exports) {
140145
moduleStatements
141-
.add(js.statement('#.# = #;', [_exportsVar, name, name]));
146+
.add(js.statement('#.# = #;', [exportsVar, name, name]));
142147
}
143148
moduleStatements
144-
.add(new JS.ExportDeclaration(_exportsVar, isDefault: true));
149+
.add(new JS.ExportDeclaration(exportsVar, isDefault: true));
145150
}
146-
// TODO(ochafik): What to do of _jsModuleValue?
151+
// TODO(ochafik): What to do of jsModuleValue?
147152
return new JS.Program(moduleStatements);
148153
}
149154
}
@@ -155,4 +160,5 @@ class ES6ModuleBuilder extends ModuleBuilder {
155160
// are generated from [JSCodegenVisitor], instead of composing them with
156161
// [_statements]).
157162
Iterable<JS.ModuleItem> _flattenBlocks(List<JS.ModuleItem> stats) =>
158-
stats.expand((item) => item is JS.Block ? item.statements : [item]);
163+
stats.expand((item) => item is JS.Block
164+
? _flattenBlocks(item.statements) : [item]);

pkg/dev_compiler/test/codegen/expect/es6_modules.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
'use strict';
21
const exports = {};
3-
import dart from "dart/_runtime";
4-
import core from "dart/core";
2+
import dart from "./dart/_runtime";
3+
import core from "./dart/core";
54
let dartx = dart.dartx;
65
const Callback = dart.typedef('Callback', () => dart.functionType(dart.void, [], {i: core.int}));
76
class A extends core.Object {}

0 commit comments

Comments
 (0)