nx icon indicating copy to clipboard operation
nx copied to clipboard

feat(bundling): added support for declarations (*.d.ts)

Open castleadmin opened this issue 1 year ago • 2 comments

Current Behavior

esbuild doesn't support the creation of declaration files (*.d.ts) and probably never will (see https://github.com/evanw/esbuild/issues/95). Since declaration files are essential for published libraries, it would be great if @nx/esbuild:esbuild would provide an option to output them.

Expected Behavior

  • Introduced a new boolean valued declaration option for the esbuild executor
  • If declaration or the tsconfig option declaration is true, then the TypeScript compiler is used before esbuild to generate the declarations
  • The output directory structure of the declarations can be influenced by setting the TypeScript rootDir via the declarationRootDir option

Related Issue(s)

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

Additional Comment

Please note that the generated declaration files directory structure is affected by the tsconfig rootDir property.

For a library that doesn't reference other libraries inside the monorepo, the rootDir property can be changed freely. If a library inside the monorepo is referenced the rootDir property must be set to the workspace root.

The tsc executor has a sophisticated check that automatically sets the rootDir to the workspace root if a library is referenced. https://github.com/nrwl/nx/blob/master/packages/js/src/executors/tsc/tsc.impl.ts#L104

This check is quite complex and specific to the tsc executor options. Therefore, it hasn't been included inside the esbuild implementation.

The current implementation leaves it to the user to solve the edge case by removing the declarationRootDir option or by setting the declarationRootDir to ..

In the future, it might make sense to generalize and use the tsc executor check.

castleadmin avatar Jan 10 '24 21:01 castleadmin

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nx-dev ✅ Ready (Inspect) Visit Preview May 9, 2024 2:08pm

vercel[bot] avatar Jan 10 '24 21:01 vercel[bot]

Any progress on this? My team is waiting for it.

Afellman avatar Mar 22 '24 20:03 Afellman

Tested this locally, and the tests on CI seem to pass, so I am going to merge.

However, once esbuild is part of the crystal project, this functionality will go away, since we will be relying on the native features of esbuild.

mandarini avatar May 16 '24 14:05 mandarini

Tested this locally, and the tests on CI seem to pass, so I am going to merge.

Thank you

However, once esbuild is part of the crystal project, this functionality will go away, since we will be relying on the native features of esbuild.

What approach would you recommend in the context of Project Crystal?

danielsharvey avatar May 17 '24 00:05 danielsharvey

An esbuild plugin could be a viable alternative to support declaration file builds.

esbuild-plugin-d.ts is listed on the esbuild plugin page and provides the needed functionality.

Another solution that doesn't rely on third-party plugins would be to split the current implementation into 2 commands.

  • Build the declaration files with the help of tsc
  • Build the project with esbuild

If the clearing of the output directory and caching doesn't interfere, then a solution that uses 2 commands should work fine.

castleadmin avatar May 17 '24 05:05 castleadmin

Another solution that doesn't rely on third-party plugins would be to split the current implementation into 2 commands.

  • Build the declaration files with the help of tsc
  • Build the project with esbuild

👍🏻 This is what I currently do (though an OOB integrated solution would be nice!)

danielsharvey avatar May 17 '24 05:05 danielsharvey

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

github-actions[bot] avatar May 25 '24 00:05 github-actions[bot]