fix: preserve 'use client' directive when injecting utilities#96
fix: preserve 'use client' directive when injecting utilities#96
Conversation
5844953 to
9a1c614
Compare
There was a problem hiding this comment.
💡 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]*)+/, |
There was a problem hiding this comment.
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.
Summary
'use client'directive at the top of the file when injecting utilities.