2

I have an external css imports inside one of my components -

import "rc-slider/assets/index.css";

However, when building with webpack, the css in not being registered. I've tried adding an import prefixed with a tilde @import '~rc-slider/assets/index.css'; in my components module.css file, but that does not work. I've also tried adding include: [path.join(__dirname, '..', 'node_modules')], to my webpack.config.js file, and it results in a failed build with multiple errors that say You may need an appropriate loader to handle this file type. for each of my files.

My webpack.config.js file is the following:

const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
    .BundleAnalyzerPlugin;
const HtmlWebPackPlugin = require("html-webpack-plugin");
const autoprefixer = require("autoprefixer");
const path = require("path");

module.exports = {
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.html$/,
                use: [
                    {
                        loader: "html-loader"
                    }
                ]
            },
            {
                test: /\.(scss|css)$/,
                use: [
                    {
                        loader: "style-loader"
                    },
                    {
                        loader: "css-loader",
                        options: {
                            importLoaders: 2,
                            modules: true,
                            localIdentName: "[name]__[local]__[hash:base64:5]"
                        }
                    },
                    {
                        loader: "sass-loader"
                    },
                    {
                        loader: "postcss-loader",
                        options: {
                            ident: "postcss",
                            plugins() {
                                return [autoprefixer];
                            }
                        }
                    },
                ]
            },
            {
                test: /\.(png|jp(e*)g|gif|svg)$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            limit: 8000,
                            name: "images/[hash]-[name].[ext]"
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebPackPlugin({
            template: "./src/index.html"
        }),
        new BundleAnalyzerPlugin()
    ]
};

This has taken up half my day already, so any input would be of huge help. Thank you.

2 Answers 2

3

After a few more hours of trial and error, I found a solution. For anyone encountering this issue in the future, I fixed it by adding the following test to my webpack.config.js file -

{
    test: /\.(scss|css)$/,
    include: [CODEMIRROR_PATH],
    use: [
        {
            loader: "style-loader"
        },
        {
            loader: "css-loader",

        },
    ]
},

My fill config setup is the following -

const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
    .BundleAnalyzerPlugin;
const HtmlWebPackPlugin = require("html-webpack-plugin");
const autoprefixer = require("autoprefixer");
const path = require("path");
const CODEMIRROR_PATH = path.resolve(
    __dirname,
    "./node_modules/rc-slider/assets/index.css"
);

module.exports = {
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.html$/,
                use: [
                    {
                        loader: "html-loader"
                    }
                ]
            },
            {
                test: /\.(scss|css)$/,
                include: [CODEMIRROR_PATH],
                use: [
                    {
                        loader: "style-loader"
                    },
                    {
                        loader: "css-loader",

                    },
                ]
            },
            {
                test: /\.(scss|css)$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: "style-loader"
                    },
                    {
                        loader: "css-loader",
                        options: {
                            importLoaders: 2,
                            modules: true,
                            localIdentName: "[name]__[local]__[hash:base64:5]"
                        }
                    },


                    {
                        loader: "sass-loader"
                    },
                    {
                        loader: "postcss-loader",
                        options: {
                            ident: "postcss",
                            plugins() {
                                return [autoprefixer];
                            }
                        }
                    }
                ]
            },
            {
                test: /\.(png|jp(e*)g|gif|svg)$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            limit: 8000,
                            name: "images/[hash]-[name].[ext]"
                        }
                    }
                ]
            }
        ]
    },    
    plugins: [
        new HtmlWebPackPlugin({
            template: "./src/index.html"
        }),
        new BundleAnalyzerPlugin()
    ]
};
Sign up to request clarification or add additional context in comments.

4 Comments

Great to see that you fixed your problem. May I suggest renaming CODEMIRROR_PATH? You're obviously not using CodeMirror, so perhaps a different name here is appropriate. :)
Absolutely. Thanks for the heads up.
A few years late, but thank you for posting your solution. This helped me out quite a bit.
Glad it helped!
0

Using the include property is definitely the way to go.

I have something similar in my code, and I used this:

{
  test: /\.css$/,
  include: [CODEMIRROR_PATH],
  use: ["style-loader", "css-loader"]
},

I'm including CodeMirror; as such, I define CODEMIRROR_PATH as follows:

const CODEMIRROR_PATH = path.resolve(__dirname, "./node_modules/codemirror");

Does something similar not work for you?

3 Comments

Unfortunately, when I add the include, all of my css files modules do not parse and I get the error: Module parse failed: Unexpected token (1:5) You may need an appropriate loader to handle this file type.. I am not sure why including a package in the node_modules folder is causing the css loaders to fail.
@Sean I think there's more to this issue than that. Do you mind updating your question to include the full error log after "Unexpected token"?
I found a solution and posted. Thank you for steering me in the right direction. Your approach worked, but I needed to add another css-loader and style-loader test, so that I had one which excludes the node_modules folder, and one that includes the specific css file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.