next-bun-compile 0.5.2 → 0.6.1
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/README.md +25 -9
- package/dist/cli.js +1 -1
- package/dist/{index-k6q8xg22.js → index-5m65k8kw.js} +82 -45
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,14 +27,24 @@ Add the adapter to your `next.config.ts`:
|
|
|
27
27
|
import type { NextConfig } from "next";
|
|
28
28
|
|
|
29
29
|
const nextConfig: NextConfig = {
|
|
30
|
-
|
|
31
|
-
adapterPath: require.resolve("next-bun-compile"),
|
|
32
|
-
},
|
|
30
|
+
adapterPath: import.meta.resolve("next-bun-compile"),
|
|
33
31
|
};
|
|
34
32
|
|
|
35
33
|
export default nextConfig;
|
|
36
34
|
```
|
|
37
35
|
|
|
36
|
+
<details>
|
|
37
|
+
<summary>Using Next.js 16.1? Use the experimental config instead</summary>
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
const nextConfig: NextConfig = {
|
|
41
|
+
experimental: {
|
|
42
|
+
adapterPath: import.meta.resolve("next-bun-compile"),
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
```
|
|
46
|
+
</details>
|
|
47
|
+
|
|
38
48
|
Update your build script in `package.json`:
|
|
39
49
|
|
|
40
50
|
```json
|
|
@@ -81,9 +91,7 @@ If you configure `assetPrefix` in your `next.config.ts`, static assets (`/_next/
|
|
|
81
91
|
```ts
|
|
82
92
|
const nextConfig: NextConfig = {
|
|
83
93
|
assetPrefix: "https://cdn.example.com",
|
|
84
|
-
|
|
85
|
-
adapterPath: require.resolve("next-bun-compile"),
|
|
86
|
-
},
|
|
94
|
+
adapterPath: import.meta.resolve("next-bun-compile"),
|
|
87
95
|
};
|
|
88
96
|
```
|
|
89
97
|
|
|
@@ -120,6 +128,16 @@ Some modules can't be resolved at compile time but are never reached in producti
|
|
|
120
128
|
|
|
121
129
|
## Troubleshooting
|
|
122
130
|
|
|
131
|
+
### Stale standalone output after upgrading Next.js
|
|
132
|
+
|
|
133
|
+
Next.js doesn't clean the standalone output directory between builds. If you upgrade Next.js versions, stale files from the old version can cause runtime errors like `Cannot find module 'next/dist/compiled/source-map'`.
|
|
134
|
+
|
|
135
|
+
**Fix:** Clean the standalone output before rebuilding:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
rm -rf .next/standalone && bun next build && next-bun-compile
|
|
139
|
+
```
|
|
140
|
+
|
|
123
141
|
### Packages with dynamic `require()` calls (e.g. pino)
|
|
124
142
|
|
|
125
143
|
Some packages like `pino` use dynamic `require()` calls internally (for worker threads, transports, etc.). Turbopack can't resolve these at build time, so they fail at runtime inside the compiled binary with errors like:
|
|
@@ -133,9 +151,7 @@ Failed to load external module pino-142500b1eb3f4baf: Cannot find package ...
|
|
|
133
151
|
```ts
|
|
134
152
|
const nextConfig: NextConfig = {
|
|
135
153
|
transpilePackages: ["pino", "pino-pretty"],
|
|
136
|
-
|
|
137
|
-
adapterPath: require.resolve("next-bun-compile"),
|
|
138
|
-
},
|
|
154
|
+
adapterPath: import.meta.resolve("next-bun-compile"),
|
|
139
155
|
};
|
|
140
156
|
```
|
|
141
157
|
|
package/dist/cli.js
CHANGED
|
@@ -147,61 +147,100 @@ let resolve = (id) => { try { return _resolve(id); } catch { return ''; } };`;
|
|
|
147
147
|
console.log("next-bun-compile: Patched require-hook.js for compiled binary compatibility");
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
|
-
function collectExternalModules(standaloneDir
|
|
151
|
-
const
|
|
152
|
-
if (!existsSync(
|
|
150
|
+
function collectExternalModules(standaloneDir) {
|
|
151
|
+
const nodeModulesDir = join(standaloneDir, "node_modules");
|
|
152
|
+
if (!existsSync(nodeModulesDir))
|
|
153
153
|
return [];
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
if (!
|
|
157
|
-
|
|
158
|
-
const content = readFileSync(absolutePath, "utf-8");
|
|
159
|
-
for (const match of content.matchAll(/require\("(next\/dist\/[^"]+)"\)/g)) {
|
|
160
|
-
seeds.add(match[1]);
|
|
161
|
-
}
|
|
154
|
+
const pkgRoots = new Map;
|
|
155
|
+
function addPkg(name, path) {
|
|
156
|
+
if (!pkgRoots.has(name))
|
|
157
|
+
pkgRoots.set(name, path);
|
|
162
158
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (deps.has(file))
|
|
159
|
+
function scanDir(dir) {
|
|
160
|
+
if (!existsSync(dir))
|
|
166
161
|
return;
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
162
|
+
for (const entry of readdirSync(dir)) {
|
|
163
|
+
if (entry.startsWith(".") || entry === "next-bun-compile")
|
|
164
|
+
continue;
|
|
165
|
+
const entryPath = join(dir, entry);
|
|
166
|
+
if (!statSync(entryPath).isDirectory())
|
|
167
|
+
continue;
|
|
168
|
+
if (entry.startsWith("@")) {
|
|
169
|
+
for (const sub of readdirSync(entryPath)) {
|
|
170
|
+
const subPath = join(entryPath, sub);
|
|
171
|
+
if (statSync(subPath).isDirectory())
|
|
172
|
+
addPkg(`${entry}/${sub}`, subPath);
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
addPkg(entry, entryPath);
|
|
172
176
|
}
|
|
173
|
-
file = file + "/index.js";
|
|
174
|
-
fullPath = join(standaloneDir, "node_modules", file);
|
|
175
177
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
178
|
+
}
|
|
179
|
+
scanDir(nodeModulesDir);
|
|
180
|
+
for (const store of [".bun", ".pnpm"]) {
|
|
181
|
+
const storeDir = join(nodeModulesDir, store);
|
|
182
|
+
if (!existsSync(storeDir))
|
|
183
|
+
continue;
|
|
184
|
+
for (const storeEntry of readdirSync(storeDir)) {
|
|
185
|
+
const nested = join(storeDir, storeEntry, "node_modules");
|
|
186
|
+
if (existsSync(nested))
|
|
187
|
+
scanDir(nested);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const results = [];
|
|
191
|
+
for (const [name, pkgPath] of pkgRoots) {
|
|
192
|
+
for (const f of walkDir(pkgPath)) {
|
|
193
|
+
results.push({
|
|
194
|
+
mod: `${name}/${f.relativePath.replace(/\\/g, "/")}`,
|
|
195
|
+
src: f.absolutePath
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return results;
|
|
200
|
+
}
|
|
201
|
+
function fixModuleResolution(standaloneDir) {
|
|
202
|
+
const nodeModulesDir = join(standaloneDir, "node_modules");
|
|
203
|
+
for (const pkgDir of findPackageDirs(nodeModulesDir, "next")) {
|
|
204
|
+
const compiledDir = join(pkgDir, "dist/compiled");
|
|
205
|
+
if (!existsSync(compiledDir))
|
|
206
|
+
continue;
|
|
207
|
+
for (const entry of readdirSync(compiledDir)) {
|
|
208
|
+
const dir = join(compiledDir, entry);
|
|
209
|
+
if (!statSync(dir).isDirectory())
|
|
210
|
+
continue;
|
|
211
|
+
const pkgJsonPath = join(dir, "package.json");
|
|
212
|
+
const indexPath = join(dir, "index.js");
|
|
213
|
+
if (!existsSync(pkgJsonPath) || existsSync(indexPath))
|
|
214
|
+
continue;
|
|
215
|
+
const pkg = JSON.parse(readFileSync(pkgJsonPath, "utf-8"));
|
|
216
|
+
if (pkg.main && pkg.main !== "index.js") {
|
|
217
|
+
writeFileSync(indexPath, `module.exports = require("./${pkg.main}");`);
|
|
191
218
|
}
|
|
192
|
-
if (resolved)
|
|
193
|
-
trace(resolved);
|
|
194
219
|
}
|
|
195
220
|
}
|
|
196
|
-
for (const
|
|
197
|
-
|
|
198
|
-
|
|
221
|
+
for (const helpersDir of findPackageDirs(nodeModulesDir, "@swc/helpers")) {
|
|
222
|
+
const cjsDir = join(helpersDir, "cjs");
|
|
223
|
+
if (!existsSync(cjsDir))
|
|
224
|
+
continue;
|
|
225
|
+
for (const file of readdirSync(cjsDir)) {
|
|
226
|
+
if (!file.endsWith(".cjs"))
|
|
227
|
+
continue;
|
|
228
|
+
const name = file.slice(0, -4);
|
|
229
|
+
const shimDir = join(helpersDir, "_", name);
|
|
230
|
+
const shimFile = join(shimDir, "index.js");
|
|
231
|
+
if (existsSync(shimFile))
|
|
232
|
+
continue;
|
|
233
|
+
mkdirSync(shimDir, { recursive: true });
|
|
234
|
+
writeFileSync(shimFile, `module.exports = require("../../cjs/${file}");`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
199
237
|
}
|
|
200
238
|
function generateEntryPoint(options) {
|
|
201
239
|
const { standaloneDir, distDir, projectDir } = options;
|
|
202
240
|
const serverDir = findServerDir(standaloneDir);
|
|
203
241
|
generateStubs(standaloneDir);
|
|
204
242
|
patchRequireHook(standaloneDir);
|
|
243
|
+
fixModuleResolution(standaloneDir);
|
|
205
244
|
const staticDir = join(distDir, "static");
|
|
206
245
|
const staticFiles = walkDir(staticDir).map((f) => ({
|
|
207
246
|
...f,
|
|
@@ -217,11 +256,9 @@ function generateEntryPoint(options) {
|
|
|
217
256
|
...f,
|
|
218
257
|
urlPath: `__runtime/.next/${f.relativePath.replace(/\\/g, "/")}`
|
|
219
258
|
}));
|
|
220
|
-
const externalModules = collectExternalModules(standaloneDir
|
|
221
|
-
const externalPaths = ["next/package.json", ...externalModules];
|
|
259
|
+
const externalModules = collectExternalModules(standaloneDir);
|
|
222
260
|
const externalDir = join(serverDir, ".next/__external");
|
|
223
|
-
for (const mod of
|
|
224
|
-
const src = join(standaloneDir, "node_modules", mod);
|
|
261
|
+
for (const { mod, src } of externalModules) {
|
|
225
262
|
if (!existsSync(src))
|
|
226
263
|
continue;
|
|
227
264
|
const dest = join(externalDir, mod);
|
package/dist/index.js
CHANGED