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

All CSS-files are treated as CSS Modules #653

Closed
artemtam opened this issue Jun 8, 2020 · 25 comments
Closed

All CSS-files are treated as CSS Modules #653

artemtam opened this issue Jun 8, 2020 · 25 comments
Labels
CSS has-fix A fix is available, but may not yet be released. upstream
Milestone

Comments

@artemtam
Copy link

artemtam commented Jun 8, 2020

Regardless --css-modules flag, it is impossible to use CSS and CSS Modules in the same project. It seems that null option works incorrectly and Rollup treats all CSS files as modules.

To reproduce, you could try to build this simple example:

import React from 'react';

import './index.css'
import styles from './index.module.css';

const App = () => (
  <div>
    <div className={styles.moduleClass}>CSS module class</div>
    <div className="local-no-module-class">No-module class</div>
  </div>
);

export default App;

Building with microbundle ./index.jsx makes the following index.css file:

._1fWqO{color:#00f}
._3umtn{color:red}

However, the expected result is:

._1fWqO{color:#00f}
.local-no-module-class{color:red}
@artemtam
Copy link
Author

artemtam commented Jun 8, 2020

It seems this is rollup-plugin-postcss issue. egoist/rollup-plugin-postcss#281

@developit
Copy link
Owner

developit commented Jun 8, 2020

Thanks for the pointer to that issue.

Just to clarify - the .local-no-module-class is coming from index.css?

I think it may be possible to work around this using :global{} in the interim:

:global {
  @import url('./index.css');
}

In terms of a longer term fix: we should be able to use the undocumented options.modules.getJSON() option in rollup-plugin-postcss:

https://github.com/egoist/rollup-plugin-postcss/blob/9489ecaabbf9a4726f67deb0095914d624e9d462/src/postcss-loader.js#L90

Another approach would be to generate a regex for globalModulePaths that matches everything except *.module.css.

@artemtam
Copy link
Author

artemtam commented Jun 8, 2020

Yes, .local-no-module-class comes from index.css.

The workaround is suitable only for local files – we can't adjust third-party dependencies. Actually, I faced this issue initially with third-party CSS.

IMO, the bug is pretty critical – it appeared in 0.12 and could break lots of projects. Clearly, rollup-plugin-postcss behavior is confusing, though it probably it makes sense to just not pass any options to the plugin, so the default class name generation algorithm will be used.

@artemtam
Copy link
Author

artemtam commented Jun 8, 2020

Ah, I am mistaken about the workaround. It probably should work, thank you.

@katywings
Copy link
Collaborator

Duplicate of #617 ;)

@artemtam
Copy link
Author

@katywings the title is confusing, so I didn't get it's the same issue :(

@FateRiddle
Copy link

I can't believe this is still open, so css modules is still the only acceptable form of css if I use microbundle? @artemtam @katywings
I come across this when using create-react-library (which use microbundle)

@katywings
Copy link
Collaborator

@FateRiddle yupp :/, and we are not alone: egoist/rollup-plugin-postcss#281 (comment)

@marvinhagemeister
Copy link
Collaborator

marvinhagemeister commented Jun 26, 2020

@FateRiddle This is an upstream bug in rollup-plugin-postcss. Nothing we can do about that in here microbundle.

EDIT: On second thought: What if we disable CSS modules and only enable it for .module.css files? If the docs are to be believed they have an autoModule flag that we should use instead.

@artemtam
Copy link
Author

@marvinhagemeister it is not about autoModules, the issue is in this line: https://github.com/egoist/rollup-plugin-postcss/blob/9489ecaabbf9a4726f67deb0095914d624e9d462/src/postcss-loader.js#L74

Basically, in case you pass anything in modules, all files are treated as CSS Modules. As far as I understand, Microbundle passes class names format via modules property. So it seems if Microbundle just doesn't pass it and sticks with the default names provided by rollup-plugin-postcss, the issue will be resolved.

@marvinhagemeister
Copy link
Collaborator

@artemtam yup, that's what I was referring to. Sounds like an easy enough fix 🎉

@olgenn
Copy link

olgenn commented Jul 1, 2020

This problem exists for me. When will the fix be made?

@marvinhagemeister
Copy link
Collaborator

marvinhagemeister commented Jul 1, 2020

@olgenn right above your comment are links to two PRs filed upstream to get this resolved. We will update microbundle once a fix is merged upstream.

@wardpeet
Copy link
Collaborator

wardpeet commented Sep 2, 2020

It should be fixed when #680 gets merged in.

@wardpeet wardpeet added the has-fix A fix is available, but may not yet be released. label Sep 5, 2020
@artemtam
Copy link
Author

Hi @wardpeet! #680 is closed, so the issue is fixed?

@wardpeet
Copy link
Collaborator

Not yet, will be fixed when #738 is merged.

@rafael-sotelo
Copy link

Hi Sorry, is this issue fixed ?

Today I created a library using "create-react-library" and I'm having this error, is there any workaround ?

I tried the

:global {
@import url('./index.css');
}

mentioned above, but not even sure where to put that

Thank you

@ajfranzoia
Copy link

@developit developit added the CSS label Dec 18, 2020
@developit
Copy link
Owner

The fix will be published in 0.13.

@developit developit added this to the 0.13.0 milestone Dec 18, 2020
@geocine
Copy link

geocine commented Jan 30, 2021

Is there any additional step needed for scss files? This is still not fixed for me I am using the CRL fork though

@katywings
Copy link
Collaborator

@geocine Do you have a quick example repo showing what you try to do? 🙂

@buddhaface
Copy link

buddhaface commented Oct 12, 2021

still doesn't work if you're changing the css-modules naming convention (e.g. --css-modules '_[name]__[local]__[hash:base64:5]' changes all css names, not just modularized css)

@leon-up9
Copy link

still doesnt work.
styles.sass is still using css modules.
only using --css-modules false works.

the problem is I want to support both by distctioning the name containing "module".
i saw postcss bug was fixed.

any workaround?

@buddhaface
Copy link

buddhaface commented Feb 12, 2023

still doesnt work. styles.sass is still using css modules. only using --css-modules false works.

the problem is I want to support both by distctioning the name containing "module". i saw postcss bug was fixed.

any workaround?

the best solution for me was to get rid of microbundle. it's been two years so this is obviously not going to be fixed.

@marvinhagemeister
Copy link
Collaborator

the best solution for me was to get rid of microbundle. it's been two years so this is obviously not going to be fixed.

That's the proper solution. We mainly wrote microbundle to make it easier to ship JS libraries like Preact itself. It was never intended to be used for full blown applications bundling with multiple asset types etc. That's not microbundle's use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CSS has-fix A fix is available, but may not yet be released. upstream
Projects
None yet
Development

No branches or pull requests