@use module adoption boundary when replacing Sass @import in Dart Sass
0 reputation · 02 Dec 2020, 04:05 UTC
0 reputation · 02 Dec 2020, 04:05 UTC
In Dart Sass versions that support the module system, the @import rule is deprecated in favor of @use and @forward. Legacy projects built on LibSass or Ruby Sass cannot adopt @use because the module system is exclusive to Dart Sass. The @use rule provides namespacing and private members, and modules are executed only once across the dependency graph, whereas @import can duplicate content and pollute the global scope.
The unresolved question is whether a stylesheet can safely combine @import and @use during a gradual migration. Documentation warns that mixing the two can lead to unpredictable compilation order and duplication, but the exact boundary of that risk is unclear. It is also uncertain how third-party libraries that still rely on @import should be integrated into a @use-based codebase without forcing an immediate rewrite.
What is the precise compatibility boundary when replacing @import with @use in Dart Sass? Is partial migration within a single file structurally safe, or does the execution model require a complete conversion? How should unconverted dependencies be handled in a project that has adopted the module system?
35275 reputation · 02 Dec 2020, 08:40 UTC
In Dart Sass, the compatibility boundary is file-based. While the compiler allows both rules to exist within a project, mixing @use and @import within a single stylesheet is not structurally safe. Because @import operates on a global scope and @use operates on a namespaced module system, combining them in one file leads to unpredictable execution order and potential CSS duplication.
The primary risk of partial migration within a single file is the breakdown of the module system's core guarantee: that a module is executed only once. When you mix these rules, the following occurs:
@import continues to pull all variables, mixins, and functions into the global namespace.@use attempts to isolate members. If a file uses both, the compiler may struggle to determine if a variable should be resolved via the module namespace or the global scope, often resulting in duplication of the compiled CSS output.When your project has adopted the module system but relies on third-party libraries that still use @import, use the following strategies:
@import-based files. Do not attempt to @use a library that is authored with @import, as it will not provide the expected namespacing and may ignore module semantics.@import remain accessible globally. You can continue to use their mixins and variables without a namespace, provided the import occurs before the styles that require them.@import statements to @use or @forward.To verify your migration boundary, run these checks in your build pipeline:
@use file. Look for duplicated rule sets which indicate the module system is being bypassed.@use modules are prefixed (e.g., colors.$primary) and that legacy imports remain unprefixed.Diagnostic Detail: Are you using the sass-migrator tool for this transition? The tool can automate the conversion of @import to @use, which eliminates the risk of manual mixing within files.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.