Skip to content

capnweb-validate: keep a module's hashbang and directive prologue first - #279

Open
martijnwalraven wants to merge 1 commit into
cloudflare:mainfrom
martijnwalraven:pr-prologue
Open

martijnwalraven wants to merge 1 commit into
cloudflare:mainfrom
martijnwalraven:pr-prologue

Conversation

@martijnwalraven

Copy link
Copy Markdown

The transform inserts its runtime import and generated validators at offset zero of a module. Two kinds of module break on that:

  • A module that begins with a directive, such as "use client" in a Next.js app, loses it from the directive prologue once an import sits above it, so the framework no longer treats the module as a client component. The symptom in Next.js 16 is a decorated class in a "use client" file compiling as a server module.
  • A module that begins with a hashbang does not parse after the insert ('#!' can only be used at the start of a file).

This moves the insert to after the hashbang and after the last directive of the prologue, in preludeEdit. Nothing else in the transform changes.

Tested with three cases in transform-module.test.ts, each parsing the output: a prologue of two directives after a comment, a hashbang alone, and a hashbang followed by directives; a string literal after an import is left where it is, since it is no directive. Found while running the transform under Turbopack in a Next.js 16.3.6 app, where the "use client" case showed up first.

Changeset: patch for capnweb-validate.

The transform inserted its runtime import and validators at offset zero.
In a module that begins with a directive such as "use client" the directive
was then no longer in the prologue, so frameworks stopped reading it, and in
a module that begins with a hashbang the result did not parse ("'#!' can
only be used at the start of a file"). The insert now goes after the
hashbang and the last directive.
@changeset-bot

changeset-bot Bot commented Oct 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5079f8c

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

This PR includes changesets to release 1 package
Name Type
capnweb-validate Patch

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

@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@martijnwalraven

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant