Reputation: 71
Can't find out why tree-shaking is not working as I expected...
My goal is to build a tree-shak-able
library.
I have a webpack application that use this lib. When I import only the Header component, the Button module is removed as expected, but my webpack
bundle contains the ButtonGroup
component.
Can someone explain why?
How can I tree-shake nested components from the webpack
build - if possible?
Thanks
Rollup & lib configuration
rollup.config.js
import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';
export default {
input: './src/index.js',
output: {
name: 'ui-component',
sourcemap: true,
sourcemapFile: 'ui-component',
format: 'es',
file: 'build/ui-component.module.js',
},
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify('production')
}),
resolve({
extensions: [ '.js', '.jsx', '.json' ]
}),
commonjs(),
babel({
exclude: 'node_modules/**',
})
],
external: ['react', 'react-dom']
};
index.js
export * from './Button';
export * from './Header';
Header.jsx - simple react component
import React, { Component } from 'react';
export class Header extends Component {
render() {
return (
<div style={ { padding: '3px 7px' } }>
<span>{this.props.children}</span>
</div>
)
}
}
Button.jsx - react component
import React, { Component } from 'react';
import { ButtonGroup } from './ButtonGroup';
class Button extends Component {
constructor(props) {
super(props);
}
render() {
const display = 'button';
return (
<ButtonGroup>
<a> {display} </a>
</ButtonGroup>
);
}
}
export { Button }
And my webpack configuration and build:
webpack.config
const path = require('path');
const express = require('express');
module.exports = {
mode: 'production',
devtool: false,
optimization: {
sideEffects: false
},
entry: {
index: './src/App.jsx'
},
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, './bundle/')
},
resolve: {
extensions: ['.js', '.jsx'],
alias: {
'ui-component': path.resolve(__dirname, '../')
}
},
module: {
rules: [
{
test: /\.(jsx|js)$/,
include: [
path.resolve(__dirname, 'src'),
path.resolve(__dirname, '../')
],
use: {
loader: 'babel-loader',
options: {
presets: [
[ "@babel/env", { "modules": false}],
[ "@babel/react" ]
],
plugins: [
["@babel/plugin-proposal-object-rest-spread"],
["@babel/plugin-proposal-class-properties"]
]
}
}
},
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: [ 'url-loader' ]
}
]
},
externals: {
react: 'React',
'react-dom': 'ReactDOM'
},
devServer: {
contentBase: './',
publicPath: '/build',
port: 8080,
before(app) {
app.use('/build', express.static(path.resolve(__dirname, '../build/')))
}
}
};
* App.jsx *
import React from 'react';
import ReactDOM from 'react-dom';
import { Header } from 'ui-component';
class App extends React.Component {
render() {
return (
<div>
<Header >
header only
</Header>
</div>
);
}
}
ReactDOM.render(
<App />, document.getElementById('app')
)
* build status *
single entry ./src/App.jsx index
| ./src/App.jsx 751 bytes [depth 0] [built]
| [no exports]
| ModuleConcatenation bailout: Module is an entry point
| single entry ./src/App.jsx index
| ../build/ui-component.module.js 1.43 KiB [depth 1] [built]
| [exports: Button, Header]
| [only some exports used: Header]
| harmony side effect evaluation ui-component ./src/App.jsx 5:0-38
| harmony import specifier ui-component ./src/App.jsx 19:64-70
* build output *
e.prototype.render=function(){return o.a.createElement("div",null,"Groups")},e}
Upvotes: 2
Views: 2906
Reputation: 5435
I have been having this issue as well with nested modules and re-exports (two layers of re-exports for simpler code maintenance - less touching of main entry point files).
Webpack doesn't handle this situation (well) as the moment, but it looks like Webpack v5 will: https://github.com/webpack/webpack/pull/9203
Upvotes: 0
Reputation: 69
If you do not want button please import it like
import Header from 'ui-component/Header';
or add
"sideEffects": false
in package.json of "ui-component";
https://github.com/webpack/webpack/tree/master/examples/side-effects
Upvotes: 3