Skip to content

Commit 720bfc0

Browse files
authored
fix(worker): patch workflow bundles with const/let/var (#2169) (#2175)
1 parent dc196d0 commit 720bfc0

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

packages/worker/src/workflow/bundler.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,21 @@ export class WorkflowCodeBundler {
132132
let code = memoryFs.readFileSync(bundleFilePath, 'utf8') as string;
133133
// Replace webpack's module cache with an object injected by the runtime.
134134
// This is the key to reusing a single v8 context.
135-
code = code.replace(
136-
'var __webpack_module_cache__ = {}',
137-
'var __webpack_module_cache__ = globalThis.__webpack_module_cache__'
138-
);
135+
// Webpack may emit the declaration as `var`, `let`, or `const` depending on the
136+
// configured output environment (webpack >= 5.108.0 defaults to `const`).
137+
let cacheDeclarationsPatched = 0;
138+
code = code.replace(/(^|\s)(var|let|const) __webpack_module_cache__ = \{\}/gm, (_match, prefix, keyword) => {
139+
cacheDeclarationsPatched++;
140+
return `${prefix}${keyword} __webpack_module_cache__ = globalThis.__webpack_module_cache__`;
141+
});
142+
if (cacheDeclarationsPatched !== 1) {
143+
throw new Error(
144+
`Failed to patch the Workflow bundle: expected to find exactly one __webpack_module_cache__ declaration ` +
145+
`emitted by webpack, but found ${cacheDeclarationsPatched}. Without this patch, Workflow isolation ` +
146+
`would be broken. This is likely due to a change in webpack output; please report this at ` +
147+
`https://github.com/temporalio/sdk-typescript/issues`
148+
);
149+
}
139150

140151
this.logger.info('Workflow bundle created', { size: `${toMB(code.length)}MB` });
141152

0 commit comments

Comments
 (0)