isaacs / tshy

Other
894 stars 18 forks source link

How to use bin option in package.json with tshy #68

Closed HermansJarno closed 5 months ago

HermansJarno commented 5 months ago

Hi,

I want to use the bin option of package.json to create a custom command. How can i combine with tshy?

Is there a simple option or will i have to keep the bin script away from the src, and check in the bin script if it is esm or commonjs project based on package.json type module?

HermansJarno commented 5 months ago

I checked the rimraf implementation. Closing issue.

HermansJarno commented 5 months ago

Nevermind, not sure if this is going to have the desired result with the rimraf example since it is mjs only.

isaacs commented 5 months ago

I recommend having the bin script be either ESM or CJS, but not both. (Ie, src/bin.cts or src/bin.mts, depending on your preference.)

Then just set the bin in package.json to the build location, like "bin": "./dist/esm/bin.mjs" (or "bin": "./dist/commonjs/bin.cjs" if you like prefer to write it in CommonJS dialect.)

A bin doesn't need to be loaded in multiple ways, since it's executed by node directly, so there's no need to build it in all dialects. Naming it .cts or .mjs will make it only available in the CommonJS or ESM dialects, respectively.