Skip to content

Cloudflare Worker Compatibility #925

Description

@samouri

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.

var ctor = new Function(code)();

My questions for the maintainers:

  • Do you know if it is possible to write this functionality in a way that doesn't use dynamic eval?
  • If yes, are you open to a PR for this?

Activity

  1. fabiosantoscode commented on Feb 7, 2021

    @fabiosantoscode
    Collaborator

    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.

  2. kalepail commented on Feb 24, 2021

    @kalepail

    I too am super interested in this functionality. Would love to help test stuff as it would be helpful.

  3. added a commit that references this issue on Apr 8, 2022
    32faf91
  4. added a commit that references this issue on Apr 9, 2022
    a8ab684
  5. fabiosantoscode commented on Apr 9, 2022

    @fabiosantoscode
    Collaborator

    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

  6. sgoo commented on Apr 11, 2022

    @sgoo
    Contributor

    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 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.

  7. jridgewell commented on Apr 11, 2022

    @jridgewell
    Collaborator

    Couldn't that new Function() just be a function () {} expression?

  8. fabiosantoscode commented on Apr 12, 2022

    @fabiosantoscode
    Collaborator

    Most likely. I'll reopen until that's dealt with

  9. added 2 commits that reference this issue on Apr 26, 2022
    d113465
    df528fb
  10. added a commit that references this issue on Apr 26, 2022
    67887ea
  11. samouri commented on Apr 27, 2022

    @samouri
    Author

    Thank you @sgoo for the impressive PR and @fabiosantoscode for reviewing it 🎉 .
    Much appreciated 🙇

  12. fabiosantoscode commented on Apr 27, 2022

    @fabiosantoscode
    Collaborator

    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.

  13. Manu1400 commented on May 14, 2025

    @Manu1400

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions