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

CSS @import does not work with absolute paths (node_modules) #614

Open
2 of 3 tasks
rschristian opened this issue May 10, 2021 · 7 comments
Open
2 of 3 tasks

CSS @import does not work with absolute paths (node_modules) #614

rschristian opened this issue May 10, 2021 · 7 comments
Labels
bug Something isn't working CSS

Comments

@rschristian
Copy link
Member

rschristian commented May 10, 2021

Edit: Problem was more widespread than I thought.

Describe the bug
CSS @import syntax only works with relative paths within the public directory. Anything else is a no-go. For example:

  • Importing from node_modules
  • Importing from src (alias)
  • Having a dependency that uses @import

are all things that you cannot do with WMR.

To Reproduce
Created a simple repo that shows off with the src alias: https://github.com/rschristian/wmr-css-import-syntax-paths

Obviously other methods fail too, though they fail for the same reason.

Expected behavior
@import syntax is supported fully. The biggest problem is with node_modules using it breaking, as that's mostly out of the control of the user. They'd need to slip in with a post-install script and cut that out else WMR will have issues.

Desktop (please complete the following information):

  • Node Version: 14.16.0
  • WMR Version: 2.1.0
@rschristian rschristian added the bug Something isn't working label May 10, 2021
@rschristian rschristian changed the title When a node module has a CSS file that imports another module's CSS, the path is treated as relative, rather than absolute CSS @import does not work with absolute paths (node_modules & aliases) or relative paths pointed outside public directory May 14, 2021
@ghost
Copy link

ghost commented May 25, 2021

import "./style.module.css" doesn't work either.
I have a module with GUI components that has import "./styles.module.css" in the index.js. When I try to use this module in my program, I get an error:

Could not load ./node_modules/my-gui-toolkit/styles.module.css (imported by npm/my-gui-toolkit@1.0.0/index.js): local access not allowed

@rschristian
Copy link
Member Author

@makoven Is that something in node_modules using import "./style.module.css"? If so that's an invalid import and the error is expected and not an issue. Node modules shouldn't be shipped with imports that require a CSS loader like that.

@ghost
Copy link

ghost commented May 25, 2021

@rschristian It's illegal to import css file in js too :) But this does not stop anyone. There is no other way to distribute a components library with real CSS modules other than to leave them in their original form. So why not?

@rschristian
Copy link
Member Author

@makoven It's acceptable to use CSS imports for web apps, yes, as you know the environment in which it will be used. You can't distribute a module knowing where it's used, however, so that is not something you can do. The error is correct here.

The correct way to distribute a component built with CSS modules is to replaces usages with generated hashes for the class names. Replacing in the component and in the CSS file means you can consume as if it's any other CSS, while ensuring no collisions due to the hashes. Take a look at microbundle if you'd like an example of a tool that'll handle this.

@ghost
Copy link

ghost commented May 25, 2021

@rschristian thanks for pointing out the microbundle

@developit developit added the CSS label Jun 12, 2021
@marvinhagemeister
Copy link
Member

FYI: The aliasing or importing modules outside the public directory feature has landed in main. It will be released as part of 3.0.0. The node_module resolution will take a bit more work to get right, but it's on my mind 👍

@marvinhagemeister marvinhagemeister changed the title CSS @import does not work with absolute paths (node_modules & aliases) or relative paths pointed outside public directory CSS @import does not work with absolute paths (node_modules) Jun 25, 2021
@zgoda
Copy link

zgoda commented Sep 29, 2021

Importing CSS from node_modules does not work in SASS/SCSS too - SASS/SCSS locations are resolved fine, the CSS breaks. I already filed #860 but if you confirm it's the same issue I will close it as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working CSS
Projects
None yet
Development

No branches or pull requests

4 participants