typescript-plugin-css-modules: SCSS - Importing SCSS variables globally breaks exported TS type to `{}`.
Thanks for the awesome TS plugin! 💪
Describe the bug
There’s certain cases when using SASS where you need the value of a variable and need to wrap it in #{}
in order to get the variable output and not the variable name.
When adding #{}
to a *.module.scss
, the file’s classNames etc are no longer exported and only shows {}
in the TS file.
To Reproduce
Add the following to any *.module.scss
file, and try and import it to a TS
file.
container.module.scss
@use "variables" as *; // Which includes $breakpoint
.container {
@media (min-width: #{$breakpoint}) {
color: red;
}
}
Expected behavior
Using the #{}
functionality shouldn’t remove the classNames.
Desktop (please complete the following information):
- OS: iOS
- Browser: Chrome
- Version: 96.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 16
- Comments: 17 (1 by maintainers)
me too, we have a lot of media query global variables on a project, but when we are using them, it’s always showing empty type
{}
With custom media global variables
@media #{$new-tablet-only} {
Without custom media global variables
@mrmckeb please help us to fix this issue
I think it’s related to https://github.com/mrmckeb/typescript-plugin-css-modules/issues/146.
Trying to add the variable directly to the
scss
fixed the error. I have a SCSS configuration in myNextJS
app which includes all my resources, and I think that needs to be set in the same way for the plugin.I will try and update the
tsconfig
plugin settings, and see if I can get it to work 😃Wow, I’ve been searching this solution for hours. Though I don’t quite enjoy how I need to
@use "..."
in everymodule.scss
files, it at least doesn’t throw type errors XD. Thank you!Adding the underscore before the file name fixed it for me – thank you!
finally could solve it by using
_
before partial imports. Basically sass doesn’t need to include the underscore when importing partials buttypescript-plugin-css-modules
seems not to understand it and requires to include it.Examples
This doesn’t work:
But this works fine:
as a result,
_index.scss
files also cannot be detected automatically by this plugin and you need to include the complete file pathCC @mrmckeb
@mrmckeb I can confirm that the following code is now working in my NX monorepo setup.
Thanks for your help and work 💪
Here’s the code if it helps others doing the same setup:
NX
Directory structuretsconfig.base.json
apps/web/components/header.tsx
The JSX component including the CSS module files. The
styles.header
is now correctly shown.apps/web/components/header.module.scss
The SCSS file loading the
libs/theme
.libs/theme/index.scss
This includes all the shared SCSS configuration, variables, mixins and functions
So, I’ve tried another solutions for my purpose, specially media-quaries global variables. I’ve removed sass variables from my project, switched to pure css modules and installed
postcss-custom-media
and others pluginsCreated a file with custom media queries variables:
Then I added a postcss.config.js
Then I’ve used this variable in my css module file
And then everything works perfectly: