Skip to content

Commit 9561277

Browse files
authoredFeb 6, 2025··
fix: remove dynamic require for map file (#347)
1 parent 5838438 commit 9561277

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed
 

‎.changeset/warm-apricots-explain.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix: remove dynamic require for map file
6+
7+
ESBuild tries to load all files in the chunks folder with `require("./chunks/" + var)`.
8+
This is an error when the folder contains map file.

‎examples/api/next.config.mjs

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
33
initOpenNextCloudflareForDev();
44

55
/** @type {import('next').NextConfig} */
6-
const nextConfig = {};
6+
const nextConfig = {
7+
experimental: {
8+
// Generate source map to validate the fix for opennextjs/opennextjs-cloudflare#341
9+
serverSourceMaps: true,
10+
},
11+
};
712

813
export default nextConfig;

‎packages/cloudflare/src/cli/build/patches/investigated/update-webpack-chunks-file/get-file-content-with-updated-webpack-f-require-code.test.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ describe("getFileContentWithUpdatedWebpackFRequireCode", () => {
1717
{ installChunk: "installChunk", installedChunks: "installedChunks" },
1818
["658"]
1919
);
20-
expect(unstyleCode(updatedFCode)).toContain(`if (installedChunks[chunkId]) return;`);
20+
expect(unstyleCode(updatedFCode)).toContain(`if (installedChunks[chunkId]) { return; }`);
2121
expect(unstyleCode(updatedFCode)).toContain(
22-
`if (chunkId === 658) return installChunk(require("./chunks/658.js"));`
22+
`if (chunkId === 658) { return installChunk(require("./chunks/658.js")); }`
2323
);
24+
expect(unstyleCode(updatedFCode)).not.toContain(`require("./chunks/" +`);
2425
});
2526

2627
test("returns the updated content of the f.require function from minified webpack runtime code", async () => {
@@ -34,8 +35,9 @@ describe("getFileContentWithUpdatedWebpackFRequireCode", () => {
3435
{ installChunk: "r", installedChunks: "e" },
3536
["658"]
3637
);
37-
expect(unstyleCode(updatedFCode)).toContain("if (e[o]) return;");
38-
expect(unstyleCode(updatedFCode)).toContain(`if (o === 658) return r(require("./chunks/658.js"));`);
38+
expect(unstyleCode(updatedFCode)).toContain("if (e[o]) { return; }");
39+
expect(unstyleCode(updatedFCode)).toContain(`if (o === 658) { return r(require("./chunks/658.js")); }`);
40+
expect(unstyleCode(updatedFCode)).not.toContain(`require("./chunks/" +`);
3941
});
4042
});
4143

‎packages/cloudflare/src/cli/build/patches/investigated/update-webpack-chunks-file/get-file-content-with-updated-webpack-f-require-code.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,20 @@ export async function getFileContentWithUpdatedWebpackFRequireCode(
8181

8282
const functionBody = webpackFRequireFunction.getBody() as ts.Block;
8383

84-
functionBody.insertStatements(0, [
85-
`if (${installedChunks}[${chunkId}]) return;`,
86-
...chunks.map(
87-
(chunk) => `\nif(${chunkId} === ${chunk}) return ${installChunk}(require("./chunks/${chunk}.js"));`
88-
),
89-
]);
84+
functionBody.replaceWithText(`
85+
{
86+
if (${installedChunks}[${chunkId}]) {
87+
return;
88+
}${chunks
89+
.map(
90+
(chunk) => `
91+
if(${chunkId} === ${chunk}) {
92+
return ${installChunk}(require("./chunks/${chunk}.js"));
93+
}`
94+
)
95+
.join("")}
96+
throw new Error(\`Unknown chunk \${${chunkId}}\`);
97+
}`);
9098

9199
return sourceFile.print();
92100
}

‎packages/cloudflare/src/cli/build/patches/investigated/update-webpack-chunks-file/test-snapshots/minified-webpacks-file.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@
7272
e[n[f]] = 1;
7373
};
7474
(t.f.require = (o, n) => {
75-
if (e[o])
75+
if (e[o]) {
7676
return;
77-
if (o === 658)
77+
}
78+
if (o === 658) {
7879
return r(require("./chunks/658.js"));
79-
e[o] || (658 != o ? r(require("./chunks/" + t.u(o))) : (e[o] = 1));
80+
}
81+
throw new Error(`Unknown chunk ${o}`);
8082
}),
8183
(module.exports = t),
8284
(t.C = r);

‎packages/cloudflare/src/cli/build/patches/investigated/update-webpack-chunks-file/test-snapshots/unminified-webpacks-file.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -205,21 +205,13 @@
205205
/******/
206206
/******/ // require() chunk loading for javascript
207207
/******/ __webpack_require__.f.require = (chunkId, promises) => {
208-
if (installedChunks[chunkId])
208+
if (installedChunks[chunkId]) {
209209
return;
210-
if (chunkId === 658)
210+
}
211+
if (chunkId === 658) {
211212
return installChunk(require("./chunks/658.js"));
212-
/******/ // "1" is the signal for "already loaded"
213-
/******/ if (!installedChunks[chunkId]) {
214-
/******/ if (658 != chunkId) {
215-
/******/ installChunk(require("./chunks/" + __webpack_require__.u(chunkId)));
216-
/******/
217-
}
218-
else
219-
installedChunks[chunkId] = 1;
220-
/******/
221213
}
222-
/******/
214+
throw new Error(`Unknown chunk ${chunkId}`);
223215
};
224216
/******/
225217
/******/ module.exports = __webpack_require__;

0 commit comments

Comments
 (0)
Please sign in to comment.