question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tree Shaking Not Working - React

See original GitHub issue

Describe the bug

I have monorepo setup with component library.

  1. I am Using Lerna for that
  2. Using vite for multiple project creation inside that packages.
  3. 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.

  1. 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,
                }),
            ],
        },
    },
});`
  1. 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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Codex-commented, Jun 1, 2022

Experiencing issues with tree shaking in basically the same style, using specific paths and esm but still getting an entire UI lib bundled:

image

1reaction
malavshah9commented, Mar 27, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found