babel doesn’t have tree shaking?

In my package.json, I have the following

"build": "babel src --out-dir dist --source-maps",

In one of my js files, I have

import { MyFunc } from 'package-example`

Then I run npm run build. It builds, creates a dist folder.

Somehow, in my built file, I find this:

const package-example = require('package-example')

This somehow means that tree shaking doesn’t work and the whole package-example gets included in the built.

Q1. Is this the correct assumption ?

Q2. How can I still have tree shaking so that only MyFunc gets imported ? I can’t use webpack for now. is this not possible without webpack/rollup at all with only babel ?


Babel processes files at an individual level. Tree-shaking requires project-wide awareness of what files are and aren’t being used, so Babel does not, and cannot, do this. You would need Webpack or Rollup or some other build/bundling tool for this.