Skip to content

Commit ca80f2c

Browse files
committed
Add JavaScriptSourceNode to represent runtime libraries (fixes #85)
[email protected] Review URL: https://codereview.chromium.org/986723002
1 parent 0991bea commit ca80f2c

File tree

3 files changed

+82
-22
lines changed

3 files changed

+82
-22
lines changed

pkg/dev_compiler/lib/devc.dart

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class Compiler {
5151
List<LibraryInfo> _libraries = <LibraryInfo>[];
5252
final List<CodeGenerator> _generators;
5353
bool _failure = false;
54-
bool _devCompilerRuntimeCopied = false;
5554

5655
factory Compiler(CompilerOptions options,
5756
[TypeResolver resolver, CheckerReporter reporter]) {
@@ -99,6 +98,8 @@ class Compiler {
9998
_buildHtmlFile(node);
10099
} else if (node is DartSourceNode) {
101100
_buildDartLibrary(node);
101+
} else if (node is JavaScriptSourceNode) {
102+
_buildJavaScriptFile(node);
102103
} else {
103104
assert(false); // should not get a build request on PartSourceNode
104105
}
@@ -119,19 +120,19 @@ class Compiler {
119120
String outputFile = path.join(_options.outputDir, filename);
120121
new File(outputFile).writeAsStringSync(output);
121122

122-
if (_options.outputDart || _devCompilerRuntimeCopied) return;
123-
// Copy the dev_compiler runtime (implicit dependency for js codegen)
124-
// TODO(sigmund): split this out as a separate node in our dependency graph
125-
// (https://github.com/dart-lang/dev_compiler/issues/85).
126-
var runtimeDir = path.join(
127-
path.dirname(path.dirname(Platform.script.path)), 'lib/runtime/');
128-
var runtimeOutput = path.join(_options.outputDir, 'dev_compiler/runtime/');
129-
new Directory(runtimeOutput).createSync(recursive: true);
130-
new File(path.join(runtimeDir, 'harmony_feature_check.js'))
131-
.copy(path.join(runtimeOutput, 'harmony_feature_check.js'));
132-
new File(path.join(runtimeDir, 'dart_runtime.js'))
133-
.copy(path.join(runtimeOutput, 'dart_runtime.js'));
134-
_devCompilerRuntimeCopied = true;
123+
if (_options.outputDart) return;
124+
}
125+
126+
void _buildJavaScriptFile(JavaScriptSourceNode node) {
127+
// JavaScriptSourceNodes are runtime .js files that just need to be copied
128+
// over to the output location. These can be external dependencies or pieces
129+
// of the dev_compiler runtime.
130+
if (_options.outputDir == null || _options.outputDart) return;
131+
assert(node.uri.scheme == 'package');
132+
var filepath = path.join(_options.outputDir, node.uri.path);
133+
var dir = path.dirname(filepath);
134+
new Directory(dir).createSync(recursive: true);
135+
new File(filepath).writeAsStringSync(node.source.contents.data);
135136
}
136137

137138
bool _isEntry(DartSourceNode node) {

pkg/dev_compiler/lib/src/dependency_graph.dart

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ class SourceGraph {
5050
var source = _context.sourceFactory.forUri(uriString);
5151
var extension = path.extension(uriString);
5252
if (extension == '.html') {
53-
return new HtmlSourceNode(uri, source);
53+
return new HtmlSourceNode(uri, source, this);
5454
} else if (extension == '.dart' || uriString.startsWith('dart:')) {
5555
return new DartSourceNode(uri, source);
56+
} else if (extension == '.js') {
57+
return new JavaScriptSourceNode(uri, source);
5658
} else {
5759
assert(false); // unreachable
5860
}
@@ -81,12 +83,12 @@ abstract class SourceNode {
8183

8284
/// Direct dependencies in the [SourceGraph]. These include script tags for
8385
/// [HtmlSourceNode]s; and imports, exports and parts for [DartSourceNode]s.
84-
Iterable<SourceNode> get allDeps;
86+
Iterable<SourceNode> get allDeps => const [];
8587

8688
/// Like [allDeps] but excludes parts for [DartSourceNode]s. For many
8789
/// operations we mainly care about dependencies at the library level, so
8890
/// parts are excluded from this list.
89-
Iterable<SourceNode> get depsWithoutParts;
91+
Iterable<SourceNode> get depsWithoutParts => const [];
9092

9193
SourceNode(this.uri, this.source);
9294

@@ -108,19 +110,27 @@ abstract class SourceNode {
108110

109111
/// A node representing an entry HTML source file.
110112
class HtmlSourceNode extends SourceNode {
113+
/// Javascript dependencies, included by default on any application.
114+
final runtimeDeps = new Set<JavaScriptSourceNode>();
115+
111116
/// Libraries referred to via script tags.
112117
Set<DartSourceNode> scripts = new Set<DartSourceNode>();
113118

114119
@override
115-
Iterable<SourceNode> get allDeps => scripts;
120+
Iterable<SourceNode> get allDeps => [scripts, runtimeDeps].expand((e) => e);
116121

117122
@override
118-
Iterable<SourceNode> get depsWithoutParts => scripts;
123+
Iterable<SourceNode> get depsWithoutParts => allDeps;
119124

120125
/// Parsed document, updated whenever [update] is invoked.
121126
Document document;
122127

123-
HtmlSourceNode(uri, source) : super(uri, source);
128+
HtmlSourceNode(uri, source, graph) : super(uri, source) {
129+
var prefix = 'package:dev_compiler/runtime';
130+
runtimeDeps
131+
..add(graph.nodeFromUri(Uri.parse('$prefix/dart_runtime.js')))
132+
..add(graph.nodeFromUri(Uri.parse('$prefix/harmony_feature_check.js')));
133+
}
124134

125135
void update(SourceGraph graph) {
126136
super.update(graph);
@@ -257,6 +267,12 @@ class DartSourceNode extends SourceNode {
257267
}
258268
}
259269

270+
/// Represents a Javascript runtime resource from our compiler that is needed to
271+
/// run an application.
272+
class JavaScriptSourceNode extends SourceNode {
273+
JavaScriptSourceNode(uri, source) : super(uri, source);
274+
}
275+
260276
/// Updates the structure and `needsRebuild` marks in nodes of [graph] reachable
261277
/// from [start].
262278
///
@@ -320,6 +336,7 @@ rebuild(SourceNode start, SourceGraph graph, bool build(SourceNode node)) {
320336
bool shouldBuildNode(SourceNode n) {
321337
if (n.needsRebuild) return true;
322338
if (n is HtmlSourceNode) return structureHasChanged;
339+
if (n is JavaScriptSourceNode) return false;
323340
return (n as DartSourceNode).imports
324341
.any((i) => apiChangeDetected.contains(i));
325342
}

pkg/dev_compiler/test/dependency_graph_test.dart

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,11 @@ main() {
550550
group('refresh structure and marks', () {
551551
test('initial marks', () {
552552
var node = nodeOf('/index3.html');
553-
expectGraph(node, 'index3.html');
553+
expectGraph(node, '''
554+
index3.html
555+
|-- dart_runtime.js
556+
|-- harmony_feature_check.js
557+
''');
554558
refreshStructureAndMarks(node, graph);
555559
expectGraph(node, '''
556560
index3.html [needs-rebuild] [structure-changed]
@@ -560,6 +564,8 @@ main() {
560564
| | |-- a10.dart [needs-rebuild]
561565
| |-- a5.dart [needs-rebuild]
562566
| |-- a6.dart (part) [needs-rebuild]
567+
|-- dart_runtime.js [needs-rebuild]
568+
|-- harmony_feature_check.js [needs-rebuild]
563569
''');
564570
});
565571

@@ -574,6 +580,8 @@ main() {
574580
| | |-- a10.dart [needs-rebuild]
575581
| |-- a5.dart [needs-rebuild]
576582
| |-- a6.dart (part) [needs-rebuild]
583+
|-- dart_runtime.js [needs-rebuild]
584+
|-- harmony_feature_check.js [needs-rebuild]
577585
''');
578586
clearMarks(node);
579587
expectGraph(node, '''
@@ -584,6 +592,8 @@ main() {
584592
| | |-- a10.dart
585593
| |-- a5.dart
586594
| |-- a6.dart (part)
595+
|-- dart_runtime.js
596+
|-- harmony_feature_check.js
587597
''');
588598

589599
refreshStructureAndMarks(node, graph);
@@ -595,6 +605,8 @@ main() {
595605
| | |-- a10.dart
596606
| |-- a5.dart
597607
| |-- a6.dart (part)
608+
|-- dart_runtime.js
609+
|-- harmony_feature_check.js
598610
''');
599611
});
600612

@@ -614,6 +626,8 @@ main() {
614626
| | |-- a10.dart
615627
| |-- a5.dart
616628
| |-- a6.dart (part)
629+
|-- dart_runtime.js
630+
|-- harmony_feature_check.js
617631
''');
618632
});
619633

@@ -636,6 +650,8 @@ main() {
636650
| | |-- a8.dart [needs-rebuild] [structure-changed]
637651
| | | |-- a8.dart...
638652
| |-- a6.dart (part)
653+
|-- dart_runtime.js
654+
|-- harmony_feature_check.js
639655
''');
640656
});
641657
});
@@ -668,7 +684,9 @@ main() {
668684
'a4.dart',
669685
'a5.dart',
670686
'a2.dart',
671-
'index3.html'
687+
'dart_runtime.js',
688+
'harmony_feature_check.js',
689+
'index3.html',
672690
]);
673691

674692
// Marks are removed automatically by rebuild
@@ -680,6 +698,8 @@ main() {
680698
| | |-- a10.dart
681699
| |-- a5.dart
682700
| |-- a6.dart (part)
701+
|-- dart_runtime.js
702+
|-- harmony_feature_check.js
683703
''');
684704
});
685705

@@ -830,6 +850,8 @@ main() {
830850
| | |-- a10.dart
831851
| |-- a5.dart
832852
| |-- a6.dart (part)
853+
|-- dart_runtime.js
854+
|-- harmony_feature_check.js
833855
''');
834856

835857
// Modify the file first:
@@ -850,6 +872,8 @@ main() {
850872
| | |-- a10.dart
851873
| |-- a5.dart
852874
| |-- a6.dart (part)
875+
|-- dart_runtime.js
876+
|-- harmony_feature_check.js
853877
''');
854878

855879
a2.source.contents.modificationTime++;
@@ -880,6 +904,8 @@ main() {
880904
| |-- a6.dart
881905
| | |-- a5.dart
882906
| |-- a5.dart...
907+
|-- dart_runtime.js
908+
|-- harmony_feature_check.js
883909
''');
884910
});
885911

@@ -897,6 +923,8 @@ main() {
897923
| | |-- a10.dart
898924
| |-- a5.dart
899925
| |-- a6.dart (part)
926+
|-- dart_runtime.js
927+
|-- harmony_feature_check.js
900928
''');
901929

902930
a2.source.contents.modificationTime++;
@@ -918,6 +946,8 @@ main() {
918946
| | |-- a10.dart
919947
| |-- a6.dart
920948
| |-- a5.dart
949+
|-- dart_runtime.js
950+
|-- harmony_feature_check.js
921951
''');
922952

923953
a6.source.contents.modificationTime++;
@@ -934,6 +964,8 @@ main() {
934964
| |-- a6.dart
935965
| | |-- a5.dart
936966
| |-- a5.dart...
967+
|-- dart_runtime.js
968+
|-- harmony_feature_check.js
937969
''');
938970
});
939971

@@ -951,6 +983,8 @@ main() {
951983
| | |-- a10.dart
952984
| |-- a5.dart
953985
| |-- a6.dart (part)
986+
|-- dart_runtime.js
987+
|-- harmony_feature_check.js
954988
''');
955989

956990
a2.source.contents.modificationTime++;
@@ -973,6 +1007,8 @@ main() {
9731007
| |-- a4.dart
9741008
| | |-- a10.dart
9751009
| |-- a5.dart
1010+
|-- dart_runtime.js
1011+
|-- harmony_feature_check.js
9761012
''');
9771013
});
9781014

@@ -990,6 +1026,8 @@ main() {
9901026
| | |-- a10.dart
9911027
| |-- a5.dart
9921028
| |-- a6.dart (part)
1029+
|-- dart_runtime.js
1030+
|-- harmony_feature_check.js
9931031
''');
9941032

9951033
a2.source.contents.modificationTime++;
@@ -1011,6 +1049,8 @@ main() {
10111049
| | |-- a10.dart
10121050
| |-- a5.dart (part)
10131051
| |-- a6.dart (part)
1052+
|-- dart_runtime.js
1053+
|-- harmony_feature_check.js
10141054
''');
10151055

10161056
a5.source.contents.modificationTime++;
@@ -1026,6 +1066,8 @@ main() {
10261066
| | |-- a10.dart
10271067
| |-- a5.dart (part)
10281068
| |-- a6.dart (part)
1069+
|-- dart_runtime.js
1070+
|-- harmony_feature_check.js
10291071
''');
10301072
});
10311073
});

0 commit comments

Comments
 (0)