gpt4 book ai didi

javascript - Parceljs/PostHTML - 防止 .htm 文件被重命名为 .html

转载 作者:行者123 更新时间:2023-12-04 13:58:02 25 4
gpt4 key购买 nike

通过parcel .htm 文件构建时,输出为.html 文件。
我更愿意将 .htm 文件保留为输出文件,但无法在 Parcel 或 Parcel 使用的 PostHTML 中找到一种方法。

我更新了一个旧的静态网站以使用 Parcel 作为构建工具。
所有网站文件都带有 .htm 文件扩展名。

我遇到了一个问题,当前包裹自动将所有 .htm 文件重命名为 .html,并自动将所有内部链接更新为 .html

这是目前的一个问题,因为该站点在搜索引擎上以 .htm 文件后缀编制索引,因此我目前需要保留两个副本或为每个 .htm 文件执行重定向。

最佳答案

我不认为这是在parcel v2 中开箱即用的支持,但可以通过自定义namer plugin 相对容易地完成。
下面是一些可以工作的代码:

import { Namer } from "@parcel/plugin";
import path from "path";

export default new Namer({
name({ bundle }) {
if (bundle.type === "html") {
const filePath = bundle.getMainEntry()?.filePath;
if (filePath) {
let baseNameWithoutExtension = path.basename(filePath, path.extname(filePath));
// See: https://parceljs.org/plugin-system/namer/#content-hashing
if (!bundle.needsStableName) {
baseNameWithoutExtension += "." + bundle.hashReference;
}
return `${baseNameWithoutExtension}.htm`;
}
}
// Returning null means parcel will keep the name of non-html bundles the same.
return null;
},
});
将此插件与parcel Hook 而不发布单独的包的最简单方法是使用 yarn's link protocol .
你可以这样构建你的项目:
project
├── .parcelrc
├── package.json
├── src
│ └── index.html
└── parcel-namer-htm
├── package.json
└── src
└── HtmNamer.js <-- the code above
您的主 package.json将链接到您的 parcel-namer-htm像这样的文件夹:
{
"name": "my-project",
"dependencies": {
"parcel": "^2.0.0",
"parcel-namer-htm": "link:./parcel-transformer-foo"
}
}
您的 .parcelrc文件看起来像这样:
{
"extends": "@parcel/config-default",
"namers": ["parcel-namer-htm", "..."]
}
parcel-namer-htm/package.json看起来像这样:
{
"name": "parcel-namer-htm",
"main": "src/HtmNamer.js",
"engines": {
"node": ">= 12.0.0",
"parcel": "^2.0.0"
},
}

关于javascript - Parceljs/PostHTML - 防止 .htm 文件被重命名为 .html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57132715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com