-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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 Module does not work #70
Comments
Currently, you need to install {
"modules": true
} This is documented briefly at https://parceljs.org/transforms.html#postcss. It's necessary to do this because we don't want to include the classes in the JS bundle by default for people that aren't using CSS modules. Perhaps there is a better way we could detect CSS modules so we don't need to require that configuration (e.g. based on import statements maybe), so open to suggestions there. |
Thanks for your help, problem resolved. |
@micooz I tried it too but it didn't work, even after cloning your repo and adding the postCss. Do you mind creating a commit with the working version and post here the hash so I can clone it and see it working? Thx |
@meiriko Here it is: micooz/react-parcel-example@77d4959 You may need to upgrade |
Using the example provided by @micooz (and in my own testing), the styles seem to be removed as soon as the module hot reloads, and are not re-applied even on refresh. |
Almost the same as @Jayphen here, global styles are applied when the module hot-reloads, local ones do not, but when I refresh the page they are applied normally. |
I am getting the same results as @Jayphen and @thomasfortes, except I am using Stylus. postcss.config.jsmodule.exports = {
modules: true,
} index.jsimport React from 'react'
import {render} from 'react-dom'
import styles from './index.styl'
console.log(styles)
const App = () => <div className={styles.app}>Hello from React!</div>
render(<App />, document.getElementById('root')) index.styl.app {
font-family: sans-serif;
} When I run I am using parcle-bundler@1.4.1. |
Hi, I meet @jrop same problem here but seems it is a problem with hot reload. When I start with parcel --no-hmr src/index.html, it works as expected. |
I've noticed that when the --no-hmr flag is set that the css classes are transpiled correctly, and without the flag, the class names are the same as they are in source. With hot modules: |
Thanks @g33kChris. That piece of knowledge did it for me. Just use the name of the class itself, not the imported classname. That confused me a bit, especially since my IDE was complaining about the unused import statement for the css file. |
Is there a way to tell postcss to only transform ".css" files to css modules and not e.g. ".scss" files? Like an |
@devongovett not sure it's the same. But I'm still getting similar error saying: even after adding "postcss-modules" along with suggested postcss settings, everything things worked before with the same setup (without postcss-modules & relative settings). node v9.5.0 |
It turned out its caused by |
Is there a way to tell the In the long term, I assume fixing HMR mode so that it respects CSS Modules naming is the fix... UPDATE: The API docs for import ParcelBundler from 'parcel-bundler'
import express from 'express'
import proxy from 'http-proxy-middleware'
const parcel = new ParcelBundler('./static/index.html', { hmr: false })
const app = express()
app.use('/api', proxy({ target: 'http://localhost:8000' }))
app.use(parcel.middleware())
app.listen(Number(process.env.UI_PORT || 1234)) Again, this is a temporary fix until HMR with CSS Modules is fixed. |
It seems like you are implying that there's a significant overhead when importing classNames, in terms of bundle size and processing time. What other reason might be there not to make it a default? I love Parcel it offers great defaults and I'm sure I'm not the only one. CSS Modules are great default imo. |
You could do it the way https://poi.js.org/ does it, |
Parcel just works... unless you use something basic like CSS, then it requires complex configurations |
I landed on this question too after thinking that CSS Modules was broken somehow. I think my confusion stemmed from the fact that on the docs page for CSS, it says up top that:
But then it's clarified below that you need to install PostCSS if you want it to work. On my first read-through, it seemed like I only needed to install PostCSS if I wanted to use plugins like autoprefixer. I assumed based on the info at the top that if I just wanted to import a plain CSS file in Javascript, it would 'just work'. |
Also, even after installing PostCSS and creating a postcssrc file, my modules didn't work. I had to find another closed Github issue which told me to clear the |
@DeMoorJasper Is .postcssrc even being handled like .babelrc regarding cache invalidation? |
Not to undermine the purpose of this issue... I recommend a css-in-js solution instead of a style sheet. I use typestyle but there are others. Just a friendly tip. For most new app development cases. |
Managed to succeed thanks to @micooz |
My bad, installing |
remove webpack logic from sync-dynamic-import plugin Approved-by: Maia Teegarden
Thanks for your awesome tool.
parcel document gives a brief introduction to CSS Modules but I stuck at my first try to use it:
I quickly cloned a demo project, add
node-sass
, importindex.scss
as CSS Modules and want to see how it works but unfortunately it doesn't work.Minimal example to reproduce: micooz/react-parcel-example@44e8c2b
Why
I guess
require
does a wrong thing when interpret scss or css file as CSS Modules, it just returns an empty object here:The text was updated successfully, but these errors were encountered: