esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

Shared code between multiple entrypoints

Open aladinflux opened this issue 3 years ago • 1 comments

We are building a package (cjs + esm) so we can't use splitting. We have 2 entry points, each one depends on a config file which contains an update-able config variable.

The problem we're facing now is that when we call setConfig imported from entry point A, it updates the config, but the code from entry point B won't be able to consume that new config, beach each generated entry point will have its own copy of the config file.

How do we make the config file a single instance between the 2 entry points?

aladinflux avatar Jun 06 '22 01:06 aladinflux

Code splitting is still very experimental in esbuild and it only supports esm. There is a tsup/cjs-splitting to polyfill it in this way:

  1. Bundle the package with format: 'esm', splitting: true, now you get many esm files.
  2. Transform these files to cjs individually with sucrase.

This simple workaround won't suit every situation.

hyrious avatar Jun 06 '22 01:06 hyrious