@@ -7,6 +7,7 @@ import 'dart:async';
7
7
import 'package:path/path.dart' as p;
8
8
9
9
import '../io.dart' ;
10
+ import './canonicalize_context.dart' ;
10
11
11
12
/// Whether the Sass compiler is currently evaluating an `@import` rule.
12
13
///
@@ -15,30 +16,35 @@ import '../io.dart';
15
16
/// canonicalization should be identical for `@import` and `@use` rules. It's
16
17
/// admittedly hacky to set this globally, but `@import` will eventually be
17
18
/// removed, at which point we can delete this and have one consistent behavior.
18
- bool get fromImport => Zone .current[#_inImportRule] as bool ? ?? false ;
19
-
20
- /// The URL of the stylesheet that contains the current load.
21
- Uri ? get containingUrl => switch (Zone .current[#_containingUrl]) {
19
+ bool get fromImport =>
20
+ ((Zone .current[#_canonicalizeContext] as CanonicalizeContext ? )
21
+ ? .fromImport ??
22
+ false );
23
+
24
+ /// The CanonicalizeContext of the current load.
25
+ CanonicalizeContext get canonicalizeContext =>
26
+ switch (Zone .current[#_canonicalizeContext]) {
22
27
null => throw StateError (
23
- "containingUrl may only be accessed within a call to canonicalize()." ),
24
- #_none => null ,
25
- Uri url => url,
28
+ "canonicalizeContext may only be accessed within a call to canonicalize()." ),
29
+ CanonicalizeContext context => context,
26
30
var value => throw StateError (
27
- "Unexpected Zone.current[#_containingUrl ] value $value ." )
31
+ "Unexpected Zone.current[#_canonicalizeContext ] value $value ." )
28
32
};
29
33
30
34
/// Runs [callback] in a context where [fromImport] returns `true` and
31
35
/// [resolveImportPath] uses `@import` semantics rather than `@use` semantics.
32
36
T inImportRule <T >(T callback ()) =>
33
- runZoned (callback, zoneValues: {#_inImportRule: true });
37
+ switch (Zone .current[#_canonicalizeContext]) {
38
+ null => runZoned (callback,
39
+ zoneValues: {#_canonicalizeContext: CanonicalizeContext (null , true )}),
40
+ CanonicalizeContext context => context.withFromImport (true , callback),
41
+ var value => throw StateError (
42
+ "Unexpected Zone.current[#_canonicalizeContext] value $value ." )
43
+ };
34
44
35
- /// Runs [callback] in a context where [containingUrl] returns [url] .
36
- ///
37
- /// If [when] is `false` , runs [callback] without setting [containingUrl] .
38
- T withContainingUrl <T >(Uri ? url, T callback ()) =>
39
- // Use #_none as a sentinel value so we can distinguish a containing URL
40
- // that's set to null from one that's unset at all.
41
- runZoned (callback, zoneValues: {#_containingUrl: url ?? #_none});
45
+ /// Runs [callback] in the given context.
46
+ T withCanonicalizeContext <T >(CanonicalizeContext ? context, T callback ()) =>
47
+ runZoned (callback, zoneValues: {#_canonicalizeContext: context});
42
48
43
49
/// Resolves an imported path using the same logic as the filesystem importer.
44
50
///
0 commit comments