Skip to content

fix: preserve 'use client' directive when injecting utilities#96

Open
colinaaa wants to merge 3 commits intov4from
fix/rsc-support
Open

fix: preserve 'use client' directive when injecting utilities#96
colinaaa wants to merge 3 commits intov4from
fix/rsc-support

Conversation

@colinaaa
Copy link
Copy Markdown
Collaborator

Summary

  • Preserves 'use client' directive at the top of the file when injecting utilities.
  • Adds test case for RSC.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9a1c614587

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

src/index.ts Outdated
const importStr = `import "${pathToFileURL(VIRTUAL_UTILITIES_ID)}?${params.toString()}";\n`;

const match = code.match(
/^(?:[\s]*|(?:\/\*[\s\S]*?\*\/)|(?:\/\/[^\n]*\n))*(?:(?:"[^"]*"|'[^']*')[ \t]*;?[\s]*)+/,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Handle inline comments after semicolonless directives

The new directive-matching regex does not include trailing inline comments, so input like 'use client' // note matches only 'use client' and injects the import before the comment, producing invalid code such as 'use client' import "...";. This is a build-breaking regression for valid JS/TS files that use semicolonless directive lines with same-line // or /* */ comments.

Useful? React with 👍 / 👎.

The regular expression used to preserve "use client" and "use server" directives was flawed and could backtrack into single-line comments that contained string literal characters. This resulted in the virtual CSS import being injected inside comments or invalid positions, causing syntax errors in files with URL queries like `?url` or `?raw` (e.g. static-assets tests).

This fix changes the comment and string literal matching to prevent backtracking and ensures we only match true top-level string directives.
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