Skip to content

fix(vite-plugin): prevent SyntaxError when SSR module ends with // comment#13125

Merged
jamesopstad merged 2 commits intocloudflare:mainfrom
kayluhb:fix/vite-plugin-sourcemap-comment-syntax-error
Apr 1, 2026
Merged

fix(vite-plugin): prevent SyntaxError when SSR module ends with // comment#13125
jamesopstad merged 2 commits intocloudflare:mainfrom
kayluhb:fix/vite-plugin-sourcemap-comment-syntax-error

Conversation

@kayluhb
Copy link
Copy Markdown
Contributor

@kayluhb kayluhb commented Mar 30, 2026

Summary

When module code produced by the SSR transform ends with a single-line comment (e.g. //# sourceMappingURL=... preserved by vite-plus), the closing } of the async arrow wrapper in runInlinedModule gets absorbed into the comment:

"use strict";async (...)=>{...//# sourceMappingURL=foo.js.map}
//                                                           ^ swallowed by comment

This produces an unclosed function body and throws SyntaxError: Unexpected end of input.

Fix

Insert a newline before the closing brace so it always starts a new line, safe from trailing // comments:

- const code = `"use strict";async (${Object.keys(context).join(",")})=>{${transformed}}`;
+ const code = `"use strict";async (${Object.keys(context).join(",")})=>{${transformed}\n}`;

This is backwards-compatible — the newline is harmless when there is no trailing comment.

Reproduction

Use @cloudflare/vite-plugin with vite-plus (which preserves sourceMappingURL comments in SSR-transformed output, unlike standard Vite which strips them).

Fixes #12681


  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because: covered by existing tests
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: bug fix
Open with Devin

…mment

When module code produced by the SSR transform ends with a single-line
comment (e.g. `//# sourceMappingURL=...` preserved by vite-plus), the
closing `}` of the async arrow wrapper in `runInlinedModule` gets
absorbed into the comment:

    "use strict";async (...)=>{...//# sourceMappingURL=foo.js.map}

This produces an unclosed function body and throws
`SyntaxError: Unexpected end of input`.

Fix: insert a newline before the closing brace so it always starts a
new line, safe from trailing `//` comments.

Fixes cloudflare#12681

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kayluhb kayluhb requested a review from a team as a code owner March 30, 2026 15:13
@kayluhb kayluhb requested a review from penalosa March 30, 2026 15:13
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 30, 2026

🦋 Changeset detected

Latest commit: 50d2c47

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@workers-devprod
Copy link
Copy Markdown
Contributor

workers-devprod commented Mar 30, 2026

Codeowners approval required for this PR:

  • ✅ @cloudflare/wrangler
Show detailed file reviewers

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

Copy link
Copy Markdown
Contributor

@penalosa penalosa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty certain this is a bug in Vite plus, but this workaround seems okay

@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Mar 30, 2026
@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Mar 30, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 30, 2026

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@13125

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@13125

miniflare

npm i https://pkg.pr.new/miniflare@13125

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@13125

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@13125

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@13125

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@13125

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@13125

wrangler

npm i https://pkg.pr.new/wrangler@13125

commit: 50d2c47

@jamesopstad jamesopstad merged commit f76652c into cloudflare:main Apr 1, 2026
46 of 48 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Apr 1, 2026
emily-shen pushed a commit that referenced this pull request Apr 8, 2026
…mment (#13125)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

vite-plugin: SyntaxError when SSR-transformed module ends with sourceMappingURL comment

4 participants