Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-types-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

Remove type-only import remnants from client server-function transforms so they do not retain server-only dependency chains.
62 changes: 62 additions & 0 deletions packages/start/src/directives/compile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, expect, it } from "vitest";
import { compile, type CompileOptions } from "./compile.ts";

const clientOptions: CompileOptions = {
env: "development",
mode: "client",
directive: "use server",
definitions: {
register: {
kind: "named",
name: "createServerReference",
source: "virtual:server-runtime",
},
clone: {
kind: "named",
name: "cloneServerReference",
source: "virtual:server-runtime",
},
},
};

describe("compile", () => {
it("removes an import when only type specifiers remain", async () => {
const result = await compile(
"/src/server-action.ts",
`
import { type Session, verify } from "./server-module.ts";

export const serverAction = async (): Promise<Session | null> => {
"use server";
return verify();
};
`,
clientOptions,
);

expect(result.valid).toBe(true);
expect(result.code).not.toContain("./server-module.ts");
});

it("preserves live value specifiers from a mixed import", async () => {
const result = await compile(
"/src/server-action.ts",
`
import { type Session, clientValue, verify } from "./server-module.ts";

export const value = clientValue;
export const serverAction = async (): Promise<Session | null> => {
"use server";
return verify();
};
`,
clientOptions,
);

expect(result.valid).toBe(true);
expect(result.code).toContain(
'import { type Session, clientValue } from "./server-module.ts";',
);
expect(result.code).not.toMatch(/\bverify\b/);
});
});
18 changes: 17 additions & 1 deletion packages/start/src/directives/remove-unused-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ function isInvalidForRemoval(path: babel.NodePath) {
return isPathValid(target, t.isObjectPattern) || isPathValid(target, t.isArrayPattern);
}

function countValidImport(node: t.ImportDeclaration): number {
if (node.importKind === "type") {
return 0;
}

let count = 0;

for (const specifier of node.specifiers) {
if (specifier.type !== "ImportSpecifier" || specifier.importKind === "value") {
count += 1;
}
}

return count;
}

export function removeUnusedVariables(program: babel.NodePath<t.Program>) {
// TODO(Alexis):
// This implementation is simple but slow
Expand All @@ -40,7 +56,7 @@ export function removeUnusedVariables(program: babel.NodePath<t.Program>) {
if (binding.references === 0 && !binding.path.removed) {
const parent = binding.path.parentPath;
if (isPathValid(parent, t.isImportDeclaration)) {
if (parent.node.specifiers.length === 1) {
if (countValidImport(parent.node) <= 1) {
parent.remove();
} else {
binding.path.remove();
Expand Down
Loading