Does lazy-loading variable evaluation impact nested mixin scope?
0 reputation · 13 Apr 2024, 16:44 UTC
0 reputation · 13 Apr 2024, 16:44 UTC
Less employs a lazy-loading variable system where variables are evaluated at the time of use rather than at the point of declaration. This behavior is intended to allow variables to be defined after they are referenced in certain contexts.
When utilizing nested mixins that rely on local variable overrides within specific CSS selectors, there is uncertainty regarding the priority of evaluation. Specifically, it is unclear if a lazy-loaded variable declared globally will override a local variable defined within a mixin if the global assignment occurs after the mixin is invoked but before the final compilation phase.
@import directives affect this evaluation sequence?34775 reputation · 13 Apr 2024, 23:57 UTC
Local variables defined inside a mixin shadow a later‑defined global variable with the same name. The lazy‑loading of variables does not change this precedence.
@import statements does not affect this rule; all imports are merged into a single global scope before lazy evaluation begins.Because variable lookup is performed after the entire stylesheet tree is built, the runtime environment walks the scope chain from the innermost block outward. A variable assigned later in the global scope is still visible, but only if no nearer definition exists. Therefore a mixin‑local assignment always overrides a global assignment, regardless of when the global value is supplied.
@color and then locally overrides @color inside the mixin.@color.lessc (e.g., lessc test.less test.css).@import statements that bring in the mixin and the global definition; the output should remain unchanged.To confirm that the behavior matches your environment, please specify the version of Less you are using (e.g., 3.x, 4.x, or an earlier release). If you are on a pre‑3.x version, the lazy‑loading rules may differ and could affect precedence.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 13 Apr 2024, 19:08 UTC
When compiling with lessc, variable resolution occurs after the full stylesheet tree is built, so a mixin‑local @var consistently overrides a globally declared @var regardless of import order or when the global assignment appears. In less.js, however, variables are evaluated at use‑time, which means @import sequence can influence which definition is encountered first during on‑demand parsing. Additionally, mixins that employ guard expressions or pattern‑matching rules may create intermediate scoping layers that interact with global overrides in ways not fully standardized across Less versions. Specifying the exact Less release is recommended when reproducing or debugging this behavior.