-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
How to import with side effects? #13
Comments
Heh. So this is true of any code that has side-effects, which leads to some interesting constraints on development. For example, consider the following: var map = (new Map)
.set("foo", 42)
.set("bar", 60);
map
.set("baz", 50);
export default function(key) {
return map.get(key);
}; If you import the default export, the code that defines the “baz” key never runs—it’s dropped by Rollup. |
As a starting point, to cover the majority of cases, I went with a simple heuristic - if an import declaration is empty (like One option would be to indicate modules with side-effects with a comment, like /*rollup sideEffects: true */
import { ease, transition } from 'd3-transition'; I'm not wild about that, partly because it's Rollup specific but also because I don't think it's a module author's job to ensure that it does the right thing. I'm not sure what a reliable alternative looks like at the moment though. For the second example, that's a straightforward bug which I think is easily solved. At the moment, given a situation like this... var obj = {};
mutate( obj );
otherObj.mutate( obj );
export { obj }; ...the second and third lines are assumed to mutate |
In 0.7.1, the Still don't have a great way to import for side-effects without duplicate import statements, so will leave this open for now |
👍 for the 0.7.1 fix. I’m happy with the simple import for importing with side-effects, now that I understand how it works. Seems like the confusion could be solved with documentation. |
Closing this issue as it was resolved a while back – Rollup now takes a more cautious approach, checking all imported modules for side-effects. In some cases that means including more code than is necessary, so #179 suggests ways we can prevent those false positives. |
This is more of a question than an issue. Does it make sense that I have to import twice in order to achieve an import with side effects? Consider the following D3 build:
The first import of d3-transition exposes ease and transition. But the second is also needed to set selection.prototype.transition.
It makes sense that Rollup would ignore the side-effects by default, so that I can explicitly chose to include that code (or not). But I wanted to double-check that this seems right to you.
The text was updated successfully, but these errors were encountered: