next-bun-compile 0.6.10 → 0.6.12

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/dist/cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  compile,
4
4
  generateEntryPoint
5
- } from "./index-rx8tn52q.js";
5
+ } from "./index-7dvf3g60.js";
6
6
 
7
7
  // src/cli.ts
8
8
  import { existsSync } from "node:fs";
@@ -167,79 +167,15 @@ function findTurbopackAliases(standaloneNextDir) {
167
167
  subpaths: Array.from(subpaths)
168
168
  }));
169
169
  }
170
- function buildCanonicalResolutions(externalRoot, aliases) {
171
- const out = new Map;
172
- const findFile = (dir, candidates) => {
173
- for (const c of candidates) {
174
- if (!c)
175
- continue;
176
- const p = join(dir, c);
177
- if (existsSync(p) && statSync(p).isFile())
178
- return c.replace(/\\/g, "/");
179
- }
180
- return null;
181
- };
182
- const resolveMain = (canonicalDir) => {
183
- const pkgPath = join(canonicalDir, "package.json");
184
- let main = "index.js";
185
- if (existsSync(pkgPath)) {
186
- try {
187
- const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
188
- if (typeof pkg.main === "string")
189
- main = pkg.main;
190
- } catch {}
191
- }
192
- return findFile(canonicalDir, [
193
- main,
194
- main + ".js",
195
- main + ".cjs",
196
- main + ".mjs",
197
- join(main, "index.js"),
198
- join(main, "index.cjs"),
199
- join(main, "index.mjs"),
200
- "index.js",
201
- "index.cjs",
202
- "index.mjs"
203
- ]);
204
- };
205
- const resolveSub = (canonicalDir, sub) => {
206
- const stripped = sub.replace(/\.(?:js|cjs|mjs|json)$/, "");
207
- return findFile(canonicalDir, [
208
- sub,
209
- stripped + ".js",
210
- stripped + ".mjs",
211
- stripped + ".cjs",
212
- stripped + ".json",
213
- join(stripped, "index.js"),
214
- join(stripped, "index.mjs"),
215
- join(stripped, "index.cjs")
216
- ]);
217
- };
218
- for (const { alias, target, subpaths } of aliases) {
219
- const canonicalDir = join(externalRoot, target);
220
- if (!existsSync(canonicalDir))
221
- continue;
222
- const main = resolveMain(canonicalDir);
223
- if (main) {
224
- out.set(alias, `.next/node_modules/${target}/${main}`);
225
- }
226
- for (const sub of subpaths) {
227
- const file = resolveSub(canonicalDir, sub);
228
- if (file) {
229
- out.set(`${alias}/${sub}`, `.next/node_modules/${target}/${file}`);
230
- }
231
- }
232
- }
233
- return out;
234
- }
235
- function rewriteTurbopackAliases(standaloneNextDir, aliases, resolutions) {
236
- if (aliases.length === 0 || resolutions.size === 0)
170
+ function rewriteTurbopackAliases(standaloneNextDir, aliases) {
171
+ if (aliases.length === 0)
237
172
  return;
238
173
  const serverDir = join(standaloneNextDir, "server");
239
174
  if (!existsSync(serverDir))
240
175
  return;
241
176
  const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
242
- const pattern = new RegExp(`(["'])((?:` + aliases.map((a) => escape(a.alias)).join("|") + `)(?:/[^"']+)?)\\1`, "g");
177
+ const pattern = new RegExp(`(["'])(` + aliases.map((a) => escape(a.alias)).join("|") + `)(?=[/"'])`, "g");
178
+ const targetByAlias = new Map(aliases.map((a) => [a.alias, a.target]));
243
179
  let rewritten = 0;
244
180
  for (const f of walkDir(serverDir)) {
245
181
  if (!f.absolutePath.endsWith(".js"))
@@ -250,12 +186,7 @@ function rewriteTurbopackAliases(standaloneNextDir, aliases, resolutions) {
250
186
  } catch {
251
187
  continue;
252
188
  }
253
- const next = content.replace(pattern, (match, quote, spec) => {
254
- const rel = resolutions.get(spec);
255
- if (!rel)
256
- return match;
257
- return `${quote}__NBC_BASE__/${rel}${quote}`;
258
- });
189
+ const next = content.replace(pattern, (_m, q, alias) => q + targetByAlias.get(alias));
259
190
  if (next !== content) {
260
191
  writeFileSync(f.absolutePath, next);
261
192
  rewritten++;
@@ -426,6 +357,7 @@ function generateEntryPoint(options) {
426
357
  }));
427
358
  const standaloneNextDir = join(serverDir, ".next");
428
359
  const turbopackAliases = findTurbopackAliases(standaloneNextDir);
360
+ rewriteTurbopackAliases(standaloneNextDir, turbopackAliases);
429
361
  const aliasNames = new Set(turbopackAliases.map((a) => a.alias));
430
362
  const runtimeFiles = walkDir(standaloneNextDir).filter((f) => {
431
363
  const m = f.relativePath.replace(/\\/g, "/").match(/^node_modules\/([^/]+)/);
@@ -451,8 +383,6 @@ function generateEntryPoint(options) {
451
383
  if (externalModules.length > 0) {
452
384
  console.log(`next-bun-compile: Embedding ${externalModules.length} external modules for SSR`);
453
385
  }
454
- const canonicalResolutions = buildCanonicalResolutions(externalDir, turbopackAliases);
455
- rewriteTurbopackAliases(standaloneNextDir, turbopackAliases, canonicalResolutions);
456
386
  const ctx = JSON.parse(readFileSync(join(distDir, "bun-compile-ctx.json"), "utf-8"));
457
387
  const { assetPrefix } = ctx;
458
388
  const assetsToEmbed = assetPrefix ? [...publicFiles, ...runtimeFiles] : [...staticFiles, ...publicFiles, ...runtimeFiles];
@@ -503,11 +433,16 @@ process.env.NODE_ENV = "production";
503
433
  // Install a fallback Module._resolveFilename hook. bun's compiled-binary
504
434
  // resolver doesn't walk node_modules from inside a node_modules entry, so
505
435
  // once execution enters an extracted package (sharp/lib/index.js → require
506
- // of detect-libc, @img/sharp-linux-x64/sharp.node, etc.) bun gives up. This
507
- // hook runs ONLY when bun's resolver throws and reimplements Node-compatible
436
+ // of detect-libc, @img/sharp-linux-x64/sharp.node, etc.) bun gives up. The
437
+ // hook also redirects turbopack-mangled aliases ("sharp-457ea9eae1af1a9c"
438
+ // → "sharp") so the chunks' externalized require/import calls land on the
439
+ // canonical packages that collectExternalModules extracted.
440
+ //
441
+ // Runs ONLY when bun's resolver throws and reimplements Node-compatible
508
442
  // resolution from scratch — walk node_modules, read package.json main,
509
443
  // honor exports maps. Generic: every externalized package's internal deps
510
444
  // get resolved without per-package patching.
445
+ const __nbcAliases = ${JSON.stringify(Object.fromEntries(turbopackAliases.map((a) => [a.alias, a.target])))};
511
446
  const __nbcOrigResolveFilename = Module._resolveFilename;
512
447
  function __nbcStatFile(p) {
513
448
  try { return fs.statSync(p).isFile() ? p : null; } catch { return null; }
@@ -576,16 +511,32 @@ function __nbcResolvePackage(request, fromDir) {
576
511
  }
577
512
  return null;
578
513
  }
514
+ function __nbcRedirectAlias(request) {
515
+ if (typeof request !== "string" || request.length === 0) return request;
516
+ // Direct alias match
517
+ if (Object.prototype.hasOwnProperty.call(__nbcAliases, request)) {
518
+ return __nbcAliases[request];
519
+ }
520
+ // Alias-with-subpath match
521
+ const slash = request.indexOf("/", request[0] === "@" ? request.indexOf("/") + 1 : 0);
522
+ if (slash === -1) return request;
523
+ const head = request.slice(0, slash);
524
+ if (Object.prototype.hasOwnProperty.call(__nbcAliases, head)) {
525
+ return __nbcAliases[head] + request.slice(slash);
526
+ }
527
+ return request;
528
+ }
579
529
  Module._resolveFilename = function(request, parent, isMain, options) {
530
+ const redirected = __nbcRedirectAlias(request);
580
531
  try {
581
- return __nbcOrigResolveFilename.call(this, request, parent, isMain, options);
532
+ return __nbcOrigResolveFilename.call(this, redirected, parent, isMain, options);
582
533
  } catch (err) {
583
534
  // Only attempt fallback for bare package specifiers
584
- if (typeof request !== "string" || request[0] === "." || request[0] === "/" || /^[a-z]+:/.test(request)) {
535
+ if (typeof redirected !== "string" || redirected[0] === "." || redirected[0] === "/" || /^[a-z]+:/.test(redirected)) {
585
536
  throw err;
586
537
  }
587
538
  const fromDir = parent && parent.filename ? path.dirname(parent.filename) : process.cwd();
588
- const resolved = __nbcResolvePackage(request, fromDir);
539
+ const resolved = __nbcResolvePackage(redirected, fromDir);
589
540
  if (resolved) return resolved;
590
541
  throw err;
591
542
  }
@@ -609,20 +560,7 @@ async function extractAssets() {
609
560
  if (fs.existsSync(fullPath)) continue;
610
561
  fs.mkdirSync(path.dirname(fullPath), { recursive: true });
611
562
  const embedded = assetMap.get(urlPath);
612
- if (!embedded) continue;
613
- // Server chunks may contain __NBC_BASE__ placeholders injected at
614
- // build time by rewriteTurbopackAliases. Substitute the real baseDir
615
- // before writing so bun resolves the absolute paths at chunk load.
616
- if (diskPath.startsWith(".next/server/")) {
617
- const text = await Bun.file(embedded).text();
618
- if (text.indexOf("__NBC_BASE__") !== -1) {
619
- await Bun.write(fullPath, text.split("__NBC_BASE__").join(baseDir));
620
- n++;
621
- continue;
622
- }
623
- }
624
- await Bun.write(fullPath, Bun.file(embedded));
625
- n++;
563
+ if (embedded) { await Bun.write(fullPath, Bun.file(embedded)); n++; }
626
564
  }
627
565
  if (n > 0) console.log(\`Extracted \${n} assets\`);
628
566
  }
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  __require,
3
3
  compile,
4
4
  generateEntryPoint
5
- } from "./index-rx8tn52q.js";
5
+ } from "./index-7dvf3g60.js";
6
6
 
7
7
  // src/index.ts
8
8
  import { join } from "node:path";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-bun-compile",
3
- "version": "0.6.10",
3
+ "version": "0.6.12",
4
4
  "description": "Next.js Build Adapter that compiles your app into a Bun single-file executable",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",