例子就是为了测试一下css-loader,结果发现css-loader和file-loader冲突,
如果我把file-loader注释了页面就正常显示,file-loader会对css里面的url的图片也会进行处理吗?
js里面只有一行代码引入css
webpack配置如下
const path = require("path");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
mode: "development",
entry: {
main: path.resolve(__dirname, "src", "index"),
},
output: {
path: path.resolve(__dirname, "dist"),
publicPath: "/syh-app/",
filename: "[name].js",
},
module: {
rules: [
{
test: /\.(css)?$/,
use: ["style-loader", "css-loader"],
},
{
test: /\.jpg?$/,
use: {
loader: "file-loader",
options: {
outputPath: "images/",
},
},
},
],
},
resolve: {
extensions: [".json", ".js", ".jsx"],
},
plugins: [
new HtmlWebpackPlugin({
template: "src/index.html",
}),
new CleanWebpackPlugin(),
],
devtool: "source-map",
devServer: {
host: "localhost",
open: true,
port: 8080,
openPage: "syh-app/",
},
};
如果楼主使用的是 webpack5 的话,那遇到的问题应该是和我一样了。在 webpack5 中应该:
type: 'asset/resource'
替代file-loader
;file-loader
但是需要设置type: 'javascript/auto'
;具体原因可以参考 👉官方文档
参考示例可以为: