@akanjs/devkit 3.2.0-rc.11 → 3.2.0-rc.13

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.
@@ -123,7 +123,7 @@ describe("PagesBundleBuilder", () => {
123
123
  "",
124
124
  "const dictionary = {};",
125
125
  "const pageProto = makePageProto<typeof dict>(dictionary);",
126
- "const fetchProto = FetchClient.build<typeof signal>(cnst, getSerializedSignal(), { Err: pageProto.Err });",
126
+ "const fetchProto = FetchClient.build<typeof signal>(cnst, getSerializedSignal());",
127
127
  'export const runtime = registerClientRuntime({ ...pageProto, ...fetchProto }, { scope: "app" });',
128
128
  "",
129
129
  ].join("\n");
@@ -132,7 +132,7 @@ describe("PagesBundleBuilder", () => {
132
132
 
133
133
  expect(transformed).toContain('import { fetch as serverFetch } from "./sig";');
134
134
  expect(transformed).toContain(
135
- "const fetchProto = FetchClient.build<typeof signal>(cnst, serverFetch.serializedSignal, { Err: pageProto.Err, base: serverFetch });",
135
+ "const fetchProto = FetchClient.build<typeof signal>(cnst, serverFetch.serializedSignal, { base: serverFetch });",
136
136
  );
137
137
  expect(transformed).not.toContain("getSerializedSignal");
138
138
  expect(transformed).not.toContain('with { type: "macro" }');
@@ -2,11 +2,10 @@ import { describe, expect, test } from "bun:test";
2
2
  import { PagesBundleBuilder } from "./pagesBundleBuilder";
3
3
 
4
4
  const macroImport = `import { getSerializedSignal } from "./sig" with { type: "macro" };`;
5
- const macroFetchProto =
6
- "const fetchProto = FetchClient.build<typeof signal>(cnst, getSerializedSignal(), { Err: pageProto.Err });";
5
+ const macroFetchProto = "const fetchProto = FetchClient.build<typeof signal>(cnst, getSerializedSignal());";
7
6
  const serverImport = `import { fetch as serverFetch } from "./sig";`;
8
7
  const serverFetchProto =
9
- "const fetchProto = FetchClient.build<typeof signal>(cnst, serverFetch.serializedSignal, { Err: pageProto.Err, base: serverFetch });";
8
+ "const fetchProto = FetchClient.build<typeof signal>(cnst, serverFetch.serializedSignal, { base: serverFetch });";
10
9
 
11
10
  const generatedUseClient = (lines: { importLine?: string; fetchProtoLine?: string } = {}) =>
12
11
  [
@@ -196,7 +196,7 @@ export class PagesBundleBuilder {
196
196
  edits.push({
197
197
  start: tokens[fetchProtoAt].start,
198
198
  end: fetchProtoEnd.end,
199
- text: "const fetchProto = FetchClient.build<typeof signal>(cnst, serverFetch.serializedSignal, { Err: pageProto.Err, base: serverFetch });",
199
+ text: "const fetchProto = FetchClient.build<typeof signal>(cnst, serverFetch.serializedSignal, { base: serverFetch });",
200
200
  });
201
201
  let transformed = source;
202
202
  for (const { start, end, text } of edits.sort((a, b) => b.start - a.start))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/devkit",
3
- "version": "3.2.0-rc.11",
3
+ "version": "3.2.0-rc.13",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -41,7 +41,7 @@
41
41
  "./*": "./*.ts"
42
42
  },
43
43
  "dependencies": {
44
- "@akanjs/language": "3.2.0-rc.11",
44
+ "@akanjs/language": "3.2.0-rc.13",
45
45
  "@earendil-works/pi-agent-core": "1.3.0",
46
46
  "@earendil-works/pi-ai": "1.3.0",
47
47
  "@earendil-works/pi-codemode": "1.3.0",
@@ -50,7 +50,7 @@
50
50
  "@earendil-works/pi-tui": "1.3.0",
51
51
  "@inquirer/prompts": "^8.7.3",
52
52
  "@tailwindcss/node": "^4.3.3",
53
- "akanjs": "3.2.0-rc.11",
53
+ "akanjs": "3.2.0-rc.13",
54
54
  "chalk": "^6.0.1",
55
55
  "commander": "^15.0.0",
56
56
  "dayjs": "^1.11.23",
package/scanInfo.ts CHANGED
@@ -10,7 +10,8 @@ import type {
10
10
  ScanResult,
11
11
  } from "./akanConfig";
12
12
  import { AkanAppConfig } from "./akanConfig";
13
- import { AppExecutor, LibExecutor, PkgExecutor, WorkspaceExecutor } from "./executors";
13
+ import type { AppExecutor, LibExecutor, PkgExecutor } from "./executors";
14
+ import { WorkspaceExecutor } from "./executors/workspaceExecutor";
14
15
  import {
15
16
  barrelFacetDirs,
16
17
  facetIndexShadowOf,
@@ -23,6 +24,9 @@ import {
23
24
  rootEntryHintOf,
24
25
  } from "./workspaceLayout";
25
26
 
27
+ //? Imported on use: sysExecutor.ts imports this file, so a static import would load a SysExecutor subclass first.
28
+ const loadLibExecutor = () => import("./executors/libExecutor");
29
+
26
30
  const scalarFileTypes = ["constant", "dictionary", "document", "template", "unit", "util", "view", "zone"] as const;
27
31
  type ScalarFileType = (typeof scalarFileTypes)[number];
28
32
  const serviceFileTypes = [
@@ -315,11 +319,11 @@ export class AppInfo extends ScanInfo {
315
319
  const existingAppInfo = AppInfo.appInfos.get(exec.name);
316
320
  if (existingAppInfo && !options.refresh) return existingAppInfo;
317
321
  const scanResult = await ScanInfo.getScanResult(exec);
318
-
322
+ const libModule = await loadLibExecutor();
319
323
  await Promise.all(
320
324
  scanResult.libDeps.map(async (libName) => {
321
325
  LibInfo.loadedLibs.add(libName);
322
- const libExecutor = LibExecutor.from(exec, libName);
326
+ const libExecutor = libModule.LibExecutor.from(exec, libName);
323
327
  LibInfo.libInfos.set(libName, await LibInfo.fromExecutor(libExecutor));
324
328
  }),
325
329
  );
@@ -368,11 +372,12 @@ export class AppInfo extends ScanInfo {
368
372
  }
369
373
 
370
374
  static async #getAllLibDeps(exec: AppExecutor, libDeps: string[], libSet = new Set<string>()) {
375
+ const libModule = await loadLibExecutor();
371
376
  await Promise.all(
372
377
  libDeps.map(async (libName) => {
373
378
  if (libSet.has(libName)) return;
374
379
  libSet.add(libName);
375
- const libExecutor = LibExecutor.from(exec, libName);
380
+ const libExecutor = libModule.LibExecutor.from(exec, libName);
376
381
  const libInfo = await LibInfo.fromExecutor(libExecutor);
377
382
  const libScanResult = libInfo.getScanResult();
378
383
  if (libScanResult.libDeps.length > 0) await AppInfo.#getAllLibDeps(exec, libScanResult.libDeps, libSet);
@@ -412,12 +417,13 @@ export class LibInfo extends ScanInfo {
412
417
  if (existingLibInfo && !refresh) return existingLibInfo;
413
418
 
414
419
  const scanResult = await ScanInfo.getScanResult(exec);
420
+ const libModule = await loadLibExecutor();
415
421
  await Promise.all(
416
422
  scanResult.libDeps
417
423
  .filter((libName) => !LibInfo.loadedLibs.has(libName))
418
424
  .map(async (libName) => {
419
425
  LibInfo.loadedLibs.add(libName);
420
- const libExecutor = LibExecutor.from(exec, libName);
426
+ const libExecutor = libModule.LibExecutor.from(exec, libName);
421
427
  LibInfo.libInfos.set(libName, await LibInfo.fromExecutor(libExecutor));
422
428
  }),
423
429
  );
@@ -512,11 +518,16 @@ export class WorkspaceInfo {
512
518
  if (existingWorkspaceInfo && !options.refresh) return existingWorkspaceInfo;
513
519
 
514
520
  const [appNames, libNames, pkgNames] = await Promise.all([exec.getApps(), exec.getLibs(), exec.getPkgs()]);
521
+ const [appModule, libModule, pkgModule] = await Promise.all([
522
+ import("./executors/appExecutor"),
523
+ loadLibExecutor(),
524
+ import("./executors/pkgExecutor"),
525
+ ]);
515
526
  // TODO: prevent duplicate scan by resolving the dependency graph
516
527
  const [appInfos, libInfos, pkgInfos] = await Promise.all([
517
- Promise.all(appNames.map(async (appName) => await AppExecutor.from(exec, appName).scan())),
518
- Promise.all(libNames.map(async (libName) => await LibExecutor.from(exec, libName).scan())),
519
- Promise.all(pkgNames.map(async (pkgName) => await PkgExecutor.from(exec, pkgName).scan())),
528
+ Promise.all(appNames.map(async (appName) => await appModule.AppExecutor.from(exec, appName).scan())),
529
+ Promise.all(libNames.map(async (libName) => await libModule.LibExecutor.from(exec, libName).scan())),
530
+ Promise.all(pkgNames.map(async (pkgName) => await pkgModule.PkgExecutor.from(exec, pkgName).scan())),
520
531
  ]);
521
532
  const workspaceInfo = new WorkspaceInfo(
522
533
  new Map(appInfos.map((app) => [app.exec.name, app as AppInfo])),
@@ -38,6 +38,12 @@ const LOCAL_NAMED_RE = /(?:^|\n)\s*export\s+\{\s*([^}]*?)\s*\}(?!\s*from)/g;
38
38
 
39
39
  const CANDIDATE_EXTS = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"];
40
40
 
41
+ interface BarrelWalk {
42
+ visited: Set<string>;
43
+ read: Set<string>;
44
+ following: Set<string>;
45
+ }
46
+
41
47
  export class BarrelAnalyzer {
42
48
  readonly #logger = new Logger("BarrelAnalyzer");
43
49
  readonly #opts: BarrelAnalyzerOptions;
@@ -96,7 +102,7 @@ export class BarrelAnalyzer {
96
102
  const map: BarrelExportMap = new Map();
97
103
  const read = new Set<string>();
98
104
  this.#read.set(pkgName, read);
99
- await this.#walk(pkg.entryFile, pkg, map, read);
105
+ await this.#walk(pkg.entryFile, pkg, map, { visited: new Set(), read, following: new Set([pkg.entryFile]) });
100
106
  return map;
101
107
  } catch (err) {
102
108
  this.#logger.error(`analyze failed for ${pkgName}: ${(err as Error).message}`);
@@ -104,9 +110,10 @@ export class BarrelAnalyzer {
104
110
  }
105
111
  }
106
112
 
107
- async #walk(absFile: string, pkg: PackageEntry, map: BarrelExportMap, visited: Set<string>): Promise<void> {
108
- if (visited.has(absFile)) return;
109
- visited.add(absFile);
113
+ async #walk(absFile: string, pkg: PackageEntry, map: BarrelExportMap, walk: BarrelWalk): Promise<void> {
114
+ if (walk.visited.has(absFile)) return;
115
+ walk.visited.add(absFile);
116
+ walk.read.add(absFile);
110
117
  const file = Bun.file(absFile);
111
118
  if (!(await file.exists())) return;
112
119
  const source = await file.text();
@@ -119,11 +126,12 @@ export class BarrelAnalyzer {
119
126
  authoritative.delete("default");
120
127
 
121
128
  const attributed = new Set<string>();
122
- const attribute = (listBody: string, subpath: string) => {
129
+ const attribute = (listBody: string, subpath: string, source?: BarrelExportMap | null) => {
123
130
  for (const item of parseNamedList(listBody)) {
124
131
  if (item.isType || item.imported === "default" || !authoritative.has(item.local)) continue;
125
132
  attributed.add(item.local);
126
- if (!map.has(item.local)) map.set(item.local, { subpath, originalName: item.imported });
133
+ if (map.has(item.local)) continue;
134
+ map.set(item.local, source?.get(item.imported) ?? { subpath, originalName: item.imported });
127
135
  }
128
136
  };
129
137
 
@@ -152,7 +160,7 @@ export class BarrelAnalyzer {
152
160
  const targetAbs = await this.#resolveRel(absFile, spec);
153
161
  if (!targetAbs) continue;
154
162
  const targetSubpath = this.#subpathFor(pkg, targetAbs);
155
- if (targetSubpath) attribute(namedList, targetSubpath);
163
+ if (targetSubpath) attribute(namedList, targetSubpath, await this.#exportsOf(targetAbs, pkg, walk));
156
164
  }
157
165
  }
158
166
 
@@ -172,10 +180,20 @@ export class BarrelAnalyzer {
172
180
 
173
181
  for (const spec of starSpecs) {
174
182
  const targetAbs = await this.#resolveRel(absFile, spec);
175
- if (targetAbs) await this.#walk(targetAbs, pkg, map, visited);
183
+ if (targetAbs) await this.#walk(targetAbs, pkg, map, walk);
176
184
  }
177
185
  }
178
186
 
187
+ //? A name re-exported by name from a barrel is mapped to the file that declares it, as a star walk would map it:
188
+ //? a rewrite to the barrel itself would load every module behind it.
189
+ async #exportsOf(absFile: string, pkg: PackageEntry, walk: BarrelWalk): Promise<BarrelExportMap | null> {
190
+ if (walk.following.has(absFile)) return null;
191
+ const map: BarrelExportMap = new Map();
192
+ const following = new Set([...walk.following, absFile]);
193
+ await this.#walk(absFile, pkg, map, { visited: new Set(), read: walk.read, following });
194
+ return map;
195
+ }
196
+
179
197
  #scanExports(source: string, absFile: string): Set<string> {
180
198
  try {
181
199
  const transpiler = [".tsx", ".jsx"].includes(path.extname(absFile)) ? this.#tsxTranspiler : this.#tsTranspiler;
@@ -209,6 +209,33 @@ describe("BarrelAnalyzer and rewriteBarrelImports", () => {
209
209
  expect(map?.get("OnlyA")).toEqual({ subpath: "@scope/pkg/a", originalName: "OnlyA" });
210
210
  });
211
211
 
212
+ test("a name re-exported by name from a sub-barrel maps to the file that declares it", async () => {
213
+ const root = await makeTempRoot();
214
+ const pkgDir = path.join(root, "pkg");
215
+ await write(
216
+ path.join(pkgDir, "index.ts"),
217
+ 'export { A, Bee as B2 } from "./sub";\nexport { C } from "./cycleA";\n',
218
+ );
219
+ await write(path.join(pkgDir, "sub", "index.ts"), 'export * from "./a";\nexport { B as Bee } from "./b";\n');
220
+ await write(path.join(pkgDir, "sub", "a.ts"), "export const A = 1;\n");
221
+ await write(path.join(pkgDir, "sub", "b.ts"), "export const B = 2;\n");
222
+ await write(path.join(pkgDir, "cycleA.ts"), 'export { C } from "./cycleB";\n');
223
+ await write(path.join(pkgDir, "cycleB.ts"), 'export { C } from "./cycleA";\n');
224
+ const analyzer = new BarrelAnalyzer({
225
+ resolvePackage: async () => ({ pkgName: "@scope/pkg", entryFile: path.join(pkgDir, "index.ts"), pkgDir }),
226
+ });
227
+
228
+ const map = await analyzer.analyze("@scope/pkg");
229
+ expect(map?.get("A")).toEqual({ subpath: "@scope/pkg/sub/a", originalName: "A" });
230
+ expect(map?.get("B2")).toEqual({ subpath: "@scope/pkg/sub/b", originalName: "B" });
231
+ expect(map?.get("C")).toEqual({ subpath: "@scope/pkg/cycleA", originalName: "C" });
232
+ expect(
233
+ (await analyzer.filesOf("@scope/pkg"))
234
+ .map((file) => path.relative(pkgDir, file))
235
+ .sort((a, b) => a.localeCompare(b)),
236
+ ).toEqual(["cycleA.ts", "cycleB.ts", "index.ts", "sub/a.ts", "sub/b.ts", "sub/index.ts"]);
237
+ });
238
+
212
239
  test("two analyses that interleave each keep their own place in their barrel", async () => {
213
240
  const root = await makeTempRoot();
214
241
  const barrels = { first: ["a1", "a2", "a3"], second: ["b1", "b2"] } as const;