next-bun-compile 0.6.10 → 0.6.11

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-8jf4ty06.js";
6
6
 
7
7
  // src/cli.ts
8
8
  import { existsSync } from "node:fs";
@@ -167,104 +167,6 @@ 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)
237
- return;
238
- const serverDir = join(standaloneNextDir, "server");
239
- if (!existsSync(serverDir))
240
- return;
241
- const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
242
- const pattern = new RegExp(`(["'])((?:` + aliases.map((a) => escape(a.alias)).join("|") + `)(?:/[^"']+)?)\\1`, "g");
243
- let rewritten = 0;
244
- for (const f of walkDir(serverDir)) {
245
- if (!f.absolutePath.endsWith(".js"))
246
- continue;
247
- let content;
248
- try {
249
- content = readFileSync(f.absolutePath, "utf-8");
250
- } catch {
251
- continue;
252
- }
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
- });
259
- if (next !== content) {
260
- writeFileSync(f.absolutePath, next);
261
- rewritten++;
262
- }
263
- }
264
- if (rewritten > 0) {
265
- console.log(`next-bun-compile: Rewrote turbopack-mangled aliases in ${rewritten} server chunks`);
266
- }
267
- }
268
170
  function findServerDir(standaloneDir) {
269
171
  if (existsSync(join(standaloneDir, "server.js"))) {
270
172
  return standaloneDir;
@@ -451,8 +353,6 @@ function generateEntryPoint(options) {
451
353
  if (externalModules.length > 0) {
452
354
  console.log(`next-bun-compile: Embedding ${externalModules.length} external modules for SSR`);
453
355
  }
454
- const canonicalResolutions = buildCanonicalResolutions(externalDir, turbopackAliases);
455
- rewriteTurbopackAliases(standaloneNextDir, turbopackAliases, canonicalResolutions);
456
356
  const ctx = JSON.parse(readFileSync(join(distDir, "bun-compile-ctx.json"), "utf-8"));
457
357
  const { assetPrefix } = ctx;
458
358
  const assetsToEmbed = assetPrefix ? [...publicFiles, ...runtimeFiles] : [...staticFiles, ...publicFiles, ...runtimeFiles];
@@ -503,11 +403,16 @@ process.env.NODE_ENV = "production";
503
403
  // Install a fallback Module._resolveFilename hook. bun's compiled-binary
504
404
  // resolver doesn't walk node_modules from inside a node_modules entry, so
505
405
  // 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
406
+ // of detect-libc, @img/sharp-linux-x64/sharp.node, etc.) bun gives up. The
407
+ // hook also redirects turbopack-mangled aliases ("sharp-457ea9eae1af1a9c"
408
+ // → "sharp") so the chunks' externalized require/import calls land on the
409
+ // canonical packages that collectExternalModules extracted.
410
+ //
411
+ // Runs ONLY when bun's resolver throws and reimplements Node-compatible
508
412
  // resolution from scratch — walk node_modules, read package.json main,
509
413
  // honor exports maps. Generic: every externalized package's internal deps
510
414
  // get resolved without per-package patching.
415
+ const __nbcAliases = ${JSON.stringify(Object.fromEntries(turbopackAliases.map((a) => [a.alias, a.target])))};
511
416
  const __nbcOrigResolveFilename = Module._resolveFilename;
512
417
  function __nbcStatFile(p) {
513
418
  try { return fs.statSync(p).isFile() ? p : null; } catch { return null; }
@@ -576,16 +481,32 @@ function __nbcResolvePackage(request, fromDir) {
576
481
  }
577
482
  return null;
578
483
  }
484
+ function __nbcRedirectAlias(request) {
485
+ if (typeof request !== "string" || request.length === 0) return request;
486
+ // Direct alias match
487
+ if (Object.prototype.hasOwnProperty.call(__nbcAliases, request)) {
488
+ return __nbcAliases[request];
489
+ }
490
+ // Alias-with-subpath match
491
+ const slash = request.indexOf("/", request[0] === "@" ? request.indexOf("/") + 1 : 0);
492
+ if (slash === -1) return request;
493
+ const head = request.slice(0, slash);
494
+ if (Object.prototype.hasOwnProperty.call(__nbcAliases, head)) {
495
+ return __nbcAliases[head] + request.slice(slash);
496
+ }
497
+ return request;
498
+ }
579
499
  Module._resolveFilename = function(request, parent, isMain, options) {
500
+ const redirected = __nbcRedirectAlias(request);
580
501
  try {
581
- return __nbcOrigResolveFilename.call(this, request, parent, isMain, options);
502
+ return __nbcOrigResolveFilename.call(this, redirected, parent, isMain, options);
582
503
  } catch (err) {
583
504
  // Only attempt fallback for bare package specifiers
584
- if (typeof request !== "string" || request[0] === "." || request[0] === "/" || /^[a-z]+:/.test(request)) {
505
+ if (typeof redirected !== "string" || redirected[0] === "." || redirected[0] === "/" || /^[a-z]+:/.test(redirected)) {
585
506
  throw err;
586
507
  }
587
508
  const fromDir = parent && parent.filename ? path.dirname(parent.filename) : process.cwd();
588
- const resolved = __nbcResolvePackage(request, fromDir);
509
+ const resolved = __nbcResolvePackage(redirected, fromDir);
589
510
  if (resolved) return resolved;
590
511
  throw err;
591
512
  }
@@ -609,20 +530,7 @@ async function extractAssets() {
609
530
  if (fs.existsSync(fullPath)) continue;
610
531
  fs.mkdirSync(path.dirname(fullPath), { recursive: true });
611
532
  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++;
533
+ if (embedded) { await Bun.write(fullPath, Bun.file(embedded)); n++; }
626
534
  }
627
535
  if (n > 0) console.log(\`Extracted \${n} assets\`);
628
536
  }
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-8jf4ty06.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.11",
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",