@akanjs/devkit 3.3.0-rc.38 → 3.3.0-rc.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/frontendBuild/allRoutesBuilder.ts +7 -2
- package/frontendBuild/buildRouteClient.test.ts +65 -0
- package/frontendBuild/clientBuildTypes.ts +11 -0
- package/frontendBuild/clientEntriesBundler.ts +21 -11
- package/frontendBuild/clientEntryDiscovery.test.ts +20 -0
- package/frontendBuild/clientEntryDiscovery.ts +10 -2
- package/frontendBuild/renderedEntries.ts +25 -10
- package/frontendBuild/routeClientBuilder.ts +65 -27
- package/frontendBuild/routeModulePreloads.test.ts +32 -0
- package/frontendBuild/routeModulePreloads.ts +20 -11
- package/frontendBuild/routeRuntimeCheck.test.ts +23 -0
- package/frontendBuild/routeRuntimeCheck.ts +10 -3
- package/package.json +3 -3
- package/transforms/barrelImportsPlugin.ts +56 -40
- package/transforms/exportReach.ts +4 -0
- package/transforms/moduleParts.test.ts +225 -0
- package/transforms/moduleParts.ts +418 -0
- package/transforms/partImports.test.ts +76 -0
- package/transforms/partImports.ts +110 -0
- package/transforms/partModules.test.ts +187 -0
- package/transforms/partModules.ts +90 -0
- package/transforms/partPurity.ts +124 -0
|
@@ -72,6 +72,29 @@ describe("what a build loading per module checks in its own output", () => {
|
|
|
72
72
|
expect(failuresOf({ imports: lazy, chunks: apart })).toEqual([]);
|
|
73
73
|
});
|
|
74
74
|
|
|
75
|
+
test("a file bundled in parts is checked part by part: only the chunk of the part importing a piece has to reach it", () => {
|
|
76
|
+
const graph = graphOf({
|
|
77
|
+
imports: { [card]: [storePiece], [storePiece]: [] },
|
|
78
|
+
chunks: { [card]: ["/out/naming.js", "/out/plain.js"], [storePiece]: ["/out/piece.js"] },
|
|
79
|
+
chunkImports: { "/out/naming.js": ["/out/piece.js"] },
|
|
80
|
+
});
|
|
81
|
+
const part = (id: string) => `${abs(card)}?akan-part=${id}`;
|
|
82
|
+
const linked = [
|
|
83
|
+
{ module: part("p0"), imports: [{ path: abs(storePiece), lazy: false }], chunks: ["/out/naming.js"] },
|
|
84
|
+
{ module: part("p1"), imports: [], chunks: ["/out/plain.js"] },
|
|
85
|
+
{ module: abs(storePiece), imports: [], chunks: ["/out/piece.js"] },
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
expect(new RouteRuntimeCheck(root, graph, null).failures).toHaveLength(1);
|
|
89
|
+
expect(new RouteRuntimeCheck(root, { ...graph, linked }, null).failures).toEqual([]);
|
|
90
|
+
const misplaced = linked.map((input) =>
|
|
91
|
+
input.module === part("p0") ? { ...input, chunks: ["/out/plain.js"] } : input,
|
|
92
|
+
);
|
|
93
|
+
expect(new RouteRuntimeCheck(root, { ...graph, linked: misplaced }, null).failures).toEqual([
|
|
94
|
+
"apps/demo/ui/Card.tsx?akan-part=p0 needs apps/demo/lib/__client/memo.st.ts loaded first, but its chunk plain.js does not import the chunk holding that piece",
|
|
95
|
+
]);
|
|
96
|
+
});
|
|
97
|
+
|
|
75
98
|
test("no store file is imported back by a runtime piece that a file it imports loads", () => {
|
|
76
99
|
const helper = "apps/demo/webkit/relabel.ts";
|
|
77
100
|
const cycle = { [storeFile]: [helper], [helper]: [storePiece], [storePiece]: [storeFile] };
|
|
@@ -119,9 +119,16 @@ export class RouteRuntimeCheck {
|
|
|
119
119
|
|
|
120
120
|
#unordered(): string[] {
|
|
121
121
|
const failures: string[] = [];
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
122
|
+
const linked =
|
|
123
|
+
this.#graph.linked ??
|
|
124
|
+
[...this.#graph.chunksByModule].map(([module, chunks]) => ({
|
|
125
|
+
module,
|
|
126
|
+
chunks,
|
|
127
|
+
imports: this.#graph.importsByModule.get(module) ?? [],
|
|
128
|
+
}));
|
|
129
|
+
for (const { module, chunks, imports } of linked)
|
|
130
|
+
for (const { path: piece, lazy } of imports) {
|
|
131
|
+
if (lazy || !this.#isPiece(piece)) continue;
|
|
125
132
|
const holdersOf = this.#loadedFirstFor(piece).map((first) => this.#graph.chunksByModule.get(first) ?? []);
|
|
126
133
|
const unlinked = chunks.find((chunk) =>
|
|
127
134
|
holdersOf.some((holders) => !holders.some((holder) => this.#reachedBy(chunk).has(holder))),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akanjs/devkit",
|
|
3
|
-
"version": "3.3.0-rc.
|
|
3
|
+
"version": "3.3.0-rc.39",
|
|
4
4
|
"sourceType": "module",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"./workspace/getRelatedCnsts": "./workspace/getRelatedCnsts.ts"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@akanjs/language": "3.3.0-rc.
|
|
53
|
+
"@akanjs/language": "3.3.0-rc.39",
|
|
54
54
|
"@earendil-works/pi-agent-core": "1.5.2",
|
|
55
55
|
"@earendil-works/pi-ai": "1.5.2",
|
|
56
56
|
"@earendil-works/pi-codemode": "1.5.2",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"@earendil-works/pi-tui": "1.5.2",
|
|
60
60
|
"@inquirer/prompts": "^8.7.3",
|
|
61
61
|
"@tailwindcss/node": "^4.3.3",
|
|
62
|
-
"akanjs": "3.3.0-rc.
|
|
62
|
+
"akanjs": "3.3.0-rc.39",
|
|
63
63
|
"chalk": "^6.0.1",
|
|
64
64
|
"commander": "^15.0.0",
|
|
65
65
|
"dayjs": "^1.11.23",
|
|
@@ -4,8 +4,10 @@ import ts from "typescript";
|
|
|
4
4
|
import { StoreBootstrap } from "../artifact/storeBootstrap";
|
|
5
5
|
import type { App } from "../commandDecorators";
|
|
6
6
|
import { BarrelAnalyzer, type BarrelExportMap, type PackageEntry } from "./barrelAnalyzer";
|
|
7
|
+
import { ModuleParts } from "./moduleParts";
|
|
7
8
|
import { loaderFor, type NamedItem } from "./moduleSyntax";
|
|
8
9
|
import { ObjectMemberImports } from "./objectMemberImports";
|
|
10
|
+
import { PartModules, type PartSource } from "./partModules";
|
|
9
11
|
import type { RouteRuntime } from "./routeRuntime";
|
|
10
12
|
|
|
11
13
|
export interface BarrelImportsPluginOptions {
|
|
@@ -21,6 +23,8 @@ export interface BarrelImportsPluginOptions {
|
|
|
21
23
|
modelRuntime?: string;
|
|
22
24
|
/** Gives each client module the runtime pieces of the modules it names, in a build loading only the page runtime. */
|
|
23
25
|
routeRuntime?: RouteRuntime | null;
|
|
26
|
+
/** Takes each workspace file apart by export, so a chunk holds the exports asked for; holds the parts read so far. */
|
|
27
|
+
parts?: Map<string, ModuleParts | null>;
|
|
24
28
|
}
|
|
25
29
|
|
|
26
30
|
export const createBarrelImportsPlugin = async (
|
|
@@ -32,6 +36,7 @@ export const createBarrelImportsPlugin = async (
|
|
|
32
36
|
barrels: only,
|
|
33
37
|
modelRuntime,
|
|
34
38
|
routeRuntime,
|
|
39
|
+
parts,
|
|
35
40
|
}: BarrelImportsPluginOptions = {},
|
|
36
41
|
): Promise<BunPlugin> => {
|
|
37
42
|
const barrels = [...new Set(only ?? (await app.getConfig()).barrelImports)].filter(
|
|
@@ -43,59 +48,70 @@ export const createBarrelImportsPlugin = async (
|
|
|
43
48
|
resolvePackage,
|
|
44
49
|
...(route ? { packageRoot: app.workspace.workspaceRoot } : {}),
|
|
45
50
|
});
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
:
|
|
51
|
+
const resolve = ObjectMemberImports.resolverOf(resolvePackage);
|
|
52
|
+
const objects = route ? new ObjectMemberImports({ resolve, barrels: { names: barrels, analyzer } }) : null;
|
|
53
|
+
|
|
54
|
+
const read = async (realPath: string): Promise<PartSource | null> => {
|
|
55
|
+
const file = Bun.file(realPath);
|
|
56
|
+
//? A build's `files` entry has no copy on disk: Bun loads it itself.
|
|
57
|
+
if (!(await file.exists())) return null;
|
|
58
|
+
if (modelRuntime !== undefined && StoreBootstrap.modelRuntimeFilter.test(realPath))
|
|
59
|
+
return { source: modelRuntime, settled: true };
|
|
60
|
+
const whole = routeRuntime?.redirectOf(realPath);
|
|
61
|
+
if (whole) return { source: whole, settled: true };
|
|
62
|
+
let source = await file.text();
|
|
63
|
+
if (skipPath(realPath)) return { source, settled: true };
|
|
64
|
+
|
|
65
|
+
// Bun's macro evaluator races when a rewritten module holds `with { type: "macro" }` imports while another
|
|
66
|
+
// macro host evaluates (a macro identifier ends up undefined), so macro hosts keep their barrel imports.
|
|
67
|
+
const macroHost = MACRO_ATTR_RE.test(source);
|
|
68
|
+
if (!macroHost) {
|
|
69
|
+
const routed = routeRuntime?.transform(source, realPath);
|
|
70
|
+
if (routed) source = routed.source;
|
|
71
|
+
const rewritten =
|
|
72
|
+
barrels.length > 0
|
|
73
|
+
? await rewriteBarrelImports(source, barrels, analyzer, {
|
|
74
|
+
perModule: routed?.perModule,
|
|
75
|
+
reexports: route,
|
|
76
|
+
})
|
|
77
|
+
: null;
|
|
78
|
+
if (rewritten !== null) source = rewritten;
|
|
79
|
+
const narrowed = await objects?.rewrite(source, realPath);
|
|
80
|
+
if (narrowed?.contents) source = narrowed.contents;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (pipeAfter) {
|
|
84
|
+
const piped = await pipeAfter(source, { path: realPath });
|
|
85
|
+
if (piped !== null) source = piped;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return { source, settled: macroHost };
|
|
89
|
+
};
|
|
90
|
+
const partModules =
|
|
91
|
+
parts && route
|
|
92
|
+
? new PartModules({ workspaceRoot: app.workspace.workspaceRoot, resolve, read, cache: parts })
|
|
93
|
+
: null;
|
|
52
94
|
|
|
53
95
|
return {
|
|
54
96
|
name: "barrel-imports",
|
|
55
97
|
setup(build) {
|
|
98
|
+
if (partModules) build.onResolve({ filter: ModuleParts.specifier }, ({ path: asked }) => ({ path: asked }));
|
|
56
99
|
// node_modules/akanjs stays in: framework `"use client"` modules still need RSC stubbing in generated workspaces.
|
|
57
100
|
// The optional `?v=N` tail matches HMR cache-busted paths; it is stripped before reading.
|
|
58
101
|
build.onLoad(
|
|
59
102
|
{
|
|
60
103
|
filter:
|
|
61
|
-
/^(?:(?!.*[\\/]node_modules[\\/]).*|.*[\\/]node_modules[\\/]akanjs[\\/].*)\.(tsx|ts|jsx|js)(\?v=\d+)?$/,
|
|
104
|
+
/^(?:(?!.*[\\/]node_modules[\\/]).*|.*[\\/]node_modules[\\/]akanjs[\\/].*)\.(tsx|ts|jsx|js)(\?v=\d+|\?akan-part=[\w$]+)?$/,
|
|
62
105
|
},
|
|
63
106
|
async (args) => {
|
|
64
107
|
const realPath = args.path.replace(/\?v=\d+$/, "");
|
|
65
|
-
const loader = loaderFor(realPath);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (modelRuntime !== undefined && StoreBootstrap.modelRuntimeFilter.test(realPath))
|
|
70
|
-
return { contents: modelRuntime, loader };
|
|
71
|
-
const whole = routeRuntime?.redirectOf(realPath);
|
|
72
|
-
if (whole) return { contents: whole, loader };
|
|
73
|
-
let source = await file.text();
|
|
74
|
-
if (skipPath(realPath)) return { contents: source, loader };
|
|
75
|
-
|
|
76
|
-
// Bun's macro evaluator races when a rewritten module holds `with { type: "macro" }` imports while another
|
|
77
|
-
// macro host evaluates (a macro identifier ends up undefined), so macro hosts keep their barrel imports.
|
|
78
|
-
if (!MACRO_ATTR_RE.test(source)) {
|
|
79
|
-
const routed = routeRuntime?.transform(source, realPath);
|
|
80
|
-
if (routed) source = routed.source;
|
|
81
|
-
const rewritten =
|
|
82
|
-
barrels.length > 0
|
|
83
|
-
? await rewriteBarrelImports(source, barrels, analyzer, {
|
|
84
|
-
perModule: routed?.perModule,
|
|
85
|
-
reexports: route,
|
|
86
|
-
})
|
|
87
|
-
: null;
|
|
88
|
-
if (rewritten !== null) source = rewritten;
|
|
89
|
-
const narrowed = await objects?.rewrite(source, realPath);
|
|
90
|
-
if (narrowed?.contents) source = narrowed.contents;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (pipeAfter) {
|
|
94
|
-
const piped = await pipeAfter(source, { path: realPath });
|
|
95
|
-
if (piped !== null) source = piped;
|
|
108
|
+
const loader = loaderFor(PartModules.fileOf(realPath));
|
|
109
|
+
if (partModules) {
|
|
110
|
+
const contents = await partModules.load(realPath);
|
|
111
|
+
return contents === null ? undefined : { contents, loader };
|
|
96
112
|
}
|
|
97
|
-
|
|
98
|
-
return { contents: source, loader };
|
|
113
|
+
const loaded = await read(realPath);
|
|
114
|
+
return loaded ? { contents: loaded.source, loader } : undefined;
|
|
99
115
|
},
|
|
100
116
|
);
|
|
101
117
|
},
|
|
@@ -245,6 +245,10 @@ export class ExportReach {
|
|
|
245
245
|
);
|
|
246
246
|
}
|
|
247
247
|
|
|
248
|
+
static namesReadBy(root: ts.Node): Set<string> {
|
|
249
|
+
return new Set(ExportReach.#unitOf(root).reads.keys());
|
|
250
|
+
}
|
|
251
|
+
|
|
248
252
|
static #unitOf(root: ts.Node): Unit {
|
|
249
253
|
const unit: Unit = { reads: new Map(), dynamic: [] };
|
|
250
254
|
const read = (name: string, member: string | null) => {
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { ModuleParts } from "./moduleParts";
|
|
3
|
+
|
|
4
|
+
const self = "/repo/apps/demo/ui/Pair.tsx";
|
|
5
|
+
const from = (id: string) => `"${self}?akan-part=${id}"`;
|
|
6
|
+
const read = (lines: string[]) => {
|
|
7
|
+
const parts = ModuleParts.read(`${lines.join("\n")}\n`, self);
|
|
8
|
+
if (!parts) throw new Error("the file stayed one module");
|
|
9
|
+
return parts;
|
|
10
|
+
};
|
|
11
|
+
const linesOf = (parts: ModuleParts, id: string) => (parts.sourceOf(id, self) ?? "").trimEnd().split("\n");
|
|
12
|
+
const partOf = (parts: ModuleParts, exported: string) => linesOf(parts, parts.partOf(exported) ?? "");
|
|
13
|
+
|
|
14
|
+
describe("ModuleParts", () => {
|
|
15
|
+
test("each export that only builds a value is a part holding the imports it alone reads", () => {
|
|
16
|
+
const parts = read([
|
|
17
|
+
'"use client";',
|
|
18
|
+
'import { cn } from "./cn";',
|
|
19
|
+
'import Heavy, { type Props, tune as tuned } from "./Heavy";',
|
|
20
|
+
'import * as icons from "./icons";',
|
|
21
|
+
"export const A = () => cn(icons.a);",
|
|
22
|
+
"export const B = (props: Props) => [Heavy, tuned, props];",
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
expect(parts.ids).toEqual(["p0", "p1"]);
|
|
26
|
+
expect(parts.exports).toEqual(["A", "B"]);
|
|
27
|
+
expect(linesOf(parts, "p0")).toEqual([
|
|
28
|
+
'"use client";',
|
|
29
|
+
'import { cn } from "./cn";',
|
|
30
|
+
'import * as icons from "./icons";',
|
|
31
|
+
"export const A = () => cn(icons.a);",
|
|
32
|
+
]);
|
|
33
|
+
expect(linesOf(parts, "p1")).toEqual([
|
|
34
|
+
'"use client";',
|
|
35
|
+
'import Heavy, { tune as tuned } from "./Heavy";',
|
|
36
|
+
"export const B = (props: Props) => [Heavy, tuned, props];",
|
|
37
|
+
]);
|
|
38
|
+
expect(parts.facade(self)).toBe(
|
|
39
|
+
['"use client";', `export { A } from ${from("p0")};`, `export { B } from ${from("p1")};`, ""].join("\n"),
|
|
40
|
+
);
|
|
41
|
+
expect(parts.sourceOf("p9", self)).toBeNull();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("what the file does when it loads is `rest`, which every part imports before anything else", () => {
|
|
45
|
+
const parts = read([
|
|
46
|
+
'import "./side.css";',
|
|
47
|
+
'import { track } from "./track";',
|
|
48
|
+
'import { cn } from "./cn";',
|
|
49
|
+
"const registry = new Map<string, number>();",
|
|
50
|
+
'registry.set("x", 1);',
|
|
51
|
+
"track();",
|
|
52
|
+
"export const A = () => cn();",
|
|
53
|
+
'export const D = () => registry.get("x");',
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
expect(parts.ids).toEqual(["p0", "p1", "rest"]);
|
|
57
|
+
expect(linesOf(parts, "rest")).toEqual([
|
|
58
|
+
'import "./side.css";',
|
|
59
|
+
'import { track } from "./track";',
|
|
60
|
+
"const registry = new Map<string, number>();",
|
|
61
|
+
'registry.set("x", 1);',
|
|
62
|
+
"track();",
|
|
63
|
+
"export { registry as __akanPart_registry };",
|
|
64
|
+
]);
|
|
65
|
+
expect(linesOf(parts, "p0")).toEqual([
|
|
66
|
+
`import ${from("rest")};`,
|
|
67
|
+
'import { cn } from "./cn";',
|
|
68
|
+
"export const A = () => cn();",
|
|
69
|
+
]);
|
|
70
|
+
expect(linesOf(parts, "p1")).toEqual([
|
|
71
|
+
`import ${from("rest")};`,
|
|
72
|
+
`import { __akanPart_registry as registry } from ${from("rest")};`,
|
|
73
|
+
'export const D = () => registry.get("x");',
|
|
74
|
+
]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("a declaration no export names joins the one export reading it, and is lent out when two read it", () => {
|
|
78
|
+
const parts = read([
|
|
79
|
+
"const double = (value: number) => value * 2;",
|
|
80
|
+
"const only = { deep: true };",
|
|
81
|
+
"const unread = 1;",
|
|
82
|
+
"export const A = () => double(1);",
|
|
83
|
+
"export const B = () => double(2);",
|
|
84
|
+
"export const C = () => only;",
|
|
85
|
+
]);
|
|
86
|
+
|
|
87
|
+
expect(parts.ids).toEqual(["p0", "p1", "p2", "p3"]);
|
|
88
|
+
expect(linesOf(parts, "p0")).toEqual([
|
|
89
|
+
"const double = (value: number) => value * 2;",
|
|
90
|
+
"export { double as __akanPart_double };",
|
|
91
|
+
]);
|
|
92
|
+
for (const [name, call] of [
|
|
93
|
+
["A", "double(1)"],
|
|
94
|
+
["B", "double(2)"],
|
|
95
|
+
] as const)
|
|
96
|
+
expect(partOf(parts, name)).toEqual([
|
|
97
|
+
`import { __akanPart_double as double } from ${from("p0")};`,
|
|
98
|
+
`export const ${name} = () => ${call};`,
|
|
99
|
+
]);
|
|
100
|
+
expect(partOf(parts, "C")).toEqual(["const only = { deep: true };", "export const C = () => only;"]);
|
|
101
|
+
for (const id of parts.ids) expect(parts.sourceOf(id, self)).not.toContain("unread");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("declarations reading each other in a cycle are one part", () => {
|
|
105
|
+
const parts = read([
|
|
106
|
+
"export function even(n: number): boolean {",
|
|
107
|
+
" return n === 0 || odd(n - 1);",
|
|
108
|
+
"}",
|
|
109
|
+
"export function odd(n: number): boolean {",
|
|
110
|
+
" return n !== 0 && even(n - 1);",
|
|
111
|
+
"}",
|
|
112
|
+
"export const other = 1;",
|
|
113
|
+
]);
|
|
114
|
+
|
|
115
|
+
expect(parts.partOf("even")).toBe(parts.partOf("odd"));
|
|
116
|
+
expect(parts.partOf("other")).not.toBe(parts.partOf("even"));
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("a member given to a declared value stays in its part, and one built by a call moves it to `rest`", () => {
|
|
120
|
+
const parts = read([
|
|
121
|
+
'import { Item } from "./Item";',
|
|
122
|
+
'import { make } from "./make";',
|
|
123
|
+
"export const List = () => null;",
|
|
124
|
+
"List.Item = Item;",
|
|
125
|
+
"export const Menu = () => null;",
|
|
126
|
+
"Object.assign(Menu, { Item });",
|
|
127
|
+
"export const Made = () => null;",
|
|
128
|
+
"Made.extra = make();",
|
|
129
|
+
"export const Plain = () => null;",
|
|
130
|
+
]);
|
|
131
|
+
|
|
132
|
+
expect(partOf(parts, "List")).toEqual([
|
|
133
|
+
`import ${from("rest")};`,
|
|
134
|
+
'import { Item } from "./Item";',
|
|
135
|
+
"export const List = () => null;",
|
|
136
|
+
"List.Item = Item;",
|
|
137
|
+
]);
|
|
138
|
+
expect(partOf(parts, "Menu")).toContain("Object.assign(Menu, { Item });");
|
|
139
|
+
expect(parts.partOf("Made")).toBe("rest");
|
|
140
|
+
expect(partOf(parts, "Plain")).toEqual([`import ${from("rest")};`, "export const Plain = () => null;"]);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("a type is no export of any part, and every part carries the file's type declarations", () => {
|
|
144
|
+
const parts = read([
|
|
145
|
+
'import type { Outer } from "./types";',
|
|
146
|
+
"export interface Props { a: Outer }",
|
|
147
|
+
"type Local = Props;",
|
|
148
|
+
'export type { Other } from "./other";',
|
|
149
|
+
'export { type Third, value } from "./third";',
|
|
150
|
+
"export const A = (props: Local) => props;",
|
|
151
|
+
]);
|
|
152
|
+
|
|
153
|
+
expect(parts.exports).toEqual(["value", "A"]);
|
|
154
|
+
for (const name of ["Props", "Other", "Third"]) expect(parts.isType(name)).toBe(true);
|
|
155
|
+
expect(parts.isType("A")).toBe(false);
|
|
156
|
+
expect(partOf(parts, "value")).toEqual([
|
|
157
|
+
"export interface Props { a: Outer }",
|
|
158
|
+
"type Local = Props;",
|
|
159
|
+
'export { value } from "./third";',
|
|
160
|
+
]);
|
|
161
|
+
expect(partOf(parts, "A")).toEqual([
|
|
162
|
+
"export interface Props { a: Outer }",
|
|
163
|
+
"type Local = Props;",
|
|
164
|
+
"export const A = (props: Local) => props;",
|
|
165
|
+
]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test("an export list, a renamed export and a default export each go to the part of what they name", () => {
|
|
169
|
+
const parts = read([
|
|
170
|
+
'import { outer } from "./outer";',
|
|
171
|
+
"const a = 1;",
|
|
172
|
+
"const b = () => a;",
|
|
173
|
+
"export { a, b as c, outer };",
|
|
174
|
+
"export default b;",
|
|
175
|
+
]);
|
|
176
|
+
|
|
177
|
+
expect(new Set(parts.exports)).toEqual(new Set(["a", "c", "default", "outer"]));
|
|
178
|
+
expect(partOf(parts, "a")).toEqual(["const a = 1;", "export { a as __akanPart_a, a };"]);
|
|
179
|
+
expect(partOf(parts, "c")).toEqual([
|
|
180
|
+
`import { __akanPart_a as a } from ${from(parts.partOf("a") ?? "")};`,
|
|
181
|
+
"const b = () => a;",
|
|
182
|
+
"export { b as __akanPart_b, b as c };",
|
|
183
|
+
]);
|
|
184
|
+
expect(partOf(parts, "default")).toEqual([
|
|
185
|
+
`import { __akanPart_b as b } from ${from(parts.partOf("c") ?? "")};`,
|
|
186
|
+
"export default b;",
|
|
187
|
+
]);
|
|
188
|
+
expect(partOf(parts, "outer")).toEqual(['import { outer } from "./outer";', "export { outer };"]);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("an overloaded function keeps its signatures with its body", () => {
|
|
192
|
+
const parts = read([
|
|
193
|
+
"export function pick(value: string): string;",
|
|
194
|
+
"export function pick(value: number): number;",
|
|
195
|
+
"export function pick(value: unknown) {",
|
|
196
|
+
" return value;",
|
|
197
|
+
"}",
|
|
198
|
+
"export const other = 1;",
|
|
199
|
+
]);
|
|
200
|
+
|
|
201
|
+
expect(partOf(parts, "pick")).toEqual([
|
|
202
|
+
"export function pick(value: string): string;",
|
|
203
|
+
"export function pick(value: number): number;",
|
|
204
|
+
"export function pick(value: unknown) {",
|
|
205
|
+
" return value;",
|
|
206
|
+
"}",
|
|
207
|
+
]);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test.each([
|
|
211
|
+
["a single export", ["export const A = 1;"]],
|
|
212
|
+
["everything running on load", ["export const A = make();", "export const B = make();"]],
|
|
213
|
+
["a top-level `let`", ["export let count = 0;", "export const A = 1;", "export const B = 2;"]],
|
|
214
|
+
["`export *`", ['export * from "./other";', "export const A = 1;", "export const B = 2;"]],
|
|
215
|
+
["a namespace", ["namespace Inner {}", "export const A = 1;", "export const B = 2;"]],
|
|
216
|
+
["`export =`", ["const A = 1;", "const B = 2;", "export = A;"]],
|
|
217
|
+
[
|
|
218
|
+
"a macro import",
|
|
219
|
+
['import { read } from "./read" with { type: "macro" };', "export const A = 1;", "export const B = 2;"],
|
|
220
|
+
],
|
|
221
|
+
["an export of a name it does not hold", ["export { missing };", "export const A = 1;", "export const B = 2;"]],
|
|
222
|
+
])("a file with %s stays one module", (_, lines) => {
|
|
223
|
+
expect(ModuleParts.read(`${lines.join("\n")}\n`, self)).toBeNull();
|
|
224
|
+
});
|
|
225
|
+
});
|