3
votes

I have issue compiling my SASS code. I would like to use a background image, but when I tried this, all my css stopped working.

My SASS file looks like this

@import "~bootstrap/scss/bootstrap";
@import "constants";

#crossroad {
  position:relative;
  background-image: url("../assets/bg.jpg");
}

My Webpack.config

const path = require('path');

module.exports = {
  entry: './src/app.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  mode: "development",
  module: {
    rules: [
      {
        test: /\.(scss|png|jpg)$/,
        use: [
          {
            // Adds CSS to the DOM by injecting a `<style>` tag
            loader: 'style-loader'
          },
          {
            // Interprets `@import` and `url()` like `import/require()` and will resolve them
            loader: 'css-loader'
          },
          {
            // Loader for webpack to process CSS with PostCSS
            loader: 'postcss-loader',
            options: {
              plugins: function () {
                return [
                  require('autoprefixer')
                ];
              }
            }
          },
          {
            // Loads a SASS/SCSS file and compiles it to CSS
            loader: 'resolve-url-loader'
          },
          {
            // Loads a SASS/SCSS file and compiles it to CSS
            loader: 'sass-loader'
          }
        ]
      }
    ]
  }
};

ERROR MESSAGE:

ERROR in ./src/assets/bg.jpg (./node_modules/css-loader/dist/cjs.js!./node_modules /postcss-loader/src??ref--4-2!./node_modules/resolve-url-loader!./node_modules/sass-loader/dist/cjs.js!./src/assets/bg.jpg)
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: Invalid CSS after "����": expected "{", was ""
on line 1 of /Users/adrianmindek/Code/PersonalPage/src/assets/bg.jpg

����
^
@ ./src/assets/bg.jpg 2:26-236
@ ./node_modules/css-loader/dist/cjs.js!./node_modules/postcss-loader/src??ref-- 4-2!./node_modules/resolve-url-loader!./node_modules/sass-loader/dist/cjs.js!./src/scss/app.scss
@ ./src/scss/app.scss
@ ./src/app.js

I tried to add resolve-url-loader to my webpack config, but nothing changed.

1

1 Answers

1
votes

You should handle file using file-loader like this

rules: [{
                test: /\.scss$/,
                use: [
                    'style-loader',
                    MiniCssExtractPlugin.loader,
                    {
                        loader: "css-loader",
                        options: {
                            minimize: true,
                            sourceMap: true
                        }
                    },
                    {
                        loader: "sass-loader"
                    }
                ]
            },
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loader: ["babel-loader", "eslint-loader"]
            },
            {
                test: /\.(jpe?g|png|gif)$/i,
                loader: "file-loader"
            },
            {
                test: /\.(woff|ttf|otf|eot|woff2|svg)$/i,
                loader: "file-loader"
            }
        ]

You can view code from here