-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 Modules in Webpacker 3.0? #775
Comments
I would like to know how to enable CSS Modules in Webpacker 3.0 as well. I was actually just about to open a new issue when I found this one that is very recent. You said you had them working on 2.0. I am brand new to Webpack(er), and so this is probably a silly question, but... To my understanding, on 2.0 the loaders were created by Webpacker at the time of install and lived in What happens when you create a loader file with the same name in The docs for Webpacker don't seem super helpful. They mention adding a new loader (in Also, how do you define what loaders are ran? Is it in the config somewhere, or is it that all |
Please see this comment: #756 (comment) Does this help? |
@gauravtiwari bingo! That fixed it. Opened a PR to update the docs to put this in as a recipe (many people want CSS modules I suspect): #777 |
I'm using the exact code linked to by guaravtiwari above. The hashed CSS-class is rendered on the element, but no styling is applied. When logging This is what I have in the file Hello.css:
|
The same with me. |
Chiming in here. CSS modules worked pretty much out of the box with Webpacker 2. I totally understand that CSS modules isn't Webpacker's responsibility, but it would be super nice if someone could provide a working code sample for CSS modules to smoothen the transition from Webpacker 2 to 3. |
@gauravtiwari I guess, we both did as described at https://github.com/rails/webpacker/blob/master/docs/webpack.md#overriding-loader-options-in-webpack-3-for-css-modules-etc
|
Just tested seems to work fine for me. The environment.js has this: const { environment } = require('@rails/webpacker')
const merge = require('webpack-merge')
const myCssLoaderOptions = {
modules: true,
sourceMap: true,
localIdentName: '[name]__[local]___[hash:base64:5]'
}
const CSSLoader = environment.loaders.get('style').use.find(el => el.loader === 'css-loader')
CSSLoader.options = merge(CSSLoader.options, myCssLoaderOptions)
module.exports = environment Works with both watcher and dev server. |
Btw you are referencing styles within your components right? import styles from './style.sass'
class HelloReact extends React.Component {
componentDidMount() {
this._node.style.fontSize = '25px'
}
handleOnClick = () =>
console.log('I got clicked')
render () {
return (
<div className={styles['hello-react']}>
<p ref={node => (this._node = node)}>Hello {this.props.name}!</p>
<img onClick={this.handleOnClick} src={logo} className={styles['react-logo']} alt="React" />
</div>
)
}
} |
@gauravtiwari thank you very much! I've investigated your example and found that I forgot to add a line declaring pack's stylesheets.
|
No problem 👍 Glad it's fixed 😄 |
Stresses that you need to use the stylesheet pack tag, based on further confusion around rails#775
Stresses that you need to use the stylesheet pack tag, based on further confusion around #775
That solved it for me too, thank you so much @gauravtiwari for the patience. I've got nobody to blame but myself, but I think it was the initial commented lines in the generated react file that made me to stop thinking (it only mentions the
|
I am trying to style an external library through the CSS loader instead of using global styles - arqex/react-datetime#439 . I am using the given loader code (from above discussion) in my import styles from 'react-datetime/css/react-datetime.css'
import Datetime from 'react-datetime';
import React from 'react';
export default class EventSearchForm extends React.Component {
render() {
return (
<div>
<Datetime className={styles['rdt']} />
<Datetime />
</div>
);
}
} The css-loader component appears to be working. The generated class name is there and it links to the included style sheet if I include the <%= javascript_pack_tag 'application' %>
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> It looks like only the top level element's style is being included though, only the Supposedly the default style-loader should take care of this, but it's not working. I printed out my style loaders in environment.js (
So I'd think style, sass, css etc loader should be working fine, but the styles are not being included. |
Stresses that you need to use the stylesheet pack tag, based on further confusion around rails/webpacker#775
Stresses that you need to use the stylesheet pack tag, based on further confusion around rails/webpacker#775
Stresses that you need to use the stylesheet pack tag, based on further confusion around rails/webpacker#775
Hi there,
I had CSS modules working with Webpacker 2.x, but when upgrading to 3 they just stop working. No errors or anything, I just get empty objects when importing a CSS file.
Here's my
loaders/style.js
:So I set modules to true. However, when compiling, I still get this:
So I don't see my options applied to the
css-loader
here.This happened also with
sass.js
as the loader - I switched tostyles.js
based on this loader.Any idea why I can't get modules to play nice here? Thank you!
The text was updated successfully, but these errors were encountered: