Skip to content

Commit

Permalink
Allow dart2js to compile with the CFE. (#1822)
Browse files Browse the repository at this point in the history
Adds support for using the common front-end in the dart2js transformer
  • Loading branch information
sigmundch committed Mar 13, 2018
1 parent 73ff0d3 commit d275574
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
16 changes: 12 additions & 4 deletions lib/src/barback/dart2js_transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:convert';

import 'package:analyzer/analyzer.dart';
import 'package:async/async.dart';
import 'package:barback/barback.dart';
import 'package:collection/collection.dart';
import 'package:path/path.dart' as p;
Expand Down Expand Up @@ -161,7 +162,8 @@ class Dart2JSTransformer extends Transformer implements LazyTransformer {
suppressPackageWarnings:
_configBool('suppressPackageWarnings', defaultsTo: true),
terse: _configBool('terse'),
includeSourceMapUrls: _generateSourceMaps);
includeSourceMapUrls: _generateSourceMaps,
platformBinaries: provider.libraryRoot.resolve('lib/_internal/').path);
}

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

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

Future<String> _readResource(Uri url) {
Future _readResource(Uri url) {
return new Future.sync(() {
// Find the corresponding asset in barback.
var id = _sourceUrlToId(url);
if (id != null) return _transform.readInputAsString(id);
if (id != null) {
if (id.extension == '.dill') {
return collectBytes(_transform.readInput(id));
} else {
return _transform.readInputAsString(id);
}
}

// Don't allow arbitrary file paths that point to things not in packages.
// Doing so won't work in Dartium.
Expand Down
11 changes: 7 additions & 4 deletions lib/src/barback/pub_package_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ class PubPackageProvider implements StaticPackageProvider {
}

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

return new Stream.fromIterable(
files.where((file) => p.extension(file) == ".dart").map((file) {
return new Stream.fromIterable(files
.where((file) =>
p.extension(file) == '.dart' || p.extension(file) == '.dill')
.map((file) {
var idPath = p.join("lib", "lib", p.relative(file, from: libPath));
return new AssetId('\$sdk', p.toUri(idPath).toString());
}));
Expand Down
6 changes: 5 additions & 1 deletion lib/src/dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ Future compile(String entrypoint, CompilerProvider provider,
bool suppressPackageWarnings: true,
bool terse: false,
bool includeSourceMapUrls: false,
bool toDart: false}) async {
bool toDart: false,
String platformBinaries}) async {
// dart2js chokes on relative paths. Including "/./" can also confuse it, so
// we normalize as well.
entrypoint = p.normalize(p.absolute(entrypoint));
Expand All @@ -85,6 +86,9 @@ Future compile(String entrypoint, CompilerProvider provider,
if (!suppressPackageWarnings) options.add('--show-package-warnings');
if (terse) options.add('--terse');
if (toDart) options.add('--output-type=dart');
if (platformBinaries != null) {
options.add('--platform-binaries=$platformBinaries');
}

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

0 comments on commit d275574

Please sign in to comment.