2

I've inherited a web app that uses webpack. In my app, I have a directory called "pub", which looks like this:

./pub
    /styles
      app.css
    /images
      brand.png

I have been trying unsuccessfully all morning to use these via webpack. In my webpack.config.js file, I have the following:

const path = require('path');

const projectRoot = path.resolve(__dirname, '../');

module.exports = {
  entry: {
    app: './src/index.js',
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/, 
        loader: "style-loader!css-loader"
      },
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 8192
            }
          }
        ]
      }      
    ]
  }
};

Then, in my index.js file, I have the following:

import logoImage from './public/images/brand.png';
require("css!./public/css/app.css");

When I run webpack, I receive an error that says:

BREAKING CHANGE: It's no longer allowed to omit the '-loader' suffix when using loaders.
                 You need to specify 'css-loader' instead of 'css',
                 see https://webpack.js.org/guides/migrating/#automatic-loader-module-name-extension-removed

I don't really understand this error. When I look at it, and then I look at my webpack.config.js file, it looks to me like I'm using css-loader. Beyond that though, how do I use a style in my webpage once the require statement is working. I'm just trying to use webpack with a web app and want to import my brand and CSS and I can't figure it out.

2 Answers 2

2

You don't need the css! in your require statement

require("css!./public/css/app.css");

You can just use

require("./public/css/app.css");

Because you are testing files with:

{
    test: /\.css$/, // <-- here
    loader: "style-loader!css-loader"
  },


Or without the rule in your webpack config

// No test in rules matched but you tell webpack
// explicitly to use the css loader
require("style-loader!css-loader!./public/css/app.css");
Sign up to request clarification or add additional context in comments.

Comments

0

Your hierarchy is pub/styles/app.css but the location you use in your require is public/css/app.css. It looks like you're trying to call your css from the wrong location.

If this doesn't solve your issue, check out this link https://webpack.github.io/docs/stylesheets.html

The first step on that page is to install css-loader and configure it, this might be a good place to start.

Comments

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.