Index: sdk/lib/_internal/compiler/implementation/dart_backend/backend.dart |
diff --git a/sdk/lib/_internal/compiler/implementation/dart_backend/backend.dart b/sdk/lib/_internal/compiler/implementation/dart_backend/backend.dart |
index b4c2001b31b6478030e1b3dc516b09feddf8b25e..4ae02a0623da974312402a111a70adbf31b87c67 100644 |
--- a/sdk/lib/_internal/compiler/implementation/dart_backend/backend.dart |
+++ b/sdk/lib/_internal/compiler/implementation/dart_backend/backend.dart |
@@ -25,6 +25,9 @@ class DartBackend extends Backend { |
final bool outputAst = false; |
final Map<ClassNode, List<Node>> memberNodes; |
+ /// If `true`, libraries are generated into separate files. |
+ final bool multiFile; |
+ |
PlaceholderRenamer placeholderRenamer; |
// TODO(zarah) Maybe change this to a command-line option. |
@@ -102,7 +105,7 @@ class DartBackend extends Backend { |
return true; |
} |
- DartBackend(Compiler compiler, List<String> strips) |
+ DartBackend(Compiler compiler, List<String> strips, {this.multiFile}) |
: tasks = <CompilerTask>[], |
memberNodes = new Map<ClassNode, List<Node>>(), |
forceStripTypes = strips.indexOf('types') != -1, |
@@ -264,6 +267,9 @@ class DartBackend extends Backend { |
} |
} |
+ List<LibraryElement> userLibraries = |
+ compiler.libraryLoader.libraries.where(isUserLibrary).toList(); |
+ |
Set<Element> topLevelElements = new Set<Element>(); |
Map<ClassElement, Set<Element>> classMembers = |
new Map<ClassElement, Set<Element>>(); |
@@ -452,18 +458,76 @@ class DartBackend extends Backend { |
topLevelNodes, collector); |
} |
- final EmitterUnparser unparser = |
- new EmitterUnparser(placeholderRenamer.renames, |
- stripTypes: forceStripTypes, |
- minify: compiler.enableMinification); |
- for (LibraryElement library in placeholderRenamer.platformImports) { |
- if (library.isPlatformLibrary && !library.isInternalLibrary) { |
- unparser.unparseImportTag(library.canonicalUri.toString()); |
+ Map<LibraryElement, String> outputPaths = new Map<LibraryElement, String>(); |
+ Map<LibraryElement, EmitterUnparser> unparsers = |
+ new Map<LibraryElement, EmitterUnparser>(); |
+ |
+ // The single unparser used if we collect all the output in one file |
+ EmitterUnparser mainUnparser = multiFile |
+ ? null |
+ : new EmitterUnparser(placeholderRenamer.renames, |
+ stripTypes: forceStripTypes, |
+ minify: compiler.enableMinification); |
+ |
+ if (multiFile) { |
+ String mainName = compiler.outputUri.pathSegments.last; |
+ String mainBaseName = mainName.endsWith(".dart") |
+ ? mainName.substring(0, mainName.length - 5) |
+ : mainName; |
+ // Map each library to a path based on the uri of the original |
+ // library and [compiler.outputUri]. |
+ Set<String> usedLibraryPaths = new Set<String>(); |
+ for (LibraryElement library in userLibraries) { |
+ if (library == compiler.mainApp) { |
+ outputPaths[library] = mainBaseName; |
+ } else { |
+ List<String> names = |
+ library.canonicalUri.pathSegments.last.split("."); |
+ if (names.last == "dart") { |
+ names = names.sublist(0, names.length - 1); |
+ } |
+ outputPaths[library] = |
+ "$mainBaseName.${makeUnique(names.join("."), usedLibraryPaths)}"; |
+ } |
+ } |
+ |
+ for(LibraryElement outputLibrary in userLibraries) { |
floitsch
2014/08/28 20:10:07
Add comment explaining that you rewrite the import
sigurdm
2014/09/03 08:24:16
Done.
|
+ EmitterUnparser unparser = new EmitterUnparser( |
+ placeholderRenamer.renames, |
+ stripTypes: forceStripTypes, |
+ minify: compiler.enableMinification); |
+ unparsers[outputLibrary] = unparser; |
+ LibraryName libraryName = outputLibrary.libraryTag; |
+ if (libraryName != null) { |
+ unparser.visitLibraryName(libraryName); |
+ } |
+ for (LibraryTag tag in outputLibrary.tags) { |
+ if (tag is! LibraryDependency) continue; |
+ LibraryDependency dependency = tag; |
+ LibraryElement libraryElement = |
+ outputLibrary.getLibraryFromTag(dependency); |
+ String uri = outputPaths.containsKey(libraryElement) |
+ ? "${outputPaths[libraryElement]}.dart" |
+ : libraryElement.canonicalUri.toString(); |
+ if (dependency is Import) { |
+ unparser.unparseImportTag(uri); |
+ } else { |
+ unparser.unparseExportTag(uri); |
+ } |
+ } |
+ } |
+ } else { |
+ for(LibraryElement library in placeholderRenamer.platformImports) { |
+ if (library.isPlatformLibrary && !library.isInternalLibrary) { |
+ mainUnparser.unparseImportTag(library.canonicalUri.toString()); |
+ } |
} |
} |
+ |
for (int i = 0; i < sortedTopLevels.length; i++) { |
Element element = sortedTopLevels[i]; |
Node node = topLevelNodes[i]; |
+ Unparser unparser = multiFile ? unparsers[element.library] : mainUnparser; |
if (node is ClassNode) { |
// TODO(smok): Filter out default constructors here. |
unparser.unparseClassWithBody(node, memberNodes[node]); |
@@ -473,13 +537,27 @@ class DartBackend extends Backend { |
unparser.newline(); |
} |
- compiler.assembledCode = unparser.result; |
- compiler.outputProvider("", "dart") |
- ..add(compiler.assembledCode) |
- ..close(); |
- // Output verbose info about size ratio of resulting bundle to all |
- // referenced non-platform sources. |
- logResultBundleSizeInfo(topLevelElements); |
+ if (multiFile) { |
+ for(LibraryElement outputLibrary in userLibraries) { |
+ compiler.outputProvider(outputPaths[outputLibrary], "dart") |
+ // TODO(sigurdm): Make the unparser output directly into the buffer instead |
floitsch
2014/08/28 20:10:06
Write comment before statement.
sigurdm
2014/09/03 08:24:16
Done.
|
+ // of caching in `.result`. |
+ ..add(unparsers[outputLibrary].result) |
+ ..close(); |
+ } |
+ // TODO(sigurdm): What to do here? Probably we should get rid of |
floitsch
2014/08/28 20:10:06
Agreed.
Until then we usually write the main unit
sigurdm
2014/09/03 08:24:16
Done.
|
+ // compiler.assembledCode |
+ compiler.assembledCode = ""; |
+ } else { |
+ compiler.assembledCode = mainUnparser.result; |
+ compiler.outputProvider("", "dart") |
+ ..add(compiler.assembledCode) |
+ ..close(); |
+ |
+ // Output verbose info about size ratio of resulting bundle to all |
+ // referenced non-platform sources. |
+ logResultBundleSizeInfo(topLevelElements); |
floitsch
2014/08/28 20:10:07
Can we do something similar?
sigurdm
2014/09/03 08:24:16
Done.
|
+ } |
} |
void logResultBundleSizeInfo(Set<Element> topLevelElements) { |