next-bun-compile 0.6.11 → 0.6.13
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 +1 -1
- package/dist/{index-8jf4ty06.js → index-3xvctb1p.js} +113 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -167,6 +167,102 @@ 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 + ".mjs",
|
|
210
|
+
stripped + ".js",
|
|
211
|
+
stripped + ".cjs",
|
|
212
|
+
stripped + ".json",
|
|
213
|
+
join(stripped, "index.mjs"),
|
|
214
|
+
join(stripped, "index.js"),
|
|
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
|
+
for (const sub of subpaths) {
|
|
226
|
+
const file = resolveSub(canonicalDir, sub);
|
|
227
|
+
if (file)
|
|
228
|
+
out.set(`${alias}/${sub}`, `.next/node_modules/${target}/${file}`);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return out;
|
|
232
|
+
}
|
|
233
|
+
function rewriteTurbopackAliases(standaloneNextDir, aliases, resolutions) {
|
|
234
|
+
if (aliases.length === 0 || resolutions.size === 0)
|
|
235
|
+
return;
|
|
236
|
+
const serverDir = join(standaloneNextDir, "server");
|
|
237
|
+
if (!existsSync(serverDir))
|
|
238
|
+
return;
|
|
239
|
+
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
240
|
+
const pattern = new RegExp(`(["'])((?:` + aliases.map((a) => escape(a.alias)).join("|") + `)(?:/[^"']+)?)\\1`, "g");
|
|
241
|
+
let rewritten = 0;
|
|
242
|
+
for (const f of walkDir(serverDir)) {
|
|
243
|
+
if (!f.absolutePath.endsWith(".js"))
|
|
244
|
+
continue;
|
|
245
|
+
let content;
|
|
246
|
+
try {
|
|
247
|
+
content = readFileSync(f.absolutePath, "utf-8");
|
|
248
|
+
} catch {
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
const next = content.replace(pattern, (match, quote, spec) => {
|
|
252
|
+
const rel = resolutions.get(spec);
|
|
253
|
+
if (!rel)
|
|
254
|
+
return match;
|
|
255
|
+
return `${quote}__NBC_BASE__/${rel}${quote}`;
|
|
256
|
+
});
|
|
257
|
+
if (next !== content) {
|
|
258
|
+
writeFileSync(f.absolutePath, next);
|
|
259
|
+
rewritten++;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (rewritten > 0) {
|
|
263
|
+
console.log(`next-bun-compile: Rewrote turbopack-mangled aliases in ${rewritten} server chunks`);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
170
266
|
function findServerDir(standaloneDir) {
|
|
171
267
|
if (existsSync(join(standaloneDir, "server.js"))) {
|
|
172
268
|
return standaloneDir;
|
|
@@ -353,6 +449,8 @@ function generateEntryPoint(options) {
|
|
|
353
449
|
if (externalModules.length > 0) {
|
|
354
450
|
console.log(`next-bun-compile: Embedding ${externalModules.length} external modules for SSR`);
|
|
355
451
|
}
|
|
452
|
+
const canonicalResolutions = buildCanonicalResolutions(externalDir, turbopackAliases);
|
|
453
|
+
rewriteTurbopackAliases(standaloneNextDir, turbopackAliases, canonicalResolutions);
|
|
356
454
|
const ctx = JSON.parse(readFileSync(join(distDir, "bun-compile-ctx.json"), "utf-8"));
|
|
357
455
|
const { assetPrefix } = ctx;
|
|
358
456
|
const assetsToEmbed = assetPrefix ? [...publicFiles, ...runtimeFiles] : [...staticFiles, ...publicFiles, ...runtimeFiles];
|
|
@@ -530,7 +628,21 @@ async function extractAssets() {
|
|
|
530
628
|
if (fs.existsSync(fullPath)) continue;
|
|
531
629
|
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
|
532
630
|
const embedded = assetMap.get(urlPath);
|
|
533
|
-
if (embedded)
|
|
631
|
+
if (!embedded) continue;
|
|
632
|
+
// Server chunks may contain __NBC_BASE__ placeholders injected at
|
|
633
|
+
// build time by rewriteTurbopackAliases. Substitute the real baseDir
|
|
634
|
+
// before writing so bun resolves the absolute paths at chunk load
|
|
635
|
+
// (works for both CJS require and ESM import).
|
|
636
|
+
if (diskPath.startsWith(".next/server/")) {
|
|
637
|
+
const text = await Bun.file(embedded).text();
|
|
638
|
+
if (text.indexOf("__NBC_BASE__") !== -1) {
|
|
639
|
+
await Bun.write(fullPath, text.split("__NBC_BASE__").join(baseDir));
|
|
640
|
+
n++;
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
await Bun.write(fullPath, Bun.file(embedded));
|
|
645
|
+
n++;
|
|
534
646
|
}
|
|
535
647
|
if (n > 0) console.log(\`Extracted \${n} assets\`);
|
|
536
648
|
}
|
package/dist/index.js
CHANGED