简体   繁体   中英

How to apply sass loaders correctly to apply styles

I am working on a project where I am using several sass files and I am calling them into one single file, the problem is that when I try to apply the styles they don't show up. sass files . Now this is how i configured my webpack.config.js file

const path = require("path");

module.exports = {
  mode: "development",
  entry: "./src/index.js",
  output: {
    filename: "main.js",
    path: path.resolve(__dirname, "dist"),
    assetModuleFilename: "images/[hash][ext][query]",
  },

  devtool: "inline-source-map",
  devServer: {
    contentBase: "./dist",
  },
  module: {
    rules: [
      {
        test: /\.(s[ac]|c)ss$/i,
        use: [
          // Creates `style` nodes from JS strings
          "style-loader",
          // Translates CSS into CommonJS
          "css-loader",
          // Compiles Sass to CSS
          "sass-loader",
        ],
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: "asset/resource",
        generator: {
          filename: "static/[hash][ext][query]",
        },
      },
    ],
  },
};

and this is the error i get

> restaurant@1.0.0 start /home/cvilla714/javascript/restaurant-page
> webpack serve --open

ℹ 「wds」: Project is running at http://localhost:8080/
ℹ 「wds」: webpack output is served from /
ℹ 「wds」: Content not from webpack is served from ./dist
ℹ 「wdm」: wait until bundle finished: /
✖ 「wdm」: asset main.js 943 KiB [emitted] (name: main)
runtime modules 1.25 KiB 6 modules
modules by path ./node_modules/ 345 KiB 26 modules
modules by path ./sass/*.scss 2.4 KiB
  ./sass/tabs.scss 366 bytes [built] [code generated]
  ./sass/main.scss 366 bytes [built] [code generated]
  ./node_modules/css-loader/dist/cjs.js!./node_modules/sass-loader/dist/cjs.js!./sass/tabs.scss 1.64 KiB [built] [code generated]
  ./node_modules/css-loader/dist/cjs.js!./node_modules/sass-loader/dist/cjs.js!./sass/main.scss 39 bytes [built] [code generated] [1 error]
modules by path ./src/*.js 4.48 KiB
  ./src/index.js 201 bytes [built] [code generated]
  ./src/tabs.js 2.42 KiB [built] [code generated]
  ./src/nav.js 1.86 KiB [built] [code generated]

ERROR in ./sass/main.scss (./node_modules/css-loader/dist/cjs.js!./node_modules/sass-loader/dist/cjs.js!./sass/main.scss)
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: expected "{".
  ╷
7 │ $font-dancingScript: "Dancing Script", cursive;
  │                                               ^
  ╵
  sass/_layout.scss 7:47  @import
  sass/main.scss 2:9      root stylesheet
 @ ./sass/main.scss 2:12-127 9:17-24 13:15-29
 @ ./src/nav.js 4:0-27
 @ ./src/index.js 3:0-31

webpack 5.18.0 compiled with 1 error in 2816 ms
ℹ 「wdm」: Failed to compile.

this is how I am importing the style to the project

import "../sass/main.scss";

I can't apply my styles to the project, I have been trying all day to fix this issue with no success, I would really appreciate if someone can please point me in the right direction.

Your Webpack config is correct. Error in your

$font-dancingScript: "Dancing Script", cursive;

adding ' must solve your problem;

$font-dancingScript: ' "Dancing Script", cursive ';

Config:

  {
    test: /\.css$/,
    use: ["style-loader", "css-loader"],
  },
  {
    test: /\.s[ac]ss$/i,
    use: ["style-loader","css-loader","sass-loader"],
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM