You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
summary Cloudflare Workers are a super fast function-as-a-service provider. They currently have a restriction that no worker code may use dynamic eval (new Function(code), or eval("code")). Unfortunately, terser uses dynamic eval in a single location.
Well, this is kind of a macro to generate our AST. It's quite fast because functions returned from eval are able to be optimized by V8
If it doesn't have a negative performance or memory impact, then I'm open to a PR. I suppose this will involve very repetitive class constructors.
I do like the idea of running Terser in a CF worker. Are you going to serve minified modules for usage in browser import? You're quickly going to find that Terser uses a lot of memory even for small payloads, and that you'd prefer to use a rust based minifier for the job, like SWC.
Right now I'm working on a very early version of a WASM wrapper for Terser, that can chunk up JS into smaller tokens and feed it into Terser web workers.
There's still one instance of new Function(), in lib/propmangle.js, but it doesn't have an argument. Let me know if it causes problems and I'll reopen the issue.
The code in propmangle is fine for us because all of the globals its looking for are present so it never calls new Function().
But I did check and even the arg less new Function() will result in EvalError: Code generation from strings disallowed for this context if eval has been disabled in v8 so this could be an issue for other runtimes.
No problem! If further issues are found with running Terser on a cloudflare worker (or other workers) let me know. I never tested anything of the sort.
Activity
fabiosantoscode commentedon Feb 7, 2021
Well, this is kind of a macro to generate our AST. It's quite fast because functions returned from
evalare able to be optimized by V8If it doesn't have a negative performance or memory impact, then I'm open to a PR. I suppose this will involve very repetitive class constructors.
I do like the idea of running Terser in a CF worker. Are you going to serve minified modules for usage in browser
import? You're quickly going to find that Terser uses a lot of memory even for small payloads, and that you'd prefer to use a rust based minifier for the job, like SWC.Right now I'm working on a very early version of a WASM wrapper for Terser, that can chunk up JS into smaller tokens and feed it into Terser web workers.
kalepail commentedon Feb 24, 2021
I too am super interested in this functionality. Would love to help test stuff as it would be helpful.
Run Eval removing codeshift
Run Eval removing codeshift (#1171)
fabiosantoscode commentedon Apr 9, 2022
There's still one instance of
new Function(), in lib/propmangle.js, but it doesn't have an argument. Let me know if it causes problems and I'll reopen the issue.Thanks to @sgoo for the clean fix
sgoo commentedon Apr 11, 2022
Thanks for merging!
The code in propmangle is fine for us because all of the globals its looking for are present so it never calls
new Function().But I did check and even the arg less
new Function()will result inEvalError: Code generation from strings disallowed for this contextif eval has been disabled in v8 so this could be an issue for other runtimes.jridgewell commentedon Apr 11, 2022
Couldn't that
new Function()just be afunction () {}expression?fabiosantoscode commentedon Apr 12, 2022
Most likely. I'll reopen until that's dealt with
Remove last new Function() call
Remove last new Function() call
Remove last new Function() call (#1184)
samouri commentedon Apr 27, 2022
Thank you @sgoo for the impressive PR and @fabiosantoscode for reviewing it 🎉 .
Much appreciated 🙇
fabiosantoscode commentedon Apr 27, 2022
No problem! If further issues are found with running Terser on a cloudflare worker (or other workers) let me know. I never tested anything of the sort.
Manu1400 commentedon May 14, 2025
FYI: Soon, you can use "eval()" and "new Function()" in global scope within Cloudflare workers! cloudflare/workerd#4142
source: https://x.com/yagiznizipli/status/1922385647085502688