处理样式资源
使用 Webpack 如何处理 Css、Less、Sass、Scss、Stylus 样式资源
介绍
Webpack 本身是不能识别样式资源的,所以我们需要借助 Loader 来帮助 Webpack 解析样式资源
我们找 Loader 都应该去官方文档中找到对应的 Loader,然后使用
官方文档找不到的话,可以从社区 Github 中搜索查询
处理CSS资源
1、下载包
npm i css-loader style-loader -D
注意:需要下载两个 loader
2、功能介绍
- css-loader:负责将 Css 文件编译成 Webpack 能识别的模块
- style-loader:会动态创建一个 Style 标签,里面放置 Webpack 中 Css 模块内容
此时样式就会以 Style 标签的形式在页面上生效
3、配置
js
const path = require('path')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js'
},
module: {
rules: [{
test: /\.css$/,
use: ['style-loader','css-loader'] // 执行顺序从右到左(css->style)
}]
},
plugins: [],
mode: 'development'
}
4、添加CSS资源
- src/css/index.css
css
.box1 {
width: 100px;
height: 100px;
background-color: pink;
}
- src/main.js
js
import count from "./js/count";
import sum from "./js/sum";
// 引入 Css 资源,Webpack才会对其打包
import "./css/index.css";
console.log(count(2, 1));
console.log(sum(1, 2, 3, 4));
- public/index.html
html
<!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>
<!-- 准备一个使用样式的 DOM 容器 -->
<div class="box1"></div>
<!-- 引入打包后的js文件,才能看到效果 -->
<script src="../dist/main.js"></script>
</body>
</html>
5、运行指令
npx webpack
处理Less资源
1、下载包
npm i less-loader -D
2、功能介绍
- less-loader:负责将 Less 文件编译成 css文件
3、配置
js
const path = require('path')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js'
},
module: {
rules: [{
test: /\.css$/,
use: ['css-loader, style-loader']
},{
test: /\.less$/,
use: ['less-loader']
}]
},
plugins: [],
mode: 'development'
}
4、添加Less资源
- src/less/index.less
css
.box2 {
width: 100px;
height: 100px;
background-color: green;
}
- src/main.js
js
import count from "./js/count";
import sum from "./js/sum";
// 引入 Css 资源,Webpack才会对其打包
import "./css/index.css";
import "./less/index.less";
console.log(count(2, 1));
console.log(sum(1, 2, 3, 4));
- public/index.html
html
<!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>
<!-- 准备一个使用样式的 DOM 容器 -->
<div class="box1"></div>
<!-- 引入打包后的js文件,才能看到效果 -->
<script src="../dist/main.js"></script>
</body>
</html>
5、运行指令
npx webpack
处理Sass和Scss资源
1、下载包
npm i sass-loader sass -D
2、功能介绍
- sass-loader:负责将 Sass 文件编译成 css文件
- sass: Sass-loader依赖Sass进行编译
3、配置
js
const path = require('path')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js'
},
module: {
rules: [{
test: /\.css$/,
use: ['css-loader, style-loader']
},{
test: /\.less$/,
use: ['css-loader, style-loader','less-loader']
},{
test: /\s[ac]ss$/,
use: ['css-loader, style-loader','sass-loader']
}]
},
plugins: [],
mode: 'development'
}
4、添加Sass资源
- src/sass/index.sass
css
.box3 {
width: 100px;
height: 100px;
background-color: red;
}
- src/sass/index.scss
css
.box4 {
width: 100px;
height: 100px;
background-color: blue;
}
- src/main.js
js
import count from "./js/count";
import sum from "./js/sum";
// 引入 Css 资源,Webpack才会对其打包
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));
- public/index.html
html
<!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>
<!-- 准备一个使用样式的 DOM 容器 -->
<div class="box1"></div>
<!-- 引入打包后的js文件,才能看到效果 -->
<script src="../dist/main.js"></script>
</body>
</html>
5、运行指令
npx webpack
处理Stylus资源
1、下载依赖
npm install stylus-loader -D
2、功能介绍
- stylus-loader:负责将 Stylus 文件编译成 Css 文件
3、配置
js
const path = require('path')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.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、添加stylus资源
- src/stylus/index.styl
css
.box5 {
width: 100px;
height: 100px;
background-color: yellow;
}
- src/main.js
js
import count from "./js/count";
import sum from "./js/sum";
// 引入 Css 资源,Webpack才会对其打包
import "./css/index.css";
import "./less/index.less";
import "./sass/index.sass";
import "./sass/index.scss";
import "./stylus/index.styl"
console.log(count(2, 1));
console.log(sum(1, 2, 3, 4));
- public/index.html
html
<!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>
<!-- 准备一个使用样式的 DOM 容器 -->
<div class="box1"></div>
<!-- 引入打包后的js文件,才能看到效果 -->
<script src="../dist/main.js"></script>
</body>
</html>
5、运行指令
npx webpack