Skip to content

Commit 8c36aa8

Browse files
Tim Blasikegluneq
authored andcommitted
feat(dart/transform): Generate all code into <file>.template.dart
Previously, we generated the code to initialize the reflector into the <file>.ng_deps.dart and the compiled template and change detector code into <file>.template.dart. Update the transformer to generate all code into <file>.template.dart to avoid the additional HTTP requests necessary when debugging applications in Dartium.
1 parent ed2dbf2 commit 8c36aa8

21 files changed

Lines changed: 141 additions & 108 deletions

modules_dart/transform/lib/src/transform/common/code/annotation_code.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class AnnotationVisitor extends SimpleAstVisitor<AnnotationModel> {
5858
}
5959

6060
/// Defines the format in which an [AnnotationModel] is expressed as Dart code
61-
/// in a `.ng_deps.dart` file.
61+
/// when registered with the reflector.
6262
abstract class AnnotationWriterMixin {
6363
StringBuffer get buffer;
6464

modules_dart/transform/lib/src/transform/common/code/import_export_code.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ void _populateCombinators(NamespaceDirective node, dynamic model) {
7878
}
7979
}
8080

81-
/// Defines the format in which an [ImportModel] is expressed as Dart code in a
82-
/// `.ng_deps.dart` file.
81+
/// Defines the format in which an [ImportModel] is expressed as Dart code when
82+
/// registered with the reflector.
8383
abstract class ImportWriterMixin {
8484
StringBuffer get buffer;
8585

@@ -96,8 +96,8 @@ abstract class ImportWriterMixin {
9696
}
9797
}
9898

99-
/// Defines the format in which an [ExportModel] is expressed as Dart code in a
100-
/// `.ng_deps.dart` file.
99+
/// Defines the format in which an [ExportModel] is expressed as Dart code when
100+
/// registered with the reflector.
101101
abstract class ExportWriterMixin {
102102
StringBuffer get buffer;
103103

modules_dart/transform/lib/src/transform/common/code/ng_deps_code.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import 'reflection_info_code.dart';
1414
import 'parameter_code.dart';
1515
import 'queries_code.dart';
1616

17-
/// Visitor responsible for parsing source Dart files (that is, not
18-
/// `.ng_deps.dart` files) into [NgDepsModel] objects.
17+
/// Visitor responsible for parsing Dart source into [NgDepsModel] objects.
1918
class NgDepsVisitor extends RecursiveAstVisitor<Object> {
2019
final AssetId processedFile;
2120
final _importVisitor = new ImportVisitor();
@@ -113,7 +112,7 @@ class NgDepsVisitor extends RecursiveAstVisitor<Object> {
113112
}
114113

115114
/// Defines the format in which an [NgDepsModel] is expressed as Dart code
116-
/// in a `.ng_deps.dart` file.
115+
/// when registered with the reflector.
117116
class NgDepsWriter extends Object
118117
with
119118
AnnotationWriterMixin,
@@ -139,10 +138,10 @@ abstract class NgDepsWriterMixin
139138

140139
void writeNgDepsModel(NgDepsModel model) {
141140
if (model.libraryUri.isNotEmpty) {
142-
buffer.writeln('library ${model.libraryUri}${DEPS_EXTENSION};\n');
141+
buffer.writeln('library ${model.libraryUri}${TEMPLATE_EXTENSION};\n');
143142
}
144143

145-
// We need to import & export the source file.
144+
// We need to import & export (see below) the source file.
146145
writeImportModel(new ImportModel()..uri = model.sourceFile);
147146

148147
// Used to register reflective information.

modules_dart/transform/lib/src/transform/common/code/parameter_code.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class ParameterVisitor extends SimpleAstVisitor<ParameterModel> {
9595
}
9696

9797
/// Defines the format in which a [ParameterModel] is expressed as Dart code
98-
/// in a `.ng_deps.dart` file.
98+
/// when registered with the reflector.
9999
abstract class ParameterWriterMixin {
100100
StringBuffer get buffer;
101101

modules_dart/transform/lib/src/transform/common/code/reflection_info_code.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class _PropertyMetadataVisitor
284284
}
285285

286286
/// Defines the format in which an [ReflectionInfoModel] is expressed as Dart
287-
/// code in a `.ng_deps.dart` file.
287+
/// code when registered with the reflector.
288288
abstract class ReflectionWriterMixin
289289
implements AnnotationWriterMixin, ParameterWriterMixin {
290290
StringBuffer get buffer;

modules_dart/transform/lib/src/transform/common/code/source_module.dart

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
11
library angular2.transform.common.code.source_module;
22

3-
import 'package:analyzer/src/generated/scanner.dart' show Keyword;
43
import 'package:angular2/src/compiler/source_module.dart';
4+
import 'package:analyzer/src/generated/scanner.dart' show Keyword;
5+
import 'package:angular2/src/transform/common/model/ng_deps_model.pb.dart';
6+
import 'package:angular2/src/transform/common/model/source_module.dart';
57

6-
import 'uri.dart';
8+
import 'ng_deps_code.dart';
79

810
/// Writes the full Dart code for the provided [SourceModule].
911
String writeSourceModule(SourceModule sourceModule, {String libraryName}) {
1012
if (sourceModule == null) return null;
1113
var buf = new StringBuffer();
14+
final writer = new NgDepsWriter(buf);
1215
var sourceWithImports = sourceModule.getSourceWithImports();
1316
libraryName = _sanitizeLibName(
1417
libraryName != null ? libraryName : sourceModule.moduleUrl);
1518
buf..writeln('library $libraryName;')..writeln();
16-
sourceWithImports.imports.forEach((import) {
17-
// Format for importLine := [uri, prefix]
18-
if (import.length != 2) {
19-
throw new FormatException(
20-
'Unexpected import format! '
21-
'Angular 2 compiler returned imports in an unexpected format. '
22-
'Expected [<import_uri>, <prefix>].',
23-
import.join(', '));
24-
}
25-
buf.writeln(writeImportUri(import[0],
26-
prefix: import[1], fromAbsolute: sourceModule.moduleUrl));
19+
20+
extractImports(sourceWithImports, sourceModule.moduleUrl).forEach((import) {
21+
writer.writeImportModel(import);
2722
});
2823
buf..writeln()..writeln(sourceWithImports.source);
2924

3025
return buf.toString();
3126
}
3227

28+
/// Uses `writer` to write a Dart library representing `model` and
29+
/// `sourceModule`.
30+
void writeTemplateFile(
31+
NgDepsWriterMixin writer, NgDepsModel model, SourceModule sourceModule) {
32+
if (model == null) return null;
33+
var sourceModuleCode = '';
34+
if (sourceModule != null) {
35+
var sourceWithImports = sourceModule.getSourceWithImports();
36+
sourceModuleCode = sourceWithImports.source;
37+
38+
// Since we modify `imports`, make a copy to avoid changing the provided
39+
// value.
40+
var sourceModuleImports =
41+
extractImports(sourceWithImports, sourceModule.moduleUrl);
42+
model = model.clone();
43+
model.imports.addAll(sourceModuleImports);
44+
}
45+
writer.writeNgDepsModel(model);
46+
writer.buffer..writeln()..writeln(sourceModuleCode);
47+
}
48+
3349
final _unsafeCharsPattern = new RegExp(r'[^a-zA-Z0-9_\.]');
3450
String _sanitizeLibName(String moduleUrl) {
3551
var sanitized =

modules_dart/transform/lib/src/transform/common/model/ng_deps_model.pb.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules_dart/transform/lib/src/transform/common/model/ng_deps_model.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ message NgDepsModel {
3333
// framework.
3434
repeated string methods = 9;
3535

36-
// Imports for the .ng_deps.dart files associated with the declared `imports`
37-
// and `exports` for this file.
36+
// Imports of the generated files associated with the declared `imports`
37+
// and `exports` of the source file.
3838
repeated ImportModel dep_imports = 10;
3939
}

modules_dart/transform/lib/src/transform/common/code/uri.dart renamed to modules_dart/transform/lib/src/transform/common/model/source_module.dart

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
1-
library angular2.transform.common.code.uri;
1+
library angular2.transform.common.model.source_module;
22

3-
import 'package:angular2/src/transform/common/url_resolver.dart';
43
import 'package:path/path.dart' as path;
54

6-
/// Generates an `import` statement for the file specified by `importPath`.
5+
import 'package:angular2/src/compiler/source_module.dart';
6+
import 'package:angular2/src/transform/common/url_resolver.dart';
7+
8+
import 'import_export_model.pb.dart';
9+
10+
/// Generates [ImportModel]s for all imports in `sourceWithImports`.
11+
///
12+
/// Imports in `sourceWithImports` are resolved relative to `moduleUrl`.
13+
List<ImportModel> extractImports(
14+
SourceWithImports sourceWithImports, String moduleUrl) {
15+
if (sourceWithImports == null) return const <ImportModel>[];
16+
return sourceWithImports.imports.map((import) {
17+
// Format for importLine := [uri, prefix]
18+
if (import.length != 2) {
19+
throw new FormatException(
20+
'Internal Angular 2 compiler error. '
21+
'Angular 2 compiler returned imports in an unexpected format. '
22+
'Expected [<import_uri>, <prefix>].',
23+
import.join(', '));
24+
}
25+
return toImportModel(import[0], prefix: import[1], fromAbsolute: moduleUrl);
26+
}).toList();
27+
}
28+
29+
/// Generates an [ImportModel] for the file specified by `importPath`.
730
///
831
/// If `fromAbsolute` is specified, `importPath` may be a relative path,
932
/// otherwise it is expected to be absolute.
10-
String writeImportUri(String importPath, {String prefix, String fromAbsolute}) {
33+
ImportModel toImportModel(String importPath,
34+
{String prefix, String fromAbsolute}) {
1135
var urlResolver = const TransformerUrlResolver();
1236
var codegenImportPath;
1337

@@ -33,10 +57,12 @@ String writeImportUri(String importPath, {String prefix, String fromAbsolute}) {
3357
}
3458
}
3559

60+
final model = new ImportModel()..uri = codegenImportPath;
61+
3662
if (prefix != null && prefix.isNotEmpty) {
37-
prefix = ' as $prefix';
63+
model.prefix = prefix;
3864
}
39-
return 'import \'$codegenImportPath\'$prefix;';
65+
return model;
4066
}
4167

4268
// For a relative import, the scheme, first (package) and second (lib|test|web)

modules_dart/transform/lib/src/transform/common/names.dart

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const TRANSFORM_DYNAMIC_MODE = 'transform_dynamic';
88
const CSS_EXTENSION = '.css';
99
const SHIMMED_STYLESHEET_EXTENSION = '.css.shim.dart';
1010
const NON_SHIMMED_STYLESHEET_EXTENSION = '.css.dart';
11-
const DEPS_EXTENSION = '.ng_deps.dart';
1211
const META_EXTENSION = '.ng_meta.json';
1312
const REFLECTION_CAPABILITIES_NAME = 'ReflectionCapabilities';
1413
const REFLECTOR_IMPORT = 'package:angular2/src/core/reflection/reflection.dart';
@@ -24,7 +23,6 @@ const TEMPLATE_EXTENSION = '.template.dart';
2423
/// important. For example, putting '.dart' first in this list will cause
2524
/// incorrect behavior.
2625
const ALL_EXTENSIONS = const [
27-
DEPS_EXTENSION,
2826
META_EXTENSION,
2927
SUMMARY_META_EXTENSION,
3028
TEMPLATE_EXTENSION,
@@ -38,7 +36,6 @@ const ALL_EXTENSIONS = const [
3836
/// any files named like transformer outputs will be reported as generated.
3937
bool isGenerated(String uri) {
4038
return const [
41-
DEPS_EXTENSION,
4239
META_EXTENSION,
4340
NON_SHIMMED_STYLESHEET_EXTENSION,
4441
SHIMMED_STYLESHEET_EXTENSION,
@@ -51,10 +48,6 @@ bool isGenerated(String uri) {
5148
String toMetaExtension(String uri) =>
5249
_toExtension(uri, ALL_EXTENSIONS, META_EXTENSION);
5350

54-
/// Returns `uri` with its extension updated to [DEPS_EXTENSION].
55-
String toDepsExtension(String uri) =>
56-
_toExtension(uri, ALL_EXTENSIONS, DEPS_EXTENSION);
57-
5851
/// Returns `uri` with its extension updated to [TEMPLATES_EXTENSION].
5952
String toTemplateExtension(String uri) =>
6053
_toExtension(uri, ALL_EXTENSIONS, TEMPLATE_EXTENSION);

0 commit comments

Comments
 (0)