例如,如果我有一个test.html
文件,代码是:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>test</title>
<link rel="stylesheet" href="/styles/test.css">
<style>
.test {
background: url("/images/banner.jpg");
}
</style>
</head>
<body>
<img src="/images/banner.jpg" alt="test"/>
</body>
</html>
我曾经html-loader
要求它,就像:
const html = require(test.html);
console.log(html);
和变量html
打印:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>test</title>
<link rel="stylesheet" href="[object Object]">
<style>
.test {
background: url("/images/banner.jpg");
}
</style>
</head>
<body>
<img src="/temp/images/banner.d0f7ff.jpg" alt="test"/>
</body>
</html>
style
-> .test
->background
无法获得正确的网址。如何解决?
这是我的webpack
配置代码html-loader
:
{
test: /\.html$/,
include: [
path.resolve(config.devDir)
],
loader: 'html',
query: {
root: config.devDir,
attrs: ['img:src', 'img:data-src', 'link:href']
}
}