Skip to content

Commit d275574

Browse files
authored
Allow dart2js to compile with the CFE. (#1822)
Adds support for using the common front-end in the dart2js transformer
1 parent 73ff0d3 commit d275574

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

lib/src/barback/dart2js_transformer.dart

+12-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:async';
66
import 'dart:convert';
77

88
import 'package:analyzer/analyzer.dart';
9+
import 'package:async/async.dart';
910
import 'package:barback/barback.dart';
1011
import 'package:collection/collection.dart';
1112
import 'package:path/path.dart' as p;
@@ -161,7 +162,8 @@ class Dart2JSTransformer extends Transformer implements LazyTransformer {
161162
suppressPackageWarnings:
162163
_configBool('suppressPackageWarnings', defaultsTo: true),
163164
terse: _configBool('terse'),
164-
includeSourceMapUrls: _generateSourceMaps);
165+
includeSourceMapUrls: _generateSourceMaps,
166+
platformBinaries: provider.libraryRoot.resolve('lib/_internal/').path);
165167
}
166168

167169
/// Parses and returns the "commandLineOptions" configuration option.
@@ -280,7 +282,7 @@ class _BarbackCompilerProvider implements dart.CompilerProvider {
280282
}
281283

282284
/// A [CompilerInputProvider] for dart2js.
283-
Future<String> provideInput(Uri resourceUri) {
285+
Future /* <String | List<int>> */ provideInput(Uri resourceUri) {
284286
// We only expect to get absolute "file:" URLs from dart2js.
285287
assert(resourceUri.isAbsolute);
286288
assert(resourceUri.scheme == "file");
@@ -391,11 +393,17 @@ class _BarbackCompilerProvider implements dart.CompilerProvider {
391393
}
392394
}
393395

394-
Future<String> _readResource(Uri url) {
396+
Future _readResource(Uri url) {
395397
return new Future.sync(() {
396398
// Find the corresponding asset in barback.
397399
var id = _sourceUrlToId(url);
398-
if (id != null) return _transform.readInputAsString(id);
400+
if (id != null) {
401+
if (id.extension == '.dill') {
402+
return collectBytes(_transform.readInput(id));
403+
} else {
404+
return _transform.readInputAsString(id);
405+
}
406+
}
399407

400408
// Don't allow arbitrary file paths that point to things not in packages.
401409
// Doing so won't work in Dartium.

lib/src/barback/pub_package_provider.dart

+7-4
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ class PubPackageProvider implements StaticPackageProvider {
7575
}
7676

7777
// "$sdk" is a pseudo-package that provides access to the Dart library
78-
// sources in the SDK. The dart2js transformer uses this to locate the Dart
79-
// sources for "dart:" libraries.
78+
// sources and kernel .dill files in the SDK. The dart2js transformer uses
79+
// this to locate the Dart sources for "dart:" libraries and the
80+
// dart2js_platform.dill files.
8081
if (id.package == r'$sdk') {
8182
// The asset path contains two "lib" entries. The first represents pub's
8283
// concept that all public assets are in "lib". The second comes from the
@@ -147,8 +148,10 @@ class PubPackageProvider implements StaticPackageProvider {
147148
files = files.map((file) => file.replaceAll(trailingUnderscore, ""));
148149
}
149150

150-
return new Stream.fromIterable(
151-
files.where((file) => p.extension(file) == ".dart").map((file) {
151+
return new Stream.fromIterable(files
152+
.where((file) =>
153+
p.extension(file) == '.dart' || p.extension(file) == '.dill')
154+
.map((file) {
152155
var idPath = p.join("lib", "lib", p.relative(file, from: libPath));
153156
return new AssetId('\$sdk', p.toUri(idPath).toString());
154157
}));

lib/src/dart.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ Future compile(String entrypoint, CompilerProvider provider,
6868
bool suppressPackageWarnings: true,
6969
bool terse: false,
7070
bool includeSourceMapUrls: false,
71-
bool toDart: false}) async {
71+
bool toDart: false,
72+
String platformBinaries}) async {
7273
// dart2js chokes on relative paths. Including "/./" can also confuse it, so
7374
// we normalize as well.
7475
entrypoint = p.normalize(p.absolute(entrypoint));
@@ -85,6 +86,9 @@ Future compile(String entrypoint, CompilerProvider provider,
8586
if (!suppressPackageWarnings) options.add('--show-package-warnings');
8687
if (terse) options.add('--terse');
8788
if (toDart) options.add('--output-type=dart');
89+
if (platformBinaries != null) {
90+
options.add('--platform-binaries=$platformBinaries');
91+
}
8892

8993
var sourceUrl = p.toUri(entrypoint);
9094
options.add("--out=$sourceUrl.js");

0 commit comments

Comments
 (0)