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

Feedback on difference with tsup #584

Open
belgattitude opened this issue Oct 8, 2024 · 12 comments
Open

Feedback on difference with tsup #584

belgattitude opened this issue Oct 8, 2024 · 12 comments

Comments

@belgattitude
Copy link

belgattitude commented Oct 8, 2024

Bunchee looks an amazing project. Very easy to use.

You might be interested in some difference I've found out trying to migrate from tsup/esbuild

I've noticed a slightly bigger bundle (as measured by size limit webpack with brotli compression)

image

I'm interested to know if it would be possible to improve the tree-shaking capabilities

And also I was wondering if there's support for browserslist... esbuild support it, I guess swc too

If you want to try out the differences:

The example repo/branch: belgattitude/httpx#1527

cd packages/exception
# build with bunchee 5.5
# "yarn build-tsup" for comparison
yarn build 
# check individual imports with webpack (size-limit.cjs)
yarn check-size
# ecmascript 2020 conformance
yarn check-dist          
@huozhi
Copy link
Owner

huozhi commented Oct 8, 2024

Appreciate the great feedback! I think browserlist is a good feature to support.

For the tree-shaking issue, could elaborate a bit more what exactly not doing here?

@belgattitude
Copy link
Author

belgattitude commented Oct 8, 2024

About tree shaking

I've been doing a test to see the difference between the two outputs... If you check the latest PR you might have some ideas. For the moment idk cause they look incredibly similar.

image

You can always regenerate the dists

cd packages/exception
yarn install
yarn build
yarn build-tsup

The command I use to check for size-limits is

yarn check-size

It's based on https://github.com/ai/size-limit/ with latest webpack enabled. You'll find the current thresholds in the size-limit.cjs file. As an example

  {
    name: "ESM (only HttpNotFound exception)",
    path: ["dist/index.mjs"],
    import: "{ HttpNotFound }",
    limit: "425B",
  },

image

Don't know if I have the talent to dig deeper. But I'd be happy if if the PR helps.

belgattitude/httpx#1527

@belgattitude
Copy link
Author

belgattitude commented Oct 8, 2024

Something that migth be worthy: tsup/esbuil/rollup will group into chunks. That might explain. I remember an option in rollup splitting every file, but I doesn't come for free (disk space, dts...).

My intuition would say, it's not so much related to the chunks but should be a side effect somewhere that prevents webpack to tree-shake the current dist. Maybe some rollup magic might help

@belgattitude
Copy link
Author

About browserslist

I tend to create a file .browserslistrc to not pollute the package.json

# Supported browsers
defaults
chrome >= 96
firefox >= 90
edge >= 91
safari >= 15
ios >= 15
opera >= 77

With tsup I use a plugin

import browserslistToEsbuild from 'browserslist-to-esbuild';
import { defineConfig } from 'tsup';

export default defineConfig((options) => {
  return {
    cjsInterop: false,
    clean: true,
    dts: true,
    entry: [
      'src/index.ts',
      'src/experimental/index.ts',
      'src/serializer/index.ts',
    ],
    bundle: true,
    format: ['esm', 'cjs'],
    minify: false,
    minifyIdentifiers: false,
    minifySyntax: false,
    minifyWhitespace: false,
    outExtension({ format }) {
      return {
        js: `.${format === 'cjs' ? 'cjs' : 'mjs'}`,
      };
    },
    // keepNames: true,
    platform: 'neutral',
    sourcemap: !options.watch,
    splitting: true,
    target: ['es2022', ...browserslistToEsbuild()],
    treeshake: true,
    outDir: './dist-tsup',
    tsconfig: './tsconfig.build.json',
  };
});

@huozhi
Copy link
Owner

huozhi commented Oct 26, 2024

Thanks for the details, sounds like you need codesplitting to ensure each file is small enough? Wonder are you loading it from CDN? cause if still want to bundle the library into a framework, the chunks probably doesn't matter anymore as the application bundler will handle it again, for instance using it in Next.js. But maybe it's a good option to have. I need more information about it to ensure I can add a proper feature to support.

@huozhi
Copy link
Owner

huozhi commented Oct 26, 2024

btw I support the browserslist in package.json in #587 , will that be enough for your case?

@huozhi
Copy link
Owner

huozhi commented Nov 24, 2024

Seems rollup doesn't have a good codesplitting support yet, I'll pause the codesplitting part yet when I found sth useful.

Curious why you need to do the size-limit check? if they're going to be bundled in webpack the chunk splitting could also be different. Or if it's CLI it also doesn't matter

@belgattitude
Copy link
Author

Thank you so much, realised I haven’t replied.

The size limit cli actually import individual modules through webpack. Kind of simulation as I understood it.

I’m not totally sure if it’s reliable totally but as far as I tested it, seems it correspond to what I see in nextjs for example.

To be sure I should try a build with bunch and see how nextjs behave… haven’t found time yet to do so.

In my previous experience with rollup, there was option to emit individual files at the cost of having a larger install size (a file in minimum 4k on a classic file system). That helps the tree shaking abilities.

@huozhi
Copy link
Owner

huozhi commented Nov 24, 2024

Is your library targeting for server side or browser side?

@huozhi
Copy link
Owner

huozhi commented Dec 27, 2024

I noticed there's tree-shaking issue that didn't tree-shake a lot of code. Fixed it in 6.0.4 and published, I think it would solve some of your issues of size 🙏

@huozhi huozhi closed this as completed Dec 27, 2024
@belgattitude
Copy link
Author

Thanks you @huozhi

I just tried with latest 6.1.2... unfortunately it doesn't change. I think the tree shake option in rollup is only relevant when importing external code.

Sorry if I cannot spend time on this... Just letting you know the results 🙏

image

@huozhi huozhi reopened this Jan 4, 2025
@huozhi
Copy link
Owner

huozhi commented Jan 4, 2025

Yea it still bundles into the same chunk I assume that's the main problem. And without having automatically chunk split the chunk the entry will still be overrized in your case. Might need to introduce some automatic chunk splitting strategy like esbuild does. Currently it only supports the shared module which is bit manual. I'll investigate that, thanks for the feedback.

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

No branches or pull requests

2 participants