#12402·swc

`@swc/html` merges adjacent classic `<script>` elements, changing evaluation semantics and dropping code

Author: oddharshCreated Sep 22, 2026Updated Sep 22, 2026

Describe the bug

@swc/html concatenates adjacent classic <script> elements into a single element. Two script elements are two separate evaluations, so the merge changes runtime behaviour in three ways, and in one case it deletes code from the output.

Measured on 1.16.2:

input output
<script>a()</script><script>b()</script> <script>a(),b()</script>
the same, with minifyJs: false <script>a();\nb();\n</script>
<script>throw 1</script><script>b()</script> <script>throw 1</script>
<script type="module">a()</script><script>b()</script> not merged

The third row is the sharp one: b() does not appear in the output at all.

Three consequences, in the order they are likely to bite:

  1. An exception no longer stops at its own script. Each script element is evaluated independently, so a throw in the first leaves the second to run. Merged into one element, it does not, and the compressor then drops the unreachable code, which is correct for one program and wrong for two.
  2. Per-script CSP hashes stop matching. script-src 'sha256-…' is computed per element, so a merged element hashes to neither of the originals. A site that hashes its inline scripts starts failing its own unchanged policy.
  3. document.currentScript changes identity. Inline config blocks that read their own data- attributes through it see a different element.

minifyJs: false still merges, so there is no way to opt out.

Input code

xml
<script>throw 1</script><script>b()</script>

Config

json
{}

(and { "minifyJs": false }, which merges too)

Link to the code that reproduces this issue

@swc/html has no playground, so the repro is inline. Save as repro.mjs, then npm i @swc/[email protected] && node repro.mjs:

javascript
// npm i @swc/[email protected] && node swc-html-script-merge.mjs
//
// @swc/html merges adjacent classic <script> elements. Two script elements are
// two evaluations, so this changes semantics, and in the third case it deletes
// code outright.
import { minify } from "@swc/html";

const show = async (label, html, opts = {}) =>
  console.log(label.padEnd(20), JSON.stringify((await minify(Buffer.from(html), opts)).code));

await show("two scripts", "<script>a()</script><script>b()</script>");
await show("minifyJs: false", "<script>a()</script><script>b()</script>", { minifyJs: false });
await show("first one throws", "<script>throw 1</script><script>b()</script>");
await show("module + classic", '<script type="module">a()</script><script>b()</script>');

// Output on 1.16.2:
// two scripts          "<script>a(),b()</script>"
// minifyJs: false      "<script>a();\nb();\n</script>"
// first one throws     "<script>throw 1</script>"        <-- b() is gone
// module + classic     "<script type=module>a()</script><script>b()</script>"

Expected behavior

Adjacent classic scripts stay separate elements. Failing that, an option to disable merging, so that a project using per-script CSP hashes or relying on per-script evaluation can opt out.

Actual behavior

They are merged unconditionally, and code after a throwing script is dropped.

Version

@swc/html 1.16.2

Additional context

Module scripts and scripts with a non-JavaScript type are not merged, so this is specific to adjacent classic scripts.