I have a React component library that I’m bundling with rollup. Then I’m consuming that library in an app setup with create-react-app which uses Webpack under the hood. I expect Webpack to tree-shake the component library. After building the app bundle and analyzing it I see that the library has either not been tree-shaken at all or that tree-shaking didn’t work on the library because it is not tree-shakable in the first place. Why is tree-shaking not working? What am I doing wrong?
rollup.config.js (bundler configuration of the React component library)
import babel from 'rollup-plugin-babel'
import commonjs from 'rollup-plugin-commonjs'
import autoExternal from 'rollup-plugin-auto-external'
import resolve from 'rollup-plugin-node-resolve'
import reactSvg from 'rollup-plugin-react-svg'
import url from 'rollup-plugin-url'
import string from 'rollup-plugin-string'
import pureanno from 'rollup-plugin-pure-annotation'
import pkg from './package.json'
const { getSVGOConfig } = require('./scripts/getSVGOConfig')
const MAX_INLINE_FILE_SIZE_KB = 100
export default {
input: 'src/index.js',
output: [
{
file: pkg.module,
format: 'es',
},
],
plugins: [
autoExternal(),
babel({
babelrc: false,
exclude: 'node_modules/**',
plugins: [
'external-helpers',
'babel-plugin-transform-react-jsx',
'babel-plugin-transform-class-properties',
'babel-plugin-transform-object-rest-spread',
'transform-react-remove-prop-types',
[
'babel-plugin-root-import',
{
'rootPathSuffix': 'src',
},
],
'babel-plugin-styled-components',
'transform-decorators-legacy',
[
'ramda',
{
'useES': true,
},
],
],
}),
resolve(),
commonjs(),
reactSvg({
svgo: getSVGOConfig(),
}),
url({
limit: MAX_INLINE_FILE_SIZE_KB * 1024,
include: ['**/*.woff', '**/*.woff2'],
}),
string({
include: '**/*.css',
}),
pureanno({
includes: ['**/*.js'],
}),
],
watch: {
chokidar: false,
},
}
src/index.js of the React component library
export { default as Theme } from './Theme'
export { default as Badge } from './components/Badge'
...
App.js (the app consuming the library)
import React from 'react';
import { Theme, Badge } from 'my-react-component-library'
function App() {
return (
<Theme>
<Badge>Hello</Badge>
</Theme>
)
}
export default App
package.json of the React component library (relevant parts)
{
"name": "my-react-component-library",
"version": "1.1.1",
"main": "dist/index.js",
"module": "dist/index.es.js",
"scripts": {
...
"build": "rollup -c",
},
"dependencies": {
...
},
"peerDependencies": {
"react": "^15.0.0 || ^16.0.0",
"react-dom": "^15.0.0 || ^16.0.0"
},
"devDependencies": {
...
},
"sideEffects": false
}
package.json of the app consuming the library (relevant parts)
{
"name": "my-app",
"version": "0.1.0",
"dependencies": {
"my-react-component-library": "^1.1.1",
"react": "^16.12.0",
"react-dom": "^16.12.0"
},
"scripts": {
...
"analyze": "source-map-explorer build/static/js/*chunk*.js build/static/js/*chunk*.js.map",
"build": "react-scripts build",
"serve": "serve -s build"
},
"devDependencies": {
...
"serve": "^11.3.0",
"source-map-explorer": "^2.2.2"
}
}
index.es.js (the bundled react component library)
https://gist.github.com/borisdiakur/ae376738955f15fb5079b5acb2ac83ad