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

Type derivation fails when using less variables. #122

Closed
wzono opened this issue May 6, 2021 · 11 comments
Closed

Type derivation fails when using less variables. #122

wzono opened this issue May 6, 2021 · 11 comments

Comments

@wzono
Copy link

wzono commented May 6, 2021

Describe the bug
Type derivation fails when using less variables.

To Reproduce
Steps to reproduce the behavior:

  1. Install dependencies & prepare the tsconfig & declare type & use workspace's ts version.
    image
    image

  2. create index.ts & create index.module.less & fill style without less variables.
    image
    image
    Now it works well

  3. Add less variables to the less file.
    image

  4. See error
    image

Expected behavior
When I use less variables, it should be work well too.

Screenshots
See ** To Reproduce **

Desktop (please complete the following information):

  • OS: [e.g. iOS]: MacOS
  • Browser [e.g. chrome, safari]: Chrome
  • Version [e.g. 22]: 90.0.4430.93

Additional context
These less variables are injected through less-loader.

@mrmckeb
Copy link
Owner

mrmckeb commented May 11, 2021

Hi @wingsico, thanks for raising this issue. Are you able to supply a small reproduction of this issue for me?

@wzono
Copy link
Author

wzono commented May 12, 2021

Hi @wingsico, thanks for raising this issue. Are you able to supply a small reproduction of this issue for me?

@mrmckeb

Ok, here is the reproduction link: https://github.com/wingsico/typescript-plugin-css-modules-issue

Focus on the src/App.tsx and src/App.m.less

@kintz09
Copy link

kintz09 commented May 20, 2021

I believe I'm experiencing a very similar issue. My project is based on CRA v4, using scss files, and CRACO to fix this bug with sass exports.

The project compiles just fine like the OP's example, and I get the same empty object for the imported stylesheet.
Module declarations are handled by CRA in this project.

Style Object Broken

Style Object Broken

Here's the plugin configuration:
image

Here's the SCSS file:
image

Here's the the TSX file showing the empty style object:
image

Style Object Working

Style Object Working

Here's the modified SCSS file not using the import:
image

Here's the same TSX file showing the working style object:
image

If you'd like me to put together a reproduction environment, just let me know and I'll do my best.

@wzono
Copy link
Author

wzono commented May 21, 2021

I believe I'm experiencing a very similar issue. My project is based on CRA v4, using scss files, and CRACO to fix this bug with sass exports.

The project compiles just fine like the OP's example, and I get the same empty object for the imported stylesheet.
Module declarations are handled by CRA in this project.

Style Object Broken

Style Object Broken
Here's the plugin configuration:
image

Here's the SCSS file:
image

Here's the the TSX file showing the empty style object:
image

Style Object Working

Style Object Working
Here's the modified SCSS file not using the import:
image

Here's the same TSX file showing the working style object:
image

If you'd like me to put together a reproduction environment, just let me know and I'll do my best.

The bug about importing is similar to this #114 . You can try for it.

@kintz09
Copy link

kintz09 commented May 21, 2021

@wingsico Thank you for pointing me in the right direction! I was able to resolve my issue by reading through issue #114, #113, and #24.

I hadn't realized my import path was non-standard.

I was able to fix the issue just by changing the path but also through this plugin's options:

  1. Changing the import statement in the SCSS file to a relative path fixed the issue:
// PlayerInit.module.scss
// @import "Player/Lib/vlp-ui/sass/materialAnimation";
@import "../../../Lib/vlp-ui/sass/materialAnimation";
...
  1. Leaving the import as is but changing the plugin's options in tsconfig.json also fixed the issue:
// tsconfig.json
{
  "compilerOptions": {
    ...
    "baseUrl": "src",
    "plugins": [
      {
        "name": "typescript-plugin-css-modules",
        "options": {
          "classnameTransform": "camelCaseOnly",
          "rendererOptions": {
            "sass": {
              "includePaths": ["src"]
            }
          }
        }
      }
    ]
  },
  "include": ["src"]
}

Thanks again for your quick support!

@mrmckeb
Copy link
Owner

mrmckeb commented May 29, 2021

Thanks for this @wingsico, I think I've found the issue.

This fails:

.App {
  color: @blue;
}

This works:

@blue: rgb(0, 0, 255);

.App {
  color: @blue;
}

This is because the less rendered doesn't automatically pick up global variables, it processes each file independently.

There are a few solutions:

  1. Import files with variables whenever you use them.
  2. Define the variables in the tsconfig.json - but that's obviously a very bad solution, and not scalable.
  3. We can try to add a feature to this plugin to find/import these variables.

An example of the second option (again, I don't recommend this)

    "plugins": [
      {
        "name": "typescript-plugin-css-modules",
        "options": {
          "rendererOptions": {
            "less": {
              "globalVars": {
                "blue": "rgb(0, 0, 255)"
              }
            }
          }
        }
      }
    ]

What do you think would be a nice solution here for you?

I think option 3 is possible, we could possibly automatically append @import "[variable-file-1].less"; @import "[variable-file-2].less"; to the top of each file... you would just need to provide the relative paths.

@wzono
Copy link
Author

wzono commented Jun 2, 2021

Thanks for this @wingsico, I think I've found the issue.

This fails:

.App {
  color: @blue;
}

This works:

@blue: rgb(0, 0, 255);

.App {
  color: @blue;
}

This is because the less rendered doesn't automatically pick up global variables, it processes each file independently.

There are a few solutions:

  1. Import files with variables whenever you use them.
  2. Define the variables in the tsconfig.json - but that's obviously a very bad solution, and not scalable.
  3. We can try to add a feature to this plugin to find/import these variables.

An example of the second option (again, I don't recommend this)

    "plugins": [
      {
        "name": "typescript-plugin-css-modules",
        "options": {
          "rendererOptions": {
            "less": {
              "globalVars": {
                "blue": "rgb(0, 0, 255)"
              }
            }
          }
        }
      }
    ]

What do you think would be a nice solution here for you?

I think option 3 is possible, we could possibly automatically append @import "[variable-file-1].less"; @import "[variable-file-2].less"; to the top of each file... you would just need to provide the relative paths.

Thanks for your reply @mrmckeb .

Actually, the option 2 works for me (because my variables are defined in a common file). But I think the plugin can ignore this kind of problem, so that the type can be displayed normally.

@mrmckeb
Copy link
Owner

mrmckeb commented Jun 5, 2021

OK, thanks for the update @wingsico. I looked, and unfortunately I don't see an option in Less to build with errors. So if a variable can't be found, classname generation will fail.

If you're happy with that solution for now, can we close this issue? Or did I misunderstand?

@wzono
Copy link
Author

wzono commented Jun 6, 2021

OK, thanks for the update @wingsico. I looked, and unfortunately I don't see an option in Less to build with errors. So if a variable can't be found, classname generation will fail.

If you're happy with that solution for now, can we close this issue? Or did I misunderstand?

@mrmckeb OK, I will close it soon. Thanks for your help. :)

@wzono wzono closed this as completed Jun 6, 2021
@Carnia
Copy link

Carnia commented Jul 10, 2021

This is because the less rendered doesn't automatically pick up global variables, it processes each file independently.

There are a few solutions:

  1. Import files with variables whenever you use them.
  2. Define the variables in the tsconfig.json - but that's obviously a very bad solution, and not scalable.
  3. We can try to add a feature to this plugin to find/import these variables.

What do you think would be a nice solution here for you?

I think option 3 is possible, we could possibly automatically append @import "[variable-file-1].less"; @import "[variable-file-2].less"; to the top of each file... you would just need to provide the relative paths.

@mrmckeb
I think option3 is what I am looking for.
In my project, i used a webpack loader called style-resources-loader, its option looks like that:

{
    loader: "style-resources-loader",
    options: {
        patterns: path.resolve(__dirname, "../src/common/global-style/*.less"),
    },
},

its desciption:

This loader is a CSS processor resources loader for webpack, which injects your style resources (e.g. variables, mixins) into multiple imported css, sass, scss, less, stylus modules.
It's mainly used to:
share your variables, mixins, functions across all style files, so you don't need to @import them manually.
override variables in style files provided by other libraries (e.g. ant-design) and customize your own theme.

In this development environment, i will use global variables in many less files(without@import "***.less"),so I think Option 3 is the best。

@cat-walk
Copy link

cat-walk commented Aug 3, 2022

OK, thanks for the update @wingsico. I looked, and unfortunately I don't see an option in Less to build with errors. So if a variable can't be found, classname generation will fail.

If you're happy with that solution for now, can we close this issue? Or did I misunderstand?

Hey, is there any progress with this problem? I think many less user suffer from this.

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

No branches or pull requests

5 participants