Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@value is missing from imported type #261

Open
ghost opened this issue Mar 11, 2024 · 7 comments
Open

@value is missing from imported type #261

ghost opened this issue Mar 11, 2024 · 7 comments
Labels
enhancement New feature or request
Milestone

Comments

@ghost
Copy link

ghost commented Mar 11, 2024

@value statements (https://github.com/css-modules/css-modules/blob/master/docs/values-variables.md) are missing from the imported types.
There doesn't appear to be any documentation or issue about this (yet).


Is your feature request related to a problem? Please describe.

Here's a minimal example consisting of 3 files:

  • externalValues.module.css

    @value externalValue 100px;
  • test.module.css

    @value externalValues: "./externalValues.module.css";
    
    @value externalValue from externalValues;
    
    @value value 200px;
    
    .class {
    }
  • index.ts

    import test from './test.module.css'
    console.log('test', JSON.stringify(test, undefined, 2))

The type for test from this plugin is (retrieved with vscode):

(alias) let test: {
    class: string;
}
import test

However, the types at runtime (from what I can tell, without explicitly loading postcss-modules-values):

  • vite devserver
    test {
      "externalValues": "\"./externalValues.module.css\"",
      "externalValue": "100px",
      "value": "200px",
      "class": "src-test-test-module__class--svpx1"
    }
  • webpack ('style-loader' → 'css-loader') devserver
    test {
      "externalValues": "\"./externalValues.module.css\"",
      "externalValue": "100px",
      "value": "200px",
      "class": "test-test-module__class--nqRDQ"
    }

This leads to issues accessing test.value due to bad typing, despite it existing at runtime.

Describe the solution you'd like

I want the typing to match the actual imported content.
At the very least, I want the @value to be respected (for "value" and "externalValue"), because our code depends on this.

Describe alternatives you've considered

We'd have to rewrite a lot of code which depends on @value and move constants from CSS to TS. But this is a bad approach as the CSS might be generated from design tools, so this would mean more work in the future.


If this is is already supported (somehow), then please document how to handle this.

@ghost
Copy link
Author

ghost commented Mar 11, 2024

Looks like the postcss-modules-values plugin is added by css-loader for webpack: https://github.com/webpack-contrib/css-loader/blob/24e114a909421ed2f817d4b5f35f14fd67a80990/src/utils.js#L758

However, to get this in the typings we'd have to create a postcss configuration, which is not something we need for any other tooling (and might even cause issues with webpack or vite, as their defaults would be modified).
It also raises the barrier of entry for this plugin.


For vite, it also appears here: https://github.com/madyankin/postcss-modules/blob/325f0b33f1b746eae7aa827504a5efd0949022ef/src/scoping.js#L6

@mrmckeb
Copy link
Owner

mrmckeb commented Mar 21, 2024

Hi @JannikGM, I can take a look soon - I'm away right now - but if you could create a reproduction in the meantime, that'll enable me to track down the issue much faster.

It's obviously very hard for this plugin to be zero-config for everyone, because there are (near) infinite ways people can write and build their CSS, but where possible we want to be zero-config.

@ghost
Copy link
Author

ghost commented Mar 21, 2024

[...] if you could create a reproduction in the meantime, that'll enable me to track down the issue much faster.

I've attached a zip with a project for reproducing the issue: typescript-plugin-css-modules-repro-master.zip

It shows a red box with the result of import css from './style.module.css' (in main.ts) at runtime. Also see my comment (about runtime / typescript type differences): in main.ts Line 1 - 17

I haven't tried to create webpack/vite builds yet, but the watch-mode of vite and webpack seems to work (or rather: shows the problem).
Let me know if you have any further questions.

@ghost
Copy link
Author

ghost commented May 10, 2024

@mrmckeb any updates on this?

@mrmckeb
Copy link
Owner

mrmckeb commented May 17, 2024

Sorry, I haven't had a chance to look into this recently. PRs are always welcome though if anyone wants to take a look!

I'll hopefully have some time in the near future.

@mrmckeb
Copy link
Owner

mrmckeb commented Oct 10, 2024

Hi @JannikGM, sorry for the delay. I changed jobs and moved cities over the last six months and haven't had much time to work on open source.

It looks like your issue is - as you pointed out - that you need to have the plugin installed.

Just ensure you are using postcss and the postcss-modules-values plugin

(Source: https://github.com/css-modules/css-modules/blob/master/docs/values-variables.md)

This plugin will load any PostCSS plugins you have installed, but expects them to be in a PostCSS config.

As this is in Webpack's CSS loader, I think it's fair to add it here. Would you be open to creating a PR? If not, I can take a look as it's a very small change either way :)

@mrmckeb mrmckeb added the enhancement New feature or request label Oct 10, 2024
@mrmckeb mrmckeb added this to the v6.0.0 milestone Oct 10, 2024
@ghost
Copy link
Author

ghost commented Oct 28, 2024

Thanks for getting back to it.

I'll be leaving the company that had this issue in the next days.
This GitHub account will be deleted very soon, so I can't do it.

Would be nice if you can still fix it.
I assume I'll use this project somewhere else in the future (and run into the same problem).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant