Pattern for using v4 modules

79 views
Skip to first unread message

Zach Bjornson

unread,
Jul 21, 2016, 5:29:29 PM7/21/16
to d3-js
Hello,

Does anyone have a good pattern yet for using a subset of d3.js v4's modules? Right now I have a helper module that extends and re-exports d3 with all of the modules that we're using:

import * as d3 from "d3-selection"; // or const d3 = require("d3-selection");
import * as d3Drag from "d3-drag";
import * as d3Scale from "d3-scale";

Object.assign(d3, d3Drag, d3Scale);

export default d3; // or module.exports = d3;

This avoids having to memorize which d3 methods live in which modules.

Not a d3 issue, but this approach does not assign the third-party d3 v4 typescript definitions for the other modules (d3-drag and d3-scale in this example) to d3 for consumption by other modules. That is, 'd3' only has the definitions for d3-selection on it.

Thanks,
Zach

Zach Bjornson

unread,
Jul 22, 2016, 2:50:35 PM7/22/16
to d3-js
While my approach works for plain JS, the author of the typescript definitions provided this method that is both clean and exports the definitions properly


// in the helper module
export * from "d3-selection";
export * from "d3-drag";

// in the consuming module
import * as d3 from "helper";

-Zach
Reply all
Reply to author
Forward
0 new messages