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

Ability to mark a code block as non-removable. #3870

Open
icywolfy opened this issue Nov 14, 2020 · 2 comments
Open

Ability to mark a code block as non-removable. #3870

icywolfy opened this issue Nov 14, 2020 · 2 comments

Comments

@icywolfy
Copy link

Feature Use Case

If rollup incorrectly removes a chunk of code, the ability to force an expression to not be tree-shaken.
Provides a workaround for:

  • users of rollup in non-standard environment, where rollup assumes a method is pure.
  • uncommon scenarios, where rollup assumes no side-effects.
  • bug in rollup, pending a fix

Admittedly, this is not for the main-stream user of rollup, but would be valuable for users who run into an issue.

Feature Proposal

/* @rollup-keep */
if (1 > 2) {
  doSomething();
}

In the specific case I am working with, the embedded js engine, in rare occasions, breaks the behaviour of >.
To work around this, we have to add the above if (1 > 2) check, which of course, is tree-shaken.
To work around rollup, we must add

  if (Math.random()*99.99 > 2) {
     doSomething();
  }

Then, on the rollup/minified bundle, replace the conditional back to (1 > 2).
Admittedly, seeing doSomething() report metrics once every few weeks is baffling.

Re: #2963
Re: #1771

@lukastaegert
Copy link
Member

I assume putting such code into a separate file for which you explicitly deactivate tree-shaking is not an option?

@cefn
Copy link

cefn commented Oct 4, 2021

This would also be valuable for my use-case. I am bundling Typescript to an ES5 environment (the CouchDB Spidermonkey engine), where a script is evaluated to define a map or reduce function.

So for example the following script evaluates to the value of myMap and defines a map function...

function myMap(doc){
  emit(doc.id)
}
myMap;

Since myMap is never actually referenced, when this runs with treeshake: true all code is removed as it is not exported. It would be valuable to be able to explicitly flag a symbol as being in use. Currently I simply have to disable treeshaking and have every imported function in its own file to prevent unused items making it into the bundle.

function myMap(doc){
  emit(doc.id)
}
/* @rollup-keep */
myMap;

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

3 participants