@confect/cli 9.0.0-next.4 → 9.0.0-next.5

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 CHANGED
@@ -1,5 +1,26 @@
1
1
  # @confect/cli
2
2
 
3
+ ## 9.0.0-next.5
4
+
5
+ ### Patch Changes
6
+
7
+ - 4cebebc: Switch the codegen bundler to [`bundle-require`](https://github.com/egoist/bundle-require).
8
+
9
+ ### Why
10
+
11
+ `9.0.0-next.4`'s `absoluteExternalsPlugin` externalized every bare-specifier import and rewrote it to a `file://` URL because the bundle was loaded via a parent-less `data:` URL. esbuild's resolver honors `tsconfig.json#compilerOptions.paths`, so a `~/src/foo`-style alias resolved to a local `.ts` file and got externalized as `file:///…/foo.ts` — which Node ESM cannot import (`ERR_UNKNOWN_FILE_EXTENSION`). The codegen bundler hand-rolled a third reimplementation of "load a TypeScript config file at runtime"; each iteration introduced a new bug.
12
+
13
+ ### What changed
14
+ - `@confect/cli/Bundler` now delegates to `bundle-require`, the library `tsup`, `unbuild`, `vite`, `vitest`, and `vuepress` use for this exact problem. `bundle-require` writes a temp `.mjs` next to the source, `import()`s it, and deletes it — so bare-specifier externals (third-party packages, workspace deps) resolve through Node's normal `node_modules` walk and tsconfig `paths` aliases stay inside the bundle.
15
+ - `@confect/cli/confect/dev`'s watcher swaps `absoluteExternalsPlugin` for `bundle-require`'s `externalPlugin`, fed the project's `tsconfig.json#paths` via `loadTsConfig` so dev-mode rebuilds also bundle aliased local source instead of erroring on it.
16
+ - The `absoluteExternalsPlugin` export is removed from `@confect/cli/Bundler`.
17
+
18
+ ### Fixes
19
+ - Restores `confect codegen` for any project that uses `tsconfig.json` `paths` aliases (e.g. `~/*`, `@/*`, `@app/*`) for its own source.
20
+ - As a side benefit, `__dirname`, `__filename`, and `import.meta.url` inside bundled impls now resolve to the original source path instead of the temporary bundle URL (`bundle-require`'s built-in injection).
21
+ - @confect/core@9.0.0-next.5
22
+ - @confect/server@9.0.0-next.5
23
+
3
24
  ## 9.0.0-next.4
4
25
 
5
26
  ### Patch Changes
package/dist/Bundler.mjs CHANGED
@@ -1,88 +1,79 @@
1
1
  import { BundlerError } from "./BuildError.mjs";
2
2
  import { Array, Effect, Option, pipe } from "effect";
3
3
  import { Path } from "@effect/platform";
4
- import * as esbuild from "esbuild";
5
- import { pathToFileURL } from "node:url";
4
+ import { dirname, isAbsolute, resolve } from "node:path";
5
+ import { bundleRequire } from "bundle-require";
6
6
 
7
7
  //#region src/Bundler.ts
8
- const isRelativeOrAbsolutePath = (importPath) => importPath.startsWith("./") || importPath.startsWith("../") || importPath.startsWith("/");
9
- const PLUGIN_DATA_SKIP = Symbol("absolute-externals.skip");
10
8
  /**
11
- * Mark every bare-specifier import as external and rewrite it to an absolute
12
- * `file://` URL. Resolution is delegated to esbuild's own resolver via
13
- * `build.resolve(...)`, which honors each package's `exports` map (preferring
14
- * the `import` condition under `format: "esm"`) and falls back to
15
- * `module`/`main` exactly the way Node's ESM resolution algorithm does.
16
- *
17
- * Bundles produced with this plugin are loaded via a data URL `import(...)`
18
- * (see {@link bundle}); rewriting bare externals to absolute file URLs is what
19
- * makes them resolvable at runtime, since a data URL has no parent file from
20
- * which a bare specifier could be resolved.
21
- *
22
- * Relative/absolute-path imports are left to esbuild to bundle as usual, and
23
- * `node:*` built-ins are passed through unchanged.
9
+ * `bundle-require` sets `absWorkingDir: cwd` on the underlying esbuild build,
10
+ * so the metafile's input keys (and each input's `imports[].path`) are stored
11
+ * relative to that cwd. Callers reach for the metafile with absolute paths
12
+ * (e.g. {@link directlyImports}), so we normalize every key/import path to
13
+ * absolute up front. That way the lookup logic stays oblivious to whatever
14
+ * cwd was used during bundling.
24
15
  */
25
- const absoluteExternalsPlugin = {
26
- name: "absolute-externals",
27
- setup(build) {
28
- build.onResolve({ filter: /.*/ }, async (args) => {
29
- if (args.pluginData?.[PLUGIN_DATA_SKIP]) return;
30
- if (args.kind !== "import-statement" && args.kind !== "dynamic-import") return;
31
- if (isRelativeOrAbsolutePath(args.path)) return;
32
- if (args.path.startsWith("node:")) return {
33
- path: args.path,
34
- external: true
35
- };
36
- const resolved = await build.resolve(args.path, {
37
- kind: args.kind,
38
- resolveDir: args.resolveDir,
39
- pluginData: { [PLUGIN_DATA_SKIP]: true }
40
- });
41
- if (resolved.errors.length > 0) return {
42
- errors: resolved.errors,
43
- warnings: resolved.warnings
44
- };
45
- return {
46
- path: pathToFileURL(resolved.path).href,
47
- external: true
48
- };
49
- });
50
- }
51
- };
52
- const buildEntry = (entryPoint) => Effect.tryPromise({
53
- try: () => esbuild.build({
54
- entryPoints: [entryPoint],
55
- bundle: true,
56
- write: false,
57
- platform: "node",
58
- format: "esm",
59
- logLevel: "silent",
60
- metafile: true,
61
- plugins: [absoluteExternalsPlugin]
62
- }),
63
- catch: (cause) => new BundlerError({ cause })
64
- });
65
- const importBundledModule = (result) => {
66
- const code = result.outputFiles[0].text;
67
- return import("data:text/javascript;base64," + Buffer.from(code).toString("base64"));
16
+ const absolutizeMetafile = (metafile, cwd) => {
17
+ const absolutize = (p) => isAbsolute(p) ? p : resolve(cwd, p);
18
+ const inputs = {};
19
+ for (const [key, value] of Object.entries(metafile.inputs)) inputs[absolutize(key)] = {
20
+ ...value,
21
+ imports: value.imports.map((i) => ({
22
+ ...i,
23
+ path: absolutize(i.path)
24
+ }))
25
+ };
26
+ const outputs = {};
27
+ for (const [key, value] of Object.entries(metafile.outputs)) outputs[absolutize(key)] = value;
28
+ return {
29
+ inputs,
30
+ outputs
31
+ };
68
32
  };
69
33
  /**
70
- * Bundle a TypeScript entry point with esbuild and import the result via a
71
- * data URL. This handles extensionless `.ts` imports regardless of whether
72
- * the user's project sets `"type": "module"` in package.json. The returned
73
- * pair carries both the imported module and the esbuild metafile so callers
74
- * can inspect the import graph (see {@link directlyImports}).
34
+ * Bundle a TypeScript entry point with esbuild via {@link bundleRequire} and
35
+ * import the result. `bundle-require` writes a temp `.mjs` next to the source,
36
+ * `import()`s it, and deletes it so bare-specifier externals (third-party
37
+ * packages, workspace deps) resolve through the user's normal `node_modules`
38
+ * walk, and tsconfig `paths` aliases stay inside the bundle.
39
+ *
40
+ * `cwd` is set to the entry's directory so `bundle-require`'s `tsconfig.json`
41
+ * discovery (which walks upward from `cwd`) lands on the project's tsconfig
42
+ * regardless of where `confect codegen` was invoked from, and so esbuild
43
+ * resolves relative imports against the entry's location.
44
+ *
45
+ * The returned pair carries both the imported module and the esbuild metafile
46
+ * so callers can inspect the import graph (see {@link directlyImports}); the
47
+ * metafile is captured via a small `onEnd` plugin because `bundle-require`
48
+ * itself only exposes a flat `dependencies: string[]`.
75
49
  */
76
50
  const bundle = (entryPoint) => Effect.gen(function* () {
77
- const result = yield* buildEntry(entryPoint);
78
- const module = yield* Effect.tryPromise({
79
- try: () => importBundledModule(result),
51
+ let metafile;
52
+ const captureMetafile = {
53
+ name: "confect:capture-metafile",
54
+ setup(build) {
55
+ build.onEnd((result) => {
56
+ metafile = result.metafile;
57
+ });
58
+ }
59
+ };
60
+ const cwd = dirname(entryPoint);
61
+ const result = yield* Effect.tryPromise({
62
+ try: () => bundleRequire({
63
+ filepath: entryPoint,
64
+ cwd,
65
+ format: "esm",
66
+ esbuildOptions: {
67
+ plugins: [captureMetafile],
68
+ logLevel: "silent"
69
+ }
70
+ }),
80
71
  catch: (cause) => new BundlerError({ cause })
81
72
  });
82
- if (!result.metafile) return yield* Effect.dieMessage("esbuild metafile missing");
73
+ if (!metafile) return yield* Effect.dieMessage("esbuild metafile missing");
83
74
  return {
84
- module,
85
- metafile: result.metafile
75
+ module: result.mod,
76
+ metafile: absolutizeMetafile(metafile, cwd)
86
77
  };
87
78
  });
88
79
  const findMetafileInputKey = (metafile, absolutePath) => Effect.gen(function* () {
@@ -106,5 +97,5 @@ const directlyImports = (bundled, sourceAbsolutePath, targetAbsolutePath) => Eff
106
97
  });
107
98
 
108
99
  //#endregion
109
- export { absoluteExternalsPlugin, bundle, directlyImports };
100
+ export { bundle, directlyImports };
110
101
  //# sourceMappingURL=Bundler.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"Bundler.mjs","names":[],"sources":["../src/Bundler.ts"],"sourcesContent":["import { pathToFileURL } from \"node:url\";\nimport { Path } from \"@effect/platform\";\nimport { Array, Effect, Option, pipe } from \"effect\";\nimport * as esbuild from \"esbuild\";\nimport { BundlerError } from \"./BuildError\";\n\nexport interface Bundled {\n readonly module: any;\n readonly metafile: esbuild.Metafile;\n}\n\nconst isRelativeOrAbsolutePath = (importPath: string) =>\n importPath.startsWith(\"./\") ||\n importPath.startsWith(\"../\") ||\n importPath.startsWith(\"/\");\n\n// Recursion guard for `absoluteExternalsPlugin`. When the plugin asks esbuild\n// to resolve a bare specifier via `build.resolve(...)`, esbuild invokes every\n// registered `onResolve` hook again for that same specifier — including this\n// one. The flag (carried through the recursive call via `pluginData`) tells\n// the recursive invocation to skip rewriting and fall through to esbuild's\n// built-in resolver, which is what we wanted from `build.resolve` in the\n// first place.\nconst PLUGIN_DATA_SKIP = Symbol(\"absolute-externals.skip\");\n\n/**\n * Mark every bare-specifier import as external and rewrite it to an absolute\n * `file://` URL. Resolution is delegated to esbuild's own resolver via\n * `build.resolve(...)`, which honors each package's `exports` map (preferring\n * the `import` condition under `format: \"esm\"`) and falls back to\n * `module`/`main` exactly the way Node's ESM resolution algorithm does.\n *\n * Bundles produced with this plugin are loaded via a data URL `import(...)`\n * (see {@link bundle}); rewriting bare externals to absolute file URLs is what\n * makes them resolvable at runtime, since a data URL has no parent file from\n * which a bare specifier could be resolved.\n *\n * Relative/absolute-path imports are left to esbuild to bundle as usual, and\n * `node:*` built-ins are passed through unchanged.\n */\nexport const absoluteExternalsPlugin: esbuild.Plugin = {\n name: \"absolute-externals\",\n setup(build) {\n build.onResolve({ filter: /.*/ }, async (args) => {\n if (args.pluginData?.[PLUGIN_DATA_SKIP]) return;\n if (args.kind !== \"import-statement\" && args.kind !== \"dynamic-import\")\n return;\n if (isRelativeOrAbsolutePath(args.path)) return;\n if (args.path.startsWith(\"node:\")) {\n return { path: args.path, external: true };\n }\n\n const resolved = await build.resolve(args.path, {\n kind: args.kind,\n resolveDir: args.resolveDir,\n pluginData: { [PLUGIN_DATA_SKIP]: true },\n });\n\n if (resolved.errors.length > 0) {\n return { errors: resolved.errors, warnings: resolved.warnings };\n }\n\n return {\n path: pathToFileURL(resolved.path).href,\n external: true,\n };\n });\n },\n};\n\nconst buildEntry = (entryPoint: string) =>\n Effect.tryPromise({\n try: () =>\n esbuild.build({\n entryPoints: [entryPoint],\n bundle: true,\n write: false,\n platform: \"node\",\n format: \"esm\",\n logLevel: \"silent\",\n metafile: true,\n plugins: [absoluteExternalsPlugin],\n }),\n catch: (cause) => new BundlerError({ cause }),\n });\n\nconst importBundledModule = (result: esbuild.BuildResult) => {\n const code = result.outputFiles![0]!.text;\n const dataUrl =\n \"data:text/javascript;base64,\" + Buffer.from(code).toString(\"base64\");\n return import(dataUrl);\n};\n\n/**\n * Bundle a TypeScript entry point with esbuild and import the result via a\n * data URL. This handles extensionless `.ts` imports regardless of whether\n * the user's project sets `\"type\": \"module\"` in package.json. The returned\n * pair carries both the imported module and the esbuild metafile so callers\n * can inspect the import graph (see {@link directlyImports}).\n */\nexport const bundle = (\n entryPoint: string,\n): Effect.Effect<Bundled, BundlerError> =>\n Effect.gen(function* () {\n const result = yield* buildEntry(entryPoint);\n const module = yield* Effect.tryPromise({\n try: () => importBundledModule(result),\n catch: (cause) => new BundlerError({ cause }),\n });\n if (!result.metafile) {\n return yield* Effect.dieMessage(\"esbuild metafile missing\");\n }\n return { module, metafile: result.metafile };\n });\n\nconst findMetafileInputKey = (\n metafile: esbuild.Metafile,\n absolutePath: string,\n) =>\n Effect.gen(function* () {\n const path = yield* Path.Path;\n const resolved = path.resolve(absolutePath);\n return Array.findFirst(\n Object.keys(metafile.inputs),\n (key) => path.resolve(key) === resolved,\n );\n });\n\n/**\n * Returns `true` when the module bundled from `sourceAbsolutePath` declares a\n * direct import of `targetAbsolutePath` (according to the bundle's esbuild\n * metafile). Returns `false` if either path is missing from the metafile.\n */\nexport const directlyImports = (\n bundled: Bundled,\n sourceAbsolutePath: string,\n targetAbsolutePath: string,\n) =>\n Effect.gen(function* () {\n const path = yield* Path.Path;\n const sourceKey = yield* findMetafileInputKey(\n bundled.metafile,\n sourceAbsolutePath,\n );\n const targetKey = yield* findMetafileInputKey(\n bundled.metafile,\n targetAbsolutePath,\n );\n\n return pipe(\n Option.all([sourceKey, targetKey]),\n Option.flatMap(([sourceKey_, targetKey_]) =>\n Option.fromNullable(bundled.metafile.inputs[sourceKey_]).pipe(\n Option.map((sourceInput) => {\n const targetResolved = path.resolve(targetKey_);\n return sourceInput.imports.some(\n (importedFile) =>\n path.resolve(importedFile.path) === targetResolved,\n );\n }),\n ),\n ),\n Option.getOrElse(() => false),\n );\n });\n"],"mappings":";;;;;;;AAWA,MAAM,4BAA4B,eAChC,WAAW,WAAW,KAAK,IAC3B,WAAW,WAAW,MAAM,IAC5B,WAAW,WAAW,IAAI;AAS5B,MAAM,mBAAmB,OAAO,0BAA0B;;;;;;;;;;;;;;;;AAiB1D,MAAa,0BAA0C;CACrD,MAAM;CACN,MAAM,OAAO;AACX,QAAM,UAAU,EAAE,QAAQ,MAAM,EAAE,OAAO,SAAS;AAChD,OAAI,KAAK,aAAa,kBAAmB;AACzC,OAAI,KAAK,SAAS,sBAAsB,KAAK,SAAS,iBACpD;AACF,OAAI,yBAAyB,KAAK,KAAK,CAAE;AACzC,OAAI,KAAK,KAAK,WAAW,QAAQ,CAC/B,QAAO;IAAE,MAAM,KAAK;IAAM,UAAU;IAAM;GAG5C,MAAM,WAAW,MAAM,MAAM,QAAQ,KAAK,MAAM;IAC9C,MAAM,KAAK;IACX,YAAY,KAAK;IACjB,YAAY,GAAG,mBAAmB,MAAM;IACzC,CAAC;AAEF,OAAI,SAAS,OAAO,SAAS,EAC3B,QAAO;IAAE,QAAQ,SAAS;IAAQ,UAAU,SAAS;IAAU;AAGjE,UAAO;IACL,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O,SAAS,CAAC,YAAY,gBAC3B,OAAO,aAAa,QAAQ,SAAS,OAAO,YAAY,CAAC,KACvD,OAAO,KAAK,gBAAgB;EAC1B,MAAM,iBAAiB,KAAK,QAAQ,WAAW;AAC/C,SAAO,YAAY,QAAQ,MACxB,iBACC,KAAK,QAAQ,aAAa,KAAK,KAAK,eACvC;GACD,CACH,CACF,EACD,OAAO,gBAAgB,MAAM,CAC9B;EACD"}
1
+ {"version":3,"file":"Bundler.mjs","names":[],"sources":["../src/Bundler.ts"],"sourcesContent":["import { dirname, isAbsolute, resolve } from \"node:path\";\nimport { Path } from \"@effect/platform\";\nimport { bundleRequire } from \"bundle-require\";\nimport { Array, Effect, Option, pipe } from \"effect\";\nimport type * as esbuild from \"esbuild\";\nimport { BundlerError } from \"./BuildError\";\n\nexport interface Bundled {\n readonly module: any;\n readonly metafile: esbuild.Metafile;\n}\n\n/**\n * `bundle-require` sets `absWorkingDir: cwd` on the underlying esbuild build,\n * so the metafile's input keys (and each input's `imports[].path`) are stored\n * relative to that cwd. Callers reach for the metafile with absolute paths\n * (e.g. {@link directlyImports}), so we normalize every key/import path to\n * absolute up front. That way the lookup logic stays oblivious to whatever\n * cwd was used during bundling.\n */\nconst absolutizeMetafile = (\n metafile: esbuild.Metafile,\n cwd: string,\n): esbuild.Metafile => {\n const absolutize = (p: string) => (isAbsolute(p) ? p : resolve(cwd, p));\n const inputs: esbuild.Metafile[\"inputs\"] = {};\n for (const [key, value] of Object.entries(metafile.inputs)) {\n inputs[absolutize(key)] = {\n ...value,\n imports: value.imports.map((i) => ({ ...i, path: absolutize(i.path) })),\n };\n }\n const outputs: esbuild.Metafile[\"outputs\"] = {};\n for (const [key, value] of Object.entries(metafile.outputs)) {\n outputs[absolutize(key)] = value;\n }\n return { inputs, outputs };\n};\n\n/**\n * Bundle a TypeScript entry point with esbuild via {@link bundleRequire} and\n * import the result. `bundle-require` writes a temp `.mjs` next to the source,\n * `import()`s it, and deletes it so bare-specifier externals (third-party\n * packages, workspace deps) resolve through the user's normal `node_modules`\n * walk, and tsconfig `paths` aliases stay inside the bundle.\n *\n * `cwd` is set to the entry's directory so `bundle-require`'s `tsconfig.json`\n * discovery (which walks upward from `cwd`) lands on the project's tsconfig\n * regardless of where `confect codegen` was invoked from, and so esbuild\n * resolves relative imports against the entry's location.\n *\n * The returned pair carries both the imported module and the esbuild metafile\n * so callers can inspect the import graph (see {@link directlyImports}); the\n * metafile is captured via a small `onEnd` plugin because `bundle-require`\n * itself only exposes a flat `dependencies: string[]`.\n */\nexport const bundle = (\n entryPoint: string,\n): Effect.Effect<Bundled, BundlerError> =>\n Effect.gen(function* () {\n let metafile: esbuild.Metafile | undefined;\n const captureMetafile: esbuild.Plugin = {\n name: \"confect:capture-metafile\",\n setup(build) {\n build.onEnd((result) => {\n metafile = result.metafile;\n });\n },\n };\n\n const cwd = dirname(entryPoint);\n const result = yield* Effect.tryPromise({\n try: () =>\n bundleRequire({\n filepath: entryPoint,\n cwd,\n format: \"esm\",\n esbuildOptions: {\n plugins: [captureMetafile],\n logLevel: \"silent\",\n },\n }),\n catch: (cause) => new BundlerError({ cause }),\n });\n\n if (!metafile) {\n return yield* Effect.dieMessage(\"esbuild metafile missing\");\n }\n\n return { module: result.mod, metafile: absolutizeMetafile(metafile, cwd) };\n });\n\nconst findMetafileInputKey = (\n metafile: esbuild.Metafile,\n absolutePath: string,\n) =>\n Effect.gen(function* () {\n const path = yield* Path.Path;\n const resolved = path.resolve(absolutePath);\n return Array.findFirst(\n Object.keys(metafile.inputs),\n (key) => path.resolve(key) === resolved,\n );\n });\n\n/**\n * Returns `true` when the module bundled from `sourceAbsolutePath` declares a\n * direct import of `targetAbsolutePath` (according to the bundle's esbuild\n * metafile). Returns `false` if either path is missing from the metafile.\n */\nexport const directlyImports = (\n bundled: Bundled,\n sourceAbsolutePath: string,\n targetAbsolutePath: string,\n) =>\n Effect.gen(function* () {\n const path = yield* Path.Path;\n const sourceKey = yield* findMetafileInputKey(\n bundled.metafile,\n sourceAbsolutePath,\n );\n const targetKey = yield* findMetafileInputKey(\n bundled.metafile,\n targetAbsolutePath,\n );\n\n return pipe(\n Option.all([sourceKey, targetKey]),\n Option.flatMap(([sourceKey_, targetKey_]) =>\n Option.fromNullable(bundled.metafile.inputs[sourceKey_]).pipe(\n Option.map((sourceInput) => {\n const targetResolved = path.resolve(targetKey_);\n return sourceInput.imports.some(\n (importedFile) =>\n path.resolve(importedFile.path) === targetResolved,\n );\n }),\n ),\n ),\n Option.getOrElse(() => false),\n );\n });\n"],"mappings":";;;;;;;;;;;;;;;AAoBA,MAAM,sBACJ,UACA,QACqB;CACrB,MAAM,cAAc,MAAe,WAAW,EAAE,GAAG,IAAI,QAAQ,KAAK,EAAE;CACtE,MAAM,SAAqC,EAAE;AAC7C,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,SAAS,OAAO,CACxD,QAAO,WAAW,IAAI,IAAI;EACxB,GAAG;EACH,SAAS,MAAM,QAAQ,KAAK,OAAO;GAAE,GAAG;GAAG,MAAM,WAAW,EAAE,KAAK;GAAE,EAAE;EACxE;CAEH,MAAM,UAAuC,EAAE;AAC/C,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,SAAS,QAAQ,CACzD,SAAQ,WAAW,IAAI,IAAI;AAE7B,QAAO;EAAE;EAAQ;EAAS;;;;;;;;;;;;;;;;;;;AAoB5B,MAAa,UACX,eAEA,OAAO,IAAI,aAAa;CACtB,IAAI;CACJ,MAAM,kBAAkC;EACtC,MAAM;EACN,MAAM,OAAO;AACX,SAAM,OAAO,WAAW;AACtB,eAAW,OAAO;KAClB;;EAEL;CAED,MAAM,MAAM,QAAQ,WAAW;CAC/B,MAAM,SAAS,OAAO,OAAO,WAAW;EACtC,WACE,cAAc;GACZ,UAAU;GACV;GACA,QAAQ;GACR,gBAAgB;IACd,SAAS,CAAC,gBAAgB;IAC1B,UAAU;IACX;GACF,CAAC;EACJ,QAAQ,UAAU,IAAI,aAAa,EAAE,OAAO,CAAC;EAC9C,CAAC;AAEF,KAAI,CAAC,SACH,QAAO,OAAO,OAAO,WAAW,2BAA2B;AAG7D,QAAO;EAAE,QAAQ,OAAO;EAAK,UAAU,mBAAmB,UAAU,IAAI;EAAE;EAC1E;AAEJ,MAAM,wBACJ,UACA,iBAEA,OAAO,IAAI,aAAa;CACtB,MAAM,OAAO,OAAO,KAAK;CACzB,MAAM,WAAW,KAAK,QAAQ,aAAa;AAC3C,QAAO,MAAM,UACX,OAAO,KAAK,SAAS,OAAO,GAC3B,QAAQ,KAAK,QAAQ,IAAI,KAAK,SAChC;EACD;;;;;;AAOJ,MAAa,mBACX,SACA,oBACA,uBAEA,OAAO,IAAI,aAAa;CACtB,MAAM,OAAO,OAAO,KAAK;CACzB,MAAM,YAAY,OAAO,qBACvB,QAAQ,UACR,mBACD;CACD,MAAM,YAAY,OAAO,qBACvB,QAAQ,UACR,mBACD;AAED,QAAO,KACL,OAAO,IAAI,CAAC,WAAW,UAAU,CAAC,EAClC,OAAO,SAAS,CAAC,YAAY,gBAC3B,OAAO,aAAa,QAAQ,SAAS,OAAO,YAAY,CAAC,KACvD,OAAO,KAAK,gBAAgB;EAC1B,MAAM,iBAAiB,KAAK,QAAQ,WAAW;AAC/C,SAAO,YAAY,QAAQ,MACxB,iBACC,KAAK,QAAQ,aAAa,KAAK,KAAK,eACvC;GACD,CACH,CACF,EACD,OAAO,gBAAgB,MAAM,CAC9B;EACD"}
@@ -5,7 +5,6 @@ import { catchAndLog } from "../CodegenError.mjs";
5
5
  import { ConvexDirectory } from "../ConvexDirectory.mjs";
6
6
  import { ConfectDirectory } from "../ConfectDirectory.mjs";
7
7
  import { FunctionPaths, diff } from "../FunctionPaths.mjs";
8
- import { absoluteExternalsPlugin } from "../Bundler.mjs";
9
8
  import { generateAuthConfig, generateCrons, generateHttp } from "../utils.mjs";
10
9
  import { discoverLeafImplFiles, isLeafImplPath, isLeafSpecPath } from "../LeafModule.mjs";
11
10
  import { codegenHandler, loadPreviousFunctionPaths } from "./codegen.mjs";
@@ -14,6 +13,7 @@ import { Command } from "@effect/cli";
14
13
  import { FileSystem, Path } from "@effect/platform";
15
14
  import { Ansi, AnsiDoc } from "@effect/printer-ansi";
16
15
  import * as esbuild from "esbuild";
16
+ import { externalPlugin, loadTsConfig, tsconfigPathsToRegExp } from "bundle-require";
17
17
 
18
18
  //#region src/confect/dev.ts
19
19
  const GENERATED_SPEC_PATH = "_generated/spec.ts";
@@ -174,7 +174,7 @@ const discoverEntryPoints = Effect.gen(function* () {
174
174
  const implEntryOptions = yield* Effect.forEach(implRelativePaths, (relativePath) => tryEntry(relativePath, "specDirty"));
175
175
  return Array.getSomes([...fixedEntryOptions, ...implEntryOptions]);
176
176
  });
177
- const esbuildOptions = (entry, signal, pendingRef, watcherErrorsRef) => {
177
+ const esbuildOptions = (entry, notExternal, signal, pendingRef, watcherErrorsRef) => {
178
178
  const initialBuildSeenRef = Ref.unsafeMake(false);
179
179
  return {
180
180
  entryPoints: [entry.absolutePath],
@@ -184,7 +184,7 @@ const esbuildOptions = (entry, signal, pendingRef, watcherErrorsRef) => {
184
184
  platform: "node",
185
185
  format: "esm",
186
186
  logLevel: "silent",
187
- plugins: [absoluteExternalsPlugin, {
187
+ plugins: [externalPlugin({ notExternal: [...notExternal] }), {
188
188
  name: "notify-rebuild",
189
189
  setup(build) {
190
190
  build.onEnd((result) => {
@@ -208,8 +208,8 @@ const esbuildOptions = (entry, signal, pendingRef, watcherErrorsRef) => {
208
208
  }]
209
209
  };
210
210
  };
211
- const createEntryPointWatcher = (entry, signal, pendingRef, watcherErrorsRef) => Effect.acquireRelease(Effect.promise(async () => {
212
- const ctx = await esbuild.context(esbuildOptions(entry, signal, pendingRef, watcherErrorsRef));
211
+ const createEntryPointWatcher = (entry, notExternal, signal, pendingRef, watcherErrorsRef) => Effect.acquireRelease(Effect.promise(async () => {
212
+ const ctx = await esbuild.context(esbuildOptions(entry, notExternal, signal, pendingRef, watcherErrorsRef));
213
213
  await ctx.watch();
214
214
  return ctx;
215
215
  }), (ctx) => Effect.gen(function* () {
@@ -232,6 +232,7 @@ const createEntryPointWatcher = (entry, signal, pendingRef, watcherErrorsRef) =>
232
232
  const entryPointsWatcher = (signal, pendingRef, restartQueue, watcherErrorsRef) => Effect.gen(function* () {
233
233
  const parentScope = yield* Effect.scope;
234
234
  const scopesRef = yield* Ref.make(/* @__PURE__ */ new Map());
235
+ const notExternal = tsconfigPathsToRegExp(loadTsConfig(yield* ProjectRoot.get)?.data.compilerOptions?.paths ?? {});
235
236
  const sync = Effect.gen(function* () {
236
237
  const desired = yield* discoverEntryPoints;
237
238
  const desiredByPath = new Map(desired.map((entryPoint) => [entryPoint.absolutePath, entryPoint]));
@@ -244,7 +245,7 @@ const entryPointsWatcher = (signal, pendingRef, restartQueue, watcherErrorsRef)
244
245
  yield* Effect.forEach(desired, (entry) => Effect.gen(function* () {
245
246
  if ((yield* Ref.get(scopesRef)).has(entry.absolutePath)) return;
246
247
  const childScope = yield* Scope.fork(parentScope, ExecutionStrategy.sequential);
247
- yield* createEntryPointWatcher(entry, signal, pendingRef, watcherErrorsRef).pipe(Scope.extend(childScope));
248
+ yield* createEntryPointWatcher(entry, notExternal, signal, pendingRef, watcherErrorsRef).pipe(Scope.extend(childScope));
248
249
  yield* Ref.update(scopesRef, (scopes) => {
249
250
  const updated = new Map(scopes);
250
251
  updated.set(entry.absolutePath, childScope);
@@ -1 +1 @@
1
- {"version":3,"file":"dev.mjs","names":["FunctionPaths.diff","CodegenError.catchAndLog"],"sources":["../../src/confect/dev.ts"],"sourcesContent":["import { Command } from \"@effect/cli\";\nimport { FileSystem, Path } from \"@effect/platform\";\nimport { Ansi, AnsiDoc } from \"@effect/printer-ansi\";\nimport {\n Array,\n Chunk,\n Clock,\n Console,\n Duration,\n Effect,\n Equal,\n ExecutionStrategy,\n Exit,\n HashSet,\n Match,\n Option,\n Order,\n pipe,\n Queue,\n Ref,\n Scope,\n Stream,\n String,\n} from \"effect\";\nimport * as esbuild from \"esbuild\";\nimport { logCoalescedBuildErrors } from \"../BuildError\";\nimport { absoluteExternalsPlugin } from \"../Bundler\";\nimport * as CodegenError from \"../CodegenError\";\nimport { ConfectDirectory } from \"../ConfectDirectory\";\nimport { ConvexDirectory } from \"../ConvexDirectory\";\nimport * as FunctionPaths from \"../FunctionPaths\";\nimport type * as GroupPaths from \"../GroupPaths\";\nimport {\n discoverLeafImplFiles,\n isLeafImplPath,\n isLeafSpecPath,\n} from \"../LeafModule\";\nimport {\n logFunctionAdded,\n logFunctionRemoved,\n logPending,\n logSuccess,\n} from \"../log\";\nimport { ProjectRoot } from \"../ProjectRoot\";\nimport { generateAuthConfig, generateCrons, generateHttp } from \"../utils\";\nimport { codegenHandler, loadPreviousFunctionPaths } from \"./codegen\";\n\nconst GENERATED_SPEC_PATH = \"_generated/spec.ts\";\nconst GENERATED_NODE_SPEC_PATH = \"_generated/nodeSpec.ts\";\n\n// Quiescence window: the sync loop waits this long for further signals\n// after each batch. One user edit fires `onEnd` on every esbuild\n// watcher that touches the file, and rebuild times vary across entry\n// points so the onEnds can be spread over hundreds of milliseconds.\n// The drain keeps extending its wait (bounded by `COALESCE_MAX_WAIT`)\n// until no new signals arrive within the window, collapsing the whole\n// burst into a single codegen cycle.\nconst COALESCE_QUIESCENCE = Duration.millis(300);\n\n// Upper bound on `drainUntilQuiescent` so a pathological infinite\n// signal stream can't pin the sync loop forever.\nconst COALESCE_MAX_WAIT = Duration.seconds(5);\n\n// How long to wait for esbuild watchers to react to codegen's own\n// writes (e.g. an updated `_generated/spec.ts`). Added on top of the\n// quiescence drain when codegen reported writes happened.\nconst ECHO_COOLDOWN = Duration.millis(500);\n\nconst emptyFunctionPaths = FunctionPaths.FunctionPaths.make(HashSet.empty());\n\ntype Pending = {\n readonly specDirty: boolean;\n readonly httpDirty: boolean;\n readonly cronsDirty: boolean;\n readonly authDirty: boolean;\n};\n\ntype PendingKey = keyof Pending;\n\nconst pendingInit: Pending = {\n specDirty: false,\n httpDirty: false,\n cronsDirty: false,\n authDirty: false,\n};\n\nconst isPendingDirty = (p: Pending): boolean =>\n p.specDirty || p.httpDirty || p.cronsDirty || p.authDirty;\n\ntype WatcherErrors = ReadonlyMap<string, readonly esbuild.Message[]>;\n\nconst emptyWatcherErrors: WatcherErrors = new Map();\n\nconst changeChar = (change: \"Added\" | \"Removed\" | \"Modified\") =>\n Match.value(change).pipe(\n Match.when(\"Added\", () => ({ char: \"+\", color: Ansi.green })),\n Match.when(\"Removed\", () => ({ char: \"-\", color: Ansi.red })),\n Match.when(\"Modified\", () => ({ char: \"~\", color: Ansi.yellow })),\n Match.exhaustive,\n );\n\nconst logFileChangeIndented = (\n change: \"Added\" | \"Removed\" | \"Modified\",\n fullPath: string,\n) =>\n Effect.gen(function* () {\n const projectRoot = yield* ProjectRoot.get;\n const path = yield* Path.Path;\n\n const prefix = projectRoot + path.sep;\n const suffix = pipe(fullPath, String.startsWith(prefix))\n ? pipe(fullPath, String.slice(prefix.length))\n : fullPath;\n\n const { char, color } = changeChar(change);\n\n yield* Console.log(\n pipe(\n AnsiDoc.char(char),\n AnsiDoc.annotate(color),\n AnsiDoc.catWithSpace(\n AnsiDoc.hcat([\n pipe(AnsiDoc.text(prefix), AnsiDoc.annotate(Ansi.blackBright)),\n pipe(AnsiDoc.text(suffix), AnsiDoc.annotate(color)),\n ]),\n ),\n AnsiDoc.render({ style: \"pretty\" }),\n ),\n );\n });\n\nconst logFunctionPathDiff = (\n previous: FunctionPaths.FunctionPaths,\n current: FunctionPaths.FunctionPaths,\n) =>\n Effect.gen(function* () {\n const {\n functionsAdded,\n functionsRemoved,\n groupsRemoved,\n groupsAdded,\n groupsChanged,\n } = FunctionPaths.diff(previous, current);\n\n const logForGroups = (\n groupPaths: GroupPaths.GroupPaths,\n fnPaths: FunctionPaths.FunctionPaths,\n logFn: typeof logFunctionAdded,\n ) =>\n Effect.forEach(groupPaths, (gp) =>\n Effect.forEach(\n Array.fromIterable(\n HashSet.filter(fnPaths, (fp) => Equal.equals(fp.groupPath, gp)),\n ),\n logFn,\n ),\n );\n\n yield* logForGroups(groupsRemoved, functionsRemoved, logFunctionRemoved);\n yield* logForGroups(groupsAdded, functionsAdded, logFunctionAdded);\n yield* Effect.forEach(groupsChanged, (gp) =>\n Effect.gen(function* () {\n yield* Effect.forEach(\n Array.fromIterable(\n HashSet.filter(functionsAdded, (fp) =>\n Equal.equals(fp.groupPath, gp),\n ),\n ),\n logFunctionAdded,\n );\n yield* Effect.forEach(\n Array.fromIterable(\n HashSet.filter(functionsRemoved, (fp) =>\n Equal.equals(fp.groupPath, gp),\n ),\n ),\n logFunctionRemoved,\n );\n }),\n );\n });\n\nexport const dev = Command.make(\"dev\", {}, () =>\n Effect.gen(function* () {\n yield* logPending(\"Performing initial sync…\");\n const previousFunctionPaths = yield* loadPreviousFunctionPaths;\n const initialResult = yield* codegenHandler.pipe(\n Effect.tap(({ functionPaths }) =>\n logFunctionPathDiff(previousFunctionPaths, functionPaths),\n ),\n Effect.tap(() => logSuccess(\"Generated files are up-to-date\")),\n CodegenError.catchAndLog,\n );\n const initialFunctionPaths = Option.match(initialResult, {\n onNone: () => emptyFunctionPaths,\n onSome: ({ functionPaths }) => functionPaths,\n });\n\n const pendingRef = yield* Ref.make<Pending>(pendingInit);\n const signal = yield* Queue.sliding<void>(1);\n const restartQueue = yield* Queue.sliding<void>(1);\n const watcherErrorsRef = yield* Ref.make<WatcherErrors>(emptyWatcherErrors);\n\n yield* Effect.all(\n [\n Effect.scoped(\n entryPointsWatcher(\n signal,\n pendingRef,\n restartQueue,\n watcherErrorsRef,\n ),\n ),\n confectStructureWatcher(signal, pendingRef, restartQueue),\n syncLoop(signal, pendingRef, initialFunctionPaths, watcherErrorsRef),\n ],\n { concurrency: \"unbounded\" },\n );\n }),\n).pipe(Command.withDescription(\"Start the Confect development server\"));\n\nconst esbuildMessageKey = (m: esbuild.Message): string =>\n `${m.location?.file ?? \"\"}:${m.location?.line ?? \"\"}:${m.location?.column ?? \"\"}:${m.text}`;\n\nconst allMessages = (errors: WatcherErrors): ReadonlyArray<esbuild.Message> =>\n pipe(Array.fromIterable(errors.values()), Array.flatten);\n\nconst dedupeWatcherErrors = (\n errors: WatcherErrors,\n): ReadonlyArray<esbuild.Message> =>\n pipe(\n allMessages(errors),\n Array.dedupeWith(\n (messageA, messageB) =>\n esbuildMessageKey(messageA) === esbuildMessageKey(messageB),\n ),\n );\n\nconst watcherErrorsSignature = (errors: WatcherErrors): string =>\n pipe(\n allMessages(errors),\n Array.map(esbuildMessageKey),\n Array.dedupe,\n Array.sort(Order.string),\n Array.join(\"\\n\"),\n );\n\n/**\n * Log any watcher errors that haven't already been logged at their\n * current signature. Suppresses the per-watcher fanout that happens\n * when one root cause (e.g. a missing import) breaks every entry\n * point's build at the same source location.\n */\nconst logChangedWatcherErrors = (\n watcherErrorsRef: Ref.Ref<WatcherErrors>,\n lastLoggedSignatureRef: Ref.Ref<string>,\n) =>\n Effect.gen(function* () {\n const errors = yield* Ref.get(watcherErrorsRef);\n const signature = watcherErrorsSignature(errors);\n const previous = yield* Ref.get(lastLoggedSignatureRef);\n if (signature === previous) return;\n yield* Ref.set(lastLoggedSignatureRef, signature);\n if (errors.size === 0) return;\n yield* logCoalescedBuildErrors(dedupeWatcherErrors(errors));\n });\n\n/**\n * Block until the signal queue has been quiet for `quiescence`. esbuild\n * watchers' `onEnd` events for a single user edit can be spread across\n * hundreds of milliseconds, so a fixed window misses late arrivals.\n * Bounded by `maxWait` so pathological signal floods can't pin the\n * loop forever.\n */\nconst drainUntilQuiescent = (\n signal: Queue.Queue<void>,\n quiescence: Duration.Duration,\n maxWait: Duration.Duration,\n) =>\n Effect.gen(function* () {\n const start = yield* Clock.currentTimeMillis;\n const maxMillis = Duration.toMillis(maxWait);\n yield* Effect.iterate(true as boolean, {\n while: (keepGoing) => keepGoing,\n body: () =>\n Effect.gen(function* () {\n yield* Effect.sleep(quiescence);\n const drained = yield* Queue.takeAll(signal);\n if (Chunk.isEmpty(drained)) return false;\n const now = yield* Clock.currentTimeMillis;\n return now - start < maxMillis;\n }),\n });\n });\n\nconst syncLoop = (\n signal: Queue.Queue<void>,\n pendingRef: Ref.Ref<Pending>,\n initialFunctionPaths: FunctionPaths.FunctionPaths,\n watcherErrorsRef: Ref.Ref<WatcherErrors>,\n) =>\n Effect.gen(function* () {\n const functionPathsRef = yield* Ref.make(initialFunctionPaths);\n const lastLoggedErrorsRef = yield* Ref.make<string>(\"\");\n\n return yield* Effect.forever(\n Effect.gen(function* () {\n yield* Effect.logDebug(\"Running sync loop…\");\n // Wait for the first signal of a burst, then keep absorbing\n // follow-up signals from other watchers' onEnds until the queue\n // stays quiet for `COALESCE_QUIESCENCE`.\n yield* Queue.take(signal);\n yield* drainUntilQuiescent(\n signal,\n COALESCE_QUIESCENCE,\n COALESCE_MAX_WAIT,\n );\n\n yield* logChangedWatcherErrors(watcherErrorsRef, lastLoggedErrorsRef);\n\n const pending = yield* Ref.getAndSet(pendingRef, pendingInit);\n\n if (!isPendingDirty(pending)) {\n // No-op signal (e.g. a late echo after a previous cycle\n // already drained). Stay silent.\n return;\n }\n\n yield* logPending(\"Dependencies may have changed, reloading…\");\n\n if (pending.specDirty) {\n const current = yield* codegenHandler.pipe(\n Effect.tap(({ functionPaths: nextFunctionPaths }) =>\n Effect.gen(function* () {\n const previous = yield* Ref.get(functionPathsRef);\n yield* logFunctionPathDiff(previous, nextFunctionPaths);\n yield* Ref.set(functionPathsRef, nextFunctionPaths);\n }),\n ),\n CodegenError.catchAndLog,\n );\n if (Option.isNone(current)) {\n return;\n }\n // Drain any stragglers from this cycle's burst (slow watchers\n // whose onEnd fired after the first quiescence) plus, when\n // codegen wrote, the echo signals esbuild emits in response\n // to our writes. Reset `pendingRef` so those drained signals\n // don't carry a dirty flag into the next cycle.\n if (current.value.anyWritesHappened) {\n yield* Effect.sleep(ECHO_COOLDOWN);\n }\n yield* drainUntilQuiescent(\n signal,\n COALESCE_QUIESCENCE,\n COALESCE_MAX_WAIT,\n );\n yield* Ref.set(pendingRef, pendingInit);\n }\n\n const dirtyOptionalFiles = [\n ...(pending.httpDirty\n ? [syncOptionalFile(generateHttp, \"http.ts\")]\n : []),\n ...(pending.cronsDirty\n ? [syncOptionalFile(generateCrons, \"crons.ts\")]\n : []),\n ...(pending.authDirty\n ? [syncOptionalFile(generateAuthConfig, \"auth.config.ts\")]\n : []),\n ];\n\n yield* Array.isNonEmptyReadonlyArray(dirtyOptionalFiles)\n ? Effect.all(dirtyOptionalFiles, { concurrency: \"unbounded\" })\n : Effect.void;\n\n yield* logSuccess(\"Generated files are up-to-date\");\n }),\n );\n });\n\ninterface EntryPoint {\n readonly absolutePath: string;\n readonly displayPath: string;\n readonly pendingKey: PendingKey;\n}\n\n/**\n * Every file whose import graph codegen should react to. Each one becomes\n * its own scoped esbuild watcher; the union of their watches gives us\n * dependency-aware tracking of anything reachable from `confect/`,\n * including files outside `confect/`.\n */\nconst discoverEntryPoints = Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const projectRoot = yield* ProjectRoot.get;\n const confectDirectory = yield* ConfectDirectory.get;\n\n const tryEntry = (relativePath: string, pendingKey: PendingKey) =>\n Effect.gen(function* () {\n const absolutePath = path.join(confectDirectory, relativePath);\n if (!(yield* fs.exists(absolutePath))) {\n return Option.none<EntryPoint>();\n }\n return Option.some<EntryPoint>({\n absolutePath,\n displayPath: path.relative(projectRoot, absolutePath),\n pendingKey,\n });\n });\n\n const fixedEntryOptions = yield* Effect.all([\n tryEntry(GENERATED_SPEC_PATH, \"specDirty\"),\n tryEntry(GENERATED_NODE_SPEC_PATH, \"specDirty\"),\n tryEntry(\"schema.ts\", \"specDirty\"),\n tryEntry(\"http.ts\", \"httpDirty\"),\n tryEntry(\"crons.ts\", \"cronsDirty\"),\n tryEntry(\"auth.ts\", \"authDirty\"),\n ]);\n\n const implRelativePaths = yield* discoverLeafImplFiles;\n const implEntryOptions = yield* Effect.forEach(\n implRelativePaths,\n (relativePath) => tryEntry(relativePath, \"specDirty\"),\n );\n\n return Array.getSomes([...fixedEntryOptions, ...implEntryOptions]);\n});\n\nconst esbuildOptions = (\n entry: EntryPoint,\n signal: Queue.Queue<void>,\n pendingRef: Ref.Ref<Pending>,\n watcherErrorsRef: Ref.Ref<WatcherErrors>,\n) => {\n // First `onEnd` fires when esbuild finishes the watcher's initial\n // build. At startup that's an echo of the just-completed initial\n // codegen pass; for a watcher spawned mid-session (e.g. a newly\n // added impl) it's an echo of the codegen run that triggered the\n // restart. Either way, the entry's contents were already accounted\n // for, so we record any errors but don't flip dirty or push a\n // signal — only genuine subsequent rebuilds should do that.\n const initialBuildSeenRef = Ref.unsafeMake(false);\n return {\n entryPoints: [entry.absolutePath],\n bundle: true,\n write: false,\n metafile: true,\n platform: \"node\" as const,\n format: \"esm\" as const,\n logLevel: \"silent\" as const,\n plugins: [\n absoluteExternalsPlugin,\n {\n name: \"notify-rebuild\",\n setup(build: esbuild.PluginBuild) {\n build.onEnd((result) => {\n Effect.runPromise(\n Effect.gen(function* () {\n const wasInitial = yield* Ref.getAndSet(\n initialBuildSeenRef,\n true,\n );\n const isInitial = !wasInitial;\n yield* Ref.update(watcherErrorsRef, (current) => {\n const next = new Map(current);\n if (result.errors.length > 0) {\n next.set(entry.absolutePath, result.errors);\n } else {\n next.delete(entry.absolutePath);\n }\n return next;\n });\n if (isInitial && result.errors.length === 0) return;\n yield* Ref.update(pendingRef, (p) => ({\n ...p,\n [entry.pendingKey]: true,\n }));\n yield* Queue.offer(signal, undefined);\n }),\n );\n });\n },\n },\n ],\n };\n};\n\nconst createEntryPointWatcher = (\n entry: EntryPoint,\n signal: Queue.Queue<void>,\n pendingRef: Ref.Ref<Pending>,\n watcherErrorsRef: Ref.Ref<WatcherErrors>,\n) =>\n Effect.acquireRelease(\n Effect.promise(async () => {\n const ctx = await esbuild.context(\n esbuildOptions(entry, signal, pendingRef, watcherErrorsRef),\n );\n await ctx.watch();\n return ctx;\n }),\n (ctx) =>\n Effect.gen(function* () {\n yield* Effect.promise(() => ctx.dispose());\n // Clear any errors recorded by this watcher so a disposed\n // watcher can't leave stale errors visible to the sync loop.\n yield* Ref.update(watcherErrorsRef, (current) => {\n if (!current.has(entry.absolutePath)) return current;\n const next = new Map(current);\n next.delete(entry.absolutePath);\n return next;\n });\n yield* Effect.logDebug(\n `esbuild watcher disposed: ${entry.displayPath}`,\n );\n }),\n );\n\n/**\n * Holds one scoped esbuild watcher per entry point and reconciles the set\n * whenever something offers to `restartQueue`. Adding or removing an entry\n * point only spawns/disposes the affected watcher; unchanged entries keep\n * their existing context, so a structural change doesn't churn watchers\n * for unrelated files.\n */\nconst entryPointsWatcher = (\n signal: Queue.Queue<void>,\n pendingRef: Ref.Ref<Pending>,\n restartQueue: Queue.Queue<void>,\n watcherErrorsRef: Ref.Ref<WatcherErrors>,\n) =>\n Effect.gen(function* () {\n const parentScope = yield* Effect.scope;\n const scopesRef = yield* Ref.make(new Map<string, Scope.CloseableScope>());\n\n const sync = Effect.gen(function* () {\n const desired = yield* discoverEntryPoints;\n const desiredByPath = new Map(\n desired.map((entryPoint) => [entryPoint.absolutePath, entryPoint]),\n );\n const current = yield* Ref.get(scopesRef);\n\n yield* Effect.forEach(\n Array.fromIterable(current),\n ([absolutePath, childScope]) =>\n desiredByPath.has(absolutePath)\n ? Effect.void\n : Scope.close(childScope, Exit.void).pipe(\n Effect.andThen(\n Ref.update(scopesRef, (scopes) => {\n const updated = new Map(scopes);\n updated.delete(absolutePath);\n return updated;\n }),\n ),\n ),\n );\n\n yield* Effect.forEach(desired, (entry) =>\n Effect.gen(function* () {\n const existing = yield* Ref.get(scopesRef);\n if (existing.has(entry.absolutePath)) return;\n\n const childScope = yield* Scope.fork(\n parentScope,\n ExecutionStrategy.sequential,\n );\n yield* createEntryPointWatcher(\n entry,\n signal,\n pendingRef,\n watcherErrorsRef,\n ).pipe(Scope.extend(childScope));\n yield* Ref.update(scopesRef, (scopes) => {\n const updated = new Map(scopes);\n updated.set(entry.absolutePath, childScope);\n return updated;\n });\n }),\n );\n });\n\n yield* sync;\n\n return yield* Effect.forever(\n Queue.take(restartQueue).pipe(Effect.andThen(sync)),\n );\n });\n\n/**\n * Single recursive `fs.watch` on `confect/`. Flips the matching dirty flag\n * for any change to an entry-point-shaped file (so codegen runs without\n * waiting on a newly spawned esbuild watcher), and offers to\n * `restartQueue` when an entry point is created or removed so the watcher\n * manager picks up the new set.\n */\nconst confectStructureWatcher = (\n signal: Queue.Queue<void>,\n pendingRef: Ref.Ref<Pending>,\n restartQueue: Queue.Queue<void>,\n) =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const confectDirectory = yield* ConfectDirectory.get;\n\n yield* pipe(\n fs.watch(confectDirectory, { recursive: true }),\n Stream.debounce(Duration.millis(200)),\n Stream.runForEach((event) =>\n handleConfectChange({\n relativePath: path.relative(confectDirectory, event.path),\n eventTag: event._tag,\n signal,\n pendingRef,\n restartQueue,\n }),\n ),\n );\n });\n\nconst TOP_LEVEL_OPTIONAL_KEYS: ReadonlyMap<string, PendingKey> = new Map([\n [\"http.ts\", \"httpDirty\"],\n [\"crons.ts\", \"cronsDirty\"],\n [\"auth.ts\", \"authDirty\"],\n]);\n\nconst flipDirtyAndSignal = (\n pendingRef: Ref.Ref<Pending>,\n signal: Queue.Queue<void>,\n key: PendingKey,\n restartQueue: Queue.Queue<void>,\n restart: boolean,\n) =>\n pipe(\n Ref.update(pendingRef, (p) => ({ ...p, [key]: true })),\n Effect.andThen(Queue.offer(signal, undefined)),\n Effect.andThen(\n restart ? Queue.offer(restartQueue, undefined) : Effect.void,\n ),\n );\n\nconst handleConfectChange = ({\n relativePath,\n eventTag,\n signal,\n pendingRef,\n restartQueue,\n}: {\n relativePath: string;\n eventTag: \"Create\" | \"Update\" | \"Remove\";\n signal: Queue.Queue<void>;\n pendingRef: Ref.Ref<Pending>;\n restartQueue: Queue.Queue<void>;\n}) => {\n // _generated/ files are written by codegen itself; reacting to them here\n // would form a loop. The esbuild watchers track the generated specs as\n // entry points, so changes there flow back through `notify-rebuild`.\n if (relativePath.split(/[/\\\\]/).includes(\"_generated\")) {\n return Effect.void;\n }\n\n if (!relativePath.endsWith(\".ts\")) {\n return Effect.void;\n }\n\n const isLifecycleChange = eventTag !== \"Update\";\n\n const topLevelKey = TOP_LEVEL_OPTIONAL_KEYS.get(relativePath);\n if (topLevelKey !== undefined) {\n return flipDirtyAndSignal(\n pendingRef,\n signal,\n topLevelKey,\n restartQueue,\n isLifecycleChange,\n );\n }\n\n if (relativePath === \"schema.ts\") {\n return flipDirtyAndSignal(\n pendingRef,\n signal,\n \"specDirty\",\n restartQueue,\n isLifecycleChange,\n );\n }\n\n if (isLeafSpecPath(relativePath) || isLeafImplPath(relativePath)) {\n return flipDirtyAndSignal(\n pendingRef,\n signal,\n \"specDirty\",\n restartQueue,\n isLifecycleChange,\n );\n }\n\n // Any other `.ts` under `confect/` (helpers like `tables/Notes.ts`).\n // Updates to such files are handled by the esbuild watcher for whichever\n // entry point imports them — its onEnd flips the right dirty flag.\n // Creates are our safety net: when a previously-missing import is added,\n // esbuild may not have its parent directory on a poll path, so we\n // re-run codegen on Create here.\n if (eventTag === \"Create\") {\n return flipDirtyAndSignal(\n pendingRef,\n signal,\n \"specDirty\",\n restartQueue,\n false,\n );\n }\n\n return Effect.void;\n};\n\nconst syncOptionalFile = (generate: typeof generateHttp, convexFile: string) =>\n pipe(\n generate,\n Effect.andThen(\n Option.match({\n onSome: ({ change, convexFilePath }) =>\n Match.value(change).pipe(\n Match.when(\"Unchanged\", () => Effect.void),\n Match.whenOr(\"Added\", \"Modified\", (addedOrModified) =>\n logFileChangeIndented(addedOrModified, convexFilePath),\n ),\n Match.exhaustive,\n ),\n onNone: () =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const convexDirectory = yield* ConvexDirectory.get;\n const convexFilePath = path.join(convexDirectory, convexFile);\n\n if (yield* fs.exists(convexFilePath)) {\n yield* fs.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MAAM,WACP;CACH,cACE,OAAO,IAAI,aAAa;EACtB,MAAM,KAAK,OAAO,WAAW;EAC7B,MAAM,OAAO,OAAO,KAAK;EACzB,MAAM,kBAAkB,OAAO,gBAAgB;EAC/C,MAAM,iBAAiB,KAAK,KAAK,iBAAiB,WAAW;AAE7D,MAAI,OAAO,GAAG,OAAO,eAAe,EAAE;AACpC,UAAO,GAAG,OAAO,eAAe;AAChC,UAAO,sBAAsB,WAAW,eAAe;;GAEzD;CACL,CAAC,CACH,CACF"}
1
+ {"version":3,"file":"dev.mjs","names":["FunctionPaths.diff","CodegenError.catchAndLog"],"sources":["../../src/confect/dev.ts"],"sourcesContent":["import { Command } from \"@effect/cli\";\nimport { FileSystem, Path } from \"@effect/platform\";\nimport { Ansi, AnsiDoc } from \"@effect/printer-ansi\";\nimport {\n Array,\n Chunk,\n Clock,\n Console,\n Duration,\n Effect,\n Equal,\n ExecutionStrategy,\n Exit,\n HashSet,\n Match,\n Option,\n Order,\n pipe,\n Queue,\n Ref,\n Scope,\n Stream,\n String,\n} from \"effect\";\nimport {\n externalPlugin,\n loadTsConfig,\n tsconfigPathsToRegExp,\n} from \"bundle-require\";\nimport * as esbuild from \"esbuild\";\nimport { logCoalescedBuildErrors } from \"../BuildError\";\nimport * as CodegenError from \"../CodegenError\";\nimport { ConfectDirectory } from \"../ConfectDirectory\";\nimport { ConvexDirectory } from \"../ConvexDirectory\";\nimport * as FunctionPaths from \"../FunctionPaths\";\nimport type * as GroupPaths from \"../GroupPaths\";\nimport {\n discoverLeafImplFiles,\n isLeafImplPath,\n isLeafSpecPath,\n} from \"../LeafModule\";\nimport {\n logFunctionAdded,\n logFunctionRemoved,\n logPending,\n logSuccess,\n} from \"../log\";\nimport { ProjectRoot } from \"../ProjectRoot\";\nimport { generateAuthConfig, generateCrons, generateHttp } from \"../utils\";\nimport { codegenHandler, loadPreviousFunctionPaths } from \"./codegen\";\n\nconst GENERATED_SPEC_PATH = \"_generated/spec.ts\";\nconst GENERATED_NODE_SPEC_PATH = \"_generated/nodeSpec.ts\";\n\n// Quiescence window: the sync loop waits this long for further signals\n// after each batch. One user edit fires `onEnd` on every esbuild\n// watcher that touches the file, and rebuild times vary across entry\n// points so the onEnds can be spread over hundreds of milliseconds.\n// The drain keeps extending its wait (bounded by `COALESCE_MAX_WAIT`)\n// until no new signals arrive within the window, collapsing the whole\n// burst into a single codegen cycle.\nconst COALESCE_QUIESCENCE = Duration.millis(300);\n\n// Upper bound on `drainUntilQuiescent` so a pathological infinite\n// signal stream can't pin the sync loop forever.\nconst COALESCE_MAX_WAIT = Duration.seconds(5);\n\n// How long to wait for esbuild watchers to react to codegen's own\n// writes (e.g. an updated `_generated/spec.ts`). Added on top of the\n// quiescence drain when codegen reported writes happened.\nconst ECHO_COOLDOWN = Duration.millis(500);\n\nconst emptyFunctionPaths = FunctionPaths.FunctionPaths.make(HashSet.empty());\n\ntype Pending = {\n readonly specDirty: boolean;\n readonly httpDirty: boolean;\n readonly cronsDirty: boolean;\n readonly authDirty: boolean;\n};\n\ntype PendingKey = keyof Pending;\n\nconst pendingInit: Pending = {\n specDirty: false,\n httpDirty: false,\n cronsDirty: false,\n authDirty: false,\n};\n\nconst isPendingDirty = (p: Pending): boolean =>\n p.specDirty || p.httpDirty || p.cronsDirty || p.authDirty;\n\ntype WatcherErrors = ReadonlyMap<string, readonly esbuild.Message[]>;\n\nconst emptyWatcherErrors: WatcherErrors = new Map();\n\nconst changeChar = (change: \"Added\" | \"Removed\" | \"Modified\") =>\n Match.value(change).pipe(\n Match.when(\"Added\", () => ({ char: \"+\", color: Ansi.green })),\n Match.when(\"Removed\", () => ({ char: \"-\", color: Ansi.red })),\n Match.when(\"Modified\", () => ({ char: \"~\", color: Ansi.yellow })),\n Match.exhaustive,\n );\n\nconst logFileChangeIndented = (\n change: \"Added\" | \"Removed\" | \"Modified\",\n fullPath: string,\n) =>\n Effect.gen(function* () {\n const projectRoot = yield* ProjectRoot.get;\n const path = yield* Path.Path;\n\n const prefix = projectRoot + path.sep;\n const suffix = pipe(fullPath, String.startsWith(prefix))\n ? pipe(fullPath, String.slice(prefix.length))\n : fullPath;\n\n const { char, color } = changeChar(change);\n\n yield* Console.log(\n pipe(\n AnsiDoc.char(char),\n AnsiDoc.annotate(color),\n AnsiDoc.catWithSpace(\n AnsiDoc.hcat([\n pipe(AnsiDoc.text(prefix), AnsiDoc.annotate(Ansi.blackBright)),\n pipe(AnsiDoc.text(suffix), AnsiDoc.annotate(color)),\n ]),\n ),\n AnsiDoc.render({ style: \"pretty\" }),\n ),\n );\n });\n\nconst logFunctionPathDiff = (\n previous: FunctionPaths.FunctionPaths,\n current: FunctionPaths.FunctionPaths,\n) =>\n Effect.gen(function* () {\n const {\n functionsAdded,\n functionsRemoved,\n groupsRemoved,\n groupsAdded,\n groupsChanged,\n } = FunctionPaths.diff(previous, current);\n\n const logForGroups = (\n groupPaths: GroupPaths.GroupPaths,\n fnPaths: FunctionPaths.FunctionPaths,\n logFn: typeof logFunctionAdded,\n ) =>\n Effect.forEach(groupPaths, (gp) =>\n Effect.forEach(\n Array.fromIterable(\n HashSet.filter(fnPaths, (fp) => Equal.equals(fp.groupPath, gp)),\n ),\n logFn,\n ),\n );\n\n yield* logForGroups(groupsRemoved, functionsRemoved, logFunctionRemoved);\n yield* logForGroups(groupsAdded, functionsAdded, logFunctionAdded);\n yield* Effect.forEach(groupsChanged, (gp) =>\n Effect.gen(function* () {\n yield* Effect.forEach(\n Array.fromIterable(\n HashSet.filter(functionsAdded, (fp) =>\n Equal.equals(fp.groupPath, gp),\n ),\n ),\n logFunctionAdded,\n );\n yield* Effect.forEach(\n Array.fromIterable(\n HashSet.filter(functionsRemoved, (fp) =>\n Equal.equals(fp.groupPath, gp),\n ),\n ),\n logFunctionRemoved,\n );\n }),\n );\n });\n\nexport const dev = Command.make(\"dev\", {}, () =>\n Effect.gen(function* () {\n yield* logPending(\"Performing initial sync…\");\n const previousFunctionPaths = yield* loadPreviousFunctionPaths;\n const initialResult = yield* codegenHandler.pipe(\n Effect.tap(({ functionPaths }) =>\n logFunctionPathDiff(previousFunctionPaths, functionPaths),\n ),\n Effect.tap(() => logSuccess(\"Generated files are up-to-date\")),\n CodegenError.catchAndLog,\n );\n const initialFunctionPaths = Option.match(initialResult, {\n onNone: () => emptyFunctionPaths,\n onSome: ({ functionPaths }) => functionPaths,\n });\n\n const pendingRef = yield* Ref.make<Pending>(pendingInit);\n const signal = yield* Queue.sliding<void>(1);\n const restartQueue = yield* Queue.sliding<void>(1);\n const watcherErrorsRef = yield* Ref.make<WatcherErrors>(emptyWatcherErrors);\n\n yield* Effect.all(\n [\n Effect.scoped(\n entryPointsWatcher(\n signal,\n pendingRef,\n restartQueue,\n watcherErrorsRef,\n ),\n ),\n confectStructureWatcher(signal, pendingRef, restartQueue),\n syncLoop(signal, pendingRef, initialFunctionPaths, watcherErrorsRef),\n ],\n { concurrency: \"unbounded\" },\n );\n }),\n).pipe(Command.withDescription(\"Start the Confect development server\"));\n\nconst esbuildMessageKey = (m: esbuild.Message): string =>\n `${m.location?.file ?? \"\"}:${m.location?.line ?? \"\"}:${m.location?.column ?? \"\"}:${m.text}`;\n\nconst allMessages = (errors: WatcherErrors): ReadonlyArray<esbuild.Message> =>\n pipe(Array.fromIterable(errors.values()), Array.flatten);\n\nconst dedupeWatcherErrors = (\n errors: WatcherErrors,\n): ReadonlyArray<esbuild.Message> =>\n pipe(\n allMessages(errors),\n Array.dedupeWith(\n (messageA, messageB) =>\n esbuildMessageKey(messageA) === esbuildMessageKey(messageB),\n ),\n );\n\nconst watcherErrorsSignature = (errors: WatcherErrors): string =>\n pipe(\n allMessages(errors),\n Array.map(esbuildMessageKey),\n Array.dedupe,\n Array.sort(Order.string),\n Array.join(\"\\n\"),\n );\n\n/**\n * Log any watcher errors that haven't already been logged at their\n * current signature. Suppresses the per-watcher fanout that happens\n * when one root cause (e.g. a missing import) breaks every entry\n * point's build at the same source location.\n */\nconst logChangedWatcherErrors = (\n watcherErrorsRef: Ref.Ref<WatcherErrors>,\n lastLoggedSignatureRef: Ref.Ref<string>,\n) =>\n Effect.gen(function* () {\n const errors = yield* Ref.get(watcherErrorsRef);\n const signature = watcherErrorsSignature(errors);\n const previous = yield* Ref.get(lastLoggedSignatureRef);\n if (signature === previous) return;\n yield* Ref.set(lastLoggedSignatureRef, signature);\n if (errors.size === 0) return;\n yield* logCoalescedBuildErrors(dedupeWatcherErrors(errors));\n });\n\n/**\n * Block until the signal queue has been quiet for `quiescence`. esbuild\n * watchers' `onEnd` events for a single user edit can be spread across\n * hundreds of milliseconds, so a fixed window misses late arrivals.\n * Bounded by `maxWait` so pathological signal floods can't pin the\n * loop forever.\n */\nconst drainUntilQuiescent = (\n signal: Queue.Queue<void>,\n quiescence: Duration.Duration,\n maxWait: Duration.Duration,\n) =>\n Effect.gen(function* () {\n const start = yield* Clock.currentTimeMillis;\n const maxMillis = Duration.toMillis(maxWait);\n yield* Effect.iterate(true as boolean, {\n while: (keepGoing) => keepGoing,\n body: () =>\n Effect.gen(function* () {\n yield* Effect.sleep(quiescence);\n const drained = yield* Queue.takeAll(signal);\n if (Chunk.isEmpty(drained)) return false;\n const now = yield* Clock.currentTimeMillis;\n return now - start < maxMillis;\n }),\n });\n });\n\nconst syncLoop = (\n signal: Queue.Queue<void>,\n pendingRef: Ref.Ref<Pending>,\n initialFunctionPaths: FunctionPaths.FunctionPaths,\n watcherErrorsRef: Ref.Ref<WatcherErrors>,\n) =>\n Effect.gen(function* () {\n const functionPathsRef = yield* Ref.make(initialFunctionPaths);\n const lastLoggedErrorsRef = yield* Ref.make<string>(\"\");\n\n return yield* Effect.forever(\n Effect.gen(function* () {\n yield* Effect.logDebug(\"Running sync loop…\");\n // Wait for the first signal of a burst, then keep absorbing\n // follow-up signals from other watchers' onEnds until the queue\n // stays quiet for `COALESCE_QUIESCENCE`.\n yield* Queue.take(signal);\n yield* drainUntilQuiescent(\n signal,\n COALESCE_QUIESCENCE,\n COALESCE_MAX_WAIT,\n );\n\n yield* logChangedWatcherErrors(watcherErrorsRef, lastLoggedErrorsRef);\n\n const pending = yield* Ref.getAndSet(pendingRef, pendingInit);\n\n if (!isPendingDirty(pending)) {\n // No-op signal (e.g. a late echo after a previous cycle\n // already drained). Stay silent.\n return;\n }\n\n yield* logPending(\"Dependencies may have changed, reloading…\");\n\n if (pending.specDirty) {\n const current = yield* codegenHandler.pipe(\n Effect.tap(({ functionPaths: nextFunctionPaths }) =>\n Effect.gen(function* () {\n const previous = yield* Ref.get(functionPathsRef);\n yield* logFunctionPathDiff(previous, nextFunctionPaths);\n yield* Ref.set(functionPathsRef, nextFunctionPaths);\n }),\n ),\n CodegenError.catchAndLog,\n );\n if (Option.isNone(current)) {\n return;\n }\n // Drain any stragglers from this cycle's burst (slow watchers\n // whose onEnd fired after the first quiescence) plus, when\n // codegen wrote, the echo signals esbuild emits in response\n // to our writes. Reset `pendingRef` so those drained signals\n // don't carry a dirty flag into the next cycle.\n if (current.value.anyWritesHappened) {\n yield* Effect.sleep(ECHO_COOLDOWN);\n }\n yield* drainUntilQuiescent(\n signal,\n COALESCE_QUIESCENCE,\n COALESCE_MAX_WAIT,\n );\n yield* Ref.set(pendingRef, pendingInit);\n }\n\n const dirtyOptionalFiles = [\n ...(pending.httpDirty\n ? [syncOptionalFile(generateHttp, \"http.ts\")]\n : []),\n ...(pending.cronsDirty\n ? [syncOptionalFile(generateCrons, \"crons.ts\")]\n : []),\n ...(pending.authDirty\n ? [syncOptionalFile(generateAuthConfig, \"auth.config.ts\")]\n : []),\n ];\n\n yield* Array.isNonEmptyReadonlyArray(dirtyOptionalFiles)\n ? Effect.all(dirtyOptionalFiles, { concurrency: \"unbounded\" })\n : Effect.void;\n\n yield* logSuccess(\"Generated files are up-to-date\");\n }),\n );\n });\n\ninterface EntryPoint {\n readonly absolutePath: string;\n readonly displayPath: string;\n readonly pendingKey: PendingKey;\n}\n\n/**\n * Every file whose import graph codegen should react to. Each one becomes\n * its own scoped esbuild watcher; the union of their watches gives us\n * dependency-aware tracking of anything reachable from `confect/`,\n * including files outside `confect/`.\n */\nconst discoverEntryPoints = Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const projectRoot = yield* ProjectRoot.get;\n const confectDirectory = yield* ConfectDirectory.get;\n\n const tryEntry = (relativePath: string, pendingKey: PendingKey) =>\n Effect.gen(function* () {\n const absolutePath = path.join(confectDirectory, relativePath);\n if (!(yield* fs.exists(absolutePath))) {\n return Option.none<EntryPoint>();\n }\n return Option.some<EntryPoint>({\n absolutePath,\n displayPath: path.relative(projectRoot, absolutePath),\n pendingKey,\n });\n });\n\n const fixedEntryOptions = yield* Effect.all([\n tryEntry(GENERATED_SPEC_PATH, \"specDirty\"),\n tryEntry(GENERATED_NODE_SPEC_PATH, \"specDirty\"),\n tryEntry(\"schema.ts\", \"specDirty\"),\n tryEntry(\"http.ts\", \"httpDirty\"),\n tryEntry(\"crons.ts\", \"cronsDirty\"),\n tryEntry(\"auth.ts\", \"authDirty\"),\n ]);\n\n const implRelativePaths = yield* discoverLeafImplFiles;\n const implEntryOptions = yield* Effect.forEach(\n implRelativePaths,\n (relativePath) => tryEntry(relativePath, \"specDirty\"),\n );\n\n return Array.getSomes([...fixedEntryOptions, ...implEntryOptions]);\n});\n\nconst esbuildOptions = (\n entry: EntryPoint,\n notExternal: ReadonlyArray<RegExp>,\n signal: Queue.Queue<void>,\n pendingRef: Ref.Ref<Pending>,\n watcherErrorsRef: Ref.Ref<WatcherErrors>,\n) => {\n // First `onEnd` fires when esbuild finishes the watcher's initial\n // build. At startup that's an echo of the just-completed initial\n // codegen pass; for a watcher spawned mid-session (e.g. a newly\n // added impl) it's an echo of the codegen run that triggered the\n // restart. Either way, the entry's contents were already accounted\n // for, so we record any errors but don't flip dirty or push a\n // signal — only genuine subsequent rebuilds should do that.\n const initialBuildSeenRef = Ref.unsafeMake(false);\n return {\n entryPoints: [entry.absolutePath],\n bundle: true,\n write: false,\n metafile: true,\n platform: \"node\" as const,\n format: \"esm\" as const,\n logLevel: \"silent\" as const,\n plugins: [\n externalPlugin({ notExternal: [...notExternal] }),\n {\n name: \"notify-rebuild\",\n setup(build: esbuild.PluginBuild) {\n build.onEnd((result) => {\n Effect.runPromise(\n Effect.gen(function* () {\n const wasInitial = yield* Ref.getAndSet(\n initialBuildSeenRef,\n true,\n );\n const isInitial = !wasInitial;\n yield* Ref.update(watcherErrorsRef, (current) => {\n const next = new Map(current);\n if (result.errors.length > 0) {\n next.set(entry.absolutePath, result.errors);\n } else {\n next.delete(entry.absolutePath);\n }\n return next;\n });\n if (isInitial && result.errors.length === 0) return;\n yield* Ref.update(pendingRef, (p) => ({\n ...p,\n [entry.pendingKey]: true,\n }));\n yield* Queue.offer(signal, undefined);\n }),\n );\n });\n },\n },\n ],\n };\n};\n\nconst createEntryPointWatcher = (\n entry: EntryPoint,\n notExternal: ReadonlyArray<RegExp>,\n signal: Queue.Queue<void>,\n pendingRef: Ref.Ref<Pending>,\n watcherErrorsRef: Ref.Ref<WatcherErrors>,\n) =>\n Effect.acquireRelease(\n Effect.promise(async () => {\n const ctx = await esbuild.context(\n esbuildOptions(\n entry,\n notExternal,\n signal,\n pendingRef,\n watcherErrorsRef,\n ),\n );\n await ctx.watch();\n return ctx;\n }),\n (ctx) =>\n Effect.gen(function* () {\n yield* Effect.promise(() => ctx.dispose());\n // Clear any errors recorded by this watcher so a disposed\n // watcher can't leave stale errors visible to the sync loop.\n yield* Ref.update(watcherErrorsRef, (current) => {\n if (!current.has(entry.absolutePath)) return current;\n const next = new Map(current);\n next.delete(entry.absolutePath);\n return next;\n });\n yield* Effect.logDebug(\n `esbuild watcher disposed: ${entry.displayPath}`,\n );\n }),\n );\n\n/**\n * Holds one scoped esbuild watcher per entry point and reconciles the set\n * whenever something offers to `restartQueue`. Adding or removing an entry\n * point only spawns/disposes the affected watcher; unchanged entries keep\n * their existing context, so a structural change doesn't churn watchers\n * for unrelated files.\n */\nconst entryPointsWatcher = (\n signal: Queue.Queue<void>,\n pendingRef: Ref.Ref<Pending>,\n restartQueue: Queue.Queue<void>,\n watcherErrorsRef: Ref.Ref<WatcherErrors>,\n) =>\n Effect.gen(function* () {\n const parentScope = yield* Effect.scope;\n const scopesRef = yield* Ref.make(new Map<string, Scope.CloseableScope>());\n const projectRoot = yield* ProjectRoot.get;\n // Discover the user's `tsconfig.json#paths` once at watcher startup so\n // `~/...`-style aliases pointing into the user's source tree get bundled\n // by esbuild instead of externalized via `bundle-require`'s `node_modules`\n // heuristic. `loadTsConfig` walks up from `projectRoot` to find a\n // `tsconfig.json`; if none exists, `paths` is empty and `notExternal` is\n // `[]`, leaving the externalization rule unchanged.\n const tsconfig = loadTsConfig(projectRoot);\n const notExternal = tsconfigPathsToRegExp(\n tsconfig?.data.compilerOptions?.paths ?? {},\n );\n\n const sync = Effect.gen(function* () {\n const desired = yield* discoverEntryPoints;\n const desiredByPath = new Map(\n desired.map((entryPoint) => [entryPoint.absolutePath, entryPoint]),\n );\n const current = yield* Ref.get(scopesRef);\n\n yield* Effect.forEach(\n Array.fromIterable(current),\n ([absolutePath, childScope]) =>\n desiredByPath.has(absolutePath)\n ? Effect.void\n : Scope.close(childScope, Exit.void).pipe(\n Effect.andThen(\n Ref.update(scopesRef, (scopes) => {\n const updated = new Map(scopes);\n updated.delete(absolutePath);\n return updated;\n }),\n ),\n ),\n );\n\n yield* Effect.forEach(desired, (entry) =>\n Effect.gen(function* () {\n const existing = yield* Ref.get(scopesRef);\n if (existing.has(entry.absolutePath)) return;\n\n const childScope = yield* Scope.fork(\n parentScope,\n ExecutionStrategy.sequential,\n );\n yield* createEntryPointWatcher(\n entry,\n notExternal,\n signal,\n pendingRef,\n watcherErrorsRef,\n ).pipe(Scope.extend(childScope));\n yield* Ref.update(scopesRef, (scopes) => {\n const updated = new Map(scopes);\n updated.set(entry.absolutePath, childScope);\n return updated;\n });\n }),\n );\n });\n\n yield* sync;\n\n return yield* Effect.forever(\n Queue.take(restartQueue).pipe(Effect.andThen(sync)),\n );\n });\n\n/**\n * Single recursive `fs.watch` on `confect/`. Flips the matching dirty flag\n * for any change to an entry-point-shaped file (so codegen runs without\n * waiting on a newly spawned esbuild watcher), and offers to\n * `restartQueue` when an entry point is created or removed so the watcher\n * manager picks up the new set.\n */\nconst confectStructureWatcher = (\n signal: Queue.Queue<void>,\n pendingRef: Ref.Ref<Pending>,\n restartQueue: Queue.Queue<void>,\n) =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const confectDirectory = yield* ConfectDirectory.get;\n\n yield* pipe(\n fs.watch(confectDirectory, { recursive: true }),\n Stream.debounce(Duration.millis(200)),\n Stream.runForEach((event) =>\n handleConfectChange({\n relativePath: path.relative(confectDirectory, event.path),\n eventTag: event._tag,\n signal,\n pendingRef,\n restartQueue,\n }),\n ),\n );\n });\n\nconst TOP_LEVEL_OPTIONAL_KEYS: ReadonlyMap<string, PendingKey> = new Map([\n [\"http.ts\", \"httpDirty\"],\n [\"crons.ts\", \"cronsDirty\"],\n [\"auth.ts\", \"authDirty\"],\n]);\n\nconst flipDirtyAndSignal = (\n pendingRef: Ref.Ref<Pending>,\n signal: Queue.Queue<void>,\n key: PendingKey,\n restartQueue: Queue.Queue<void>,\n restart: boolean,\n) =>\n pipe(\n Ref.update(pendingRef, (p) => ({ ...p, [key]: true })),\n Effect.andThen(Queue.offer(signal, undefined)),\n Effect.andThen(\n restart ? Queue.offer(restartQueue, undefined) : Effect.void,\n ),\n );\n\nconst handleConfectChange = ({\n relativePath,\n eventTag,\n signal,\n pendingRef,\n restartQueue,\n}: {\n relativePath: string;\n eventTag: \"Create\" | \"Update\" | \"Remove\";\n signal: Queue.Queue<void>;\n pendingRef: Ref.Ref<Pending>;\n restartQueue: Queue.Queue<void>;\n}) => {\n // _generated/ files are written by codegen itself; reacting to them here\n // would form a loop. The esbuild watchers track the generated specs as\n // entry points, so changes there flow back through `notify-rebuild`.\n if (relativePath.split(/[/\\\\]/).includes(\"_generated\")) {\n return Effect.void;\n }\n\n if (!relativePath.endsWith(\".ts\")) {\n return Effect.void;\n }\n\n const isLifecycleChange = eventTag !== \"Update\";\n\n const topLevelKey = TOP_LEVEL_OPTIONAL_KEYS.get(relativePath);\n if (topLevelKey !== undefined) {\n return flipDirtyAndSignal(\n pendingRef,\n signal,\n topLevelKey,\n restartQueue,\n isLifecycleChange,\n );\n }\n\n if (relativePath === \"schema.ts\") {\n return flipDirtyAndSignal(\n pendingRef,\n signal,\n \"specDirty\",\n restartQueue,\n isLifecycleChange,\n );\n }\n\n if (isLeafSpecPath(relativePath) || isLeafImplPath(relativePath)) {\n return flipDirtyAndSignal(\n pendingRef,\n signal,\n \"specDirty\",\n restartQueue,\n isLifecycleChange,\n );\n }\n\n // Any other `.ts` under `confect/` (helpers like `tables/Notes.ts`).\n // Updates to such files are handled by the esbuild watcher for whichever\n // entry point imports them — its onEnd flips the right dirty flag.\n // Creates are our safety net: when a previously-missing import is added,\n // esbuild may not have its parent directory on a poll path, so we\n // re-run codegen on Create here.\n if (eventTag === \"Create\") {\n return flipDirtyAndSignal(\n pendingRef,\n signal,\n \"specDirty\",\n restartQueue,\n false,\n );\n }\n\n return Effect.void;\n};\n\nconst syncOptionalFile = (generate: typeof generateHttp, convexFile: string) =>\n pipe(\n generate,\n Effect.andThen(\n Option.match({\n onSome: ({ change, convexFilePath }) =>\n Match.value(change).pipe(\n Match.when(\"Unchanged\", () => Effect.void),\n Match.whenOr(\"Added\", \"Modified\", (addedOrModified) =>\n logFileChangeIndented(addedOrModified, convexFilePath),\n ),\n Match.exhaustive,\n ),\n onNone: () =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const convexDirectory = yield* ConvexDirectory.get;\n const convexFilePath = path.join(convexDirectory, convexFile);\n\n if (yield* fs.exists(convexFilePath)) {\n yield* fs.remove(convexFilePath);\n yield* logFileChangeIndented(\"Removed\", convexFilePath);\n }\n }),\n }),\n ),\n );\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmDA,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C,sBAAsB,iBAAiB,eAAe,CACvD,EACD,MAAM,WACP;CACH,cACE,OAAO,IAAI,aAAa;EACtB,MAAM,KAAK,OAAO,WAAW;EAC7B,MAAM,OAAO,OAAO,KAAK;EACzB,MAAM,kBAAkB,OAAO,gBAAgB;EAC/C,MAAM,iBAAiB,KAAK,KAAK,iBAAiB,WAAW;AAE7D,MAAI,OAAO,GAAG,OAAO,eAAe,EAAE;AACpC,UAAO,GAAG,OAAO,eAAe;AAChC,UAAO,sBAAsB,WAAW,eAAe;;GAEzD;CACL,CAAC,CACH,CACF"}
package/dist/package.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "9.0.0-next.4";
2
+ var version = "9.0.0-next.5";
3
3
 
4
4
  //#endregion
5
5
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@confect/cli",
3
- "version": "9.0.0-next.4",
3
+ "version": "9.0.0-next.5",
4
4
  "description": "Developer tooling for codegen and sync",
5
5
  "repository": {
6
6
  "type": "git",
@@ -44,6 +44,7 @@
44
44
  "@effect/platform-node-shared": "0.59.0",
45
45
  "@effect/printer": "^0.49.0",
46
46
  "@effect/printer-ansi": "^0.49.0",
47
+ "bundle-require": "^5.1.0",
47
48
  "code-block-writer": "^13.0.3",
48
49
  "esbuild": "^0.27.3"
49
50
  },
@@ -65,8 +66,8 @@
65
66
  },
66
67
  "peerDependencies": {
67
68
  "effect": "^3.21.2",
68
- "@confect/core": "^9.0.0-next.4",
69
- "@confect/server": "^9.0.0-next.4"
69
+ "@confect/core": "^9.0.0-next.5",
70
+ "@confect/server": "^9.0.0-next.5"
70
71
  },
71
72
  "engines": {
72
73
  "node": ">=22",
package/src/Bundler.ts CHANGED
@@ -1,7 +1,8 @@
1
- import { pathToFileURL } from "node:url";
1
+ import { dirname, isAbsolute, resolve } from "node:path";
2
2
  import { Path } from "@effect/platform";
3
+ import { bundleRequire } from "bundle-require";
3
4
  import { Array, Effect, Option, pipe } from "effect";
4
- import * as esbuild from "esbuild";
5
+ import type * as esbuild from "esbuild";
5
6
  import { BundlerError } from "./BuildError";
6
7
 
7
8
  export interface Bundled {
@@ -9,108 +10,84 @@ export interface Bundled {
9
10
  readonly metafile: esbuild.Metafile;
10
11
  }
11
12
 
12
- const isRelativeOrAbsolutePath = (importPath: string) =>
13
- importPath.startsWith("./") ||
14
- importPath.startsWith("../") ||
15
- importPath.startsWith("/");
16
-
17
- // Recursion guard for `absoluteExternalsPlugin`. When the plugin asks esbuild
18
- // to resolve a bare specifier via `build.resolve(...)`, esbuild invokes every
19
- // registered `onResolve` hook again for that same specifier — including this
20
- // one. The flag (carried through the recursive call via `pluginData`) tells
21
- // the recursive invocation to skip rewriting and fall through to esbuild's
22
- // built-in resolver, which is what we wanted from `build.resolve` in the
23
- // first place.
24
- const PLUGIN_DATA_SKIP = Symbol("absolute-externals.skip");
25
-
26
13
  /**
27
- * Mark every bare-specifier import as external and rewrite it to an absolute
28
- * `file://` URL. Resolution is delegated to esbuild's own resolver via
29
- * `build.resolve(...)`, which honors each package's `exports` map (preferring
30
- * the `import` condition under `format: "esm"`) and falls back to
31
- * `module`/`main` exactly the way Node's ESM resolution algorithm does.
32
- *
33
- * Bundles produced with this plugin are loaded via a data URL `import(...)`
34
- * (see {@link bundle}); rewriting bare externals to absolute file URLs is what
35
- * makes them resolvable at runtime, since a data URL has no parent file from
36
- * which a bare specifier could be resolved.
37
- *
38
- * Relative/absolute-path imports are left to esbuild to bundle as usual, and
39
- * `node:*` built-ins are passed through unchanged.
14
+ * `bundle-require` sets `absWorkingDir: cwd` on the underlying esbuild build,
15
+ * so the metafile's input keys (and each input's `imports[].path`) are stored
16
+ * relative to that cwd. Callers reach for the metafile with absolute paths
17
+ * (e.g. {@link directlyImports}), so we normalize every key/import path to
18
+ * absolute up front. That way the lookup logic stays oblivious to whatever
19
+ * cwd was used during bundling.
40
20
  */
41
- export const absoluteExternalsPlugin: esbuild.Plugin = {
42
- name: "absolute-externals",
43
- setup(build) {
44
- build.onResolve({ filter: /.*/ }, async (args) => {
45
- if (args.pluginData?.[PLUGIN_DATA_SKIP]) return;
46
- if (args.kind !== "import-statement" && args.kind !== "dynamic-import")
47
- return;
48
- if (isRelativeOrAbsolutePath(args.path)) return;
49
- if (args.path.startsWith("node:")) {
50
- return { path: args.path, external: true };
51
- }
52
-
53
- const resolved = await build.resolve(args.path, {
54
- kind: args.kind,
55
- resolveDir: args.resolveDir,
56
- pluginData: { [PLUGIN_DATA_SKIP]: true },
57
- });
58
-
59
- if (resolved.errors.length > 0) {
60
- return { errors: resolved.errors, warnings: resolved.warnings };
61
- }
62
-
63
- return {
64
- path: pathToFileURL(resolved.path).href,
65
- external: true,
66
- };
67
- });
68
- },
69
- };
70
-
71
- const buildEntry = (entryPoint: string) =>
72
- Effect.tryPromise({
73
- try: () =>
74
- esbuild.build({
75
- entryPoints: [entryPoint],
76
- bundle: true,
77
- write: false,
78
- platform: "node",
79
- format: "esm",
80
- logLevel: "silent",
81
- metafile: true,
82
- plugins: [absoluteExternalsPlugin],
83
- }),
84
- catch: (cause) => new BundlerError({ cause }),
85
- });
86
-
87
- const importBundledModule = (result: esbuild.BuildResult) => {
88
- const code = result.outputFiles![0]!.text;
89
- const dataUrl =
90
- "data:text/javascript;base64," + Buffer.from(code).toString("base64");
91
- return import(dataUrl);
21
+ const absolutizeMetafile = (
22
+ metafile: esbuild.Metafile,
23
+ cwd: string,
24
+ ): esbuild.Metafile => {
25
+ const absolutize = (p: string) => (isAbsolute(p) ? p : resolve(cwd, p));
26
+ const inputs: esbuild.Metafile["inputs"] = {};
27
+ for (const [key, value] of Object.entries(metafile.inputs)) {
28
+ inputs[absolutize(key)] = {
29
+ ...value,
30
+ imports: value.imports.map((i) => ({ ...i, path: absolutize(i.path) })),
31
+ };
32
+ }
33
+ const outputs: esbuild.Metafile["outputs"] = {};
34
+ for (const [key, value] of Object.entries(metafile.outputs)) {
35
+ outputs[absolutize(key)] = value;
36
+ }
37
+ return { inputs, outputs };
92
38
  };
93
39
 
94
40
  /**
95
- * Bundle a TypeScript entry point with esbuild and import the result via a
96
- * data URL. This handles extensionless `.ts` imports regardless of whether
97
- * the user's project sets `"type": "module"` in package.json. The returned
98
- * pair carries both the imported module and the esbuild metafile so callers
99
- * can inspect the import graph (see {@link directlyImports}).
41
+ * Bundle a TypeScript entry point with esbuild via {@link bundleRequire} and
42
+ * import the result. `bundle-require` writes a temp `.mjs` next to the source,
43
+ * `import()`s it, and deletes it so bare-specifier externals (third-party
44
+ * packages, workspace deps) resolve through the user's normal `node_modules`
45
+ * walk, and tsconfig `paths` aliases stay inside the bundle.
46
+ *
47
+ * `cwd` is set to the entry's directory so `bundle-require`'s `tsconfig.json`
48
+ * discovery (which walks upward from `cwd`) lands on the project's tsconfig
49
+ * regardless of where `confect codegen` was invoked from, and so esbuild
50
+ * resolves relative imports against the entry's location.
51
+ *
52
+ * The returned pair carries both the imported module and the esbuild metafile
53
+ * so callers can inspect the import graph (see {@link directlyImports}); the
54
+ * metafile is captured via a small `onEnd` plugin because `bundle-require`
55
+ * itself only exposes a flat `dependencies: string[]`.
100
56
  */
101
57
  export const bundle = (
102
58
  entryPoint: string,
103
59
  ): Effect.Effect<Bundled, BundlerError> =>
104
60
  Effect.gen(function* () {
105
- const result = yield* buildEntry(entryPoint);
106
- const module = yield* Effect.tryPromise({
107
- try: () => importBundledModule(result),
61
+ let metafile: esbuild.Metafile | undefined;
62
+ const captureMetafile: esbuild.Plugin = {
63
+ name: "confect:capture-metafile",
64
+ setup(build) {
65
+ build.onEnd((result) => {
66
+ metafile = result.metafile;
67
+ });
68
+ },
69
+ };
70
+
71
+ const cwd = dirname(entryPoint);
72
+ const result = yield* Effect.tryPromise({
73
+ try: () =>
74
+ bundleRequire({
75
+ filepath: entryPoint,
76
+ cwd,
77
+ format: "esm",
78
+ esbuildOptions: {
79
+ plugins: [captureMetafile],
80
+ logLevel: "silent",
81
+ },
82
+ }),
108
83
  catch: (cause) => new BundlerError({ cause }),
109
84
  });
110
- if (!result.metafile) {
85
+
86
+ if (!metafile) {
111
87
  return yield* Effect.dieMessage("esbuild metafile missing");
112
88
  }
113
- return { module, metafile: result.metafile };
89
+
90
+ return { module: result.mod, metafile: absolutizeMetafile(metafile, cwd) };
114
91
  });
115
92
 
116
93
  const findMetafileInputKey = (
@@ -22,9 +22,13 @@ import {
22
22
  Stream,
23
23
  String,
24
24
  } from "effect";
25
+ import {
26
+ externalPlugin,
27
+ loadTsConfig,
28
+ tsconfigPathsToRegExp,
29
+ } from "bundle-require";
25
30
  import * as esbuild from "esbuild";
26
31
  import { logCoalescedBuildErrors } from "../BuildError";
27
- import { absoluteExternalsPlugin } from "../Bundler";
28
32
  import * as CodegenError from "../CodegenError";
29
33
  import { ConfectDirectory } from "../ConfectDirectory";
30
34
  import { ConvexDirectory } from "../ConvexDirectory";
@@ -430,6 +434,7 @@ const discoverEntryPoints = Effect.gen(function* () {
430
434
 
431
435
  const esbuildOptions = (
432
436
  entry: EntryPoint,
437
+ notExternal: ReadonlyArray<RegExp>,
433
438
  signal: Queue.Queue<void>,
434
439
  pendingRef: Ref.Ref<Pending>,
435
440
  watcherErrorsRef: Ref.Ref<WatcherErrors>,
@@ -451,7 +456,7 @@ const esbuildOptions = (
451
456
  format: "esm" as const,
452
457
  logLevel: "silent" as const,
453
458
  plugins: [
454
- absoluteExternalsPlugin,
459
+ externalPlugin({ notExternal: [...notExternal] }),
455
460
  {
456
461
  name: "notify-rebuild",
457
462
  setup(build: esbuild.PluginBuild) {
@@ -489,6 +494,7 @@ const esbuildOptions = (
489
494
 
490
495
  const createEntryPointWatcher = (
491
496
  entry: EntryPoint,
497
+ notExternal: ReadonlyArray<RegExp>,
492
498
  signal: Queue.Queue<void>,
493
499
  pendingRef: Ref.Ref<Pending>,
494
500
  watcherErrorsRef: Ref.Ref<WatcherErrors>,
@@ -496,7 +502,13 @@ const createEntryPointWatcher = (
496
502
  Effect.acquireRelease(
497
503
  Effect.promise(async () => {
498
504
  const ctx = await esbuild.context(
499
- esbuildOptions(entry, signal, pendingRef, watcherErrorsRef),
505
+ esbuildOptions(
506
+ entry,
507
+ notExternal,
508
+ signal,
509
+ pendingRef,
510
+ watcherErrorsRef,
511
+ ),
500
512
  );
501
513
  await ctx.watch();
502
514
  return ctx;
@@ -534,6 +546,17 @@ const entryPointsWatcher = (
534
546
  Effect.gen(function* () {
535
547
  const parentScope = yield* Effect.scope;
536
548
  const scopesRef = yield* Ref.make(new Map<string, Scope.CloseableScope>());
549
+ const projectRoot = yield* ProjectRoot.get;
550
+ // Discover the user's `tsconfig.json#paths` once at watcher startup so
551
+ // `~/...`-style aliases pointing into the user's source tree get bundled
552
+ // by esbuild instead of externalized via `bundle-require`'s `node_modules`
553
+ // heuristic. `loadTsConfig` walks up from `projectRoot` to find a
554
+ // `tsconfig.json`; if none exists, `paths` is empty and `notExternal` is
555
+ // `[]`, leaving the externalization rule unchanged.
556
+ const tsconfig = loadTsConfig(projectRoot);
557
+ const notExternal = tsconfigPathsToRegExp(
558
+ tsconfig?.data.compilerOptions?.paths ?? {},
559
+ );
537
560
 
538
561
  const sync = Effect.gen(function* () {
539
562
  const desired = yield* discoverEntryPoints;
@@ -569,6 +592,7 @@ const entryPointsWatcher = (
569
592
  );
570
593
  yield* createEntryPointWatcher(
571
594
  entry,
595
+ notExternal,
572
596
  signal,
573
597
  pendingRef,
574
598
  watcherErrorsRef,