|
| 1 | +import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import type { Plugin } from "vite"; |
| 5 | +import { afterEach, expect, it } from "vitest"; |
| 6 | + |
| 7 | +import { solidStart, type SolidStartOptions } from "./index.ts"; |
| 8 | + |
| 9 | +const cwd = process.cwd(); |
| 10 | +const roots: string[] = []; |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + process.chdir(cwd); |
| 14 | + for (const root of roots.splice(0)) rmSync(root, { recursive: true }); |
| 15 | +}); |
| 16 | + |
| 17 | +async function resolveDevConfig(options?: SolidStartOptions) { |
| 18 | + const root = realpathSync.native(mkdtempSync(join(tmpdir(), "solid-start-dev-toolbar-deps-"))); |
| 19 | + roots.push(root); |
| 20 | + mkdirSync(join(root, "src")); |
| 21 | + writeFileSync(join(root, "package.json"), "{}"); |
| 22 | + writeFileSync(join(root, "src/app.tsx"), "export default function App() {}"); |
| 23 | + process.chdir(root); |
| 24 | + |
| 25 | + const plugin = solidStart(options).find( |
| 26 | + (candidate): candidate is Plugin => |
| 27 | + typeof candidate === "object" && |
| 28 | + candidate !== null && |
| 29 | + "name" in candidate && |
| 30 | + candidate.name === "solid-start:config", |
| 31 | + ); |
| 32 | + const config = plugin?.config; |
| 33 | + const handler = typeof config === "function" ? config : config?.handler; |
| 34 | + return (await handler?.call({} as never, {}, { command: "serve", mode: "development" })) as |
| 35 | + | { environments?: { client?: { optimizeDeps?: { include?: string[] } } } } |
| 36 | + | undefined; |
| 37 | +} |
| 38 | + |
| 39 | +it("pre-bundles the dev toolbar's CommonJS dependencies", async () => { |
| 40 | + const config = await resolveDevConfig(); |
| 41 | + |
| 42 | + expect(config?.environments?.client?.optimizeDeps?.include).toEqual([ |
| 43 | + "@solidjs/start > source-map-js", |
| 44 | + "@solidjs/start > error-stack-parser", |
| 45 | + ]); |
| 46 | +}); |
| 47 | + |
| 48 | +it("leaves them alone when the dev toolbar is disabled", async () => { |
| 49 | + const config = await resolveDevConfig({ devOverlay: false }); |
| 50 | + |
| 51 | + expect(config?.environments?.client?.optimizeDeps).toBeUndefined(); |
| 52 | +}); |
0 commit comments