next-bun-compile 0.6.7 → 0.6.9

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-qhwqnqff.js";
5
+ } from "./index-h8ak29p2.js";
6
6
 
7
7
  // src/cli.ts
8
8
  import { existsSync } from "node:fs";
@@ -114,15 +114,12 @@ function generateStubs(standaloneDir) {
114
114
  function findTurbopackAliases(standaloneNextDir) {
115
115
  const seen = new Map;
116
116
  const ensure = (alias) => {
117
- let entry = seen.get(alias);
118
- if (!entry) {
119
- entry = {
120
- target: alias.replace(/-[0-9a-f]{16}$/, ""),
121
- subpaths: new Set
122
- };
123
- seen.set(alias, entry);
117
+ let e = seen.get(alias);
118
+ if (!e) {
119
+ e = { target: alias.replace(/-[0-9a-f]{16}$/, ""), subpaths: new Set };
120
+ seen.set(alias, e);
124
121
  }
125
- return entry;
122
+ return e;
126
123
  };
127
124
  const serverDir = join(standaloneNextDir, "server");
128
125
  if (existsSync(serverDir)) {
@@ -170,6 +167,104 @@ function findTurbopackAliases(standaloneNextDir) {
170
167
  subpaths: Array.from(subpaths)
171
168
  }));
172
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
+ }
173
268
  function findServerDir(standaloneDir) {
174
269
  if (existsSync(join(standaloneDir, "server.js"))) {
175
270
  return standaloneDir;
@@ -356,6 +451,8 @@ function generateEntryPoint(options) {
356
451
  if (externalModules.length > 0) {
357
452
  console.log(`next-bun-compile: Embedding ${externalModules.length} external modules for SSR`);
358
453
  }
454
+ const canonicalResolutions = buildCanonicalResolutions(externalDir, turbopackAliases);
455
+ rewriteTurbopackAliases(standaloneNextDir, turbopackAliases, canonicalResolutions);
359
456
  const ctx = JSON.parse(readFileSync(join(distDir, "bun-compile-ctx.json"), "utf-8"));
360
457
  const { assetPrefix } = ctx;
361
458
  const assetsToEmbed = assetPrefix ? [...publicFiles, ...runtimeFiles] : [...staticFiles, ...publicFiles, ...runtimeFiles];
@@ -413,7 +510,6 @@ if (Number.isNaN(keepAliveTimeout) || !Number.isFinite(keepAliveTimeout) || keep
413
510
  }
414
511
 
415
512
  const extractions = ${JSON.stringify(assetExtractions)};
416
- const turbopackAliases = ${JSON.stringify(turbopackAliases.map((a) => [a.alias, a.target, a.subpaths]))};
417
513
  async function extractAssets() {
418
514
  let n = 0;
419
515
  for (const [urlPath, diskPath] of extractions) {
@@ -421,35 +517,20 @@ async function extractAssets() {
421
517
  if (fs.existsSync(fullPath)) continue;
422
518
  fs.mkdirSync(path.dirname(fullPath), { recursive: true });
423
519
  const embedded = assetMap.get(urlPath);
424
- if (embedded) { await Bun.write(fullPath, Bun.file(embedded)); n++; }
425
- }
426
- for (const [alias, target, subpaths] of turbopackAliases) {
427
- const aliasPath = path.join(baseDir, ".next/node_modules", alias);
428
- if (!fs.existsSync(aliasPath)) {
429
- fs.mkdirSync(aliasPath, { recursive: true });
430
- fs.writeFileSync(
431
- path.join(aliasPath, "package.json"),
432
- JSON.stringify({ name: alias, main: "index.js" })
433
- );
434
- // Absolute path. bun's compiled-binary resolver won't traverse "."/".."
435
- // segments out of a dynamically-written shim (presumably tied to the
436
- // module's original compile-time location, not its on-disk path), so
437
- // a literal absolute target is the only thing that resolves reliably.
438
- fs.writeFileSync(
439
- path.join(aliasPath, "index.js"),
440
- "module.exports = require(" + JSON.stringify(path.join(baseDir, ".next/node_modules", target)) + ");"
441
- );
442
- }
443
- for (const sub of subpaths) {
444
- const subKey = sub.replace(/\\.js$/, "");
445
- const shimFile = path.join(aliasPath, subKey + ".js");
446
- if (fs.existsSync(shimFile)) continue;
447
- fs.mkdirSync(path.dirname(shimFile), { recursive: true });
448
- fs.writeFileSync(
449
- shimFile,
450
- "module.exports = require(" + JSON.stringify(path.join(baseDir, ".next/node_modules", target, subKey)) + ");"
451
- );
520
+ if (!embedded) continue;
521
+ // Server chunks may contain __NBC_BASE__ placeholders injected at
522
+ // build time by rewriteTurbopackAliases. Substitute the real baseDir
523
+ // before writing so bun resolves the absolute paths at chunk load.
524
+ if (diskPath.startsWith(".next/server/")) {
525
+ const text = await Bun.file(embedded).text();
526
+ if (text.indexOf("__NBC_BASE__") !== -1) {
527
+ await Bun.write(fullPath, text.split("__NBC_BASE__").join(baseDir));
528
+ n++;
529
+ continue;
530
+ }
452
531
  }
532
+ await Bun.write(fullPath, Bun.file(embedded));
533
+ n++;
453
534
  }
454
535
  if (n > 0) console.log(\`Extracted \${n} assets\`);
455
536
  }
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  __require,
3
3
  compile,
4
4
  generateEntryPoint
5
- } from "./index-qhwqnqff.js";
5
+ } from "./index-h8ak29p2.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.7",
3
+ "version": "0.6.9",
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",