Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * Code transform for pre-processing CSS files any extension of .scss is | |
| 7 * transformed to runable CSS styling (nesting, calc, var, @define, @extends, | |
| 8 * @mixin, etc). | |
| 9 */ | |
| 10 library csslib.transform; | |
| 11 | |
| 12 import 'dart:async'; | |
| 13 | |
| 14 import 'package:barback/barback.dart'; | |
| 15 import 'package:source_maps/span.dart' show SourceFile; | |
| 16 import 'package:csslib/parser.dart' show parse, analyze; | |
| 17 import 'package:csslib/visitor.dart' show StyleSheet, CssPrinter; | |
| 18 | |
| 19 class ScssTransformer extends Transformer { | |
| 20 Future<bool> isPrimary(Asset input) => | |
| 21 new Future.value((input.id.extension == '.scss')); | |
|
Siggi Cherem (dart-lang)
2013/09/08 20:31:33
replace the above two lines with:
/** Only run th
| |
| 22 | |
| 23 Future apply(Transform transform) { | |
| 24 return transform.primaryInput.readAsString().then((content) { | |
| 25 // Generate .css output file. | |
| 26 var id = transform.primaryInput.id.changeExtension('.css'); | |
| 27 | |
| 28 // Parse the .scss file. | |
| 29 var sourceFile = new SourceFile.text(id.path, content); | |
| 30 var stylesheet = _parseCss(content, sourceFile, transform.logger); | |
| 31 | |
| 32 analyze([stylesheet]); | |
| 33 | |
| 34 transform.addOutput(new Asset.fromString(id, _prettyPrint(stylesheet))); | |
| 35 }); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 StyleSheet _parseCss(String content, SourceFile sourceFile, logger) { | |
| 40 if (content.trim().isEmpty) return null; | |
| 41 | |
| 42 var errors = []; | |
| 43 | |
| 44 // TODO(terry): Add --checked when fully implemented and error handling as | |
| 45 // part of validation transform. | |
| 46 // TODO(terry): All warnings as errors. | |
| 47 var stylesheet = parse(content, errors: errors, options: | |
| 48 ['--warnings_as_errors', 'memory']); | |
| 49 | |
| 50 // Note: errors aren't fatal in HTML (unless strict mode is on). | |
| 51 // So just print them as warnings. | |
| 52 for (var e in errors) { | |
| 53 logger.warning(e.message, e.span); | |
| 54 } | |
| 55 | |
| 56 return stylesheet; | |
| 57 } | |
| 58 | |
| 59 /** Pretty printer for CSS. */ | |
| 60 String _prettyPrint(StyleSheet ss) { | |
| 61 // Walks the style sheet tree and emits readable CSS. | |
| 62 var formatCss = (new CssPrinter()..visitTree(ss, pretty: true)).toString(); | |
| 63 return "/***** Generated by SCSS transform. *****/\n\n$formatCss"; | |
| 64 } | |
| 65 | |
| OLD | NEW |