Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

javascript - 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

> [email protected] 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.

question from:https://stackoverflow.com/questions/65933467/how-to-apply-sass-loaders-correctly-to-apply-styles

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...