webpack - 如何处理 HTML 文件中的链接标签
问题描述
我有一个带有单个 link
元素的简单 HTML 文件。
<link rel="stylesheet" href="../node_modules/@fortawesome/fontawesome-free/css/all.css">
与身体
<body>
<div class="container">
<i class="fas fa-book-open"></i>
</div><!-- container -->
</body>
在 all.css 的末尾有一个指向字体 ( url("../webfonts/fa-brands-400.woff2") format("woff2")
) 的 URL,所以我也需要它们。我想使用 Webpack 以以下结构“构建”我的网站
dist
|
|--css
| |--all.css
|--webfonts
|--|--fonts required by all.css
|--index.html
到目前为止,我的 webpack.config.js 看起来像这样:
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");
module.exports = {
mode: 'development',
target: 'web',
output: {
clean: true,
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "src", "index.html"),
}),
],
module: {
rules: [
{
test: /\.css$/i,
type: "asset/resource",
generator: {
filename: "css/[name][ext]",
},
},
{
test: /\.(woff(2)?|eot|ttf|otf|svg|)$/i,
type: "asset/resource",
generator: {
filename: "webfonts/[name][ext]",
},
},
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: false,
},
},
],
},
};
输出如下所示:
dist
|
|--css
| |--all.css
|--index.html
如何配置 Webpack (5.36.2) 以正确处理 Urls 到字体?
解决方案
推荐阅读
- javascript - 防止组件在 DOM 的不同部分显示时卸载
- javascript - Typescript 中的可摇树策略模式
- google-bigquery - 如何在我的 terraform 文件中为 BigQuery 创建 INTEGER 类型记录
- php - 如何动态地将数据库中的图像获取到反应轮播中?
- angular - 如何使用 HTTP 拦截器在 Angular 应用程序中注入 API 密钥
- c# - 使用 Autofac 的通用 DBContext 注入
- react-native - React-native 将颜色设置为单选按钮的标签
- google-kubernetes-engine - 可以将 GKE 多专区集群迁移到单专区集群吗?
- javascript - 用于 Javascript 的类似 SQL 的内部联接
- vb.net - 以 DataTable 作为 DataSource 的 DataGridView 中未显示数据