# ts-dual-module

> Easily build dual module packages (esm + cjs) using typescript

Latest version **0.6.3** (published 2024-03-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-dual-module
pnpm add ts-dual-module
yarn add ts-dual-module
bun add ts-dual-module
```

Provides the command `tsmod`.

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.3 |
| Published | 2024-03-27 |
| First published | 2024-02-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 29.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Bogdan Stefanescu |
| Maintainers | bstefanescu |
| Keywords | typescript, build, exports, tsc, esm, cjs, dual-module |

## Links

- npm: https://www.npmjs.com/package/ts-dual-module
- Repository: https://github.com/bstefanescu/ts-dual-module
- Issues: https://github.com/bstefanescu/ts-dual-module/issues
- npm.io page: https://npm.io/package/ts-dual-module

## Dependencies (1)

- [enquirer](https://npm.io/package/enquirer.md) ^2.4.1

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 0.6.3 (latest) — 2024-03-27
- 0.6.2 — 2024-02-12
- 0.6.1 — 2024-02-09
- 0.6.0 — 2024-02-09
- 0.5.0 — 2024-02-08
- 0.4.3 — 2024-02-08
- 0.4.2 — 2024-02-08
- 0.4.1 — 2024-02-08
- 0.4.0 — 2024-02-08
- 0.3.0 — 2024-02-08

## README

# ts-dual-module
Easily build ts projects as dual packaged modules (cjs+esm)

The tool is designed to work on ESM packages (type: "module") and will build both ESM and commonjs versions of your code using typescript. 
The package.json exports is automatically updated to match your build.

A dual module project is usually storing the source files in a directoy like `./src`` and the generated files in a directory like `./dist`. The minimal layout of a projet is:

```
./dist
./src
package.json
tsconfig.json
```

The first time the build is done (or if you run `npx tsmod init`) you will be asked for the oputput directory name where the generated files will be stored.

You must create a `./tsconfig.json` file in the project root, which will be used by the tool to as the base tsc configuration.  If you don't have one just run `npx tsmod init` and it will create one for you. 

If you need to customize the `tsconfig.json` file differently for esm or cjs you can create a specific files for each build: `tsconfig.esm.json` and `tsconfig.cjs.esm` hich will take precendence over `tsconfig.json`.

To start a build run: `npx tsmod build`. You can pass any additional tsc options you need.

### Sub-paths exports

Also, sub-paths exports are supported (this is not working by default with commonjs builds because typescript is only handling subpath exports if moduleResolution >= Node16).

The tool is fxing this by mapping types using a `typesVersions` entry in the package.json

See:
* https://stackoverflow.com/questions/76236503/not-able-to-get-typescript-definitions-working-when-using-subpath-exports
* https://www.npmjs.com/package/typescript-subpath-exports-workaround

To add an export path run: `npx tsmod export mypath path/to/out/file.js`

---
_Source: https://npm.io/package/ts-dual-module · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
