Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to turn off lowering for script inlining #2649

Closed
jdanyow opened this issue Nov 1, 2022 · 6 comments
Closed

Option to turn off lowering for script inlining #2649

jdanyow opened this issue Nov 1, 2022 · 6 comments

Comments

@jdanyow
Copy link

jdanyow commented Nov 1, 2022

Code like the following is lowered due to 58ac420 always lower template literals containing "</script>".

const page = html`
<!DOCTYPE html>
<html>
  <head>
    <script>
      // script in tagged template literal introduces lowering...
    </script>
  </head>
  <body>
    ... lots of html ...
   </body>
</html>

This breaks compatibility with libs like minify-html-literals because esbuild has lowered the template string to plain string syntax, making it impossible for minify-html-literals to find the tagged template literal.

Would it make sense to have an option to disable script inlining compatibility?

@hyrious
Copy link

hyrious commented Nov 1, 2022

What about enable --supported:template-literal=true?

@jdanyow
Copy link
Author

jdanyow commented Nov 1, 2022

The lowering occurs despite template-literal being supported at line 12600.

shouldLowerTemplateLiteral := p.options.unsupportedJSFeatures.Has(compat.TemplateLiteral)
// Lower tagged template literals that include "</script"
// since we won't be able to escape it without lowering it
if !shouldLowerTemplateLiteral && e.TagOrNil.Data != nil {
if containsClosingScriptTag(e.HeadRaw) {
shouldLowerTemplateLiteral = true
} else {
for _, part := range e.Parts {
if containsClosingScriptTag(part.TailRaw) {
shouldLowerTemplateLiteral = true
break
}
}
}
}

@lydell
Copy link

lydell commented Nov 1, 2022

@jdanyow
Copy link
Author

jdanyow commented Nov 2, 2022

@lydell right- esbuild is lowering to ensure the output can be the contents of a<script> element but this is not necessary when the output is not inlined, eg <script src="/path/to/esbuild/output.js">.

@evanw evanw closed this as completed in 328ce12 Dec 7, 2022
@evanw
Copy link
Owner

evanw commented Dec 7, 2022

In the next release, esbuild will only do this if the platform setting is set to browser (the default value). Setting platform to node or neutral will disable this behavior. You will also be able to disable this behavior with --supported:inline-script=false (for JS) and --supported:inline-style=false (for CSS).

@jdanyow
Copy link
Author

jdanyow commented Dec 13, 2022

thank you @evanw!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants