next-bun-compile 0.6.12 → 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-7dvf3g60.js → index-3xvctb1p.js} +89 -7
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -167,15 +167,77 @@ function findTurbopackAliases(standaloneNextDir) {
|
|
|
167
167
|
subpaths: Array.from(subpaths)
|
|
168
168
|
}));
|
|
169
169
|
}
|
|
170
|
-
function
|
|
171
|
-
|
|
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)
|
|
172
235
|
return;
|
|
173
236
|
const serverDir = join(standaloneNextDir, "server");
|
|
174
237
|
if (!existsSync(serverDir))
|
|
175
238
|
return;
|
|
176
239
|
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
177
|
-
const pattern = new RegExp(`(["'])(
|
|
178
|
-
const targetByAlias = new Map(aliases.map((a) => [a.alias, a.target]));
|
|
240
|
+
const pattern = new RegExp(`(["'])((?:` + aliases.map((a) => escape(a.alias)).join("|") + `)(?:/[^"']+)?)\\1`, "g");
|
|
179
241
|
let rewritten = 0;
|
|
180
242
|
for (const f of walkDir(serverDir)) {
|
|
181
243
|
if (!f.absolutePath.endsWith(".js"))
|
|
@@ -186,7 +248,12 @@ function rewriteTurbopackAliases(standaloneNextDir, aliases) {
|
|
|
186
248
|
} catch {
|
|
187
249
|
continue;
|
|
188
250
|
}
|
|
189
|
-
const next = content.replace(pattern, (
|
|
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
|
+
});
|
|
190
257
|
if (next !== content) {
|
|
191
258
|
writeFileSync(f.absolutePath, next);
|
|
192
259
|
rewritten++;
|
|
@@ -357,7 +424,6 @@ function generateEntryPoint(options) {
|
|
|
357
424
|
}));
|
|
358
425
|
const standaloneNextDir = join(serverDir, ".next");
|
|
359
426
|
const turbopackAliases = findTurbopackAliases(standaloneNextDir);
|
|
360
|
-
rewriteTurbopackAliases(standaloneNextDir, turbopackAliases);
|
|
361
427
|
const aliasNames = new Set(turbopackAliases.map((a) => a.alias));
|
|
362
428
|
const runtimeFiles = walkDir(standaloneNextDir).filter((f) => {
|
|
363
429
|
const m = f.relativePath.replace(/\\/g, "/").match(/^node_modules\/([^/]+)/);
|
|
@@ -383,6 +449,8 @@ function generateEntryPoint(options) {
|
|
|
383
449
|
if (externalModules.length > 0) {
|
|
384
450
|
console.log(`next-bun-compile: Embedding ${externalModules.length} external modules for SSR`);
|
|
385
451
|
}
|
|
452
|
+
const canonicalResolutions = buildCanonicalResolutions(externalDir, turbopackAliases);
|
|
453
|
+
rewriteTurbopackAliases(standaloneNextDir, turbopackAliases, canonicalResolutions);
|
|
386
454
|
const ctx = JSON.parse(readFileSync(join(distDir, "bun-compile-ctx.json"), "utf-8"));
|
|
387
455
|
const { assetPrefix } = ctx;
|
|
388
456
|
const assetsToEmbed = assetPrefix ? [...publicFiles, ...runtimeFiles] : [...staticFiles, ...publicFiles, ...runtimeFiles];
|
|
@@ -560,7 +628,21 @@ async function extractAssets() {
|
|
|
560
628
|
if (fs.existsSync(fullPath)) continue;
|
|
561
629
|
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
|
562
630
|
const embedded = assetMap.get(urlPath);
|
|
563
|
-
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++;
|
|
564
646
|
}
|
|
565
647
|
if (n > 0) console.log(\`Extracted \${n} assets\`);
|
|
566
648
|
}
|
package/dist/index.js
CHANGED