From d2755745e88b63448d0adfbf2d04af9050f45ed3 Mon Sep 17 00:00:00 2001 From: sigmundch Date: Mon, 12 Mar 2018 19:34:28 -0700 Subject: [PATCH] Allow dart2js to compile with the CFE. (#1822) Adds support for using the common front-end in the dart2js transformer --- lib/src/barback/dart2js_transformer.dart | 16 ++++++++++++---- lib/src/barback/pub_package_provider.dart | 11 +++++++---- lib/src/dart.dart | 6 +++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/src/barback/dart2js_transformer.dart b/lib/src/barback/dart2js_transformer.dart index 28c976a83..e9678ff1a 100644 --- a/lib/src/barback/dart2js_transformer.dart +++ b/lib/src/barback/dart2js_transformer.dart @@ -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; @@ -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. @@ -280,7 +282,7 @@ class _BarbackCompilerProvider implements dart.CompilerProvider { } /// A [CompilerInputProvider] for dart2js. - Future provideInput(Uri resourceUri) { + Future /* > */ provideInput(Uri resourceUri) { // We only expect to get absolute "file:" URLs from dart2js. assert(resourceUri.isAbsolute); assert(resourceUri.scheme == "file"); @@ -391,11 +393,17 @@ class _BarbackCompilerProvider implements dart.CompilerProvider { } } - Future _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. diff --git a/lib/src/barback/pub_package_provider.dart b/lib/src/barback/pub_package_provider.dart index 482bf05dd..66cbcb289 100644 --- a/lib/src/barback/pub_package_provider.dart +++ b/lib/src/barback/pub_package_provider.dart @@ -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 @@ -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()); })); diff --git a/lib/src/dart.dart b/lib/src/dart.dart index c5d236d0c..2dec254f8 100644 --- a/lib/src/dart.dart +++ b/lib/src/dart.dart @@ -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)); @@ -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");