top level return for IIFEs
Dinhero21 opened this issue · 2 comments
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()
toreturn
- aliasing a global
exit()
(window.exit()
) toreturn
- aliasing
window.return()
toreturn
process.exit()
to return
alias should probably be done by node polyfill plugins. (still requires return
to be unlocked though)
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.