05-webpack打包less或sass资源

  1. 下载插件
    less 下载 less包和less-loader
    sass 下载node-sass和sass-loader

  2. 使用插件
    webpack.config.js

 module: {                  //css打包规则
        rules: [{
            test: /\.css$/,        //把项目中所有以.css结尾的文件打包,插入到html里
            use: ["style-loader","css-loader"]  //css兼容loader,单独的css文件
        }, {
            test: /\.less$/,
            use: ["style-loader","css-loader","less-loader"]   //从右到左,内联样式
        },{
            test: /\.scss$/,
            use: ["style-loader","css-loader","sass-loader"]
        }]
    },
  1. 目录结构
    lessstyle.less
@width:200px;
@height:200px;
@color:red;

body {
  margin: 0;
  padding: 0;
}
p {
  color: @color;
  font-size: 25px;
}
h1 {
  color: blue;
  font-size: 88px;
}
.box2 {
  width: @width;
  height: @height;
  background-color: @color;
}

sassstyle.scss

$w:50px;
$h:100px;
.box3 {
  width: $w;
  height: $h * 3;
  background-color: greenyellow;
  color: bisque;
}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>商城首页~~~~~~</h1>
<p>打包css</p>
<div class="box">
    this is a box1
</div>
<div class="box2">
    this is a box2
</div>
<div class="box3">
    this is a box3
</div>
</body>
</html>

index.js

require("../css/style.css")
require("../css/lessstyle.less")
require("../css/sassstyle.scss")
console.log("首页专用js文件");
  1. 执行webpack
    html页面
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值