Parcel: 馃悰 Tags that do not belong to HTML should be ignored

Created on 11 Jan 2018  路  12Comments  路  Source: parcel-bundler/parcel

For services rendered as the template file, the template engine <%%>,{{}} marker package should be ignored, but the label disrupted

馃捇 Demo Code

<!DOCTYPE html>
<html>
<head>
    <title><%=title%></title>
    <link href="./style.css">
</head>
<body>
    <h1>Welcome : </h1>
<% if(data){ %>
    <p><%=data%></p>
<%}else{%>
    <p><%=request.session._flash||' Error '%></p>
<%}%>
</body>
<script src="./index.js"></script>
</html>

馃 Expected Behavior

<!DOCTYPE html> <html> <head> <title><%=title%></title> <link href="/dist/1fb0a95e00274611850900f80a9d9d1e.css"> </head> <body> <h1>Welcome : </h1> <% if(data){ %> <p><%=data%></p> <%}else{%> <p><%=request.session._flash||' Error ' %></p> <%}%> </body> <script src="/dist/e2e07bfc7be96fd730ba9a0cd2160e84.js"></script> </html>

馃槸 Current Behavior

<!DOCTYPE html> <html> <head> <title><%=title%></%=title%></title> <link href="/dist/1fb0a95e00274611850900f80a9d9d1e.css"> </head> <body> <h1>Welcome : </h1> <% if(data){="" %=""> <p><%=data%></%=data%></p> <%}else{%> <p><%=request.session._flash||' Error="" '%=""></%=request.session._flash||'></p> <%}%> </%}%></%}else{%></%></body> <script src="/dist/e2e07bfc7be96fd730ba9a0cd2160e84.js"></script> </html>

馃拋 Possible Solution

parcel build test/demo.html -d ./test/dist/ --mangle-tag=<%%>,{{}}

馃實 My Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.4.1 |
| Node | v8.8.0 |
| npm/Yarn | Yarn |
| Operating System | MacOS 10.12.6 |

Bug

All 12 comments

maybe we can add add custom tags for {{}} and <% %> in HTMLAsset when parsing and rendering HTML file to solve this issue

If i'm not mistaken this is probably related to the postHTML processing we do, and i'm pretty sure there is an option to ignore certain syntaxes or to make processing less strict in postHTML. I'll try fix it or look for related code, so someone else can take a look.
Related code:
https://github.com/parcel-bundler/parcel/blob/master/src/assets/HTMLAsset.js#L69
Maybe https://github.com/parcel-bundler/parcel/blob/master/src/assets/HTMLAsset.js#L34

Yes, it's an effect of postHTML:

posthtml = require('posthtml');

const html = `
<!DOCTYPE html>
<html>
<head>
    <title><%=title%></title>
</head>
<body>
</body>
`

const result = posthtml()
  .process(html, { sync: true })
  .html

console.log(result)

Will output

<!DOCTYPE html>
<html>
<head>
    <title><%=title%></%=title%></title>
</head>
<body>
</body>
</html>

Now, the harder part. There doesn't seem to be an option to disable this and no plugin for that kind of ignore.

I would be interested in writing a posthtml plugin that would take a list of templates and ignore processing of tags matching them. If you would like that approach, let me know.

@maciej-ka i've looked into this as well, that will probably be the best solution, feel free to take a go at this

Turned out that posthtml plugin can only transform AST while the closing of tag happens inside posthtml-renderer. There is an option to define tags that don't need closing (like <br>) however it supports only strings (and a tagname in this case is %=varname%).

I submitted PR that would allow regexp in renderer options. With that we could:

const posthtmlRender = require('posthtml-render');
posthtml([plugin()]).process(html, {render: (tree) => render(tree, {singleTags: [/^%.*%$/]})})

Awesome work @maciej-ka , hope they merge it soonish :)

Question: why are you processing template files with Parcel? Shouldn't you be running them through the template engine first? Should we support an asset type for that template engine?

I'm just worried that these aren't really valid HTML. How do we decide which tags to support? The tags might conflict between different template engines. We can't possibly support them all by default, without explicit asset types for them I don't think.

Closing, since there has been no response.

The tag in svgs also completely breaks the svg in parcel. Works fine in regular html. The title tag is really important for accessibility in svgs so this could be a big concern.</p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars1.githubusercontent.com/u/4078018?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="vpicone picture"> <strong>vpicone</strong> <span class="text-muted ml-1">on 8 Feb 2019</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <p>Tried using <code><title></code> inside a SVG myself, and noticed that the tag were stripped. Too bad when it comes to accessibility.</p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars2.githubusercontent.com/u/33870508?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="MindTooth picture"> <strong>MindTooth</strong> <span class="text-muted ml-1">on 4 Nov 2019</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <p>This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.</p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars2.githubusercontent.com/in/15368?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="github-actions[bot] picture"> <strong>github-actions[bot]</strong> <span class="text-muted ml-1">on 3 May 2020</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <blockquote> <p>Question: why are you processing template files with Parcel? Shouldn't you be running them through the template engine first? Should we support an asset type for that template engine?</p> <p>I'm just worried that these aren't really valid HTML. How do we decide which tags to support? The tags might conflict between different template engines. We can't possibly support them all by default, without explicit asset types for them I don't think.</p> </blockquote> <p>No, we should (if we want use parcel). The thing is there're not only frontend things. For example now I'm developing small golang project with html templates (just to test one thing). So, since it small I don't want to use something like gulp or webpack, but anyway I want to have compressed assets. Golang html templates is html with <code>{{ . . . }}</code> blocks. So, parcel can't parse it.<br /> But since gotemplates is backend templating I can't run it through the template engine first.</p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars0.githubusercontent.com/u/20650419?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="DimaGashko picture"> <strong>DimaGashko</strong> <span class="text-muted ml-1">on 6 Jul 2020</span> </div> <div class="col text-right"> 👍<span class="ml-2 mr-3">1</span> </div> </div> </div> </div> </div> <div class="col-12"> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown text-center helpful"> <div class="title">Was this page helpful?</div> <div class="mt-1" onMouseLeave="rating(287723644, 0);"> <i class="fas fa-star inactive" id="star-1" onMouseOver="rating(287723644, 1);" onclick="rate(287723644, 1);"></i> <i class="fas fa-star inactive" id="star-2" onMouseOver="rating(287723644, 2);" onclick="rate(287723644, 2);"></i> <i class="fas fa-star inactive" id="star-3" onMouseOver="rating(287723644, 3);" onclick="rate(287723644, 3);"></i> <i class="fas fa-star inactive" id="star-4" onMouseOver="rating(287723644, 4);" onclick="rate(287723644, 4);"></i> <i class="fas fa-star inactive" id="star-5" onMouseOver="rating(287723644, 5);" onclick="rate(287723644, 5);"></i> </div> <div class="description text-small"><span id="rating-val">0</span> / 5 - <span id="rating-count">0</span> ratings</div> </div> </div> <div class="mb-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="3452512275" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> </div> </div> <div class="col-12 col-lg-4"> <div id="ph-above-related"></div> <div class="card card-custom issue-box"> <div class="card-body pt-3 pb-5"> <h2 class="mb-4">Related issues</h2> <div> <strong> <a href="/parcel/284844835/install-peer-dependencies-of-autoinstalled-modules">馃檵 Install peer dependencies of autoinstalled modules</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars0.githubusercontent.com/u/19409?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="devongovett picture"> <strong class="pr-1" dir="ltr">devongovett</strong>  路  <span class="px-1" dir="ltr">3</span><span>Comments</span> </div> <hr /> <div> <strong> <a href="/parcel/301245684/rfc-overhaul-plugin-architecture">[RFC] Overhaul Plugin Architecture</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars0.githubusercontent.com/u/6759650?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="davidnagli picture"> <strong class="pr-1" dir="ltr">davidnagli</strong>  路  <span class="px-1" dir="ltr">3</span><span>Comments</span> </div> <hr /> <div> <strong> <a href="/parcel/291170209/parcel-crashes-looking-for-sourcemappackager">parcel crashes looking for SourceMapPackager</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars1.githubusercontent.com/u/230962?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="dotdash picture"> <strong class="pr-1" dir="ltr">dotdash</strong>  路  <span class="px-1" dir="ltr">3</span><span>Comments</span> </div> <hr /> <div> <strong> <a href="/parcel/282039964/preact-hmr">Preact HMR</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars2.githubusercontent.com/u/28024000?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="philipodev picture"> <strong class="pr-1" dir="ltr">philipodev</strong>  路  <span class="px-1" dir="ltr">3</span><span>Comments</span> </div> <hr /> <div> <strong> <a href="/parcel/300627867/multi-plugin-for-same-file-only-execute-one">multi plugin for same file only execute one </a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars0.githubusercontent.com/u/10148735?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="466023746 picture"> <strong class="pr-1" dir="ltr">466023746</strong>  路  <span class="px-1" dir="ltr">3</span><span>Comments</span> </div> </div> </div> <div class="sticky-top pt-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="3919948963" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div id="ph-below-related-2" class="mt-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="3919948963" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> </div> <div class="col-12 col-lg-4"> </div> </div> <div class="skyscraper-container"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="7879185320" data-ad-format="vertical" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="mt-5 spacer"></div> <footer class="mt-5 pb-2 py-4 text-center mt-auto"> <div class="container"> <a class="navbar-brand logo mr-5" href="/"> <img src="/assets/img/logo.svg" width="40" height="40" alt="bleepingcoder logo"> bleeping<strong>coder</strong> </a> <div class="mt-4"> bleepingcoder.com uses publicly licensed GitHub information to provide developers around the world with solutions to their problems. We are not affiliated with GitHub, Inc. or with any developers who use GitHub for their projects. We do not host any of the videos or images on our servers. All rights belong to their respective owners. </div> <div> Source for this page: <a href="https://www.github.com/parcel-bundler/parcel/issues/544" rel="nofollow noreferrer" target="_blank">Source</a> </div> </div> <hr class="mb-5 mt-5"> <div class="container"> <div class="row"> <div class="col-sm-4 col-lg mb-sm-0 mb-5"> <strong>Popular programming languages</strong> <ul class="list-unstyled mb-0 mt-2"> <li class="mb-2"> <a href="/python" dir="ltr">Python</a> </li> <li class="mb-2"> <a href="/javascript" dir="ltr">JavaScript</a> </li> <li class="mb-2"> <a href="/typescript" dir="ltr">TypeScript</a> </li> <li class="mb-2"> <a href="/cpp" dir="ltr">C++</a> </li> <li class="mb-2"> <a href="/csharp" dir="ltr">C#</a> </li> </ul> </div> <div class="col-sm-4 col-lg mb-sm-0 mb-5"> <strong>Popular GitHub projects</strong> <ul class="list-unstyled mb-0 mt-2"> <li class="mb-2"> <a href="/microsoft/vscode" dir="ltr">vscode</a> </li> <li class="mb-2"> <a href="/numpy/numpy" dir="ltr">numpy</a> </li> <li class="mb-2"> <a href="/ant-design/ant-design" dir="ltr">ant-design</a> </li> <li class="mb-2"> <a href="/mui-org/material-ui" dir="ltr">material-ui</a> </li> <li class="mb-2"> <a href="/vercel/next-js" dir="ltr">next.js</a> </li> </ul> </div> <div class="col-sm-4 col-lg mb-0"> <strong>More GitHub projects</strong> <ul class="list-unstyled mb-0 mt-2"> <li class="mb-2"> <a href="/rust-lang/rust" dir="ltr">rust</a> </li> <li class="mb-2"> <a href="/moment/moment" dir="ltr">moment</a> </li> <li class="mb-2"> <a href="/yarnpkg/yarn" dir="ltr">yarn</a> </li> <li class="mb-2"> <a href="/mozilla/pdf-js" dir="ltr">pdf.js</a> </li> <li class="mb-2"> <a href="/JuliaLang/julia" dir="ltr">julia</a> </li> </ul> </div> </div> </div> <hr class="mb-5 mt-5"> <div class="container text-muted"> 漏 2026 bleepingcoder.com - <a href="/bleeps" rel="nofollow">Contact</a><br /> By using our site, you acknowledge that you have read and understand our <a href="/cookies" rel="nofollow">Cookie Policy</a> and <a href="/privacy" rel="nofollow">Privacy Policy</a>. </div> </footer> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=" crossorigin="anonymous"></script> <script async src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> <!--<script defer type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5fb2db66acbd74b2"></script>--> <script type="text/javascript" src="/assets/js/main.js"></script> <script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script></body> </html>