Comments (5)
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.
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.
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.
π 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.
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)
- Rollup generates malformed URL with import.meta.ROLLUP_FILE_URL if asset name contains `%` HOT 1
- Wrong sanitization between inputBase and module ID HOT 2
- Supported "linux-arm" not detected correctly (Error: Your current platform "linux" and architecture "arm" combination is not yet supported by the native Rollup build) HOT 2
- Source maps: breakpoints don't work in IntelliJ HOT 3
- How to bundle require statements from esm? HOT 3
- Only transform input file and keep index.html untouched. HOT 1
- CJS output contains a reference to non-existent variable when exporting default and reexporting everything with interop: compat/auto HOT 2
- Document behavior of dynamic expressions in import() function
- Webpack promise external equivalent?
- Support for OpenBSD/adJ
- TypeError: Cannot read properties of null (reading 'render') HOT 12
- Treeshaking breaks acorn-walk "simple" 4.16.0+ HOT 7
- Support for FreeBSD HOT 4
- 4.16.4 throws Cannot find module @rollup/rollup-darwin-arm64 error MODULE_NOT_FOUND in Ember Embroider app HOT 4
- Node 16 not supported any longer, can't run more modern Node's either HOT 5
- Provide a mechanism to build manualChunks which can be reused across multiple outputs.
- Upgrade from vite 4 to vite 5 crashes in rollup with maximum call stack exceeded HOT 2
- function parameter tracking does not handle spread syntax in arguments correctly HOT 3
- top level return for IIFEs HOT 2
- Function body being mistakenly optimized away HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rollup.