处理样式资源
Webpack 如何处理 Css、Less、Sass、Scss、Styl 样式资源
介绍
Webpack 本身是不能识别样式资源的,所以我们需要借助 Loader 来帮助 Webpack 解析样式资源
我们找 Loader 都应该去官方文档中找到对应的 Loader,然后使用
官方文档找不到的话,可以从社区 Github 中搜索查询
Webpack 官方 Loader 文档open in new window
处理 Css 资源
1. 下载包
1
| npm i css-loader style-loader -D
|
注意:需要下载两个 loader
2. 功能介绍
css-loader
:负责将 Css 文件编译成 Webpack 能识别的模块
style-loader
:会动态创建一个 Style 标签,里面放置 Webpack 中 Css 模块内容
此时样式就会以 Style 标签的形式在页面上生效
3. 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| const path = require("path");
module.exports = { entry: "./src/main.js", output: { path: path.resolve(__dirname, "dist"), filename: "main.js", }, module: { rules: [ { test: /\.css$/, use: ["style-loader", "css-loader"], }, ], }, plugins: [], mode: "development", };
|
4. 添加 Css 资源
1 2 3 4 5
| .box1 { width: 100px; height: 100px; background-color: pink; }
|
1 2 3 4 5 6 7
| import count from "./js/count"; import sum from "./js/sum";
import "./css/index.css";
console.log(count(2, 1)); console.log(sum(1, 2, 3, 4));
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>webpack5</title> </head> <body> <h1>Hello Webpack5</h1> <div class="box1"></div> <script src="../dist/main.js"></script> </body> </html>
|
5. 运行指令
打开 index.html 页面查看效果
处理 Less 资源
1. 下载包
2. 功能介绍
less-loader
:负责将 Less 文件编译成 Css 文件
3. 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| const path = require("path");
module.exports = { entry: "./src/main.js", output: { path: path.resolve(__dirname, "dist"), filename: "main.js", }, module: { rules: [ { test: /\.css$/, use: ["style-loader", "css-loader"], }, { test: /\.less$/, use: ["style-loader", "css-loader", "less-loader"], }, ], }, plugins: [], mode: "development", };
|
4. 添加 Less 资源
1 2 3 4 5
| .box2 { width: 100px; height: 100px; background-color: deeppink; }
|
1 2 3 4 5 6 7 8
| import count from "./js/count"; import sum from "./js/sum";
import "./css/index.css"; import "./less/index.less";
console.log(count(2, 1)); console.log(sum(1, 2, 3, 4));
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>webpack5</title> </head> <body> <h1>Hello Webpack5</h1> <div class="box1"></div> <div class="box2"></div> <script src="../dist/main.js"></script> </body> </html>
|
5. 运行指令
打开 index.html 页面查看效果
处理 Sass 和 Scss 资源
1. 下载包
1
| npm i sass-loader sass -D
|
注意:需要下载两个
2. 功能介绍
sass-loader
:负责将 Sass 文件编译成 css 文件
sass
:sass-loader
依赖 sass
进行编译
3. 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| const path = require("path");
module.exports = { entry: "./src/main.js", output: { path: path.resolve(__dirname, "dist"), filename: "main.js", }, module: { rules: [ { test: /\.css$/, use: ["style-loader", "css-loader"], }, { test: /\.less$/, use: ["style-loader", "css-loader", "less-loader"], }, { test: /\.s[ac]ss$/, use: ["style-loader", "css-loader", "sass-loader"], }, ], }, plugins: [], mode: "development", };
|
4. 添加 Sass 资源
1 2 3 4 5
| .box3 width: 100px height: 100px background-color: hotpink
|
1 2 3 4 5
| .box4 { width: 100px; height: 100px; background-color: lightpink; }
|
1 2 3 4 5 6 7 8 9 10
| import count from "./js/count"; import sum from "./js/sum";
import "./css/index.css"; import "./less/index.less"; import "./sass/index.sass"; import "./sass/index.scss";
console.log(count(2, 1)); console.log(sum(1, 2, 3, 4));
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>webpack5</title> </head> <body> <h1>Hello Webpack5</h1> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="box4"></div> <script src="../dist/main.js"></script> </body> </html>
|
5. 运行指令
打开 index.html 页面查看效果
处理 Styl 资源
1. 下载包
2. 功能介绍
stylus-loader
:负责将 Styl 文件编译成 Css 文件
3. 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| const path = require("path");
module.exports = { entry: "./src/main.js", output: { path: path.resolve(__dirname, "dist"), filename: "main.js", }, module: { rules: [ { test: /\.css$/, use: ["style-loader", "css-loader"], }, { test: /\.less$/, use: ["style-loader", "css-loader", "less-loader"], }, { test: /\.s[ac]ss$/, use: ["style-loader", "css-loader", "sass-loader"], }, { test: /\.styl$/, use: ["style-loader", "css-loader", "stylus-loader"], }, ], }, plugins: [], mode: "development", };
|
4. 添加 Styl 资源
1 2 3 4 5
| .box width 100px height 100px background-color pink
|
1 2 3 4 5 6 7 8 9 10 11 12
| import { add } from "./math"; import count from "./js/count"; import sum from "./js/sum";
import "./css/index.css"; import "./less/index.less"; import "./sass/index.sass"; import "./sass/index.scss"; import "./styl/index.styl";
console.log(count(2, 1)); console.log(sum(1, 2, 3, 4));
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>webpack5</title> </head> <body> <h1>Hello Webpack5</h1> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="box4"></div> <div class="box5"></div> <script src="../dist/main.js"></script> </body> </html>
|
5. 运行指令
打开 index.html 页面查看效果