developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8.06k stars 361 forks source link

All CSS-files are treated as CSS Modules #653

Closed artemtam closed 3 years ago

artemtam commented 4 years ago

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 commented 4 years ago

It seems this is rollup-plugin-postcss issue. https://github.com/egoist/rollup-plugin-postcss/issues/281

developit commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

katywings commented 4 years ago

Duplicate of #617 ;)

artemtam commented 4 years ago

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

FateRiddle commented 4 years ago

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 commented 4 years ago

@FateRiddle yupp :/, and we are not alone: https://github.com/egoist/rollup-plugin-postcss/issues/281#issuecomment-649499833

marvinhagemeister commented 4 years ago

@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 commented 4 years ago

@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 commented 4 years ago

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

olgenn commented 4 years ago

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

marvinhagemeister commented 4 years ago

@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 commented 4 years ago

It should be fixed when https://github.com/developit/microbundle/pull/680 gets merged in.

artemtam commented 4 years ago

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

wardpeet commented 4 years ago

Not yet, will be fixed when https://github.com/developit/microbundle/pull/738 is merged.

rafael-sotelo commented 3 years ago

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 commented 3 years ago

@rafael-sotelo https://github.com/developit/microbundle/issues/653#issuecomment-714378710

developit commented 3 years ago

The fix will be published in 0.13.

geocine commented 3 years ago

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

katywings commented 3 years ago

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

buddhaface commented 3 years ago

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 commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

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.