Giter VIP home page Giter VIP logo

Comments (5)

mbostock avatar mbostock commented on May 28, 2024

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.

from rollup.

Rich-Harris avatar Rich-Harris commented on May 28, 2024

As a starting point, to cover the majority of cases, I went with a simple heuristic - if an import declaration is empty (like import 'polyfills'), Rollup assumes the module is being imported for its side-effects and includes all its statements - and left the trickier cases as a bridge to be crossed later.

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 obj, and so are included after obj if it ends up in the bundle (tests here and here). (At some point I'd like it if Rollup could analyse the mutate function and decide whether it's capable of mutating obj, rather than including it just in case, but that's another story). Just need to add some similar logic that assumes that a method call mutates the thing that it's a method of. Marking this as a bug.

from rollup.

Rich-Harris avatar Rich-Harris commented on May 28, 2024

In 0.7.1, the map.set("baz", 50) call will be included, as method calls are assumed to mutate their owners. (This does of course mean that map.get('baz') would also be included, potentially unnecessarily, but that's definitely the lesser evil.)

Still don't have a great way to import for side-effects without duplicate import statements, so will leave this open for now

from rollup.

mbostock avatar mbostock commented on May 28, 2024

πŸ‘ 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.

from rollup.

Rich-Harris avatar Rich-Harris commented on May 28, 2024

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.

from rollup.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.