css-loader
npm install css-loader
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/,use: 'css-loader'
}
]
}
};
rules
use
串联多个loader
npm install style-loader
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/,use: ['style-loader','css-loader']
}
]
},}
/* style.css */
body {
background-color:black;
}
// index.js
import './style.css'
- Webpack尝试解析 style.css 文件
- 文件名与正则表达式
/\.css$/
匹配
- 文件被 css-loader 编译
-
css-loader 处理后的结果会被传到 style-loader
- 最后,style-loader 返回一串JavaScript代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Webpack App</title>
<style type="text/css">body {
background-color:black;
}</style></head>
<body>
<script type="text/javascript" src="bundle.js"></script>
</body>
</html>
sass-loader
npm install sass-loader
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.scss$/,'css-loader','sass-loader']
}
]
},}
npm install url-loader file-loader
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.scss$/,'sass-loader']
},{
test: /\.(png|jpg|gif)$/,use: [
{
loader: 'url-loader',options: {
limit: 5000
}
}
]
}
]
}
};
body {
background-image: url('./big-background.png');
}
.icon {
background-image: url('./icon.png');
}
<style type="text/css">body {
background-image: url(ca3ebe0891c7823ff1e137d8eb5b4609.png); }
.icon {
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAALElEQVR4AWMYIWAU1FPLoP9AXEFI0QEi8H+YYdQyqIEaXuumRhh1DZdUMwoATlYWfwh9eYkAAAAASUVORK5CYII=); }
</style>
npm install babel-loader @babel/core @babel/preset-env
module.exports = {
module: {
rules: [
{
test: /\.js$/,exclude: /(node_modules)/,use: {
loader: 'babel-loader',options: {
presets: ['@babel/preset-env']
}
}
}
]
}
};