import { rollup } from "rollup"; import type { LogLevel, Plugin, RollupLog } from "rollup"; import { swc } from "../../src/swc"; type TransformResult = { code: string; map?: unknown }; /** * Invokes the swc plugin's `transform` hook directly. The hook loads * `@swc/core` (Node) under the hood, so these are real transforms. */ const runTransform = async ( code: string, id = "/project/src/component.tsx", ): Promise => { const plugin: Plugin = swc(); if (typeof plugin.transform !== "function") { throw new Error("swc plugin does not expose a transform hook"); } const result = await ( plugin.transform as ( code: string, id: string, ) => Promise ).call({} as never, code, id); if (!result) { throw new Error("swc plugin returned no result"); } return result; }; describe("swc plugin", () => { describe("RSC directive stripping", () => { test("blanks a leading 'use client' directive", async () => { const body = 'import { x } from "./x";\nexport const y = x;\n'; const withDirective = await runTransform(`"use client";\n${body}`); const baseline = await runTransform(body); // The directive text is gone from the output entirely. expect(withDirective.code).not.toContain("use client"); // It is blanked in place (not deleted): the first line is whitespace, // the line count is preserved, and the rest of the module is byte-for-byte // identical to transforming the same source without the directive. const [firstLine, ...rest] = withDirective.code.split("\n"); expect(firstLine).toMatch(/^\s*$/); expect(rest.join("\n")).toBe(baseline.code); }); test("blanks a leading 'use server' directive", async () => { const result = await runTransform( '"use server";\nexport const action = () => 1;\n', ); expect(result.code).not.toContain("use server"); expect(result.code.split("\n")[0]).toMatch(/^\s*$/); expect(result.code).toContain("action"); }); test("handles single-quoted directives", async () => { const result = await runTransform("'use client';\nexport const x = 1;\n"); expect(result.code).not.toContain("use client"); expect(result.code).toContain("x = 1"); }); test("preserves a 'use strict' directive (Rollup does not warn about it)", async () => { const result = await runTransform('"use strict";\nexport const x = 1;\n'); expect(result.code).toContain('"use strict"'); }); test("leaves a module without a directive untouched", async () => { const result = await runTransform('export const x = "use client";\n'); // The string is a value, not a directive — it must survive. expect(result.code).toContain('"use client"'); expect(result.code.startsWith(" ")).toBe(false); }); test("returns a sourcemap alongside the stripped code", async () => { const result = await runTransform('"use client";\nexport const x = 1;\n'); expect(result.map).toBeDefined(); }); }); describe("integration with Rollup", () => { const virtualEntry = (code: string): Plugin => ({ name: "virtual-entry", resolveId: (id) => (id === "entry.js" ? id : null), load: (id) => (id === "entry.js" ? code : null), }); const collectWarnings = async (plugins: Plugin[]): Promise => { const warnings: string[] = []; const bundle = await rollup({ input: "entry.js", plugins, onLog: (level: LogLevel, log: RollupLog) => { if (level === "warn" && log.code) { warnings.push(log.code); } }, }); await bundle.close(); return warnings; }; const SOURCE = '"use client";\nexport const x = 1;\n'; test("does not emit MODULE_LEVEL_DIRECTIVE when the swc plugin runs", async () => { const warnings = await collectWarnings([virtualEntry(SOURCE), swc()]); expect(warnings).not.toContain("MODULE_LEVEL_DIRECTIVE"); }); // Control: proves the warning is real and that the swc plugin is what // suppresses it — without the plugin the directive reaches Rollup's parse. test("control: Rollup DOES warn when the directive is left in place", async () => { const warnings = await collectWarnings([virtualEntry(SOURCE)]); expect(warnings).toContain("MODULE_LEVEL_DIRECTIVE"); }); }); });