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

top level return for IIFEs #5509

Open
Dinhero21 opened this issue May 3, 2024 · 2 comments
Open

top level return for IIFEs #5509

Dinhero21 opened this issue May 3, 2024 · 2 comments

Comments

@Dinhero21
Copy link

Feature Use Case

Stopping the current script's execution.

Useful in userscripts or more generally in cases where you don't want to close the entire webpage or pollute the console with should-be-ignored errors.

Feature Proposal

The closest you can currently get to aborting the current script is throwing an error, which is sub-optimal as it will emit an error event and log to the console.

Functions can end early by the use of the return keyword. When output.format is set to iife, the context already lives in a function and thus, the use of return will cleanly stop execution of the current script.

But top-level returns are blocked by Rollup with the error RollupError: Return statement is not allowed here.

Possible solutions (when IIFE):

  • allowing the use of top-level returns
  • aliasing process.exit() to return
  • aliasing a global exit() (window.exit()) to return
  • aliasing window.return() to return
@Dinhero21
Copy link
Author

process.exit() to return alias should probably be done by node polyfill plugins. (still requires return to be unlocked though)

@lukastaegert
Copy link
Member

Plugins can already enable this for their own parsing https://rollupjs.org/plugin-development/#this-parse
However, the goal is to deprecate returning an AST from plugins, so this would not fix it. But we might consider exposing allowReturnOutsideFunction as a general option.
In the meantime, you could write a plugin that uses renderChunk to inject returns into the IIFE in the output.

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

No branches or pull requests

2 participants