nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.28k stars 2.32k forks source link

Support building multiple module formats with `tsc` #20010

Open ajwootto opened 10 months ago

ajwootto commented 10 months ago

Description

Executors such as tsc should be able to output a module in ESM or Commonjs according to executor settings in project.json. Similar to the rollup executor, it should support a formats field that also allows "hybrid" packages to be produced by specifying both ESM and commonjs.

Motivation

Currently the only way to make a "hybrid" package with Nx is using the Rollup executor, but it is often the case that a library author doesn't want to bundle the files together when distributing the library. One current reason for "why" is that bundlers remove "use client" directives needed to distribute React client component libraries: https://github.com/nrwl/nx/issues/19714

Another is that bundling should be the responsibility of the consumer of a library, it doesn't really make sense to "pre-bundle" files that only really need to be bundled if they are going into an application bundle, in which case another bundler is already going to be in use.

Also as noted in the TS docs, type checking of source files is only applied using one of the two module systems, and using a 3rd party bundler like Rollup to generate two kinds of output means that only one of them is being type checked. https://www.typescriptlang.org/docs/handbook/modules/guides/choosing-compiler-options.html#notes-on-dual-emit-solutions Using tsc for both kinds of output would allow the build to fail if either of those kinds of output is invalid.

Suggested Implementation

End-user option could look the same as the rollup one, with formats allowing multiple formats to be listed. Under the hood, I'd imagine it would run tsc twice while overriding the "module" setting for one of the builds, and then properly structure the package.json output to be a hybrid package.

Alternate Implementations

potouridisio commented 10 months ago

@ajwootto Excuse me, is there a workaround when using rollup bundler for a next library?

ajwootto commented 10 months ago

@potouridisio not sure, but I filed a separate issue for it

https://github.com/nrwl/nx/issues/19714

github-actions[bot] commented 4 months ago

This issue has been automatically marked as stale because it hasn't had any activity for 6 months. Many things may have changed within this time. The issue may have already been fixed or it may not be relevant anymore. If at this point, this is still an issue, please respond with updated information. It will be closed in 21 days if no further activity occurs. Thanks for being a part of the Nx community! 🙏

ajwootto commented 4 months ago

not stale