How to globally inject SCSS variables now that Sass has deprecated @import? #18388
Replies: 3 comments 5 replies
-
I managed to get something working, but I can't say I love it. Vue componentsVue components can inherit Sass variables from the vite: {
css: {
preprocessorOptions: {
scss: {
api: 'modern-compiler',
additionalData: `
@use "sass:color";
// Add to the global Sass namespace
@use "my-package/tokens/scss/variables" as *;
`,
},
},
},
} Sass filesSeparate usage within // _layout.scss partial
@use "my-package/tokens/scss/variables" as *;
body {
color: $my-var;
} // app.scss
@use "layout"; |
Beta Was this translation helpful? Give feedback.
-
Not sure if this thread is appropriate for my question, but similar to you, I'm trying to import global scss files that contain rules/properties and not just variables (or mixins or functions). Edit: I've figured it out. You can import an SCSS file in main.ts like import '@/main.scss' |
Beta Was this translation helpful? Give feedback.
-
Hi mate, I got the same issue after migration to use About undefined variable error Solution? loader: 'sass-loader',
options: {
sassOptions: {
importers: [{
canonicalize(url) {
if (url === "<custom_scss_file_url>")) {
return new URL("<your_unique_custom_url>");
}
return null;
},
load(canonicalUrl) {
if (canonicalUrl.href === "<your_unique_custom_url>") {
return {
contents: "<put_any_custom_data_as_your_wish_here>",
syntax: "scss"
};
}
return null;
}
}]
} and in another scss file. You need to import your <custom_scss_file_url> as global. E.g: @use <custom_scss_file_url> as *; That's how I can fix this issue. Hoping you guys can find out the same solution in Vite |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
How would you inject global SCSS variables into all Vue components, non-namespaced, in a project that utilizes Vite?
I have some SCSS variables that I would like to be available in all my Vue components within the project, as well as available in any
**/*.scss
files in the same project.The official docs recommend recommend utilizing
additionalData
as shown below, although doing so with the new@import -> @use
change doesn't seem to work.What I have below works if you change
@use
to@import
just fine, but not as-is.I started a related discussion in the sass repository in an issue that was already open (they don't really seem concerned with the Vite ecosystem or the repercussions of deprecating the existing
@import
functionality).Beta Was this translation helpful? Give feedback.
All reactions