Skip to content

Commit a7f7651

Browse files
patch import_unsupported definition
1 parent 58eeb7b commit a7f7651

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ globalThis.__dangerous_ON_edge_converter_returns_request = true;
139139
console.log(`\x1b[35mWorker saved in \`${openNextServerBundle}\` 🚀\n\x1b[0m`);
140140
}
141141

142+
/**
143+
* Next.js sets this `__import_unsupported` on `globalThis`:
144+
* https://github.com/vercel/next.js/blob/5b7833e3/packages/next/src/server/web/globals.ts#L94-L98
145+
*
146+
* For some reason on us this gets run more than once causing a `Cannot redefine property: __import_unsupported`
147+
* runtime error.
148+
*
149+
* So here we patch the function to only define the property if it is not already there
150+
*
151+
* TODO: this should ideally be done in a more robust way with ts-morph
152+
*/
153+
export function patchEnhanceGlobals(code: string) {
154+
return code.replace(
155+
`Object.defineProperty(globalThis, "__import_unsupported",`,
156+
`if(!("__import_unsupported" in globalThis)) Object.defineProperty(globalThis, "__import_unsupported",`
157+
);
158+
}
159+
142160
/**
143161
* This function applies string replacements on the bundled worker code necessary to get it to run in workerd
144162
*
@@ -164,6 +182,7 @@ async function updateWorkerBundledCode(
164182
patchedCode = await patchCache(patchedCode, openNextOptions);
165183
patchedCode = inlineMiddlewareManifestRequire(patchedCode, config);
166184
patchedCode = patchExceptionBubbling(patchedCode);
185+
patchedCode = patchEnhanceGlobals(patchedCode);
167186

168187
patchedCode = patchedCode
169188
// workers do not support dynamic require nor require.resolve

0 commit comments

Comments
 (0)