Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep-names may cause some errors #345

Closed
alienzhou opened this issue Jun 8, 2022 · 0 comments
Closed

keep-names may cause some errors #345

alienzhou opened this issue Jun 8, 2022 · 0 comments

Comments

@alienzhou
Copy link
Contributor

I found that using KeepNames will add a def function (__name) at the begin. When the module uses .toString() to get a function's source code and use it to execute (new Function / new Worker). The def function will be out of the scope.

An original module below

function main(a, b) {
    function log(a1, b1) {
        console.log(a1, b1);
    }
    log(a, b);
}

const code = `(${main.toString()})(1, 2)`;
new Worker(URL.createObjectURL(new Blob([code])));

will be transformed to

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
function main(a, b) {
  function log(a1, b1) {
    console.log(a1, b1);
  }
  __name(log, "log");
  log(a, b);
}
__name(main, "main");
const code = `(${main.toString()})(1, 2)`;
new Worker(URL.createObjectURL(new Blob([code])));

Now main needs __name when it is executed. However there no definition in main.toString().


canvas-confetti is a real package with the edge case.

After enabling useWorker:

confetti1.create(document.getElementById('ca1'), {
  resize: true,
  useWorker: true,
});

image

Here's a simple demo: https://gist.github.com/alienzhou/75e1f1a4c7fdbbf7cabce4469688df1b#file-esm-sh-simple-example-html
(works well in v82 and is broken in v83)

It's more like a problem of esbuild. Maybe adding an option for users?

@alienzhou alienzhou changed the title keep-names may occurs some errors keep-names may occur some errors Jun 8, 2022
@alienzhou alienzhou changed the title keep-names may occur some errors keep-names may cause some errors Jun 8, 2022
@ije ije closed this as completed in 28f20a1 Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant