Skip to content

Commit cce2a50

Browse files
patch import_unsupported definition
1 parent fd9408f commit cce2a50

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

packages/cloudflare/src/cli/build/build-worker.ts

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

136+
/**
137+
* Next.js sets this `__import_unsupported` on `globalThis`:
138+
* https://github.com/vercel/next.js/blob/5b7833e3/packages/next/src/server/web/globals.ts#L94-L98
139+
*
140+
* For some reason on us this gets run more than once causing a `Cannot redefine property: __import_unsupported`
141+
* runtime error.
142+
*
143+
* So here we patch the function to only define the property if it is not already there
144+
*
145+
* TODO: this should ideally be done in a more robust way with ts-morph
146+
*/
147+
export function patchEnhanceGlobals(code: string) {
148+
return code.replace(
149+
`Object.defineProperty(globalThis, "__import_unsupported",`,
150+
`if(!("__import_unsupported" in globalThis)) Object.defineProperty(globalThis, "__import_unsupported",`
151+
);
152+
}
153+
136154
/**
137155
* This function applies string replacements on the bundled worker code necessary to get it to run in workerd
138156
*
@@ -154,6 +172,7 @@ async function updateWorkerBundledCode(workerOutputFile: string, config: Config)
154172
patchedCode = await patchCache(patchedCode, config);
155173
patchedCode = inlineMiddlewareManifestRequire(patchedCode, config);
156174
patchedCode = patchExceptionBubbling(patchedCode);
175+
patchedCode = patchEnhanceGlobals(patchedCode);
157176

158177
await writeFile(workerOutputFile, patchedCode);
159178
}

0 commit comments

Comments
 (0)