Skip to content

Commit 16a7884

Browse files
committed
fixup! external with absolute paths
1 parent e29a59f commit 16a7884

File tree

3 files changed

+67
-26
lines changed

3 files changed

+67
-26
lines changed

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { build, Plugin } from "esbuild";
1010

1111
import { patchOptionalDependencies } from "./patches/ast/optional-deps.js";
1212
import * as patches from "./patches/index.js";
13-
import InlineRequirePagePlugin from "./patches/plugins/require-page.js";
13+
import inlineRequirePagePlugin from "./patches/plugins/require-page.js";
14+
import setWranglerExternal from "./patches/plugins/wrangler-external.js";
1415
import { normalizePath, patchCodeWithValidations } from "./utils/index.js";
1516

1617
/** The dist directory of the Cloudflare adapter package */
@@ -49,16 +50,17 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
4950
format: "esm",
5051
target: "esnext",
5152
minify: false,
52-
plugins: [createFixRequiresESBuildPlugin(buildOpts), InlineRequirePagePlugin(buildOpts)],
53+
plugins: [
54+
createFixRequiresESBuildPlugin(buildOpts),
55+
inlineRequirePagePlugin(buildOpts),
56+
setWranglerExternal(),
57+
],
5358
external: [
5459
"./middleware/handler.mjs",
5560
// Next optional dependencies.
5661
"caniuse-lite",
5762
"jimp",
5863
"probe-image-size",
59-
// Dependencies handled by wrangler.
60-
"*.bin",
61-
"*.wasm?module",
6264
],
6365
alias: {
6466
// Note: we apply an empty shim to next/dist/compiled/ws because it generates two `eval`s:

packages/cloudflare/src/cli/build/patches/plugins/require-page.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,27 @@ import { readFile } from "node:fs/promises";
33
import { join } from "node:path";
44

55
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
6+
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
67
import type { PluginBuild } from "esbuild";
78

89
import { patchCode, type RuleConfig } from "../ast/util.js";
910

10-
export default function InlineRequirePagePlugin(buildOpts: BuildOptions) {
11+
export default function inlineRequirePagePlugin(buildOpts: BuildOptions) {
1112
return {
1213
name: "inline-require-page",
1314

1415
setup: async (build: PluginBuild) => {
15-
build.onLoad({ filter: /\/next\/dist\/server\/require\.js$/ }, async ({ path }) => {
16-
const jsCode = await readFile(path, "utf8");
17-
if (/function requirePage\(/.test(jsCode)) {
18-
return { contents: patchCode(jsCode, getRule(buildOpts)) };
16+
build.onLoad(
17+
{
18+
filter: getCrossPlatformPathRegex(String.raw`/next/dist/server/require\.js$`, { escape: false }),
19+
},
20+
async ({ path }) => {
21+
const jsCode = await readFile(path, "utf8");
22+
if (/function requirePage\(/.test(jsCode)) {
23+
return { contents: patchCode(jsCode, getRule(buildOpts)) };
24+
}
1925
}
20-
});
26+
);
2127
},
2228
};
2329
}
@@ -42,24 +48,24 @@ function getRule(buildOpts: BuildOptions) {
4248
const jsFiles = manifests.filter((file) => file.endsWith(".js"));
4349

4450
const fnBody = `
45-
// html
46-
${htmlFiles
51+
// html
52+
${htmlFiles
53+
.map(
54+
(file) => `if (pagePath.endsWith("${file}")) {
55+
return ${JSON.stringify(readFileSync(join(serverDir, file), "utf-8"))};
56+
}`
57+
)
58+
.join("\n")}
59+
// js
60+
process.env.__NEXT_PRIVATE_RUNTIME_TYPE = isAppPath ? 'app' : 'pages';
61+
try {
62+
${jsFiles
4763
.map(
4864
(file) => `if (pagePath.endsWith("${file}")) {
49-
return ${JSON.stringify(readFileSync(join(serverDir, file), "utf-8"))};
50-
}`
65+
return require(${JSON.stringify(join(serverDir, file))});
66+
}`
5167
)
5268
.join("\n")}
53-
// js
54-
process.env.__NEXT_PRIVATE_RUNTIME_TYPE = isAppPath ? 'app' : 'pages';
55-
try {
56-
${jsFiles
57-
.map(
58-
(file) => `if (pagePath.endsWith("${file}")) {
59-
return require(${JSON.stringify(join(serverDir, file))});
60-
}`
61-
)
62-
.join("\n")}
6369
} finally {
6470
process.env.__NEXT_PRIVATE_RUNTIME_TYPE = '';
6571
}
@@ -70,7 +76,7 @@ function getRule(buildOpts: BuildOptions) {
7076
pattern: `
7177
function requirePage($PAGE, $DIST_DIR, $IS_APPP_ATH) {
7278
const $_ = getPagePath($$$ARGS);
73-
$$$
79+
$$$_BODY
7480
}`,
7581
},
7682
fix: `
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Makes files handled by wrangler external.
3+
*
4+
* Paths need to be absolute so that their are valid in the output bundle.
5+
*/
6+
7+
import { dirname, resolve } from "node:path";
8+
9+
import type { PluginBuild } from "esbuild";
10+
11+
export default function setWranglerExternal() {
12+
return {
13+
name: "wrangler-externals",
14+
15+
setup: async (build: PluginBuild) => {
16+
const namespace = "wrangler-externals-plugin";
17+
18+
build.onResolve({ filter: /(\.bin|\.wasm\?module)$/ }, ({ path, importer }) => {
19+
return {
20+
path: resolve(dirname(importer), path),
21+
namespace,
22+
external: true,
23+
};
24+
});
25+
26+
build.onLoad({ filter: /.*/, namespace }, async ({ path }) => {
27+
return {
28+
contents: `export * from '${path}';`,
29+
};
30+
});
31+
},
32+
};
33+
}

0 commit comments

Comments
 (0)