Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree shaking dynamic import from VIte bundled React component library #17460

Open
7 tasks done
SamyZog opened this issue Jun 13, 2024 · 3 comments
Open
7 tasks done

Tree shaking dynamic import from VIte bundled React component library #17460

SamyZog opened this issue Jun 13, 2024 · 3 comments

Comments

@SamyZog
Copy link

SamyZog commented Jun 13, 2024

Describe the bug

I am building a private React component library using Vite, I am using a barrel file for all my exports.

src/core/index.ts

export * from './components';
export * from './hooks';
export * from './icons';
export type * from './types';
export * from './utils';
package.json

 "files": [
    "dist"
  ],
  "main": "./dist/index.js",
  "module": "./dist/index.js",
  "name": "ui-kit",
  "sideEffects": false,
  "type": "module",
  "types": "dist/index.d.ts",

The library is private and I am using it inside my company's internal repo using git+http

Directly importing my components works fine, every module is tree-shaked, but when using Nextjs dynamic import, it pulls all the library

import { Button } from "@ui-kit" => 2kb
const Button = dynamic(() => import('@ui-kit').then(mod => mod.Button), {
  ssr: false,
}); => 148kb

This is my vite config

/* eslint-disable import/no-extraneous-dependencies */
import react from '@vitejs/plugin-react';
import path from 'path';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react(),
    dts({
      include: ['src/core'],
    }),
  ],
  build: {
    target: 'esnext',
    minify: false,
    copyPublicDir: false,
    sourcemap: true,
    lib: {
      entry: './src/core/index.ts',
      formats: ['es'],
      fileName: 'index',
    },
    rollupOptions: {
      output: {
        preserveModules: true,
      },
      external: ['react', 'react-dom', 'date-fns', '@emotion/react', '@emotion/styled'],
    },
  },
  resolve: {
    alias: {
      src: path.resolve(__dirname, './src'),
    },
  },
});

Reproduction

N/A

Steps to reproduce

No response

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 AMD Ryzen 9 5980HX with Radeon Graphics
    Memory: 16.40 GB / 31.41 GB
  Binaries:
    Node: 20.9.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - C:\Program Files\nodejs\yarn.CMD
    npm: 10.7.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (125.0.2535.92)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @vitejs/plugin-react: 4.2.1 => 4.2.1
    vite: 5.2.12 => 5.2.12

Used Package Manager

npm

Logs

No response

Validations

@bluwy
Copy link
Member

bluwy commented Jun 13, 2024

Is this the same as #14145? It's fixed in the 5.3 beta

@SamyZog
Copy link
Author

SamyZog commented Jun 13, 2024

Is this the same as #14145? It's fixed in the 5.3 beta

I am not sure if this is related, correct me if I am wrong, as I understood the mentioned issue talks about building the final bundle, that uses the library, with Vite. Maybe my issue is a configuration issue?

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Jun 16, 2024

when using Nextjs dynamic import, it pulls all the library

This sounds like a Next.js bundler concern and I'm not sure how this relates to Vite library mode.
Do you know if this issues shows up only for ui library built with Vite?

They seem to have this https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading#with-named-exports, but either way, this seems more of Next.js side inconsistency (static named import vs dynamic named import), so you should probably look for help there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants