我们在./src/目录下新建一个index.css文件
body{
background-color: red;
}
import "./index.css"
npm run build
ERROR in ./src/index.css 1:4
Module parse failed: Unexpected token (1:4)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> body{
| background-color: #ddd;
| }
@ ./src/index.js 1:0-20
const path = require("path");
module.exports = {
entry: "./src/index.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
{
test: /.css$/, //匹配规则,匹配就使用一下Loader去处理
use: [ "style-loader","css-loader",] //这里要注意顺序,Loader加载顺序规定为从右到左
}
]
}
};
我们再次打包
npm run build
npm install file-loader –save-dev
const path = require("path");
module.exports = {
entry: "./src/index.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
{
test: /.css$/,
use: [ "style-loader","css-loader",]
},
{
test: /.(png|svg|jpg|gif)$/,
use: [ "file-loader"]
}
]
}
};
body{
background-color: red;
background-image: url(./yasuo.jpg);
}
npm run build
原文:https://www.cnblogs.com/hkfyf/p/11696416.html