-
Notifications
You must be signed in to change notification settings - Fork 425
fix: keep non-picked route exports that picked exports reference #2205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brenelz
wants to merge
2
commits into
main
Choose a base branch
from
fix/tree-shake-referenced-exports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+192
−17
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@solidjs/start": patch | ||
| --- | ||
|
|
||
| fix: keep non-picked route exports that picked exports reference instead of deleting their bindings, so API handlers can call helpers exported from the same file (#2100); also fixes picked exports declared via `export { ... }` specifiers being dropped (#1659) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { treeShake } from "./tree-shake.ts"; | ||
|
|
||
| async function shake(code: string, pick: string[]) { | ||
| const plugin = treeShake() as any; | ||
| const id = `/routes/route.ts?${pick.map(p => `pick=${p}`).join("&")}`; | ||
| const result = await plugin.transform(code, id); | ||
| return result?.code as string | undefined; | ||
| } | ||
|
|
||
| describe("treeShake", () => { | ||
| it("keeps only the picked export", async () => { | ||
| const code = ` | ||
| export const GET = () => "get"; | ||
| export const POST = () => "post"; | ||
| `; | ||
|
|
||
| const output = await shake(code, ["GET"]); | ||
|
|
||
| expect(output).toContain("export const GET"); | ||
| expect(output).not.toContain("POST"); | ||
| }); | ||
|
|
||
| // https://github.com/solidjs/solid-start/issues/2100 | ||
| it("keeps a non-picked export that a picked export references", async () => { | ||
| const code = ` | ||
| export const hello = () => "hello"; | ||
| export const GET = async () => { | ||
| return hello(); | ||
| }; | ||
| `; | ||
|
|
||
| const output = await shake(code, ["GET"]); | ||
|
|
||
| expect(output).toContain(`const hello = () => "hello"`); | ||
| expect(output).not.toContain("export const hello"); | ||
| expect(output).toContain("export const GET"); | ||
| }); | ||
|
|
||
| it("keeps a non-picked exported function that a picked export references", async () => { | ||
| const code = ` | ||
| export function helper() { | ||
| return "helper"; | ||
| } | ||
| export const GET = () => helper(); | ||
| `; | ||
|
|
||
| const output = await shake(code, ["GET"]); | ||
|
|
||
| expect(output).toContain("function helper()"); | ||
| expect(output).not.toContain("export function helper"); | ||
| expect(output).toContain("export const GET"); | ||
| }); | ||
|
|
||
| it("removes a non-picked export that nothing references", async () => { | ||
| const code = ` | ||
| export const secret = globalThis.createSecret(); | ||
| export const GET = () => "ok"; | ||
| `; | ||
|
|
||
| const output = await shake(code, ["GET"]); | ||
|
|
||
| expect(output).not.toContain("secret"); | ||
| expect(output).toContain("export const GET"); | ||
| }); | ||
|
|
||
| // https://github.com/solidjs/solid-start/issues/1659 | ||
| it("keeps picked exports declared via specifiers, including aliases", async () => { | ||
| const code = ` | ||
| const handler = () => "ok"; | ||
| export { handler as GET, handler as POST }; | ||
| `; | ||
|
|
||
| const output = await shake(code, ["GET"]); | ||
|
|
||
| expect(output).toContain("handler as GET"); | ||
| expect(output).not.toContain("POST"); | ||
| }); | ||
|
|
||
| it("splits mixed variable declarations while preserving evaluation order", async () => { | ||
| const code = ` | ||
| export const first = 1, GET = () => first; | ||
| `; | ||
|
|
||
| const output = await shake(code, ["GET"]); | ||
|
|
||
| expect(output).toMatch(/const first = 1;\s*export const GET/); | ||
| expect(output).not.toContain("export const first"); | ||
| }); | ||
|
|
||
| it("keeps a named default export that a picked export references", async () => { | ||
| const code = ` | ||
| export default function Page() { | ||
| return "page"; | ||
| } | ||
| export const GET = () => Page(); | ||
| `; | ||
|
|
||
| const output = await shake(code, ["GET"]); | ||
|
|
||
| expect(output).toContain("function Page()"); | ||
| expect(output).not.toContain("export default"); | ||
| expect(output).toContain("export const GET"); | ||
| }); | ||
|
|
||
| it("removes an unreferenced default export, including anonymous ones", async () => { | ||
| const named = await shake( | ||
| ` | ||
| export default function Page() { return "page"; } | ||
| export const GET = () => "ok"; | ||
| `, | ||
| ["GET"], | ||
| ); | ||
| expect(named).not.toContain("Page"); | ||
|
|
||
| const anonymous = await shake( | ||
| ` | ||
| export default function () { return "page"; } | ||
| export const GET = () => "ok"; | ||
| `, | ||
| ["GET"], | ||
| ); | ||
| expect(anonymous).not.toContain("export default"); | ||
| expect(anonymous).toContain("export const GET"); | ||
| }); | ||
|
|
||
| it("removes imports only used by removed exports", async () => { | ||
| const code = ` | ||
| import { db } from "./db.ts"; | ||
| export const POST = () => db.write(); | ||
| export const GET = () => "ok"; | ||
| `; | ||
|
|
||
| const output = await shake(code, ["GET"]); | ||
|
|
||
| expect(output).not.toContain("db"); | ||
| expect(output).toContain("export const GET"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lxsmnsyc do these changes make sense?