简体   繁体   English

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

[英]Parceljs / PostHTML - keeping .htm files from being renamed to .html

When building through parcel .htm files, the output is .html files.通过parcel .htm 文件构建时,输出为.html 文件。 I much rather just keep the .htm files as output files, but couldn't figure out a way to do it in either Parcel or PostHTML which is being used by parcel.我更愿意将 .htm 文件保留为输出文件,但无法在 Parcel 或 Parcel 使用的 PostHTML 中找到一种方法。

I've updated an old static website to use Parcel as the build tool.我更新了一个旧的静态网站以使用 Parcel 作为构建工具。 All the website files are with a .htm file extension.所有网站文件都带有 .htm 文件扩展名。

I'm having a problem where currently parcel automatically renames all the .htm files to .html, and also auto updates all the internal links to .html我遇到了一个问题,当前包裹自动将所有 .htm 文件重命名为 .html,并自动将所有内部链接更新为 .html

This is currently an issue because the site is indexed on search engines with .htm file suffixes, so I currently either need to keep two copies or perform redirects for each .htm file.这是目前的一个问题,因为该站点在搜索引擎上以 .htm 文件后缀编制索引,因此我目前需要保留两个副本或为每个 .htm 文件执行重定向。

I don't think this is supported out-of-the-box in parcel v2, but it could be accomplished relatively easily with a custom namer plugin我不认为这是在parcel v2 中开箱即用的支持,但是使用自定义名称插件可以相对轻松地完成

Here's some code that will work:下面是一些可以工作的代码:

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;
  },
});

The easiest way to hook this plugin up with parcel without publishing a separate package is to use yarn's link protocol .将此插件与parcel 挂钩而不发布单独的包的最简单方法是使用yarn 的链接协议

You'd structure your project like this:你可以这样构建你的项目:

project
├── .parcelrc
├── package.json
├── src
│   └── index.html
└── parcel-namer-htm
    ├── package.json
    └── src
        └── HtmNamer.js <-- the code above

Your main package.json would link to your parcel-namer-htm folder like this:您的主要package.json将链接到您的parcel-namer-htm文件夹,如下所示:

{
  "name": "my-project",
  "dependencies": {
    "parcel": "^2.0.0",
    "parcel-namer-htm": "link:./parcel-transformer-foo"
  }
}

Your .parcelrc file would look like this:您的.parcelrc文件如下所示:

{
  "extends": "@parcel/config-default",
  "namers": ["parcel-namer-htm", "..."]
}

And the parcel-namer-htm/package.json would look like this:parcel-namer-htm/package.json将如下所示:

{
  "name": "parcel-namer-htm",
  "main": "src/HtmNamer.js",
  "engines": {
    "node": ">= 12.0.0",
    "parcel": "^2.0.0"
  },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM