Skip to content

Migrate to webpack2 and css modules is not working #489

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

Closed
aj120426394 opened this issue Apr 15, 2017 · 2 comments
Closed

Migrate to webpack2 and css modules is not working #489

aj120426394 opened this issue Apr 15, 2017 · 2 comments
Assignees

Comments

@aj120426394
Copy link

I move one of my project to webpack 2 recently. I currently only test on webpack-dev-server mode and seems every things work fine except css module.
Here is part of my webpack configuration which relate to css module:

version:
css-loader: 0.14.5 (for the better performance)
sass-loader: 6.0.3
webpack: 2.3.3
webpack-dev-server: 2.4.2

module:{
    rules: [
        {
            test: /\.css$/,
            loader: extractCSS.extract({
                fallback: 'style-loader',
                use: [
                    {
                        loader: 'css-loader',
                        options: {
                            sourceMap: env === 'development'
                        }
                    },
                    postCssLoader
                ]
            })
        },
        {
            test: /\.scss$/,
            use: [
                'style-loader',
                {
                    loader: 'css-loader',
                    options: {
                        sourceMap: env === 'development',
                        modules: true,
                        importLoaders: 3,
                        localIdentName: '[hash:base64]-[name]-[local]'
                    }
                },
                postCssLoader,
                {
                    loader: 'resolve-url-loader'
                },
                {
                    loader: 'sass-loader',
                    options: {
                        sourceMap: true,
                        includePaths: []
                    }
                }
            ]
        }
    ]
}

When I import the .scss file into .js file as beneath, the className of div shows nothing, and the className of h4 only show that class='text-white'. I tried to console.log out the imported variable and it shows an empty object.

import styles from '../style/layout/_l-header.scss';
<div className={styles.header}>
      <h4 className={classnames(styles.header__title, 'text-white')}>
           testing
       </h4>
</div>

I did another testing which make an error code in '_l-header.scss' that can generate some error message in Terminal to prove I use the right configuration of css-module. And here is the error message:

ERROR in ./~/css-loader?{"sourceMap":true,"modules":true,"importLoaders":3,"localIdentName":"[hash:base64]-[name]-[local]"}!./~/postcss-loader?{}!./~/resolve-url-loader!./~/sass-loader/lib/loader.js?{"sourceMap":true,"includePaths":[]}!./app/style/layout/_l-header.scss
Module build failed: 
  @extend .blue;
         ^
      ".header" failed to @extend ".blue".

According to the message above, I think the loader configuration is correct. Does anyone engage the same issue, or have some solution for this?

I'll really appreciate that!!

@michael-ciniawsky
Copy link
Member

css-loader: 0.14.5 (for the better performance)

😛 That's the reason I guess, if I remember right css-loader < v0.15.0 didn't use PostCSS and therefore has no { modules: true } (CSSModules), could you try with latest css-loader and see if your config works? You're right css-loader has some perf issues, but these are more related to the bloated code base and we're working on that 😉

@aj120426394
Copy link
Author

Thanks @michael-ciniawsky !

After I update to the latest css-loader version, it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants