Skip to content

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

@fengmk2

Description

@fengmk2

What versions & operating system are you using?

System:
  OS: macOS Darwin 25.2.0 (arm64)
  Node: v22.x
npmPackages:
  @cloudflare/vite-plugin: 1.25.5
  vite: npm:@voidzero-dev/vite-plus-core@0.0.0-g0fd4d06d.20260225-1306 (reports as vite 8.0.0-beta.15)
  wrangler: 4.68.1

Please provide a minimal reproduction

Use @cloudflare/vite-plugin with @voidzero-dev/vite-plus-core (vite+), which preserves //# sourceMappingURL=... comments in SSR-transformed module output.

Describe the Bug

The runInlinedModule function in runner-worker.js wraps transformed module code in an async arrow function using a template literal:

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

When the transformed code ends with a single-line comment like //# sourceMappingURL=next_link.js.map, the closing } of the arrow function gets absorbed into the comment:

"use strict";async (...)=>{...//# sourceMappingURL=next_link.js.map}
//                                                                 ^ this } is part of the comment

This results in an unclosed arrow function body and throws:

SyntaxError: Unexpected end of input
    at Object.runInlinedModule (workers/runner-worker.js:1314:35)
    at CustomModuleRunner.directRequest (workers/runner-worker.js:1166:80)
    at CustomModuleRunner.cachedRequest (workers/runner-worker.js:1084:73)

Standard vite strips sourceMappingURL comments from SSR-transformed output, so this doesn't surface there. However, vite-plus-core (vite+) preserves them, triggering this bug.

Fix

Add a newline before the closing brace so it can never be swallowed by a trailing // comment:

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

This is a safe, backwards-compatible change — the newline is harmless when there is no trailing comment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions