Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Edit pruneCss flag to allow printing pruned css instead of overwriting. #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Examples: see [example/angular1](https://github.com/google/dart-scissors/tree/ma
```

Valid settings:
- `pruneCss` (boolean): by default, `true` in `release` only
- `pruneCss` (`skip` or `print` or `overwrite`): by default, `overwrite` in `release` only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, can you deprecate the existing flag and add a new one? I found 1 existing usage of pruneCss with a bool value, this would break it.

Alternatively, we can keep it and accept true, false, warnOnly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Olivier.
Can you please point me to existing usage of pruneCss. Because AFAIS I have replaced all the usages. So this should work fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, can you please document each enum value in the readme with the same format as the other enums?

- `imageInlining`: default is `linkInlinedImages` in `debug`, `inlineInlinedImages` in `release`
- `inlineAllUrls`: treats `url` as `inline-image`
- `inlineInlinedImages`: simply honours `inline-image`
Expand Down
2 changes: 1 addition & 1 deletion example/angular1/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ transformers:
cssJanusPath: ${PWD}/../../.dependencies/cssjanus/cssjanus.py
release:
imageInlining: inlineAllUrls
# pruneCss: false
# pruningScheme: skip
# optimizeSvg: false
# optimizePng: false
compiledCssExtension: replace
Expand Down
2 changes: 1 addition & 1 deletion example/angular2/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ transformers:
# - scissors/eager_transformer
- scissors:
verbose: true
# pruneCss: false
# pruningScheme: skip
# optimizeSvg: false
# optimizePng: false
- angular2/transform/codegen
Expand Down
7 changes: 6 additions & 1 deletion lib/eager_transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ library scissors.scissors_transformer;
import 'package:barback/barback.dart';

import 'src/css_mirroring/transformer.dart';
import 'src/css_pruning/css_utils.dart' show PruningScheme;
import 'src/css_pruning/transformer.dart';
import 'src/image_inlining/transformer.dart';
import 'src/png_optimization/transformer.dart';
Expand Down Expand Up @@ -46,7 +47,11 @@ List<List<Transformer>> _createPhases(_ScissorsSettings settings) {
: null,
settings.compileSass.value ? new SassCTransformer(settings) : null
],
[settings.pruneCss.value ? new CssPruningTransformer(settings) : null],
[
settings.pruningScheme.value != PruningScheme.skip
? new CssPruningTransformer(settings)
: null
],
[
settings.imageInlining.value != ImageInliningMode.disablePass
? new ImageInliningTransformer(settings)
Expand Down
17 changes: 10 additions & 7 deletions lib/src/css_pruning/css_pruning.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import 'rule_set_index.dart';
import 'template_extractor.dart' show extractTemplates;
import 'usage_collector.dart';
import 'transformer.dart' show CssPruningSettings;
import 'css_utils.dart' show PruningScheme;

Future<String> findHtmlTemplate(Transform transform, AssetId cssAssetId) async {
try {
Expand Down Expand Up @@ -69,7 +70,6 @@ dropUnusedCssRules(
SourceFile cssSourceFile,
String htmlTemplate) {
hacks.useCssLib();

final StyleSheet cssTree =
new css_parser.Parser(cssSourceFile, transaction.original).parse();
final List<dom.Node> htmlTrees =
Expand All @@ -89,15 +89,18 @@ dropUnusedCssRules(

final fileLength = transaction.file.length;
topLevelsToDropWithIndex.forEach((TreeNode topLevel, int i) {
if (settings.verbose.value) {
if (settings.verbose.value ||
settings.pruningScheme.value == PruningScheme.print) {
transform.logger.info("Dropping unused CSS rule: "
"${_printCss(new StyleSheet([topLevel], null))}");
}
final start = topLevel.span.start.offset;
final end = i == topLevels.length - 1
? fileLength
: topLevels[i + 1].span.start.offset;
transaction.edit(start, end, '');
if (settings.pruningScheme.value == PruningScheme.overwrite) {
final start = topLevel.span.start.offset;
final end = i == topLevels.length - 1
? fileLength
: topLevels[i + 1].span.start.offset;
transaction.edit(start, end, '');
}
});
}

Expand Down
3 changes: 3 additions & 0 deletions lib/src/css_pruning/css_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library scissors.src.css_pruning.css_utils;

enum PruningScheme { skip, print, overwrite }
6 changes: 4 additions & 2 deletions lib/src/css_pruning/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ part of scissors.src.css_pruning.transformer;
abstract class CssPruningSettings {
Setting<bool> get verbose;

final pruneCss =
new Setting<bool>('pruneCss', debugDefault: false, releaseDefault: true);
final pruningScheme = new Setting<PruningScheme>('pruningScheme',
debugDefault: PruningScheme.skip,
releaseDefault: PruningScheme.overwrite,
parser: new EnumParser<PruningScheme>(PruningScheme.values).parse);

// final bidiCss = new Setting<bool>('bidiCss',
// comment:
Expand Down
5 changes: 4 additions & 1 deletion lib/src/css_pruning/transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import 'package:barback/barback.dart';
import 'package:source_maps/refactor.dart';
import 'package:source_span/source_span.dart';

import '../utils/enum_parser.dart';
import '../utils/path_utils.dart';
import '../utils/settings_base.dart';
import '../utils/file_skipping.dart';
import '../utils/delta_format.dart';
import 'css_pruning.dart';
import 'css_utils.dart';

part 'settings.dart';

Expand All @@ -39,7 +41,8 @@ class CssPruningTransformer extends Transformer
final String allowedExtensions = ".css .css.map";

@override
bool isPrimary(AssetId id) => _settings.pruneCss.value && super.isPrimary(id);
bool isPrimary(AssetId id) =>
_settings.pruningScheme != PruningScheme.skip && super.isPrimary(id);

@override
declareOutputs(DeclaringTransform transform) {
Expand Down