I'm getting started with Rollup and D3 version 4, which is written in ES2015 modules. I've written a some code using the traditional D3 namespace "d3". Now I want to create a custom bundle using Rollup. I want to using tree-shaking, because I'm probably only using about half the functions in d3, and I want to keep things as light as possible.
I'm clear that I can import functions selectively, e.g.:
import {scaleLinear} from "d3-scale";
import {
event,
select,
selectAll
} from "d3-selection";
That is going to get very verbose very fast, because half of d3 is a lot of functions. I can live with that. The bigger problem is that it also would require completely rewriting all my function identifiers without a namespace. I don't much care for that, because I prefer to namespace library code.
I understand that I can import all of the module:
import * as d3 from "d3";
which preserves the d3 object namespace, which is good for my code organization. But then Rollup can't tree-shake the unused functions out of the bundle.
What I'm dreaming of is something like:
import {
event,
select,
selectAll
} as d3 from "d3-selection";
but that sort of feature/syntax doesn't seem to exist in the spec. How can I both selectively target individual parts of a module, and preserve namespacing during an import?
You need a re-exporting module for that:
export {
event,
select,
selectAll
} from "d3-selection";
import * as d3 from './d3';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With