Skip to content

Commit db13a37

Browse files
authored
fix(worker): patch workflow bundles with const/let/var (#2169) (#2178)
1 parent 6148da0 commit db13a37

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
@@ -120,10 +120,21 @@ export class WorkflowCodeBundler {
120120
let code = memoryFs.readFileSync(bundleFilePath, 'utf8') as string;
121121
// Replace webpack's module cache with an object injected by the runtime.
122122
// This is the key to reusing a single v8 context.
123-
code = code.replace(
124-
'var __webpack_module_cache__ = {}',
125-
'var __webpack_module_cache__ = globalThis.__webpack_module_cache__'
126-
);
123+
// Webpack may emit the declaration as `var`, `let`, or `const` depending on the
124+
// configured output environment (webpack >= 5.108.0 defaults to `const`).
125+
let cacheDeclarationsPatched = 0;
126+
code = code.replace(/(^|\s)(var|let|const) __webpack_module_cache__ = \{\}/gm, (_match, prefix, keyword) => {
127+
cacheDeclarationsPatched++;
128+
return `${prefix}${keyword} __webpack_module_cache__ = globalThis.__webpack_module_cache__`;
129+
});
130+
if (cacheDeclarationsPatched !== 1) {
131+
throw new Error(
132+
`Failed to patch the Workflow bundle: expected to find exactly one __webpack_module_cache__ declaration ` +
133+
`emitted by webpack, but found ${cacheDeclarationsPatched}. Without this patch, Workflow isolation ` +
134+
`would be broken. This is likely due to a change in webpack output; please report this at ` +
135+
`https://github.com/temporalio/sdk-typescript/issues`
136+
);
137+
}
127138

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

0 commit comments

Comments
 (0)