Skip to content

[Feature] add CSS Modules (postcss-modules) support #227

Closed

Activity

  1. added this to the milestone on May 10, 2017
  2. changed the title [-][2.1.0] Add CSS Module Support[/-] [+][2.1.0] Add CSS Module Support (`postcss-modules`)[/+] on May 10, 2017
  3. felixsanz commented on May 18, 2017

    @felixsanz

    @michael-ciniawsky Looks like with 2.x my CSS modules are now broken. Is this related?

    Is there a workaround? Thanks!

  4. michael-ciniawsky commented on May 18, 2017

    @michael-ciniawsky
    ContributorAuthor

    @felixsanz Are you using LoaderOptionsPlugin in your webpack.config.js? Normally it shouldn't affect the CSS Modules implementation of css-loader, but this was claimed a few times now. #232 #222

  5. TrySound commented on May 18, 2017

    @TrySound

    Shouldn't postcss-modules use messages for output instead of callback for this feature?
    Btw, this works great in modular-css

  6. felixsanz commented on May 18, 2017

    @felixsanz

    @michael-ciniawsky I'm using css-loader's modules: true option. I'm not using LoaderOptionsPlugin anywhere. I'll continue in #232

  7. changed the title [-][2.1.0] Add CSS Module Support (`postcss-modules`)[/-] [+][2.1.0] Add CSS Modules(`postcss-modules`)[/+] on May 25, 2017
  8. changed the title [-][2.1.0] Add CSS Modules(`postcss-modules`)[/-] [+][2.1.0] Add CSS Modules (`postcss-modules`)[/+] on May 25, 2017
  9. 5 remaining items

  10. michael-ciniawsky commented on Oct 30, 2017

    @michael-ciniawsky
    ContributorAuthor

    I don't think so, show your config

  11. michael-ciniawsky commented on Oct 30, 2017

    @michael-ciniawsky
    ContributorAuthor

    When did it exactly stop working?

  12. michael-ciniawsky commented on Oct 30, 2017

    @michael-ciniawsky
    ContributorAuthor

    You can't really use the postcss-modules plugin with webpack. It purpose is for other build tools like gulp/grunt. CSS Modules are handled by css-loader which is responsible for exporting the locals correctly

    import styles from './file.css' // <= only works with 'css-loader' atm
    
    export default (props) => (
      <div className={styles.container}></div>
    )
    import './file.css'
    import styles from './modules.json' // <= `postcss-modules` (Crap, don't use it)
    
    export default (props) => (
      <div className={styles.container}></div>
    )
  13. michael-ciniawsky commented on Oct 30, 2017

    @michael-ciniawsky
    ContributorAuthor

    That's why this issue exists 😛

  14. michael-ciniawsky commented on Oct 30, 2017

    @michael-ciniawsky
    ContributorAuthor

    You are using postcss.config.js atm ? Remove the postcss-modules plugin from it and change your webpack.config.js to the following.

     {
       test: /\.css$/,
       use: env === 'production' 
        ? ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            { 
              loader: 'css-loader',
              options: { 
                modules: true,
                localIdentName: '[name]__[local]___[hash:base64:5]',
                importLoaders: 1
              },
              'postcss-loader'
           ]
         }),
         : [ // `style-loader` pitches the HMR Code and enables HMR
           { loader: 'style-loader', options: { sourceMap: true } },
           { 
             loader: 'css-loader',
             options: { 
               modules: true,
               localIdentName: '[name]__[local]___[hash:base64:5]',
               importLoaders: 1,
               sourceMap: true
             },
             { loader: 'postcss-loader', options: { sourceMap: true } }
        ],
        include: [
          path.resolve(__dirname, 'src')
        ]
     }

    Also remove your custom HMR Code from the component,style-loader adds the necessary HMR Code automatically. HMR doesn't work with the extract-text-webpack-plugin (full reload only in watch mode)

  15. michael-ciniawsky commented on Nov 1, 2017

    @michael-ciniawsky
    ContributorAuthor
    if (process.env.NODE_ENV !== 'production') {
      if (module.hot) {
        const reloadTheme = require('provide-theme').reloadTheme;
    
        module.hot.accept([
          './root.css',
          './App.css',
          './Foo.css',
          './Bar.css'
        ], () => {
          reloadTheme('BasicTheme', {
            classes: {
    -          ...require('./root.css'),
    +          ...require('./root.css').default,
    -          ...require('./App.css'),
    +          ...require('./App.css').default,
    -          ...require('./Foo.css'),
    +          ...require('./Foo.css').default,
    -          ...require('./Bar.css')
    +          ...require('./Bar.css').default
            }
          });
        });
      }
    }
  16. michael-ciniawsky commented on Nov 1, 2017

    @michael-ciniawsky
    ContributorAuthor

    from.js

    const x = {}
    
    export default x

    toCJS.js

    const x = require('./from.js').default

    toESM.js

    import x from './from.js'
  17. michael-ciniawsky commented on Nov 1, 2017

    @michael-ciniawsky
    ContributorAuthor

    Otherwise please post your issue on e.g StackOverflow as it is unrelated to this issue and postcss-loader

  18. removed this from the milestone on Feb 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions