esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

[BUG] The interaction between esm and cjs when sideEffects is `false`

Open bvanjoi opened this issue 3 years ago • 1 comments

When the sideEffects of the library in node_module is false, there may be some problems with the interaction between esm and CommonJS, which is a bit tedious to describe, please see the demo.

How to reproduce:

  1. git clone https://github.com/bvanjoi/esbuild-bug-demo-under-sideEffects.git
  2. cd esbuild-bug-demo-under-sideEffects && node index.js.

Results show:

image

bvanjoi avatar Jun 17 '22 16:06 bvanjoi

Thanks for the report. I am able to reproduce the issue. However, I'm not immediately sure what the right fix is because I haven't touched this code in a long time. A workaround for now could be to ignore the sideEffects annotation.

Edit: For my own reference, this is a simple end-to-end test that reproduces the issue:

test(['--bundle', 'entry.js', '--outfile=node.js'], {
  'entry.js': `import { foo } from 'pkg'; if (foo.bar !== 123 || require('./foo').foo.bar !== 123) throw 'fail'`,
  'foo.js': `export let foo = { bar: 123 }`,
  'node_modules/pkg/index.js': `export * from '../../foo'`,
  'node_modules/pkg/package.json': `{ "sideEffects": false }`,
}),

evanw avatar Jun 18 '22 22:06 evanw