Skip to content

Commit 484cc2b

Browse files
committed
fix(docs): make shiki patch resilient to bundler param name changes
The dependency update in #490 caused the Turbopack bundler to rename the externalImport function parameter from `id` to `id2`. The hardcoded regex in patch-worker.mjs silently failed to match, leaving shiki modules unresolvable at runtime on Cloudflare Workers (500 Internal Server Error). - Use `(\w+)` capture group instead of literal `id` in the regex - Dynamically reference the captured param name in the replacement - Add validation that exits with error if the patch fails to match
1 parent cdd47ed commit 484cc2b

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

docs/scripts/patch-worker.mjs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,22 @@ const __EXTERNAL_MODULES = {
4242
handler = staticImports + handler;
4343

4444
// Patch externalImport to check __EXTERNAL_MODULES first
45+
// The parameter name varies across bundler versions (id, id2, etc.), so match any identifier
46+
let externalImportPatched = false;
4547
handler = handler.replace(
46-
/async function externalImport\(id\)\{let raw;try\{/g,
47-
'async function externalImport(id){let raw;try{if(typeof __EXTERNAL_MODULES!=="undefined"&&id in __EXTERNAL_MODULES){raw=__EXTERNAL_MODULES[id]}else '
48+
/async function externalImport\((\w+)\)\{let raw;try\{/g,
49+
(_match, paramName) => {
50+
externalImportPatched = true;
51+
return `async function externalImport(${paramName}){let raw;try{if(typeof __EXTERNAL_MODULES!=="undefined"&&${paramName} in __EXTERNAL_MODULES){raw=__EXTERNAL_MODULES[${paramName}]}else `;
52+
}
4853
);
4954

55+
if (!externalImportPatched) {
56+
console.error('ERROR: Failed to patch externalImport function — regex did not match.');
57+
console.error('The bundled handler.mjs may have changed its function signature.');
58+
process.exit(1);
59+
}
60+
5061
writeFileSync(HANDLER_PATH, handler);
5162

5263
console.log(`Patched ${HANDLER_PATH} (${originalSize} -> ${handler.length} bytes)`);

0 commit comments

Comments
 (0)