-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Hey,
I know the CommonJS wrappers/helpers code has come up in a few discussions & that the gist of your position is that it's all needed to properly emulate ESM exports behavior.
While I certainly understand that viewpoint and that need -- and while I agree it should probably remain as the default behavior -- I think it would be worthwhile to add a "loose"/non-strict mode that produces the CJS variant using solely the module.exports = { foo, bar }
form. Even though this allows the exports to be mutable, this has a few benefits:
- For library authors, this allows the module to be smaller -- especially if/when there's no concern of mutation.
- For library consumers, existing Rollup/webpack/Parcel systems can treeshake the exports. (I'm 90% sure that this is only done thru static analysis, which means esbuild's runtime helpers can't be understood.)
- This allows consumers/authors to incorporate mutation as part of their CJS code, which has been possible for all of Node's timeline.
Separately, this strictness could be applies to imports too.
I've noticed that importing library code (which only has CJS) into my bundle: true, format: esm, splitting: false
build will include all of the CJS helper code even though the library may just have 2 simple function exports.
I was initially going to request that the exports be statically analyzed and inlined (matching Rollup's commonjs plugin behavior), but I think that's a big ask -- and I think esbuild is already capable of this if it wasn't auto-applying a glass box around any CJS code.