You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Addressing Limitations with Dynamic SCSS Imports in Vite and Sass
Currently, there is a need to dynamically import all component-specific SCSS files into the global.scss file to streamline maintenance and avoid manual additions. However, there are significant challenges with achieving this using modern Sass and Vite configurations:
Using a Vite Plugin for Dynamic Import:
A Vite plugin can be implemented to dynamically import SCSS files using glob patterns.
This approach relies on the deprecated @import directive in Sass.
Deprecation of @import in Sass:
While the plugin works with @import, using this directive generates deprecation warnings.
Dart Sass plans to remove @import in version 3.0.0, making it an unsuitable long-term solution.
Limitations of @use with Dynamic Imports:
Switching to the recommended @use directive breaks the plugin’s functionality, as it does not respond to @use.
This means there is no immediate way to dynamically import SCSS files using @use.
Future Considerations:
There is hope that either Sass or Vite tooling will introduce a solution that supports dynamic imports with @use in the near future.
Until then, the only viable approach is to use @import and tolerate the deprecation warnings or manually maintain imports in global.scss.
importglobfrom"glob"exportdefault{css: {preprocessorOptions: {scss: {importer(url){// Check for the wildcard importif(url==="./components/**/*.scss"){// Fetch all matching SCSS files using globconstfiles=glob.sync("./src/components/**/*.scss")// Generate `@use` statements with namespacesconstcontents=files.map((file)=>{// Generate a unique namespace for each file based on its filenameconstnamespace=file.replace(/^.*[\\/]/,"")// Remove the path.replace(/\.scss$/,"")// Remove the `.scss` extension.replace(/[^a-zA-Z0-9]/g,"_")// Sanitize invalid characters for namespacesreturn`@use "${file}" as ${namespace};`}).join("\n")return{ contents }}returnnull},},},},}
Addressing Limitations with Dynamic SCSS Imports in Vite and Sass
Currently, there is a need to dynamically import all component-specific SCSS files into the
global.scss
file to streamline maintenance and avoid manual additions. However, there are significant challenges with achieving this using modern Sass and Vite configurations:Using a Vite Plugin for Dynamic Import:
@import
directive in Sass.Deprecation of
@import
in Sass:@import
, using this directive generates deprecation warnings.@import
in version 3.0.0, making it an unsuitable long-term solution.Limitations of
@use
with Dynamic Imports:@use
directive breaks the plugin’s functionality, as it does not respond to@use
.@use
.Future Considerations:
@use
in the near future.@import
and tolerate the deprecation warnings or manually maintain imports inglobal.scss
.global.scss
Related PR
The text was updated successfully, but these errors were encountered: