next-bun-compile 0.6.6 → 0.6.8

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-c4yr0spy.js";
5
+ } from "./index-mx555e90.js";
6
6
 
7
7
  // src/cli.ts
8
8
  import { existsSync } from "node:fs";
@@ -113,20 +113,9 @@ function generateStubs(standaloneDir) {
113
113
  }
114
114
  function findTurbopackAliases(standaloneNextDir) {
115
115
  const seen = new Map;
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);
124
- }
125
- return entry;
126
- };
127
116
  const serverDir = join(standaloneNextDir, "server");
128
117
  if (existsSync(serverDir)) {
129
- const re = /["']([^"'\s/]+-[0-9a-f]{16})(?:\/([^"'\s]+))?["']/g;
118
+ const re = /["']([^"'\s/]+-[0-9a-f]{16})(?:\/[^"'\s]+)?["']/g;
130
119
  for (const f of walkDir(serverDir)) {
131
120
  if (!f.absolutePath.endsWith(".js"))
132
121
  continue;
@@ -138,9 +127,10 @@ function findTurbopackAliases(standaloneNextDir) {
138
127
  }
139
128
  let m;
140
129
  while (m = re.exec(content)) {
141
- const entry = ensure(m[1]);
142
- if (m[2])
143
- entry.subpaths.add(m[2]);
130
+ const alias = m[1];
131
+ if (seen.has(alias))
132
+ continue;
133
+ seen.set(alias, alias.replace(/-[0-9a-f]{16}$/, ""));
144
134
  }
145
135
  }
146
136
  }
@@ -155,20 +145,42 @@ function findTurbopackAliases(standaloneNextDir) {
155
145
  try {
156
146
  if (!lstatSync(aliasPath).isSymbolicLink())
157
147
  continue;
158
- seen.set(name, {
159
- target: basename(realpathSync(aliasPath)),
160
- subpaths: new Set
161
- });
148
+ seen.set(name, basename(realpathSync(aliasPath)));
162
149
  } catch {
163
150
  continue;
164
151
  }
165
152
  }
166
153
  }
167
- return Array.from(seen, ([alias, { target, subpaths }]) => ({
168
- alias,
169
- target,
170
- subpaths: Array.from(subpaths)
171
- }));
154
+ return Array.from(seen, ([alias, target]) => ({ alias, target }));
155
+ }
156
+ function rewriteTurbopackAliases(standaloneNextDir, aliases) {
157
+ if (aliases.length === 0)
158
+ return;
159
+ const serverDir = join(standaloneNextDir, "server");
160
+ if (!existsSync(serverDir))
161
+ return;
162
+ const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
163
+ const pattern = new RegExp(`(["'])(` + aliases.map((a) => escape(a.alias)).join("|") + `)(?=[/"'])`, "g");
164
+ const targetByAlias = new Map(aliases.map((a) => [a.alias, a.target]));
165
+ let rewritten = 0;
166
+ for (const f of walkDir(serverDir)) {
167
+ if (!f.absolutePath.endsWith(".js"))
168
+ continue;
169
+ let content;
170
+ try {
171
+ content = readFileSync(f.absolutePath, "utf-8");
172
+ } catch {
173
+ continue;
174
+ }
175
+ const next = content.replace(pattern, (_m, q, alias) => q + targetByAlias.get(alias));
176
+ if (next !== content) {
177
+ writeFileSync(f.absolutePath, next);
178
+ rewritten++;
179
+ }
180
+ }
181
+ if (rewritten > 0) {
182
+ console.log(`next-bun-compile: Rewrote turbopack-mangled aliases in ${rewritten} server chunks`);
183
+ }
172
184
  }
173
185
  function findServerDir(standaloneDir) {
174
186
  if (existsSync(join(standaloneDir, "server.js"))) {
@@ -331,6 +343,7 @@ function generateEntryPoint(options) {
331
343
  }));
332
344
  const standaloneNextDir = join(serverDir, ".next");
333
345
  const turbopackAliases = findTurbopackAliases(standaloneNextDir);
346
+ rewriteTurbopackAliases(standaloneNextDir, turbopackAliases);
334
347
  const aliasNames = new Set(turbopackAliases.map((a) => a.alias));
335
348
  const runtimeFiles = walkDir(standaloneNextDir).filter((f) => {
336
349
  const m = f.relativePath.replace(/\\/g, "/").match(/^node_modules\/([^/]+)/);
@@ -413,7 +426,6 @@ if (Number.isNaN(keepAliveTimeout) || !Number.isFinite(keepAliveTimeout) || keep
413
426
  }
414
427
 
415
428
  const extractions = ${JSON.stringify(assetExtractions)};
416
- const turbopackAliases = ${JSON.stringify(turbopackAliases.map((a) => [a.alias, a.target, a.subpaths]))};
417
429
  async function extractAssets() {
418
430
  let n = 0;
419
431
  for (const [urlPath, diskPath] of extractions) {
@@ -423,35 +435,6 @@ async function extractAssets() {
423
435
  const embedded = assetMap.get(urlPath);
424
436
  if (embedded) { await Bun.write(fullPath, Bun.file(embedded)); n++; }
425
437
  }
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
- // Relative path bypasses bun's package-name resolver, which doesn't
435
- // walk up to the parent node_modules from inside an alias directory.
436
- fs.writeFileSync(
437
- path.join(aliasPath, "index.js"),
438
- "module.exports = require(" + JSON.stringify("../" + target) + ");"
439
- );
440
- }
441
- for (const sub of subpaths) {
442
- const subKey = sub.replace(/\\.js$/, "");
443
- const shimFile = path.join(aliasPath, subKey + ".js");
444
- if (fs.existsSync(shimFile)) continue;
445
- fs.mkdirSync(path.dirname(shimFile), { recursive: true });
446
- const canonicalFile = path.join(baseDir, ".next/node_modules", target, subKey);
447
- const rel = path.relative(path.dirname(shimFile), canonicalFile);
448
- const spec = rel.startsWith(".") ? rel : "./" + rel;
449
- fs.writeFileSync(
450
- shimFile,
451
- "module.exports = require(" + JSON.stringify(spec) + ");"
452
- );
453
- }
454
- }
455
438
  if (n > 0) console.log(\`Extracted \${n} assets\`);
456
439
  }
457
440
 
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  __require,
3
3
  compile,
4
4
  generateEntryPoint
5
- } from "./index-c4yr0spy.js";
5
+ } from "./index-mx555e90.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.6",
3
+ "version": "0.6.8",
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",