Add a compressed output style and pipe it through to the executable

This commit is contained in:
Natalie Weizenbaum 2018-01-14 16:05:19 -08:00
parent 116bd3a8af
commit 8d7964e9be
4 changed files with 44 additions and 12 deletions

View File

@ -9,11 +9,13 @@ import 'src/compile.dart' as c;
import 'src/exception.dart';
import 'src/importer.dart';
import 'src/sync_package_resolver.dart';
import 'src/visitor/serialize.dart';
export 'src/callable.dart' show Callable, AsyncCallable;
export 'src/importer.dart';
export 'src/value.dart' show ListSeparator;
export 'src/value/external/value.dart';
export 'src/visitor/serialize.dart' show OutputStyle;
/// Loads the Sass file at [path], compiles it to CSS, and returns the result.
///
@ -38,19 +40,23 @@ export 'src/value/external/value.dart';
/// Each [Callable] defines a top-level function that will be invoked when the
/// given name is called from Sass.
///
/// The [style] parameter controls the style of the resulting CSS.
///
/// Throws a [SassException] if conversion fails.
String compile(String path,
{bool color: false,
Iterable<Importer> importers,
Iterable<String> loadPaths,
SyncPackageResolver packageResolver,
Iterable<Callable> functions}) {
Iterable<Callable> functions,
OutputStyle style}) {
var result = c.compile(path,
color: color,
importers: importers,
loadPaths: loadPaths,
packageResolver: packageResolver,
functions: functions);
functions: functions,
style: style);
return result.css;
}
@ -79,6 +85,8 @@ String compile(String path,
/// Each [Callable] defines a top-level function that will be invoked when the
/// given name is called from Sass.
///
/// The [style] parameter controls the style of the resulting CSS.
///
/// The [url] indicates the location from which [source] was loaded. It may be a
/// [String] or a [Uri]. If [importer] is passed, [url] must be passed as well
/// and `importer.load(url)` should return `source`.
@ -91,6 +99,7 @@ String compileString(String source,
SyncPackageResolver packageResolver,
Iterable<String> loadPaths,
Iterable<Callable> functions,
OutputStyle style,
Importer importer,
url}) {
var result = c.compileString(source,
@ -100,6 +109,7 @@ String compileString(String source,
packageResolver: packageResolver,
loadPaths: loadPaths,
functions: functions,
style: style,
importer: importer,
url: url);
return result.css;
@ -115,13 +125,15 @@ Future<String> compileAsync(String path,
Iterable<AsyncImporter> importers,
SyncPackageResolver packageResolver,
Iterable<String> loadPaths,
Iterable<AsyncCallable> functions}) async {
Iterable<AsyncCallable> functions,
OutputStyle style}) async {
var result = await c.compileAsync(path,
color: color,
importers: importers,
loadPaths: loadPaths,
packageResolver: packageResolver,
functions: functions);
functions: functions,
style: style);
return result.css;
}
@ -137,6 +149,7 @@ Future<String> compileStringAsync(String source,
SyncPackageResolver packageResolver,
Iterable<String> loadPaths,
Iterable<AsyncCallable> functions,
OutputStyle style,
AsyncImporter importer,
url}) async {
var result = await c.compileStringAsync(source,
@ -146,6 +159,7 @@ Future<String> compileStringAsync(String source,
packageResolver: packageResolver,
loadPaths: loadPaths,
functions: functions,
style: style,
importer: importer,
url: url);
return result.css;

View File

@ -20,7 +20,7 @@ main(List<String> args) async {
..addOption('style',
abbr: 's',
help: 'Output style.',
allowed: ['expanded'],
allowed: ['expanded', 'compressed'],
defaultsTo: 'expanded')
..addFlag('color', abbr: 'c', help: 'Whether to emit terminal colors.')
..addFlag('trace', help: 'Print full Dart stack traces for exceptions.')
@ -59,19 +59,22 @@ main(List<String> args) async {
var color =
options.wasParsed('color') ? options['color'] as bool : hasTerminal;
var style = options['style'] == 'compressed'
? OutputStyle.compressed
: OutputStyle.expanded;
var asynchronous = options['async'] as bool;
try {
String css;
if (stdinFlag) {
css = await _compileStdin(asynchronous: asynchronous);
css = await _compileStdin(style: style, asynchronous: asynchronous);
} else {
var input = options.rest.first;
if (input == '-') {
css = await _compileStdin(asynchronous: asynchronous);
css = await _compileStdin(style: style, asynchronous: asynchronous);
} else if (asynchronous) {
css = await compileAsync(input, color: color);
css = await compileAsync(input, color: color, style: style);
} else {
css = compile(input, color: color);
css = compile(input, color: color, style: style);
}
}
@ -135,13 +138,14 @@ Future<String> _loadVersion() async {
/// Compiles Sass from standard input and returns the result.
Future<String> _compileStdin(
{bool asynchronous: false, bool color: false}) async {
{bool color: false, OutputStyle style, bool asynchronous: false}) async {
var text = await readStdin();
var importer = new FilesystemImporter('.');
if (asynchronous) {
return await compileStringAsync(text, color: color, importer: importer);
return await compileStringAsync(text,
color: color, style: style, importer: importer);
} else {
return compileString(text, color: color, importer: importer);
return compileString(text, color: color, style: style, importer: importer);
}
}

View File

@ -239,6 +239,7 @@ NodeImporter _parseImporter(RenderOptions options, DateTime start) {
/// Parse [style] into an [OutputStyle].
OutputStyle _parseOutputStyle(String style) {
if (style == null || style == 'expanded') return OutputStyle.expanded;
if (style == 'compressed') return OutputStyle.compressed;
throw new ArgumentError('Unsupported output style "$style".');
}

View File

@ -981,8 +981,21 @@ class _SerializeVisitor implements CssVisitor, ValueVisitor, SelectorVisitor {
/// An enum of generated CSS styles.
class OutputStyle {
/// The standard CSS style, with each declaration on its own line.
///
/// ```css
/// .sidebar {
/// width: 100px;
/// }
/// ```
static const expanded = const OutputStyle._("expanded");
/// A CSS style that produces as few bytes of output as possible.
///
/// ```css
/// .sidebar{width:100px}
/// ```
static const compressed = const OutputStyle._("compressed");
/// The name of the style.
final String _name;