安装
yarn add css-loaderyarn add style-loaderornpm i style-loader css-loader
index.js
import "./css/index.css"
webpack.config.js
const path = require("path");module.exports = { entry: "./index.js", output: { filename: "bundle.js", path:path.join(__dirname,"dist") }, module: { rules: [ { test: /\.css$/, use: [ 'style-loader', 'css-loader' ] } ] }, mode:"development"}
css/index.css
div{ width: 200px; height: 200px; background-color: red;}
也可以打包图片
div{ width: 200px; height: 200px; background: url("../images/4.png");}

npm start index.html页面 alt+b 查看
处理图片
yarn add url-loader
webpack.config.js
const path = require("path");module.exports = { entry: "./index.js", output: { filename: "bundle.js", path:path.join(__dirname,"dist") }, module: { rules: [ { test: /\.css$/, use: [ 'style-loader', 'css-loader' ] }, { test: /\.(png|jpg|gif)$/i, use: ["url-loader"] } ] }, mode:"development"}