Skip to content

Support for optional (closing) tags #5603

Closed
@nikeee

Description

@nikeee

Describe the bug

Some tags are optional in HTML. For example, <head>, <body> and <html>. The closing tag of <html> can be omitted. A body is started implicitly when the first non-<head>-element is created.

This seems to cause issues with vite's parser.

Reproduction

This HTML template:

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Hi</title>
<div>Hi</div>
<script type="module" src="/src/main.ts"></script>

Causes a parsing error, probably due to the missing </html>:

[vite:build-html] Unable to parse {"file":"/<path/>index.html","line":2,"column":1}
1  |  <!DOCTYPE html>
   |                  ^
2  |  <html lang="en">
3  |  <meta charset="utf-8">

Adding </html> fixes this:

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Hi</title>
<div>Hi</div>
<script type="module" src="/src/main.ts"></script>
</html>

But then, the script and styles are positioned before <!doctype html>:

<script type="module" crossorigin src="/assets/index.ad4f7fa4.js"></script>
<link rel="stylesheet" href="/assets/index.06d14ce2.css">
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Hi</title>
<div>Hi</div>

</html>

Adding a <head> and <body> also fixes this.

System Info

System:
    OS: Linux 4.4 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor
    Memory: 21.49 GB / 31.95 GB
    Container: Yes
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.0 - /usr/bin/node
    Yarn: 1.22.11 - ~/.npm-bin/bin/yarn
    npm: 7.24.1 - ~/.npm-bin/bin/npm
  npmPackages:
    vite: ^2.6.4 => 2.6.14

Used Package Manager

npm

Logs

vite v2.6.14 building for production...
✓ 0 modules transformed.
[vite:build-html] Unable to parse {"file":"<path>/vite-project/index.html","line":2,"column":1}
1  |  <!DOCTYPE html>
   |                  ^
2  |  <html lang="en">
3  |  <meta charset="utf-8">
file: <path>/vite-project/index.html
error during build:
Error: Unable to parse {"file":"<path>/vite-project/index.html","line":2,"column":1}
1  |  <!DOCTYPE html>
   |                  ^
2  |  <html lang="en">
3  |  <meta charset="utf-8">
    at traverseHtml (<path>/vite-project/node_modules/vite/dist/node/chunks/dep-e0fe87f8.js:21240:15)
    at async Object.transform (<path>/vite-project/node_modules/vite/dist/node/chunks/dep-e0fe87f8.js:21305:17)
    at async ModuleLoader.addModuleSource (<path>/vite-project/node_modules/rollup/dist/shared/rollup.js:22148:30)
    at async ModuleLoader.fetchModule (<path>/vite-project/node_modules/rollup/dist/shared/rollup.js:22201:9)
    at async Promise.all (index 0)

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug: upstreamBug in a dependency of Vitefeat: htmlp3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    Status

    Discussing

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions