• 使用webpack处理scss、sass、styl资源


    1.下载scss、sass资源

    下载依赖包

    npm i sass-loader sass -D

    在这里一定要注意是下载两个依赖包

    功能介绍

    • sass-loader:负责将 Sass 文件编译成 css 文件
    • sasssass-loader 依赖 sass 进行编译

    配置:

    1. const path = require("path");
    2. module.exports = {
    3. entry: "./src/main.js",
    4. output: {
    5. path: path.resolve(__dirname, "dist"),
    6. filename: "main.js",
    7. },
    8. module: {
    9. rules: [
    10. {
    11. // 用来匹配 .css 结尾的文件
    12. test: /\.css$/,
    13. // use 数组里面 Loader 执行顺序是从右到左
    14. use: ["style-loader", "css-loader"],
    15. },
    16. {
    17. test: /\.less$/,
    18. use: ["style-loader", "css-loader", "less-loader"],
    19. },
    20. {
    21. test: /\.s[ac]ss$/,
    22. use: ["style-loader", "css-loader", "sass-loader"],
    23. },
    24. ],
    25. },
    26. plugins: [],
    27. mode: "development",
    28. };

    添加Sass资源

    • src/sass/index.sass
      1. /* 可以省略大括号和分号 */
      2. .box3
      3. width: 100px
      4. height: 100px
      5. background-color: hotpink

    • src/sass/index.scss
      1. .box4 {
      2. width: 100px;
      3. height: 100px;
      4. background-color: lightpink;
      5. }

      src/main.js

      1. import count from "./js/count";
      2. import sum from "./js/sum";
      3. // 引入资源,Webpack才会对其打包
      4. import "./css/index.css";
      5. import "./less/index.less";
      6. import "./sass/index.sass";
      7. import "./sass/index.scss";
      8. console.log(count(2, 1));
      9. console.log(sum(1, 2, 3, 4));

    • public/index.html
      1. <!DOCTYPE html>
      2. <html lang="en">
      3. <head>
      4. <meta charset="UTF-8" />
      5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      7. <title>webpack5</title>
      8. </head>
      9. <body>
      10. <h1>Hello Webpack5</h1>
      11. <div class="box1"></div>
      12. <div class="box2"></div>
      13. <div class="box3"></div>
      14. <div class="box4"></div>
      15. <script src="../dist/main.js"></script>
      16. </body>
      17. </html>

      最后再用

      npx webpack

      2.处理styl资源

    下载依赖包

    npm i stylus-loader -D

    功能介绍

    stylus-loader:负责将 Styl 文件编译成 Css 文件
    

    配置

    1. const path = require("path");
    2. module.exports = {
    3. entry: "./src/main.js",
    4. output: {
    5. path: path.resolve(__dirname, "dist"),
    6. filename: "main.js",
    7. },
    8. module: {
    9. rules: [
    10. {
    11. // 用来匹配 .css 结尾的文件
    12. test: /\.css$/,
    13. // use 数组里面 Loader 执行顺序是从右到左
    14. use: ["style-loader", "css-loader"],
    15. },
    16. {
    17. test: /\.less$/,
    18. use: ["style-loader", "css-loader", "less-loader"],
    19. },
    20. {
    21. test: /\.s[ac]ss$/,
    22. use: ["style-loader", "css-loader", "sass-loader"],
    23. },
    24. {
    25. test: /\.styl$/,
    26. use: ["style-loader", "css-loader", "stylus-loader"],
    27. },
    28. ],
    29. },
    30. plugins: [],
    31. mode: "development",
    32. };

    添加资源

    • src/styl/index.styl
      1. /* 可以省略大括号、分号、冒号 */
      2. .box
      3. width 100px
      4. height 100px
      5. background-color pink

    • src/main.js
      1. import { add } from "./math";
      2. import count from "./js/count";
      3. import sum from "./js/sum";
      4. // 引入资源,Webpack才会对其打包
      5. import "./css/index.css";
      6. import "./less/index.less";
      7. import "./sass/index.sass";
      8. import "./sass/index.scss";
      9. import "./styl/index.styl";
      10. console.log(count(2, 1));
      11. console.log(sum(1, 2, 3, 4));

    • public/index.html
      1. <!DOCTYPE html>
      2. <html lang="en">
      3. <head>
      4. <meta charset="UTF-8" />
      5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      7. <title>webpack5</title>
      8. </head>
      9. <body>
      10. <h1>Hello Webpack5</h1>
      11. <!-- 准备一个使用样式的 DOM 容器 -->
      12. <div class="box1"></div>
      13. <div class="box2"></div>
      14. <div class="box3"></div>
      15. <div class="box4"></div>
      16. <div class="box5"></div>
      17. <script src="../dist/main.js"></script>
      18. </body>
      19. </html>

      最后再

      npx webpack

      打开 index.html 页面查看效果

  • 相关阅读:
    Python 算法高级篇:递归与迭代的比较与应用
    如何用Python语音合成,以及文字转语音~
    uniapp - 微信小程序 - 自定义底部tabbar
    万得凯通过注册:年营收7.5亿 为钟兴富及其连襟家族企业
    【LeetCode】按要求补齐数组 [H](贪心)
    计算机毕业设计springboot+vue基本微信小程序的校园跑腿平台 uniapp
    当Map的值为NULL
    K8S中的ingress
    Dubbo—Admin 整体架构与安装步骤
    基于SpringCloud的面试刷题系统,项目经验统统搞定
  • 原文地址:https://blog.csdn.net/m0_68997646/article/details/127626539