Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2586)

Unified Diff: sdk/lib/_internal/pub/lib/src/barback/dart2js_transformer.dart

Issue 24886002: Support dart2js in pub serve. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/pub/lib/src/barback/dart2js_transformer.dart
diff --git a/sdk/lib/_internal/pub/lib/src/barback/dart2js_transformer.dart b/sdk/lib/_internal/pub/lib/src/barback/dart2js_transformer.dart
new file mode 100644
index 0000000000000000000000000000000000000000..a8fdbfec0c5672fa264e55726f278d70abb5af68
--- /dev/null
+++ b/sdk/lib/_internal/pub/lib/src/barback/dart2js_transformer.dart
@@ -0,0 +1,132 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library pub.dart2js_transformer;
+
+import 'dart:async';
+import 'dart:io';
+
+import 'package:barback/barback.dart';
+import 'package:path/path.dart' as path;
+
+import '../../../../compiler/implementation/source_file_provider.dart'
+ show SourceFileProvider;
+import '../../../../compiler/implementation/source_file.dart';
+import '../barback.dart';
+import '../dart.dart' as dart;
+import '../io.dart';
+import '../package.dart';
+import '../package_graph.dart';
+
+class Dart2JSTransformer extends Transformer {
nweiz 2013/09/27 22:21:17 This deserves at least a short docstring.
Bob Nystrom 2013/09/28 00:56:11 Oops. Done.
+ final PackageGraph _graph;
+
+ Dart2JSTransformer(this._graph);
+
+ /// Only ".dart" files within "web/" are processed.
+ Future<bool> isPrimary(Asset asset) {
+ return new Future.value(
+ asset.id.extension == ".dart" &&
+ asset.id.path.startsWith("web/") &&
+ !asset.id.path.contains("/packages/"));
nweiz 2013/09/27 22:21:17 Pub's barback integration should already ensure th
Bob Nystrom 2013/09/28 00:56:11 It wasn't. Done now.
+ }
+
+ Future apply(Transform transform) {
+ var stopwatch = new Stopwatch();
+ stopwatch.start();
+
+ return transform.primaryInput.readAsString().then((code) {
+ var provider = new BarbackSourceFileProvider(_graph, transform);
+
+ // Create a "path" to the entrypoint script. The entrypoint may not
+ // actually be on disk, but this gives dart2js something to resolve
+ // relative paths to.
nweiz 2013/09/27 22:21:17 "gives dart2js a root to resolve relative paths ag
Bob Nystrom 2013/09/28 00:56:11 Dangling prepositions are perfectly grammatical! :
+ var id = transform.primaryInput.id;
+ var entrypoint = path.url.join(
+ path.toUri(_graph.packages[id.package].dir).path,
nweiz 2013/09/27 22:21:17 [id.package] will always be the entrypoint, right?
Bob Nystrom 2013/09/28 00:56:11 It will always be the entrypoint, but this is the
nweiz 2013/09/30 17:33:34 Right, I get that; what I'm saying is that [id.pac
Bob Nystrom 2013/10/01 19:08:55 Ah, I see what you're getting at. It is true that
+ id.path);
+
+ var packageRoot = path.join(_graph.entrypoint.root.dir, "packages");
+
+ return dart.compile(entrypoint,
+ packageRoot: packageRoot, provider: provider);
+ }).then((js) {
+ var id = transform.primaryInput.id.changeExtension(".dart.js");
+ transform.addOutput(new Asset.fromString(id, js));
+
+ stopwatch.stop();
+ transform.logger.info("Generated $id (${js.length} characters) in ${stopwatch.elapsed}");
nweiz 2013/09/27 22:21:17 Line length.
Bob Nystrom 2013/09/28 00:56:11 Done.
+ });
+ }
+}
+
+/// A [SourceFileProvider] that dart2js will use to load files that are
+/// produced by Barback.
+class BarbackSourceFileProvider implements SourceFileProvider {
nweiz 2013/09/27 22:21:17 Make this private.
Bob Nystrom 2013/09/28 00:56:11 Done.
+ final PackageGraph _graph;
+ final Transform _transform;
+
+ /// The map of previously loaded files.
+ final sourceFiles = new Map<String, SourceFile>();
nweiz 2013/09/27 22:21:17 Does dart2js automatically memoize using this fiel
Bob Nystrom 2013/09/28 00:56:11 It does. I really wish this class separated out th
+
+ BarbackSourceFileProvider(this._graph, this._transform);
+
+ Future<String> readStringFromUri(Uri resourceUri) {
nweiz 2013/09/27 22:21:17 It's unclear to me what [resourceUri] is going to
Bob Nystrom 2013/09/28 00:56:11 From what I've seen, it's always absolute. It look
+ var sourcePath = path.fromUri(resourceUri);
+
+ return _readResource(sourcePath).then((source) {
+ sourceFiles[resourceUri.toString()] =
+ new SourceFile(path.relative(sourcePath), source);
+ return source;
+ });
+ }
+
+ Future<String> call(Uri resourceUri) => readStringFromUri(resourceUri);
nweiz 2013/09/27 22:21:17 What's the difference between [call] and [readStri
Bob Nystrom 2013/09/28 00:56:11 No idea. Dart2js has this in its interface, so I'm
+
+ Future<String> _readResource(String sourcePath) {
+ // See if the path is within a package. If so, use Barback so we can use
+ // generated Dart assets.
+ var id = _sourcePathToId(sourcePath);
+ if (id != null) return _transform.readInputAsString(id);
+
+ // If we get here, the path doesn't appear to be in a package, so we'll
+ // skip Barback and just hit the file system. This will occur at the very
+ // least for dart2js's implementations of the core libraries.
+ return new File(sourcePath).readAsString();
+ }
+
+ AssetId _sourcePathToId(String sourcePath) {
+ // See if it's a special path with "packages" or "assets" in it.
+ var id = specialPathToId(path.split(sourcePath));
+ if (id != null) return id;
+
+ // See if it's a local path within some known package. This can occur, for
+ // example, with a path dependency. In that case, the path we get from
nweiz 2013/09/27 22:21:17 I don't understand why this would happen with a pa
Bob Nystrom 2013/09/28 00:56:11 I thought I ran into a case while manually testing
+ // dart2js will be the actual path to the dependency's folder and not a
+ // path containing "packages".
+ for (var name in _graph.packages.keys) {
+ var package = _graph.packages[name];
+ if (isBeneath(sourcePath, package.dir)) {
+ var relative = path.relative(sourcePath, from: package.dir);
+ return new AssetId(name, relative);
+ }
+ }
+
+ return null;
+ }
+
+ // TODO(rnystrom): These are in the public SourceFileProvider interface, but
+ // aren't actually used by dart2js. Ideally, these would be taken out of
+ // SourceFileProvider. Until then, just shut up the warnings.
nweiz 2013/09/27 22:21:17 File an issue for this and reference the number he
Bob Nystrom 2013/09/28 00:56:11 Done.
+ bool get isWindows => _notSupported();
+ set isWindows(value) => _notSupported();
+ Uri get cwd => _notSupported();
+ set cwd(value) => _notSupported();
+ int get dartCharactersRead => _notSupported();
+ set dartCharactersRead(value) => _notSupported();
+ set sourceFiles(value) => _notSupported();
+
+ _notSupported() => throw new UnsupportedError(
+ "This should be private in SourceFileProvider.");
+}

Powered by Google App Engine
This is Rietveld 408576698