Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - entrypoint detection is a Node filesystem boundary.
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import * as NodeURL from "node:url";
6
7import { describe, expect, it } from "vite-plus/test";
8
9import { isEntrypoint } from "./entrypoint.ts";
10
11const makeTempDir = () => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-entrypoint-test-"));
12
13describe("isEntrypoint", () => {
14 it("uses the runtime answer when Node provides one", () => {
15 // Node 22.18+ and 24.2+ populate `import.meta.main`; nothing else is consulted.
16 expect(
17 isEntrypoint({
18 moduleUrl: "file:///somewhere/bin.mjs",
19 entryPath: "/elsewhere/other.mjs",
20 runtimeMain: true,
21 }),
22 ).toBe(true);
23 expect(
24 isEntrypoint({
25 moduleUrl: "file:///somewhere/bin.mjs",
26 entryPath: "/somewhere/bin.mjs",
27 runtimeMain: false,
28 }),
29 ).toBe(false);
30 });
31
32 it("matches the entrypoint path when the runtime has no import.meta.main", () => {
33 // Node 22.16, 22.17 and 23.11 are inside `engines.node` but leave it undefined.
34 const dir = makeTempDir();
35 const entry = NodePath.join(dir, "bin.mjs");
36 NodeFS.writeFileSync(entry, "");
37
38 expect(
39 isEntrypoint({
40 moduleUrl: NodeURL.pathToFileURL(entry).href,
41 entryPath: entry,
42 runtimeMain: undefined,
43 }),
44 ).toBe(true);
45 });
46
47 it("matches through a symlinked entrypoint, as npm and npx install it", () => {
48 const dir = makeTempDir();
49 const real = NodePath.join(dir, "bin.mjs");
50 const link = NodePath.join(dir, "t3");
51 NodeFS.writeFileSync(real, "");
52 NodeFS.symlinkSync(real, link);
53
54 expect(
55 isEntrypoint({
56 moduleUrl: NodeURL.pathToFileURL(real).href,
57 entryPath: link,
58 runtimeMain: undefined,
59 }),
60 ).toBe(true);
61 });
62
63 it("stays false for an imported module that is not the entrypoint", () => {
64 // This is what keeps `bin.test.ts` from launching the CLI on import.
65 const dir = makeTempDir();
66 const entry = NodePath.join(dir, "bin.mjs");
67 const imported = NodePath.join(dir, "cli.mjs");
68 NodeFS.writeFileSync(entry, "");
69 NodeFS.writeFileSync(imported, "");
70
71 expect(
72 isEntrypoint({
73 moduleUrl: NodeURL.pathToFileURL(imported).href,
74 entryPath: entry,
75 runtimeMain: undefined,
76 }),
77 ).toBe(false);
78 });
79
80 it("stays false when there is no entrypoint argument", () => {
81 expect(
82 isEntrypoint({
83 moduleUrl: "file:///somewhere/bin.mjs",
84 entryPath: undefined,
85 runtimeMain: undefined,
86 }),
87 ).toBe(false);
88 });
89});