kentcdodds / mdx-bundler

🦤 Give me MDX/TSX strings and I'll give you back a component you can render. Supports imports!
MIT License
1.78k stars 75 forks source link

Tree shaking not working when importing from a component library inside a MDX file #156

Open jimzer opened 2 years ago

jimzer commented 2 years ago

First of all, thank you for this awesome bundler!

Relevant code or config

I use Next JS, here is my bundler configuration

  const { code, frontmatter } = await bundleMDX({
    file: path.resolve(path.join(postPath, 'index.mdx')),
    //cwd: path.resolve(postPath),
    cwd: process.env.PWD,
    esbuildOptions(options) {
      // options.minify = false
      options.target = ['esnext']
      options.platform = 'node'
      options.treeShaking = true
      return options
    },
    xdmOptions(options) {
      options.remarkPlugins = [...(options.remarkPlugins ?? []), remarkMath]
      options.rehypePlugins = [...(options.rehypePlugins ?? []), rehypeMathjax]
      return options
    },
  })

What you did:

I import a library of components into my MDX page like so:

import { Box } from '@components/core'

What happened:

The whole @components/core library is bundled into my web page.

Problem description:

I expected that tree shaking would eliminate all the unused exported components. Is it normal that it did not work? Maybe it's not possible to get the MDX content tree shaked?

FradSer commented 2 years ago

Your question is very similar to https://github.com/kentcdodds/mdx-bundler/issues/165 , you should check it.

joe-bell commented 2 years ago

I've actually noticed that I have a similar experience with local components – unused ones aren't eliminated from the resulting bundle