作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
通过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/
通过parcel .htm 文件构建时,输出为.html 文件。 我更愿意将 .htm 文件保留为输出文件,但无法在 Parcel 或 Parcel 使用的 PostHTML 中找到一种方法。 我更新
我是一名优秀的程序员,十分优秀!