@h-rig/core 0.0.6-alpha.82 → 0.0.6-alpha.84

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,6 +1,6 @@
1
1
  // @bun
2
2
  // packages/core/src/load-config.ts
3
- import { existsSync, mkdtempSync, readFileSync, rmSync } from "fs";
3
+ import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync } from "fs";
4
4
  import { tmpdir } from "os";
5
5
  import { dirname, join } from "path";
6
6
  import { fileURLToPath, pathToFileURL } from "url";
@@ -38,6 +38,118 @@ function isModuleResolutionError(error) {
38
38
  function runningFromCompiledBinary() {
39
39
  return import.meta.url.includes("$bunfs");
40
40
  }
41
+ function packageNameAndSubpath(specifier) {
42
+ if (specifier.startsWith(".") || specifier.startsWith("/") || /^[a-zA-Z]+:/.test(specifier))
43
+ return null;
44
+ const parts = specifier.split("/");
45
+ const packageName = specifier.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
46
+ if (!packageName)
47
+ return null;
48
+ const rest = parts.slice(specifier.startsWith("@") ? 2 : 1).join("/");
49
+ return { packageName, subpath: rest ? `./${rest}` : "." };
50
+ }
51
+ function exportTargetFromEntry(entry) {
52
+ if (typeof entry === "string")
53
+ return entry;
54
+ if (entry && typeof entry === "object" && !Array.isArray(entry)) {
55
+ const conditions = entry;
56
+ for (const key of ["bun", "import", "default", "require"]) {
57
+ if (typeof conditions[key] === "string")
58
+ return conditions[key];
59
+ }
60
+ }
61
+ return null;
62
+ }
63
+ function patternExportTarget(record, subpath) {
64
+ for (const [pattern, entry] of Object.entries(record)) {
65
+ if (!pattern.includes("*"))
66
+ continue;
67
+ const [prefix = "", suffix = ""] = pattern.split("*");
68
+ if (!subpath.startsWith(prefix) || !subpath.endsWith(suffix))
69
+ continue;
70
+ const replacement = subpath.slice(prefix.length, subpath.length - suffix.length);
71
+ const target = exportTargetFromEntry(entry);
72
+ if (target)
73
+ return target.replace("*", replacement);
74
+ }
75
+ return null;
76
+ }
77
+ function exportTargetFromPackageJson(pkg, subpath) {
78
+ const exportsField = pkg.exports;
79
+ const target = (() => {
80
+ if (typeof exportsField === "string" && subpath === ".")
81
+ return exportsField;
82
+ if (!exportsField || typeof exportsField !== "object" || Array.isArray(exportsField))
83
+ return null;
84
+ const record = exportsField;
85
+ return exportTargetFromEntry(record[subpath] ?? (subpath === "." ? record["."] : undefined)) ?? patternExportTarget(record, subpath);
86
+ })();
87
+ if (target)
88
+ return target;
89
+ return subpath === "." && typeof pkg.module === "string" ? pkg.module : subpath === "." && typeof pkg.main === "string" ? pkg.main : null;
90
+ }
91
+ function resolvePackageDirFromBunStore(packageName, nodeModulesDir) {
92
+ const storeDir = join(nodeModulesDir, ".bun");
93
+ if (!existsSync(storeDir))
94
+ return null;
95
+ const encoded = packageName.replace("/", "+");
96
+ try {
97
+ for (const entry of readdirSync(storeDir).sort()) {
98
+ if (!entry.startsWith(`${encoded}@`))
99
+ continue;
100
+ const candidate = join(storeDir, entry, "node_modules", packageName);
101
+ if (existsSync(join(candidate, "package.json")))
102
+ return candidate;
103
+ }
104
+ } catch {
105
+ return null;
106
+ }
107
+ return null;
108
+ }
109
+ function resolvePackageExportFromDir(packageDir, subpath) {
110
+ const packageJsonPath = join(packageDir, "package.json");
111
+ if (!existsSync(packageJsonPath))
112
+ return null;
113
+ try {
114
+ const pkg = JSON.parse(readFileSync(packageJsonPath, "utf8"));
115
+ const target = exportTargetFromPackageJson(pkg, subpath);
116
+ if (target) {
117
+ const resolved = join(packageDir, target);
118
+ if (existsSync(resolved))
119
+ return resolved;
120
+ }
121
+ } catch {
122
+ return null;
123
+ }
124
+ return null;
125
+ }
126
+ function resolveBarePackageExport(specifier, parentDir) {
127
+ const parsed = packageNameAndSubpath(specifier);
128
+ if (!parsed)
129
+ return null;
130
+ const packageNames = parsed.packageName.startsWith("@rig/") ? [parsed.packageName, parsed.packageName.replace(/^@rig\//, "@h-rig/")] : [parsed.packageName];
131
+ let current = parentDir;
132
+ while (true) {
133
+ const nodeModulesDir = join(current, "node_modules");
134
+ for (const packageName of packageNames) {
135
+ const directPackageDir = join(nodeModulesDir, packageName);
136
+ const resolvedDirect = resolvePackageExportFromDir(directPackageDir, parsed.subpath);
137
+ if (resolvedDirect)
138
+ return resolvedDirect;
139
+ const bunStorePackageDir = resolvePackageDirFromBunStore(packageName, nodeModulesDir);
140
+ if (bunStorePackageDir) {
141
+ const resolvedFromStore = resolvePackageExportFromDir(bunStorePackageDir, parsed.subpath);
142
+ if (resolvedFromStore)
143
+ return resolvedFromStore;
144
+ }
145
+ }
146
+ const next = dirname(current);
147
+ if (next === current)
148
+ break;
149
+ current = next;
150
+ }
151
+ return null;
152
+ }
41
153
  var runtimeBundleQueue = Promise.resolve();
42
154
  function enqueueRuntimeBundle(operation) {
43
155
  const next = runtimeBundleQueue.then(operation, operation);
@@ -67,16 +179,20 @@ async function importConfigViaRuntimeBundleUnserialized(configPath) {
67
179
  setup(build) {
68
180
  build.onResolve({ filter: /^@rig\// }, (args) => {
69
181
  const candidates = [
70
- [args.path, configDir],
71
182
  [args.path, hostDir],
72
- [args.path.replace(/^@rig\//, "@h-rig/"), hostDir]
183
+ [args.path.replace(/^@rig\//, "@h-rig/"), hostDir],
184
+ [args.path, configDir]
73
185
  ];
74
186
  for (const [specifier, parent] of candidates) {
75
187
  try {
76
- const resolved = bun.resolveSync?.(specifier, parent);
188
+ const resolved = bun.resolveSync?.(specifier, parent) ?? resolveBarePackageExport(specifier, parent);
77
189
  if (resolved)
78
190
  return { path: resolved };
79
- } catch {}
191
+ } catch {
192
+ const resolved = resolveBarePackageExport(specifier, parent);
193
+ if (resolved)
194
+ return { path: resolved };
195
+ }
80
196
  }
81
197
  return;
82
198
  });
@@ -85,10 +201,13 @@ async function importConfigViaRuntimeBundleUnserialized(configPath) {
85
201
  return;
86
202
  const parent = args.importer ? dirname(args.importer) : configDir;
87
203
  try {
88
- const resolved = bun.resolveSync?.(args.path, parent);
204
+ const resolved = bun.resolveSync?.(args.path, parent) ?? resolveBarePackageExport(args.path, parent);
89
205
  if (resolved)
90
206
  return { path: resolved };
91
207
  } catch {
208
+ const resolved = resolveBarePackageExport(args.path, parent);
209
+ if (resolved)
210
+ return { path: resolved };
92
211
  return { path: args.path, namespace: UNRESOLVED_NAMESPACE };
93
212
  }
94
213
  return;
@@ -132,6 +251,11 @@ async function loadConfig(cwd) {
132
251
  if (runningFromCompiledBinary()) {
133
252
  return importConfigViaRuntimeBundleUnserialized(p);
134
253
  }
254
+ const source = readFileSync(p, "utf8");
255
+ const importsRigHostPackages = /(?:import\s+[^;]*?from\s*|import\s*\()\s*["']@rig\//.test(source);
256
+ if (importsRigHostPackages) {
257
+ return importConfigViaRuntimeBundleUnserialized(p);
258
+ }
135
259
  try {
136
260
  return await import(pathToFileURL(p).href);
137
261
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h-rig/core",
3
- "version": "0.0.6-alpha.82",
3
+ "version": "0.0.6-alpha.84",
4
4
  "type": "module",
5
5
  "description": "Rig package",
6
6
  "license": "UNLICENSED",
@@ -37,7 +37,7 @@
37
37
  "module": "./dist/src/index.js",
38
38
  "types": "./dist/src/index.d.ts",
39
39
  "dependencies": {
40
- "@rig/contracts": "npm:@h-rig/contracts@0.0.6-alpha.82",
40
+ "@rig/contracts": "npm:@h-rig/contracts@0.0.6-alpha.84",
41
41
  "effect": "4.0.0-beta.78"
42
42
  }
43
43
  }