npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-transform-d3-imports

v0.2.1

Published

Optimize bundle size for D3@5 with ESModule

Downloads

865

Readme

babel-plugin-transform-d3-imports

Optimize bundle size for D3@5 with ESModule

npm travisCI codecov license

ExampleInstallationUsageOptionsWhyHow it worksQ&A

Example

In

import { select, event } from "d3-selection";
import { arc, pie } from "d3-shape";
import { scaleBand } from "d3-scale";
import { csv } from "d3-fetch";

Out

import select from "d3-selection/src/select";
import { event } from "d3-selection/src/selection/on";
import arc from "d3-shape/src/arc";
import pie from "d3-shape/src/pie";
import scaleBand from "d3-scale/src/band";
import { csv } from "d3-fetch/src/dsv";

Installation

Via yarn

yarn add -D babel-plugin-transform-d3-imports

Via npm

npm install --save-dev babel-plugin-transform-d3-imports

Usage

Via babel.config.js (aka .babelrc)

babel.config.js

module.exports = function () {
  return {
    plugins: ['babel-plugin-transform-d3-imports']
  }
}

Options

Currently none.

Why

Since you import any of:

  • default: import shape from "d3-shape";, (p.s. this won't work, d3-* doesnot contains default exports)
  • named: import { arc } from "d3-shape";, or
  • namespaced: import * as d3Shape from "d3-shape";,

you're importing the whole d3-shape module, includes a lot you've didn't need like area, line, pie, ...everything of d3-shape.

Why webpack/my bundler don't do a tree-shake for me?

D3 uses import-then-export syntax for all the documented API in an single entry file, once you import, you're importing everything.

But D3 packed their source code together with NPM, so we can import the source code directly like:

import arc from "d3-shape/src/arc";
import pie from "d3-shape/src/pie";
import select from "d3-selection/src/select";

to shrink the bundle size.

Of course you can do the same way by yourself, but with babel-plugin-transform-d3-imports, you can just:

import { arc, pie } from "d3-shape";
import { select } from "d3-selection";

How it works

We created a static mapping for all dependencies of d3@^5.16.0, so we can rewrite all importDeclaration correctly.

Don't worry about if the map had inconsistent between this plugins and latest version of D3. If it contains any not-matched import, we will not modify your imports.

Q & A

Who may need this plugin?

If you're using D3 with any bundler which can parsed by Babel, you'll need this plugin.

And if you're meet the followings, you will NOT suitable for this plugin:

  • When using CDN, or already packed D3 distribution
  • When still using ES5 or unable to parse ESModule
  • Didn't care about the additional several kb of bundle size