-
Notifications
You must be signed in to change notification settings - Fork 424
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
Merged
+479
−34
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7f10eeb
fix: keep non-picked route exports that picked exports reference
brenelz f70faed
fix: satisfy validate-imports in tree-shake spec fixture
brenelz 26ea77c
Merge branch 'main' into fix/tree-shake-referenced-exports
birkskyum d1fca4d
Merge branch 'main' into fix/tree-shake-referenced-exports
birkskyum 6564baf
Merge branch 'main' into fix/tree-shake-referenced-exports
brenelz c137f34
fix: remove unreachable route export dependencies
brenelz 3347ea6
Merge branch 'main' into fix/tree-shake-referenced-exports
birkskyum 2e841f9
fix: improve tree-shake logic and variable naming for clarity
birkskyum 60aa949
Merge branch 'main' into fix/tree-shake-referenced-exports
birkskyum 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.
Uh oh!
There was an error while loading. Please reload this page.