Add SWC plugin imports after "use client" directive (#9445) #9603
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
When adding a "use client" directive to a module, it must come before any other expressions. Instead of always inserting import declarations as the first item, the SWC plugin now checks if the first expression in a module's body is a "use client" directive and inserts its imports after it, if that's the case.
Related: #9445
Type of change
How Has This Been Tested?
I've tested this by using it with a Next.js app directory project where I defined a query in the same module as a "use client" directive; verifying that it would not build before but would with this fix.
I have not updated the unit tests for the Rust plugin, to verify that imports are inserted after the "use client" directive. Should probably add that.
Test Environment:
@graphql-codegen/cli
:^5.0.0
@graphql-codegen/client-preset
:^4.1.0
Checklist:
Further comments
If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...