@better-builds/ts-duality

中文文档
1.3.2发布于 2026/01/22GPL-3.0-onlyTypeScriptESM + CJS近 30 天下载 7266

A no BS, easy-to-use utility for compiling TypeScript code to both ESM and CJS targets, as well as properly wiring up package.json exports.

原始使用文档
文档目录6 节
  1. Get Started
  2. Installation
  3. API
  4. CLI
  5. JavaScript API
  6. Contributing

ts-duality

A slightly-opinionated but no BS, easy-to-use utility for compiling TypeScript code to both ESM and A no BS, easy-to-use utility for compiling TypeScript code to both ESM and CJS targets, as well as properly wiring up package.json exports.

ts-duality will do the whole-song-and-dance for you, and by default, it will render to both CJS and ESM. You can optionally target one-specific output, or you can just compile without generating typings.

ts-duality will also render CJS and ESM modules with their correct .cjs and .esm formats, respectively, while ensuring that any relative imports used in your code are rewritten to also point to .cjs and .esm files.

Every file that TypeScript detects as part of your source will be automatically added to your package.json#exports, and if there is a an index file, this will be linked as your package.json#main and package.json#module.

For an example of what this looks like, checkout the package.json file for ts-duality. ts-duality is compiled by itself 🔁.

Get Started

Installation

Install ts-duality with your favorite package manager:

npm

npm i @better-builds/ts-duality --save-dev

pnpm

pnpm i @better-builds/ts-duality -D

yarn

yarn add @better-builds/ts-duality --dev

bun

bun add @better-builds/ts-duality -d

API

CLI

Run npx ts-duality (or your equivalent for your package manager of choice) to kick off a build.

To see the help menu and all of its options, run npx ts-duality --help:

Options:
  --version            Show version number                             [boolean]
  --clean              if set, will clean out the build dirs before compiling
                       anything                       [boolean] [default: false]
  --copyOtherFiles     if true, will copy any non source files (anything that
                       doesn't end with .js, .jsx, .cjs, .mts, .ts or .tsx)
                       to the output folder, while maintining the location of
                       the files
                       to match where they were in your source folder
                                                      [boolean] [default: false]
  --cwd                the CWD to use when building
       [string] [default: "/Users/benjaminduran/dddddddd/opensource/ts-duality"]
  --jsx                the type of JSX runtime to use when compiling your code
   [string] [choices: "automatic", "classic", "preserve"] [default: "automatic"]
  --noCjs              if true, will not build the CommonJS variant of this
                       package                        [boolean] [default: false]
  --noDts              if set, will not write typescript typings
                                                      [boolean] [default: false]
  --noEsm              if true, will not build the ESM variant of this package
                                                      [boolean] [default: false]
  --noGenerateExports
                        if true, will NOT modify your package.json file
                        to generate main, module and exports fields.

                        Use this if you just want a simple transpiler that
                        strips your typescript types.
                                                      [boolean] [default: false]
  --noStripLeading     if set, will not strip the leading, last common portion
                       of your input file paths when writing output file paths.
                       if your code is located in a "src/" folder, you want to
                       leave this unset.
                       NOTE: this does *not* affect how typescript compiles
                       typings, so if your tsconfig#compilerOptions#rootDir is
                       misconfigured,
                       or you are mixing roots from across your package, your
                       typings might end up in a different folder than you
                       expect                         [boolean] [default: false]
  --outDir             the folder where the built files will be written
                                                      [string] [default: "dist"]
  --tsconfig           if provided, will explicitly use this tsconfig.json
                       location instead of searching for a tsconfig.build.json
                       or a plain tsconfig.json                         [string]
  --watch              if set, will automatically watch for any changes to this
                       library and rebuild, making it easier for you to consume
                       changes in the monorepo while doing local development
                                                      [boolean] [default: false]
  --help               Show help                                       [boolean]

JavaScript API

If you would prefer to interact with ts-duality in a JavaScript or TypeScript file, you can import its API as follows:

javascript
import { buildTsPackage } from '@better-builds/ts-duality';

await buildTsPackage({ /* accepts the same options as the CLI */ });

Contributing

Ensure you have the proper versions of the tools installed in your environment. You can see the versions in the .tool-versions file.

If you prefer, you can use the mise utility to quickly get started by simply cloning this repository, then running mise x -- bun ci to install all dependencies.

Once you have them installed, simply run bun ci to get started developing in this project.

版本与发布记录

@better-builds/ts-duality 共发布 48 个版本,首次发布于 2025/10/28;过去 12 个月发布了 18 个版本。

版本发布日期距今
1.3.2latest2026/01/229 个月前
1.3.2-beta.1beta2026/01/229 个月前
1.3.12026/01/219 个月前
1.3.02026/01/199 个月前
1.2.52026/01/149 个月前
1.2.32026/01/149 个月前

关于 @better-builds/ts-duality 的常见问题

@better-builds/ts-duality 是什么?

A no BS, easy-to-use utility for compiling TypeScript code to both ESM and CJS targets, as well as properly wiring up package.json exports.

如何安装 @better-builds/ts-duality?

在项目目录运行 npm install @better-builds/ts-duality,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(^5)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 ts-duality,可以用 npx ts-duality 直接运行。

@better-builds/ts-duality 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@better-builds/ts-duality 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@better-builds/ts-duality 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.14.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@better-builds/ts-duality 可以商用吗?

@better-builds/ts-duality 使用 GPL-3.0-only 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@better-builds/ts-duality 的下载量有多少?

根据 npm 官方下载统计,@better-builds/ts-duality 在 2026/10/08 – 2026/11/06 这 30 天内被下载 7,266 次,日均约 242 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@better-builds/ts-duality 还在维护吗?

最新版本 1.3.2 发布于 2026/01/22(9 个月前);至今共发布 48 个版本,其中过去 12 个月发布了 18 个;首次发布于 2025/10/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。