@h-rig/core 0.0.6-alpha.34 → 0.0.6-alpha.35

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.
@@ -1,17 +1,63 @@
1
1
  // @bun
2
2
  // packages/core/src/load-config.ts
3
- import { existsSync, readFileSync } from "fs";
3
+ import { existsSync, mkdtempSync, readFileSync, rmSync } from "fs";
4
+ import { tmpdir } from "os";
4
5
  import { join } from "path";
5
6
  import { pathToFileURL } from "url";
6
7
  import { Schema } from "effect";
7
8
  import { RigConfig } from "@rig/contracts";
8
9
  var TS_NAMES = ["rig.config.ts", "rig.config.mts"];
9
10
  var JSON_NAMES = ["rig.config.json"];
11
+ function isModuleResolutionError(error) {
12
+ const message = error instanceof Error ? error.message : String(error);
13
+ return message.includes("Cannot find module") || message.includes("Could not resolve");
14
+ }
15
+ function runningFromCompiledBinary() {
16
+ return import.meta.url.includes("$bunfs");
17
+ }
18
+ async function importConfigViaRuntimeBundle(configPath) {
19
+ const bun = globalThis.Bun;
20
+ if (!bun?.build) {
21
+ throw new Error(`Failed to import ${configPath}: bare imports could not be resolved and no Bun.build runtime bundler is available.`);
22
+ }
23
+ const result = await bun.build({
24
+ entrypoints: [configPath],
25
+ target: "bun",
26
+ format: "esm",
27
+ throw: false
28
+ });
29
+ if (!result.success || !result.outputs[0]) {
30
+ const detail = result.logs.map((log) => String(log)).join(`
31
+ `);
32
+ throw new Error(`Failed to bundle ${configPath}: ${detail || "unknown bundler error"}`);
33
+ }
34
+ const dir = mkdtempSync(join(tmpdir(), "rig-config-bundle-"));
35
+ try {
36
+ const bundledPath = join(dir, "rig.config.bundled.js");
37
+ await bun.write(bundledPath, await result.outputs[0].text());
38
+ return await import(pathToFileURL(bundledPath).href);
39
+ } finally {
40
+ try {
41
+ rmSync(dir, { recursive: true, force: true });
42
+ } catch {}
43
+ }
44
+ }
10
45
  async function loadConfig(cwd) {
11
46
  for (const name of TS_NAMES) {
12
47
  const p = join(cwd, name);
13
48
  if (existsSync(p)) {
14
- const mod = await import(pathToFileURL(p).href);
49
+ let mod;
50
+ if (runningFromCompiledBinary()) {
51
+ mod = await importConfigViaRuntimeBundle(p);
52
+ } else {
53
+ try {
54
+ mod = await import(pathToFileURL(p).href);
55
+ } catch (error) {
56
+ if (!isModuleResolutionError(error))
57
+ throw error;
58
+ mod = await importConfigViaRuntimeBundle(p);
59
+ }
60
+ }
15
61
  const raw = mod.default ?? mod.config;
16
62
  return decodePreservingRuntime(raw);
17
63
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h-rig/core",
3
- "version": "0.0.6-alpha.34",
3
+ "version": "0.0.6-alpha.35",
4
4
  "type": "module",
5
5
  "description": "Rig package",
6
6
  "license": "UNLICENSED",
@@ -31,7 +31,7 @@
31
31
  "main": "./dist/src/index.js",
32
32
  "module": "./dist/src/index.js",
33
33
  "dependencies": {
34
- "@rig/contracts": "npm:@h-rig/contracts@0.0.6-alpha.34",
34
+ "@rig/contracts": "npm:@h-rig/contracts@0.0.6-alpha.35",
35
35
  "effect": "4.0.0-beta.78"
36
36
  }
37
37
  }