Skip to content

Commit

Permalink
Reenable the common front-end in dart2js by creating a .packages file (
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmundch authored Mar 15, 2018
1 parent 5f329bc commit dbe8be2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
23 changes: 19 additions & 4 deletions lib/src/barback/dart2js_transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ class Dart2JSTransformer extends Transformer implements LazyTransformer {

var entrypoint = _environment.graph.packages[id.package].path(id.path);

// We define the packageRoot in terms of the entrypoint directory, and not
// the rootPackage, to ensure that the generated source-maps are valid.
// We define the .packages file in terms of the entrypoint directory, and
// not the rootPackage, to ensure that the generated source-maps are valid.
// Source-maps contain relative URLs to package sources and these relative
// URLs should be self-contained within the paths served by pub-serve.
// See #1511 for details.
var buildDir = _environment.getSourceDirectoryContaining(id.path);
var packageRoot = _environment.rootPackage.path(buildDir, "packages");
var packageConfig = _environment.rootPackage.path(buildDir, ".packages");

// TODO(rnystrom): Should have more sophisticated error-handling here. Need
// to report compile errors to the user in an easily visible way. Need to
Expand All @@ -154,7 +154,7 @@ class Dart2JSTransformer extends Transformer implements LazyTransformer {
defaultsTo: _settings.mode == BarbackMode.RELEASE),
verbose: _configBool('verbose'),
environment: _configEnvironment,
packageRoot: packageRoot,
packageConfig: packageConfig,
analyzeAll: _configBool('analyzeAll'),
preserveUris: _configBool('preserveUris'),
suppressWarnings: _configBool('suppressWarnings'),
Expand Down Expand Up @@ -223,6 +223,7 @@ class _BarbackCompilerProvider implements dart.CompilerProvider {
final AssetEnvironment _environment;
final Transform _transform;
String _libraryRootPath;
String _packagesFileContents;

/// The map of previously loaded files.
///
Expand Down Expand Up @@ -279,6 +280,18 @@ class _BarbackCompilerProvider implements dart.CompilerProvider {
.getSourceDirectoryContaining(_transform.primaryInput.id.path);
_libraryRootPath =
_environment.rootPackage.path(buildDir, "packages", r"$sdk");

// We also define the entries within the .packages file in terms of the
// entrypoint directory, and not the rootPackage, to ensure that the
// generated source-maps are valid.
// Source-maps contain relative URLs to package sources and these relative
// URLs should be self-contained within the paths served by pub-serve.
// See #1511 for details.
var sb = new StringBuffer();
for (var package in _environment.graph.packages.keys) {
sb.write('$package:packages/$package/\n');
}
_packagesFileContents = '$sb';
}

/// A [CompilerInputProvider] for dart2js.
Expand Down Expand Up @@ -400,6 +413,8 @@ class _BarbackCompilerProvider implements dart.CompilerProvider {
if (id != null) {
if (id.extension == '.dill') {
return collectBytes(_transform.readInput(id));
} else if (id.path.endsWith('/.packages')) {
return _packagesFileContents;
} else {
return _transform.readInputAsString(id);
}
Expand Down
19 changes: 9 additions & 10 deletions lib/src/dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'dart:isolate';
import 'package:analyzer/analyzer.dart';
import 'package:barback/barback.dart';
import 'package:compiler_unsupported/compiler.dart' as compiler;
import 'package:compiler_unsupported/src/filenames.dart' show appendSlash;
import 'package:path/path.dart' as p;

import 'exceptions.dart';
Expand Down Expand Up @@ -51,16 +50,16 @@ abstract class CompilerProvider {
/// Uses [provider] to communcate between dart2js and the caller. Returns a
/// future that completes when compilation is done.
///
/// By default, the package root is assumed to be adjacent to [entrypoint], but
/// if [packageRoot] is passed that will be used instead.
/// By default, the .packages file is assumed to be adjacent to [entrypoint],
/// but if [packageConfig] is passed that will be used instead.
Future compile(String entrypoint, CompilerProvider provider,
{Iterable<String> commandLineOptions,
bool checked: false,
bool csp: false,
bool minify: true,
bool verbose: false,
Map<String, String> environment,
String packageRoot,
String packageConfig,
bool analyzeAll: false,
bool preserveUris: false,
bool suppressWarnings: false,
Expand Down Expand Up @@ -89,7 +88,6 @@ Future compile(String entrypoint, CompilerProvider provider,
if (platformBinaries != null) {
options.add('--platform-binaries=$platformBinaries');
}
options.add('--use-old-frontend');

var sourceUrl = p.toUri(entrypoint);
options.add("--out=$sourceUrl.js");
Expand All @@ -102,21 +100,22 @@ Future compile(String entrypoint, CompilerProvider provider,
if (environment == null) environment = {};
if (commandLineOptions != null) options.addAll(commandLineOptions);

if (packageRoot == null) {
packageRoot = p.join(p.dirname(entrypoint), 'packages');
if (packageConfig == null) {
packageConfig = p.join(p.dirname(entrypoint), '.packages');
} else {
packageRoot = p.normalize(p.absolute(packageRoot));
packageConfig = p.normalize(p.absolute(packageConfig));
}

await compiler.compile(
p.toUri(entrypoint),
provider.libraryRoot,
p.toUri(appendSlash(packageRoot)),
null,
provider.provideInput,
provider.handleDiagnostic,
options,
provider.provideOutput,
environment);
environment,
p.toUri(packageConfig));
}

/// Returns whether [dart] looks like an entrypoint file.
Expand Down

0 comments on commit dbe8be2

Please sign in to comment.