vite icon indicating copy to clipboard operation
vite copied to clipboard

Support parsing .htm as same as .html

Open PeyaPeyaPeyang opened this issue 3 years ago • 8 comments

Description

We are currently working on a rebuild of our old website.
I included the .htm file in the build target for now, but the build failed with a Parse error.
.htm files are rendered correctly in most modern browsers.
According to RFC2854 and this IANA text, .html and .htm are commonly used HTML extensions and I believe they should be used in vite.

It is a small thing, but I think it will be useful.

Also, I found a regular expression in the HTML plugin file that seems to support .htm, but it is not actually supported.
If the suggestion in this Issue is not good, why is this regex in there?

Suggested solution

Actual works method

in node/plugins/html/ts:301

    async transform(html, id) {
      if (id.endsWith('.html')) {
        const relativeUrlPath = path.posix.relative(

to

    async transform(html, id) {
      if (id.endsWith('.html') || id.endsWith('.htm')) {
        const relativeUrlPath = path.posix.relative(

Alternative

No response

Additional context

Parse error I got:

> [email protected] build T:\projects\vite-test
> vite build

vite v3.2.4 building for production...
✓ 0 modules transformed.
[vite:build-import-analysis] Parse error @:8:28
file: T:/projects/vite-test/src/pages/a.htm:8:27
 6:           content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 7:     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 8:     <title>Document</title>
                               ^
 9: </head>
10: <body>
error during build:
Error: Parse error @:8:28
    at parse$b (file:///T:/projects/vite-test/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-67e7f8ab.js:32642:355)
    at Object.transform (file:///T:/projects/vite-test/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-67e7f8ab.js:42081:27)
 ERROR  Command failed with exit code 1.

Validations

PeyaPeyaPeyang avatar Nov 19 '22 20:11 PeyaPeyaPeyang

It seems there's many places using id.endsWith('.html'). I think it makes sense to use that htmlLangRE.

sapphi-red avatar Nov 20 '22 10:11 sapphi-red

Hello @PeyaPeyaPeyang. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it!

github-actions[bot] avatar Nov 20 '22 10:11 github-actions[bot]

I've made a PR for this: #11001

Potato1682 avatar Nov 20 '22 12:11 Potato1682

I think it's a nice to have if it's simple to support, but with the html fallback this makes it a bit complicated.

  • Should we support reading from both index.htm and index.html fallback?
  • MPA support should consider .htm too?
  • The optimizer entries glob also needs to consider it

It feels like it would bring a lot of legacy extensions handling for a modern tool.

bluwy avatar Nov 20 '22 14:11 bluwy

I think index.html should be the priority, too. We might have to implement warnings when both the .html and .htm files are detected and use .html as possible.

Potato1682 avatar Nov 20 '22 15:11 Potato1682

A situation where .html and .htm with the same name exist at the same time is the wrong way to build a site, I think. If anything, it would be better to issue a build error or warning and give priority to the .html.

PeyaPeyaPeyang avatar Nov 20 '22 15:11 PeyaPeyaPeyang

Oh, I didn't think of the SPA fallback. sirv already supports .htm. So in preview, index.htm currently works. https://github.com/vitejs/vite/blob/a03860f1320c0bff8e2b8b7fd0e1c42a7565e767/packages/vite/src/node/preview.ts#L115-L126

I guess htmlFallbackMiddleware will be a bit complicated but other parts won't be so complicated.

sapphi-red avatar Nov 21 '22 01:11 sapphi-red

This issue happens for any non-html extension. I ran into it trying to get it to export a .aspx file so that I could run it on the server in ASP.NET and send it to the user with data pre-populated.

I fixed it in a similar method to the user above, searching vite's node_modules folder for the string id.endsWith('.html'), then adding an or clause to include the extension I wanted.

I'd suggest that whoever fixes this for the project modify that line to allow any extension that's being used in the rolloutOptions section of vite.config.

jasonkester avatar Jan 31 '23 14:01 jasonkester

It would also help with support for non-html file types like .pug, .twig, .liquid, .md etc. So replacing endsWith('.html') with htmlLangRE with extendned file types support would be great, or even better to add config option similar to https://vitejs.dev/config/shared-options.html#resolve-extensions

This would also help resolve https://github.com/vitejs/vite/issues/8000

lubomirblazekcz avatar Mar 08 '23 20:03 lubomirblazekcz