style(dart/transform): Do not format generated code by default

Formatting code requires time and memory during the build -- do not do
it unless explicitly requested via a parameter to the transformer.

Add an entry to the
[wiki](https://github.com/angular/angular/wiki/Angular-2-Dart-Transformer)
describing the added parameter.
This commit is contained in:
Tim Blasi 2015-08-06 14:14:27 -07:00
parent 3db0ae1dac
commit 40a3cd2ab1
5 changed files with 45 additions and 17 deletions

View File

@ -2,18 +2,31 @@ library angular2.transform.common.formatter;
import 'package:dart_style/dart_style.dart'; import 'package:dart_style/dart_style.dart';
import 'logging.dart'; AngularDartFormatter _formatter = null;
DartFormatter _formatter = null;
void init(DartFormatter formatter) { void init(DartFormatter formatter) {
_formatter = formatter; _formatter = new _RealFormatter(formatter);
} }
DartFormatter get formatter { AngularDartFormatter get formatter {
if (_formatter == null) { if (_formatter == null) {
logger.info('Formatter never initialized, using default formatter.'); _formatter = new _PassThroughFormatter();
_formatter = new DartFormatter();
} }
return _formatter; return _formatter;
} }
abstract class AngularDartFormatter {
String format(String source, {uri});
}
class _PassThroughFormatter implements AngularDartFormatter {
String format(String source, {uri}) => source;
}
class _RealFormatter implements AngularDartFormatter {
final DartFormatter _formatter;
_RealFormatter(this._formatter);
String format(source, {uri}) => _formatter.format(source, uri: uri);
}

View File

@ -8,6 +8,7 @@ const DEFAULT_OPTIMIZATION_PHASES = 5;
const CUSTOM_ANNOTATIONS_PARAM = 'custom_annotations'; const CUSTOM_ANNOTATIONS_PARAM = 'custom_annotations';
const ENTRY_POINT_PARAM = 'entry_points'; const ENTRY_POINT_PARAM = 'entry_points';
const FORMAT_CODE_PARAM = 'format_code';
const GENERATE_CHANGE_DETECTORS_PARAM = 'generate_change_detectors'; const GENERATE_CHANGE_DETECTORS_PARAM = 'generate_change_detectors';
const INIT_REFLECTOR_PARAM = 'init_reflector'; const INIT_REFLECTOR_PARAM = 'init_reflector';
const INLINE_VIEWS_PARAM = 'inline_views'; const INLINE_VIEWS_PARAM = 'inline_views';
@ -51,6 +52,11 @@ class TransformerOptions {
/// The "correct" number of phases varies with the structure of the app. /// The "correct" number of phases varies with the structure of the app.
final int optimizationPhases; final int optimizationPhases;
/// Whether to format generated code.
/// Code that is only modified will never be formatted because doing so may
/// invalidate the source maps generated by `dart2js` and/or other tools.
final bool formatCode;
TransformerOptions._internal( TransformerOptions._internal(
this.entryPoints, this.entryPoints,
this.reflectionEntryPoints, this.reflectionEntryPoints,
@ -59,8 +65,9 @@ class TransformerOptions {
this.initReflector, this.initReflector,
this.annotationMatcher, this.annotationMatcher,
this.optimizationPhases, this.optimizationPhases,
this.generateChangeDetectors, {this.generateChangeDetectors,
this.inlineViews); this.inlineViews,
this.formatCode});
factory TransformerOptions(List<String> entryPoints, factory TransformerOptions(List<String> entryPoints,
{List<String> reflectionEntryPoints, {List<String> reflectionEntryPoints,
@ -70,7 +77,8 @@ class TransformerOptions {
List<ClassDescriptor> customAnnotationDescriptors: const [], List<ClassDescriptor> customAnnotationDescriptors: const [],
int optimizationPhases: DEFAULT_OPTIMIZATION_PHASES, int optimizationPhases: DEFAULT_OPTIMIZATION_PHASES,
bool inlineViews: true, bool inlineViews: true,
bool generateChangeDetectors: true}) { bool generateChangeDetectors: true,
bool formatCode: false}) {
if (reflectionEntryPoints == null || reflectionEntryPoints.isEmpty) { if (reflectionEntryPoints == null || reflectionEntryPoints.isEmpty) {
reflectionEntryPoints = entryPoints; reflectionEntryPoints = entryPoints;
} }
@ -85,7 +93,8 @@ class TransformerOptions {
initReflector, initReflector,
annotationMatcher, annotationMatcher,
optimizationPhases, optimizationPhases,
generateChangeDetectors, generateChangeDetectors: generateChangeDetectors,
inlineViews); inlineViews: inlineViews,
formatCode: formatCode);
} }
} }

View File

@ -15,6 +15,7 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) {
var inlineViews = _readBool(config, INLINE_VIEWS_PARAM, defaultValue: true); var inlineViews = _readBool(config, INLINE_VIEWS_PARAM, defaultValue: true);
var generateChangeDetectors = var generateChangeDetectors =
_readBool(config, GENERATE_CHANGE_DETECTORS_PARAM, defaultValue: true); _readBool(config, GENERATE_CHANGE_DETECTORS_PARAM, defaultValue: true);
var formatCode = _readBool(config, FORMAT_CODE_PARAM, defaultValue: false);
String mirrorModeVal = String mirrorModeVal =
config.containsKey(MIRROR_MODE_PARAM) ? config[MIRROR_MODE_PARAM] : ''; config.containsKey(MIRROR_MODE_PARAM) ? config[MIRROR_MODE_PARAM] : '';
var mirrorMode = MirrorMode.none; var mirrorMode = MirrorMode.none;
@ -39,7 +40,8 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) {
inlineViews: inlineViews, inlineViews: inlineViews,
customAnnotationDescriptors: _readCustomAnnotations(config), customAnnotationDescriptors: _readCustomAnnotations(config),
optimizationPhases: optimizationPhases, optimizationPhases: optimizationPhases,
generateChangeDetectors: generateChangeDetectors); generateChangeDetectors: generateChangeDetectors,
formatCode: formatCode);
} }
bool _readBool(Map config, String paramName, {bool defaultValue}) { bool _readBool(Map config, String paramName, {bool defaultValue}) {

View File

@ -18,8 +18,10 @@ export 'common/options.dart';
/// Replaces Angular 2 mirror use with generated code. /// Replaces Angular 2 mirror use with generated code.
class AngularTransformerGroup extends TransformerGroup { class AngularTransformerGroup extends TransformerGroup {
AngularTransformerGroup._(phases) : super(phases) { AngularTransformerGroup._(phases, {bool formatCode: false}) : super(phases) {
formatter.init(new DartFormatter()); if (formatCode) {
formatter.init(new DartFormatter());
}
} }
factory AngularTransformerGroup(TransformerOptions options) { factory AngularTransformerGroup(TransformerOptions options) {
@ -38,7 +40,8 @@ class AngularTransformerGroup extends TransformerGroup {
[new BindGenerator(options)], [new BindGenerator(options)],
[new TemplateCompiler(options)] [new TemplateCompiler(options)]
]); ]);
return new AngularTransformerGroup._(phases); return new AngularTransformerGroup._(phases,
formatCode: options.formatCode);
} }
factory AngularTransformerGroup.asPlugin(BarbackSettings settings) { factory AngularTransformerGroup.asPlugin(BarbackSettings settings) {

View File

@ -14,7 +14,8 @@ main() {
var formatter = new DartFormatter(); var formatter = new DartFormatter();
var transform = new AngularTransformerGroup(new TransformerOptions( var transform = new AngularTransformerGroup(new TransformerOptions(
['web/index.dart'], ['web/index.dart'],
reflectionEntryPoints: ['web/index.dart'])); reflectionEntryPoints: ['web/index.dart'],
formatCode: true));
class IntegrationTestConfig { class IntegrationTestConfig {
final String name; final String name;