Webpack will include all of d3.js in the bundle when doing import { select } from 'd3'
as seen in the following bundle visualization. The problem can be solved by doing import { select } from 'd3-selection'
but this defeats the purpose of automatic tree shaking somewhat.
Tree shaking does appear to work in simple cases on our non-library code.
We use "modules": false
in our .babelrc
in order to retain the module syntax and use 'module'
resolve in our webpack.config.js resolve: { mainFields: ['module', 'browser', 'main'] }
in order to select d3's module based code. As you can see the imported node_modules/d3/index.js is partitioned in ES6 modules instead of a monolithic browser bundle or CommonJS export.
Should this be posted as an issue on the webpack github page or am I doing something wrong? This is using all the latest versions of the libraries (d3@4.5.0
, webpack@2.2.1
, etc)
edit: Seems like this is related to the following issues: