Webpack不会用背景图像编译我的SASS

时间:2019-12-22 20:33:33

标签: javascript node.js npm webpack sass

嗨,我在我的SASS代码编译中遇到了麻烦。 我有一个简单的应用程序,我想在其中使用背景图片,但是当我尝试使用它时,所有的CSS都停止工作。

我的SASS文件如下

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

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

我的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 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

如何停止这种行为并使之起作用?谢谢

我尝试将resolve-url-loader添加到我的webpack配置中,但没有任何变化。

1 个答案:

答案 0 :(得分:1)

您应该使用文件加载器来处理文件

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"
            }
        ]

您可以从here

查看代码