Skip to content

Commit d660bf1

Browse files
committed
Add an option to the CLI and Dart Sass to silence warnings from deps
Closes #672
1 parent 7f982a1 commit d660bf1

File tree

13 files changed

+212
-23
lines changed

13 files changed

+212
-23
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
## 1.33.1
1+
## 1.34.0
22

33
* Don't emit the same warning in the same location multiple times.
44

5+
### Command Line Interface
6+
7+
* Add a `--quiet-deps` flag which silences compiler warnings from stylesheets
8+
loaded through `--load-path`s.
9+
10+
### Dart API
11+
12+
* Add a `quietDeps` argument to `compile()`, `compileString()`,
13+
`compileAsync()`, and `compileStringAsync()` which silences compiler warnings
14+
from stylesheets loaded through importers, load paths, and `package:` URLs.
15+
516
## 1.33.0
617

718
* Deprecate the use of `/` for division. The new `math.div()` function should be

lib/sass.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ export 'src/warn.dart' show warn;
6161
///
6262
/// The [style] parameter controls the style of the resulting CSS.
6363
///
64+
/// If [quietDeps] is `true`, this will silence compiler warnings emitted for
65+
/// stylesheets loaded through [importers], [loadPaths], or [packageConfig].
66+
///
6467
/// If [sourceMap] is passed, it's passed a [SingleMapping] that indicates which
6568
/// sections of the source file(s) correspond to which in the resulting CSS.
6669
/// It's called immediately before this method returns, and only if compilation
@@ -94,6 +97,7 @@ String compile(String path,
9497
PackageConfig? packageConfig,
9598
Iterable<Callable>? functions,
9699
OutputStyle? style,
100+
bool quietDeps = false,
97101
void sourceMap(SingleMapping map)?,
98102
bool charset = true}) {
99103
logger ??= Logger.stderr(color: color);
@@ -106,6 +110,7 @@ String compile(String path,
106110
packageConfig: packageConfig),
107111
functions: functions,
108112
style: style,
113+
quietDeps: quietDeps,
109114
sourceMap: sourceMap != null,
110115
charset: charset);
111116
result.sourceMap.andThen(sourceMap);
@@ -150,6 +155,9 @@ String compile(String path,
150155
/// [String] or a [Uri]. If [importer] is passed, [url] must be passed as well
151156
/// and `importer.load(url)` should return `source`.
152157
///
158+
/// If [quietDeps] is `true`, this will silence compiler warnings emitted for
159+
/// stylesheets loaded through [importers], [loadPaths], or [packageConfig].
160+
///
153161
/// If [sourceMap] is passed, it's passed a [SingleMapping] that indicates which
154162
/// sections of the source file(s) correspond to which in the resulting CSS.
155163
/// It's called immediately before this method returns, and only if compilation
@@ -186,6 +194,7 @@ String compileString(String source,
186194
OutputStyle? style,
187195
Importer? importer,
188196
Object? url,
197+
bool quietDeps = false,
189198
void sourceMap(SingleMapping map)?,
190199
bool charset = true,
191200
@Deprecated("Use syntax instead.") bool indented = false}) {
@@ -202,6 +211,7 @@ String compileString(String source,
202211
style: style,
203212
importer: importer,
204213
url: url,
214+
quietDeps: quietDeps,
205215
sourceMap: sourceMap != null,
206216
charset: charset);
207217
result.sourceMap.andThen(sourceMap);
@@ -221,6 +231,7 @@ Future<String> compileAsync(String path,
221231
Iterable<String>? loadPaths,
222232
Iterable<AsyncCallable>? functions,
223233
OutputStyle? style,
234+
bool quietDeps = false,
224235
void sourceMap(SingleMapping map)?}) async {
225236
logger ??= Logger.stderr(color: color);
226237
var result = await c.compileAsync(path,
@@ -232,6 +243,7 @@ Future<String> compileAsync(String path,
232243
packageConfig: packageConfig),
233244
functions: functions,
234245
style: style,
246+
quietDeps: quietDeps,
235247
sourceMap: sourceMap != null);
236248
result.sourceMap.andThen(sourceMap);
237249
return result.css;
@@ -253,6 +265,7 @@ Future<String> compileStringAsync(String source,
253265
OutputStyle? style,
254266
AsyncImporter? importer,
255267
Object? url,
268+
bool quietDeps = false,
256269
void sourceMap(SingleMapping map)?,
257270
bool charset = true,
258271
@Deprecated("Use syntax instead.") bool indented = false}) async {
@@ -269,6 +282,7 @@ Future<String> compileStringAsync(String source,
269282
style: style,
270283
importer: importer,
271284
url: url,
285+
quietDeps: quietDeps,
272286
sourceMap: sourceMap != null,
273287
charset: charset);
274288
result.sourceMap.andThen(sourceMap);

lib/src/async_compile.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Future<CompileResult> compileAsync(String path,
3434
bool useSpaces = true,
3535
int? indentWidth,
3636
LineFeed? lineFeed,
37+
bool quietDeps = false,
3738
bool sourceMap = false,
3839
bool charset = true}) async {
3940
// If the syntax is different than the importer would default to, we have to
@@ -43,7 +44,8 @@ Future<CompileResult> compileAsync(String path,
4344
(syntax == null || syntax == Syntax.forPath(path))) {
4445
importCache ??= AsyncImportCache.none(logger: logger);
4546
stylesheet = (await importCache.importCanonical(
46-
FilesystemImporter('.'), p.toUri(canonicalize(path)), p.toUri(path)))!;
47+
FilesystemImporter('.'), p.toUri(canonicalize(path)),
48+
originalUrl: p.toUri(path)))!;
4749
} else {
4850
stylesheet = Stylesheet.parse(
4951
readFile(path), syntax ?? Syntax.forPath(path),
@@ -61,6 +63,7 @@ Future<CompileResult> compileAsync(String path,
6163
useSpaces,
6264
indentWidth,
6365
lineFeed,
66+
quietDeps,
6467
sourceMap,
6568
charset);
6669
}
@@ -83,6 +86,7 @@ Future<CompileResult> compileStringAsync(String source,
8386
int? indentWidth,
8487
LineFeed? lineFeed,
8588
Object? url,
89+
bool quietDeps = false,
8690
bool sourceMap = false,
8791
bool charset = true}) async {
8892
var stylesheet =
@@ -99,6 +103,7 @@ Future<CompileResult> compileStringAsync(String source,
99103
useSpaces,
100104
indentWidth,
101105
lineFeed,
106+
quietDeps,
102107
sourceMap,
103108
charset);
104109
}
@@ -117,6 +122,7 @@ Future<CompileResult> _compileStylesheet(
117122
bool useSpaces,
118123
int? indentWidth,
119124
LineFeed? lineFeed,
125+
bool quietDeps,
120126
bool sourceMap,
121127
bool charset) async {
122128
var evaluateResult = await evaluateAsync(stylesheet,
@@ -125,6 +131,7 @@ Future<CompileResult> _compileStylesheet(
125131
importer: importer,
126132
functions: functions,
127133
logger: logger,
134+
quietDeps: quietDeps,
128135
sourceMap: sourceMap);
129136

130137
var serializeResult = serialize(evaluateResult.stylesheet,

lib/src/async_import_cache.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ Relative canonical URLs are deprecated and will eventually be disallowed.
162162
var tuple = await canonicalize(url,
163163
baseImporter: baseImporter, baseUrl: baseUrl, forImport: forImport);
164164
if (tuple == null) return null;
165-
var stylesheet =
166-
await importCanonical(tuple.item1, tuple.item2, tuple.item3);
165+
var stylesheet = await importCanonical(tuple.item1, tuple.item2,
166+
originalUrl: tuple.item3);
167167
if (stylesheet == null) return null;
168168
return Tuple2(tuple.item1, stylesheet);
169169
}
@@ -177,9 +177,12 @@ Relative canonical URLs are deprecated and will eventually be disallowed.
177177
/// into [canonicalUrl]. It's used to resolve a relative canonical URL, which
178178
/// importers may return for legacy reasons.
179179
///
180+
/// If [quiet] is `true`, this will disable logging warnings when parsing the
181+
/// newly imported stylesheet.
182+
///
180183
/// Caches the result of the import and uses cached results if possible.
181184
Future<Stylesheet?> importCanonical(AsyncImporter importer, Uri canonicalUrl,
182-
[Uri? originalUrl]) async {
185+
{Uri? originalUrl, bool quiet = false}) async {
183186
return await putIfAbsentAsync(_importCache, canonicalUrl, () async {
184187
var result = await importer.load(canonicalUrl);
185188
if (result == null) return null;
@@ -191,7 +194,7 @@ Relative canonical URLs are deprecated and will eventually be disallowed.
191194
url: originalUrl == null
192195
? canonicalUrl
193196
: originalUrl.resolveUri(canonicalUrl),
194-
logger: _logger);
197+
logger: quiet ? Logger.quiet : _logger);
195198
});
196199
}
197200

lib/src/compile.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// DO NOT EDIT. This file was generated from async_compile.dart.
66
// See tool/grind/synchronize.dart for details.
77
//
8-
// Checksum: dcb7cfbedf1e1189808c0056debf6a68bd387dab
8+
// Checksum: bdf01f7ff8eea0efafa6c7c93920caf26e324f4e
99
//
1010
// ignore_for_file: unused_import
1111

@@ -44,6 +44,7 @@ CompileResult compile(String path,
4444
bool useSpaces = true,
4545
int? indentWidth,
4646
LineFeed? lineFeed,
47+
bool quietDeps = false,
4748
bool sourceMap = false,
4849
bool charset = true}) {
4950
// If the syntax is different than the importer would default to, we have to
@@ -53,7 +54,8 @@ CompileResult compile(String path,
5354
(syntax == null || syntax == Syntax.forPath(path))) {
5455
importCache ??= ImportCache.none(logger: logger);
5556
stylesheet = importCache.importCanonical(
56-
FilesystemImporter('.'), p.toUri(canonicalize(path)), p.toUri(path))!;
57+
FilesystemImporter('.'), p.toUri(canonicalize(path)),
58+
originalUrl: p.toUri(path))!;
5759
} else {
5860
stylesheet = Stylesheet.parse(
5961
readFile(path), syntax ?? Syntax.forPath(path),
@@ -71,6 +73,7 @@ CompileResult compile(String path,
7173
useSpaces,
7274
indentWidth,
7375
lineFeed,
76+
quietDeps,
7477
sourceMap,
7578
charset);
7679
}
@@ -93,6 +96,7 @@ CompileResult compileString(String source,
9396
int? indentWidth,
9497
LineFeed? lineFeed,
9598
Object? url,
99+
bool quietDeps = false,
96100
bool sourceMap = false,
97101
bool charset = true}) {
98102
var stylesheet =
@@ -109,6 +113,7 @@ CompileResult compileString(String source,
109113
useSpaces,
110114
indentWidth,
111115
lineFeed,
116+
quietDeps,
112117
sourceMap,
113118
charset);
114119
}
@@ -127,6 +132,7 @@ CompileResult _compileStylesheet(
127132
bool useSpaces,
128133
int? indentWidth,
129134
LineFeed? lineFeed,
135+
bool quietDeps,
130136
bool sourceMap,
131137
bool charset) {
132138
var evaluateResult = evaluate(stylesheet,
@@ -135,6 +141,7 @@ CompileResult _compileStylesheet(
135141
importer: importer,
136142
functions: functions,
137143
logger: logger,
144+
quietDeps: quietDeps,
138145
sourceMap: sourceMap);
139146

140147
var serializeResult = serialize(evaluateResult.stylesheet,

lib/src/executable/compile_stylesheet.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ Future<void> compileStylesheet(ExecutableOptions options, StylesheetGraph graph,
6868
importCache: importCache,
6969
importer: FilesystemImporter('.'),
7070
style: options.style,
71+
quietDeps: options.quietDeps,
7172
sourceMap: options.emitSourceMap,
7273
charset: options.charset)
7374
: await compileAsync(source,
7475
syntax: syntax,
7576
logger: options.logger,
7677
importCache: importCache,
7778
style: options.style,
79+
quietDeps: options.quietDeps,
7880
sourceMap: options.emitSourceMap,
7981
charset: options.charset);
8082
} else {
@@ -85,13 +87,15 @@ Future<void> compileStylesheet(ExecutableOptions options, StylesheetGraph graph,
8587
importCache: graph.importCache,
8688
importer: FilesystemImporter('.'),
8789
style: options.style,
90+
quietDeps: options.quietDeps,
8891
sourceMap: options.emitSourceMap,
8992
charset: options.charset)
9093
: compile(source,
9194
syntax: syntax,
9295
logger: options.logger,
9396
importCache: graph.importCache,
9497
style: options.style,
98+
quietDeps: options.quietDeps,
9599
sourceMap: options.emitSourceMap,
96100
charset: options.charset);
97101
}

lib/src/executable/options.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ class ExecutableOptions {
9999
..addFlag('unicode',
100100
help: 'Whether to use Unicode characters for messages.')
101101
..addFlag('quiet', abbr: 'q', help: "Don't print warnings.")
102+
..addFlag('quiet-deps',
103+
help: "Don't print compiler warnings from dependencies.\n"
104+
"Stylesheets imported through load paths count as dependencies.")
102105
..addFlag('trace', help: 'Print full Dart stack traces for exceptions.')
103106
..addFlag('help',
104107
abbr: 'h', help: 'Print this usage information.', negatable: false)
@@ -163,9 +166,12 @@ class ExecutableOptions {
163166
? _options['unicode'] as bool
164167
: !term_glyph.ascii;
165168

166-
/// Whether to silence normal output.
169+
/// Whether to silence all warnings.
167170
bool get quiet => _options['quiet'] as bool;
168171

172+
/// Whether to silence warnings in dependencies.
173+
bool get quietDeps => _options['quiet-deps'] as bool;
174+
169175
/// The logger to use to emit messages from Sass.
170176
Logger get logger => quiet ? Logger.quiet : Logger.stderr(color: color);
171177

lib/src/import_cache.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// DO NOT EDIT. This file was generated from async_import_cache.dart.
66
// See tool/grind/synchronize.dart for details.
77
//
8-
// Checksum: 950db49eb9e3a85f35bc4a3d7cfe029fb60ae498
8+
// Checksum: 6821c9a63333c3c99b0c9515aa04e73a14e0f141
99
//
1010
// ignore_for_file: unused_import
1111

@@ -161,7 +161,8 @@ Relative canonical URLs are deprecated and will eventually be disallowed.
161161
var tuple = canonicalize(url,
162162
baseImporter: baseImporter, baseUrl: baseUrl, forImport: forImport);
163163
if (tuple == null) return null;
164-
var stylesheet = importCanonical(tuple.item1, tuple.item2, tuple.item3);
164+
var stylesheet =
165+
importCanonical(tuple.item1, tuple.item2, originalUrl: tuple.item3);
165166
if (stylesheet == null) return null;
166167
return Tuple2(tuple.item1, stylesheet);
167168
}
@@ -175,9 +176,12 @@ Relative canonical URLs are deprecated and will eventually be disallowed.
175176
/// into [canonicalUrl]. It's used to resolve a relative canonical URL, which
176177
/// importers may return for legacy reasons.
177178
///
179+
/// If [quiet] is `true`, this will disable logging warnings when parsing the
180+
/// newly imported stylesheet.
181+
///
178182
/// Caches the result of the import and uses cached results if possible.
179183
Stylesheet? importCanonical(Importer importer, Uri canonicalUrl,
180-
[Uri? originalUrl]) {
184+
{Uri? originalUrl, bool quiet = false}) {
181185
return _importCache.putIfAbsent(canonicalUrl, () {
182186
var result = importer.load(canonicalUrl);
183187
if (result == null) return null;
@@ -189,7 +193,7 @@ Relative canonical URLs are deprecated and will eventually be disallowed.
189193
url: originalUrl == null
190194
? canonicalUrl
191195
: originalUrl.resolveUri(canonicalUrl),
192-
logger: _logger);
196+
logger: quiet ? Logger.quiet : _logger);
193197
});
194198
}
195199

lib/src/stylesheet_graph.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ class StylesheetGraph {
9595
var node = _nodes[canonicalUrl];
9696
if (node != null) return const {};
9797

98-
var stylesheet = _ignoreErrors(
99-
() => importCache.importCanonical(importer, canonicalUrl, originalUrl));
98+
var stylesheet = _ignoreErrors(() => importCache
99+
.importCanonical(importer, canonicalUrl, originalUrl: originalUrl));
100100
if (stylesheet == null) return const {};
101101

102102
node = StylesheetNode._(stylesheet, importer, canonicalUrl,
@@ -278,8 +278,8 @@ class StylesheetGraph {
278278
/// error will be produced during compilation.
279279
if (active.contains(canonicalUrl)) return null;
280280

281-
var stylesheet = _ignoreErrors(
282-
() => importCache.importCanonical(importer, canonicalUrl, resolvedUrl));
281+
var stylesheet = _ignoreErrors(() => importCache
282+
.importCanonical(importer, canonicalUrl, originalUrl: resolvedUrl));
283283
if (stylesheet == null) return null;
284284

285285
active.add(canonicalUrl);

0 commit comments

Comments
 (0)