Tree Shaking Not Working - React
See original GitHub issueDescribe the bug
I have monorepo setup with component library.
- I am Using Lerna for that
- Using vite for multiple project creation inside that packages.
- Using component kit as component library to use those components.
I am using vite as bundler for building library as well as other projects inside that packages folder.
Currently component library is pretty big, which is around 8 MB after build.
This 8 MB is being downloaded for each project even though I am not using all components for particular project.
Checked with production build.
- Vite Config file of Component Lib.
import { defineConfig } from 'vite';
import path from 'path';
import typescript from '@rollup/plugin-typescript';
export default defineConfig({
build: {
lib: {
entry: path.resolve(__dirname, 'src/index.ts'),
name: 'index',
formats: ['es'],
},
rollupOptions: {
external: ['react', 'react-router-dom', 'rc-time-picker', 'moment'],
output: {
globals: {
react: 'react',
},
strict: false,
dir: 'lib',
sourcemap: false,
},
plugins: [
typescript({
allowJs: true,
allowSyntheticDefaultImports: true,
exclude: path.resolve(__dirname, './node_modules/**'),
declaration: true,
declarationDir: 'lib',
rootDir: path.resolve(__dirname, './src'),
sourceRoot: path.resolve(__dirname, './src/index.ts'),
emitDeclarationOnly: true,
}),
],
},
},
});`
- Used following vite config file for each package
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
build: {
polyfillDynamicImport: true,
},
publicDir: "public",
define: {
global: "window", // fix for packages that support both node and browser
},
});
Reproduction
Use reproduction step mentioned in description.
System Info
System:
OS: macOS 11.5.2
CPU: (8) arm64 Apple M1
Memory: 83.00 MB / 8.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.6.0 - ~/.nvm/versions/node/v16.6.0/bin/node
Yarn: 1.22.17 - /opt/homebrew/bin/yarn
npm: 7.19.1 - ~/.nvm/versions/node/v16.6.0/bin/npm
Browsers:
Chrome: 99.0.4844.83
Firefox: 98.0.2
Safari: 14.1.2
Used Package Manager
yarn
Logs
System:
OS: macOS 11.5.2
CPU: (8) arm64 Apple M1
Memory: 100.28 MB / 8.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.6.0 - ~/.nvm/versions/node/v16.6.0/bin/node
Yarn: 1.22.17 - /opt/homebrew/bin/yarn
npm: 7.19.1 - ~/.nvm/versions/node/v16.6.0/bin/npm
Browsers:
Chrome: 99.0.4844.83
Firefox: 98.0.2
Safari: 14.1.2
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Production build tree shaking not working #9717 - nrwl/nx
Tree shaking does not work. I created a React app with both Nx and create-react-app, and imported a single lodash function.
Read more >Why is my React component library not tree-shakable?
If your library doesn't tree-shake, try to check it with this tool: github.com/Rich-Harris/agadoo It may provide a little insight on what's ...
Read more >Tree Shaking - webpack
Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of ES2015 module...
Read more >Tree-Shaking Problems with Component Libraries - Medium
If you're publishing a component library, offer an ES Module build — and avoid rolling all your modules into a single file. PropTypes...
Read more >What is Tree Shaking and Implementation in React - Folio3
By implementing tree shaking practices website performance will get improved and reduce bundle size. Tree shaking depends on the static ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Experiencing issues with tree shaking in basically the same style, using specific paths and esm but still getting an entire UI lib bundled:
@bluwy Here is the Repo Link.
Pushed with node modules intentionally as using some private packages.
My problem is:
Even though I have only imported Button Component, Why did bundle get the code for both component Input and Button.