|
| 1 | +const path = require("path"); |
| 2 | + |
| 3 | +const compilerDir = path.resolve(__dirname, ".."); |
| 4 | +const { transformDirectives, transformLazy, transformRefresh } = require(compilerDir); |
| 5 | + |
| 6 | +// Another front end (ReactiveScript's `.rx`, through a bundler's frontends hook) |
| 7 | +// lowers its own modules to Solid's output, then hands that ordinary |
| 8 | +// JavaScript to the secondary passes under the module's own filename — the |
| 9 | +// path server-function ids hash, as the bundler's manifest keys it. |
| 10 | +const ROOT = "/project"; |
| 11 | +const FILENAME = `${ROOT}/src/routes/App.rx`; |
| 12 | +const generated = ` |
| 13 | + export async function save(value) { |
| 14 | + "use server"; |
| 15 | + return value; |
| 16 | + } |
| 17 | +`; |
| 18 | + |
| 19 | +describe("secondary passes for another front end's modules", () => { |
| 20 | + it.each(["server", "client"])( |
| 21 | + "transforms the generated JavaScript under the original path in %s mode", |
| 22 | + mode => { |
| 23 | + const result = transformDirectives(generated, { |
| 24 | + filename: FILENAME, |
| 25 | + root: ROOT, |
| 26 | + mode, |
| 27 | + env: "production" |
| 28 | + }); |
| 29 | + expect(result.valid).toBe(true); |
| 30 | + expect(result.functions).toHaveLength(1); |
| 31 | + expect(result.functions[0].name).toBe("save"); |
| 32 | + } |
| 33 | + ); |
| 34 | + |
| 35 | + it("hashes the module's own path, not a borrowed extension", () => { |
| 36 | + const own = transformDirectives(generated, { |
| 37 | + filename: FILENAME, |
| 38 | + root: ROOT, |
| 39 | + mode: "server", |
| 40 | + env: "production" |
| 41 | + }); |
| 42 | + const client = transformDirectives(generated, { |
| 43 | + filename: FILENAME, |
| 44 | + root: ROOT, |
| 45 | + mode: "client", |
| 46 | + env: "production" |
| 47 | + }); |
| 48 | + const borrowed = transformDirectives(generated, { |
| 49 | + filename: `${FILENAME}.tsx`, |
| 50 | + root: ROOT, |
| 51 | + mode: "server", |
| 52 | + env: "production" |
| 53 | + }); |
| 54 | + expect(client.functions[0].id).toBe(own.functions[0].id); |
| 55 | + expect(borrowed.functions[0].id).not.toBe(own.functions[0].id); |
| 56 | + }); |
| 57 | + |
| 58 | + it("runs the lazy and refresh passes under that filename", () => { |
| 59 | + const lazy = transformLazy( |
| 60 | + `import { lazy } from "solid-js";\nexport const Page = lazy(() => import("./Page.rx"));\n`, |
| 61 | + { filename: FILENAME } |
| 62 | + ); |
| 63 | + expect(lazy.code).toContain("lazy("); |
| 64 | + const refresh = transformRefresh(`export function App() { return null; }\n`, { |
| 65 | + filename: FILENAME, |
| 66 | + bundler: "vite", |
| 67 | + jsx: false |
| 68 | + }); |
| 69 | + expect(typeof refresh.code).toBe("string"); |
| 70 | + }); |
| 71 | +}); |
0 commit comments