-
Notifications
You must be signed in to change notification settings - Fork 358
/
sass.dart
510 lines (497 loc) · 18.7 KB
/
sass.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Copyright 2016 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
/// We strongly recommend importing this library with the prefix `sass`.
library sass;
import 'package:meta/meta.dart';
import 'package:package_config/package_config_types.dart';
import 'package:source_maps/source_maps.dart';
import 'src/async_import_cache.dart';
import 'src/callable.dart';
import 'src/compile.dart' as c;
import 'src/compile_result.dart';
import 'src/deprecation.dart';
import 'src/exception.dart';
import 'src/import_cache.dart';
import 'src/importer.dart';
import 'src/importer/utils.dart';
import 'src/logger.dart';
import 'src/syntax.dart';
import 'src/util/nullable.dart';
import 'src/visitor/serialize.dart';
export 'src/callable.dart' show Callable, AsyncCallable;
export 'src/compile_result.dart';
export 'src/deprecation.dart';
export 'src/exception.dart' show SassException;
export 'src/importer.dart';
export 'src/logger.dart' show Logger;
export 'src/syntax.dart';
export 'src/value.dart'
hide
ColorChannel,
ColorFormat,
LinearChannel,
SassApiColorSpace,
SpanColorFormat;
export 'src/visitor/serialize.dart' show OutputStyle;
export 'src/evaluation_context.dart' show warn;
/// Loads the Sass file at [path], compiles it to CSS, and returns a
/// [CompileResult] containing the CSS and additional metadata about the
/// compilation.
///
/// If [color] is `true`, this will use terminal colors in warnings. It's
/// ignored if [logger] is passed.
///
/// If [logger] is passed, it's used to emit all messages that are generated by
/// Sass code. Users may pass custom subclasses of [Logger].
///
/// Imports are resolved by trying, in order:
///
/// * Loading a file relative to [path].
///
/// * Each importer in [importers].
///
/// * Each load path in [loadPaths]. Note that this is a shorthand for adding
/// [FilesystemImporter]s to [importers].
///
/// * Each load path specified in the `SASS_PATH` environment variable, which
/// should be semicolon-separated on Windows and colon-separated elsewhere.
///
/// * `package:` resolution using [packageConfig], which is a
/// [`PackageConfig`][] from the `package_resolver` package. Note that
/// this is a shorthand for adding a [PackageImporter] to [importers].
///
/// [`PackageConfig`]: https://pub.dev/documentation/package_config/latest/package_config.package_config/PackageConfig-class.html
///
/// Dart functions that can be called from Sass may be passed using [functions].
/// 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.
///
/// If [quietDeps] is `true`, this will silence compiler warnings emitted for
/// stylesheets loaded through [importers], [loadPaths], or [packageConfig].
///
/// By default, once a deprecation warning for a given feature is printed five
/// times, further warnings for that feature are silenced. If [verbose] is true,
/// all deprecation warnings are printed instead.
///
/// If [sourceMap] is `true`, [CompileResult.sourceMap] will be set to a
/// [SingleMapping] that indicates which sections of the source file(s)
/// correspond to which in the resulting CSS. [SingleMapping.targetUrl] will be
/// `null`. It's up to the caller to save this mapping to disk and add a source
/// map comment to [CompileResult.css] pointing to it. Users using the
/// [SingleMapping] API should be sure to add the [`source_maps`][] package to
/// their pubspec.
///
/// [`source_maps`]: https://pub.dartlang.org/packages/source_maps
///
/// If [charset] is `true`, this will include a `@charset` declaration or a
/// UTF-8 [byte-order mark][] if the stylesheet contains any non-ASCII
/// characters. Otherwise, it will never include a `@charset` declaration or a
/// byte-order mark.
///
/// [byte-order mark]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
///
/// Throws a [SassException] if conversion fails.
///
/// {@category Compile}
CompileResult compileToResult(String path,
{bool color = false,
Logger? logger,
Iterable<Importer>? importers,
Iterable<String>? loadPaths,
PackageConfig? packageConfig,
Iterable<Callable>? functions,
OutputStyle? style,
bool quietDeps = false,
bool verbose = false,
bool sourceMap = false,
bool charset = true,
Iterable<Deprecation>? silenceDeprecations,
Iterable<Deprecation>? fatalDeprecations,
Iterable<Deprecation>? futureDeprecations}) =>
c.compile(path,
logger: logger,
importCache: ImportCache(
importers: importers,
loadPaths: loadPaths,
packageConfig: packageConfig),
functions: functions,
style: style,
quietDeps: quietDeps,
verbose: verbose,
sourceMap: sourceMap,
charset: charset,
silenceDeprecations: silenceDeprecations,
fatalDeprecations: fatalDeprecations,
futureDeprecations: futureDeprecations);
/// Compiles [source] to CSS and returns a [CompileResult] containing the CSS
/// and additional metadata about the compilation..
///
/// This parses the stylesheet as [syntax], which defaults to [Syntax.scss].
///
/// If [color] is `true`, this will use terminal colors in warnings. It's
/// ignored if [logger] is passed.
///
/// If [logger] is passed, it's used to emit all messages that are generated by
/// Sass code. Users may pass custom subclasses of [Logger].
///
/// Imports are resolved by trying, in order:
///
/// * The given [importer], with the imported URL resolved relative to [url].
///
/// * Each importer in [importers].
///
/// * Each load path in [loadPaths]. Note that this is a shorthand for adding
/// [FilesystemImporter]s to [importers].
///
/// * Each load path specified in the `SASS_PATH` environment variable, which
/// should be semicolon-separated on Windows and colon-separated elsewhere.
///
/// * `package:` resolution using [packageConfig], which is a
/// [`PackageConfig`][] from the `package_resolver` package. Note that
/// this is a shorthand for adding a [PackageImporter] to [importers].
///
/// [`PackageConfig`]: https://pub.dev/documentation/package_config/latest/package_config.package_config/PackageConfig-class.html
///
/// Dart functions that can be called from Sass may be passed using [functions].
/// 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`.
///
/// If [quietDeps] is `true`, this will silence compiler warnings emitted for
/// stylesheets loaded through [importers], [loadPaths], or [packageConfig].
///
/// By default, once a deprecation warning for a given feature is printed five
/// times, further warnings for that feature are silenced. If [verbose] is true,
/// all deprecation warnings are printed instead.
///
/// If [sourceMap] is `true`, [CompileResult.sourceMap] will be set to a
/// [SingleMapping] that indicates which sections of the source file(s)
/// correspond to which in the resulting CSS. [SingleMapping.targetUrl] will be
/// `null`. It's up to the caller to save this mapping to disk and add a source
/// map comment to [CompileResult.css] pointing to it. Users using the
/// [SingleMapping] API should be sure to add the [`source_maps`][] package to
/// their pubspec.
///
/// [`source_maps`]: https://pub.dartlang.org/packages/source_maps
///
/// If [charset] is `true`, this will include a `@charset` declaration or a
/// UTF-8 [byte-order mark][] if the stylesheet contains any non-ASCII
/// characters. Otherwise, it will never include a `@charset` declaration or a
/// byte-order mark.
///
/// [byte-order mark]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
///
/// Throws a [SassException] if conversion fails.
///
/// {@category Compile}
CompileResult compileStringToResult(String source,
{Syntax? syntax,
bool color = false,
Logger? logger,
Iterable<Importer>? importers,
PackageConfig? packageConfig,
Iterable<String>? loadPaths,
Iterable<Callable>? functions,
OutputStyle? style,
Importer? importer,
Object? url,
bool quietDeps = false,
bool verbose = false,
bool sourceMap = false,
bool charset = true,
Iterable<Deprecation>? silenceDeprecations,
Iterable<Deprecation>? fatalDeprecations,
Iterable<Deprecation>? futureDeprecations}) =>
c.compileString(source,
syntax: syntax,
logger: logger,
importCache: ImportCache(
importers: importers,
packageConfig: packageConfig,
loadPaths: loadPaths),
functions: functions,
style: style,
importer: importer,
url: url,
quietDeps: quietDeps,
verbose: verbose,
sourceMap: sourceMap,
charset: charset,
silenceDeprecations: silenceDeprecations,
fatalDeprecations: fatalDeprecations,
futureDeprecations: futureDeprecations);
/// Like [compileToResult], except it runs asynchronously.
///
/// Running asynchronously allows this to take [AsyncImporter]s rather than
/// synchronous [Importer]s. However, running asynchronously is also somewhat
/// slower, so [compileToResult] should be preferred if possible.
Future<CompileResult> compileToResultAsync(String path,
{bool color = false,
Logger? logger,
Iterable<AsyncImporter>? importers,
PackageConfig? packageConfig,
Iterable<String>? loadPaths,
Iterable<AsyncCallable>? functions,
OutputStyle? style,
bool quietDeps = false,
bool verbose = false,
bool sourceMap = false,
bool charset = true,
Iterable<Deprecation>? silenceDeprecations,
Iterable<Deprecation>? fatalDeprecations,
Iterable<Deprecation>? futureDeprecations}) =>
c.compileAsync(path,
logger: logger,
importCache: AsyncImportCache(
importers: importers,
loadPaths: loadPaths,
packageConfig: packageConfig),
functions: functions,
style: style,
quietDeps: quietDeps,
verbose: verbose,
sourceMap: sourceMap,
charset: charset,
silenceDeprecations: silenceDeprecations,
fatalDeprecations: fatalDeprecations,
futureDeprecations: futureDeprecations);
/// Like [compileStringToResult], except it runs asynchronously.
///
/// Running asynchronously allows this to take [AsyncImporter]s rather than
/// synchronous [Importer]s. However, running asynchronously is also somewhat
/// slower, so [compileStringToResult] should be preferred if possible.
///
/// {@category Compile}
Future<CompileResult> compileStringToResultAsync(String source,
{Syntax? syntax,
bool color = false,
Logger? logger,
Iterable<AsyncImporter>? importers,
PackageConfig? packageConfig,
Iterable<String>? loadPaths,
Iterable<AsyncCallable>? functions,
OutputStyle? style,
AsyncImporter? importer,
Object? url,
bool quietDeps = false,
bool verbose = false,
bool sourceMap = false,
bool charset = true,
Iterable<Deprecation>? silenceDeprecations,
Iterable<Deprecation>? fatalDeprecations,
Iterable<Deprecation>? futureDeprecations}) =>
c.compileStringAsync(source,
syntax: syntax,
logger: logger,
importCache: AsyncImportCache(
importers: importers,
packageConfig: packageConfig,
loadPaths: loadPaths),
functions: functions,
style: style,
importer: importer,
url: url,
quietDeps: quietDeps,
verbose: verbose,
sourceMap: sourceMap,
charset: charset,
silenceDeprecations: silenceDeprecations,
fatalDeprecations: fatalDeprecations,
futureDeprecations: futureDeprecations);
/// Like [compileToResult], but returns [CompileResult.css] rather than
/// returning [CompileResult] directly.
///
/// If [sourceMap] is passed, it's passed a [SingleMapping] that indicates which
/// sections of the source file(s) correspond to which in the resulting CSS.
/// It's called immediately before this method returns, and only if compilation
/// succeeds. Note that [SingleMapping.targetUrl] will always be `null`. Users
/// using the [SingleMapping] API should be sure to add the [`source_maps`][]
/// package to their pubspec.
///
/// [`source_maps`]: https://pub.dartlang.org/packages/source_maps
///
/// This parameter is meant to be used as an out parameter, so that users who
/// want access to the source map can get it. For example:
///
/// ```dart
/// SingleMapping sourceMap;
/// var css = compile(sassPath, sourceMap: (map) => sourceMap = map);
/// ```
///
/// {@category Compile}
@Deprecated("Use compileToResult() instead.")
String compile(String path,
{bool color = false,
Logger? logger,
Iterable<Importer>? importers,
Iterable<String>? loadPaths,
PackageConfig? packageConfig,
Iterable<Callable>? functions,
OutputStyle? style,
bool quietDeps = false,
bool verbose = false,
@Deprecated("Use CompileResult.sourceMap from compileToResult() instead.")
void sourceMap(SingleMapping map)?,
bool charset = true}) {
var result = compileToResult(path,
logger: logger,
importers: importers,
loadPaths: loadPaths,
packageConfig: packageConfig,
functions: functions,
style: style,
quietDeps: quietDeps,
verbose: verbose,
sourceMap: sourceMap != null,
charset: charset);
result.sourceMap.andThen(sourceMap);
return result.css;
}
/// Like [compileStringToResult], but returns [CompileResult.css] rather than
/// returning [CompileResult] directly.
///
/// If [sourceMap] is passed, it's passed a [SingleMapping] that indicates which
/// sections of the source file(s) correspond to which in the resulting CSS.
/// It's called immediately before this method returns, and only if compilation
/// succeeds. Note that [SingleMapping.targetUrl] will always be `null`. Users
/// using the [SingleMapping] API should be sure to add the [`source_maps`][]
/// package to their pubspec.
///
/// [`source_maps`]: https://pub.dartlang.org/packages/source_maps
///
/// This parameter is meant to be used as an out parameter, so that users who
/// want access to the source map can get it. For example:
///
/// ```dart
/// SingleMapping sourceMap;
/// var css = compileString(sass, sourceMap: (map) => sourceMap = map);
/// ```
///
/// {@category Compile}
@Deprecated("Use compileStringToResult() instead.")
String compileString(String source,
{Syntax? syntax,
bool color = false,
Logger? logger,
Iterable<Importer>? importers,
PackageConfig? packageConfig,
Iterable<String>? loadPaths,
Iterable<Callable>? functions,
OutputStyle? style,
Importer? importer,
Object? url,
bool quietDeps = false,
bool verbose = false,
@Deprecated(
"Use CompileResult.sourceMap from compileStringToResult() instead.")
void sourceMap(SingleMapping map)?,
bool charset = true,
@Deprecated("Use syntax instead.") bool indented = false}) {
var result = compileStringToResult(source,
syntax: syntax ?? (indented ? Syntax.sass : Syntax.scss),
logger: logger,
importers: importers,
packageConfig: packageConfig,
loadPaths: loadPaths,
functions: functions,
style: style,
importer: importer,
url: url,
quietDeps: quietDeps,
verbose: verbose,
sourceMap: sourceMap != null,
charset: charset);
result.sourceMap.andThen(sourceMap);
return result.css;
}
/// Like [compile], except it runs asynchronously.
///
/// Running asynchronously allows this to take [AsyncImporter]s rather than
/// synchronous [Importer]s. However, running asynchronously is also somewhat
/// slower, so [compile] should be preferred if possible.
///
/// {@category Compile}
@Deprecated("Use compileToResultAsync() instead.")
Future<String> compileAsync(String path,
{bool color = false,
Logger? logger,
Iterable<AsyncImporter>? importers,
PackageConfig? packageConfig,
Iterable<String>? loadPaths,
Iterable<AsyncCallable>? functions,
OutputStyle? style,
bool quietDeps = false,
bool verbose = false,
@Deprecated(
"Use CompileResult.sourceMap from compileToResultAsync() instead.")
void sourceMap(SingleMapping map)?}) async {
var result = await compileToResultAsync(path,
logger: logger,
importers: importers,
loadPaths: loadPaths,
packageConfig: packageConfig,
functions: functions,
style: style,
quietDeps: quietDeps,
verbose: verbose,
sourceMap: sourceMap != null);
result.sourceMap.andThen(sourceMap);
return result.css;
}
/// Like [compileString], except it runs asynchronously.
///
/// Running asynchronously allows this to take [AsyncImporter]s rather than
/// synchronous [Importer]s. However, running asynchronously is also somewhat
/// slower, so [compileString] should be preferred if possible.
///
/// {@category Compile}
@Deprecated("Use compileStringToResultAsync() instead.")
Future<String> compileStringAsync(String source,
{Syntax? syntax,
bool color = false,
Logger? logger,
Iterable<AsyncImporter>? importers,
PackageConfig? packageConfig,
Iterable<String>? loadPaths,
Iterable<AsyncCallable>? functions,
OutputStyle? style,
AsyncImporter? importer,
Object? url,
bool quietDeps = false,
bool verbose = false,
@Deprecated(
"Use CompileResult.sourceMap from compileStringToResultAsync() instead.")
void sourceMap(SingleMapping map)?,
bool charset = true,
@Deprecated("Use syntax instead.") bool indented = false}) async {
var result = await compileStringToResultAsync(source,
syntax: syntax ?? (indented ? Syntax.sass : Syntax.scss),
logger: logger,
importers: importers,
packageConfig: packageConfig,
loadPaths: loadPaths,
functions: functions,
style: style,
importer: importer,
url: url,
quietDeps: quietDeps,
verbose: verbose,
sourceMap: sourceMap != null,
charset: charset);
result.sourceMap.andThen(sourceMap);
return result.css;
}
/// Runs [callback] in a context where [AsyncImporter.fromImport] returns
/// `true`.
///
/// This is only intended for use when testing custom importers.
@visibleForTesting
T fakeFromImport<T>(T callback()) => inImportRule(callback);