跳到正文

包校验(publint 和 attw)

tsdown 集成了 publintAre the types wrong?(attw),用于在发布前校验你的包。这些工具可以检查 package.json 配置和类型定义中的常见问题。

安装

两个工具都是可选依赖 —— 仅在需要使用时安装:

bash
npm install -D publint
bash
npm install -D @arethetypeswrong/core
bash
npm install -D publint @arethetypeswrong/core

publint

publint 检查你的包是否正确配置以供发布。它会验证 package.json 中的 exportsmainmoduletypes 等字段与实际输出文件是否一致。

启用 publint

tsdown.config.ts
ts
import { defineConfig } from 'tsdown'

export default defineConfig({
  publint: true,
})

配置

直接传递选项来自定义 publint 的行为:

tsdown.config.ts
ts
import { defineConfig } from 'tsdown'

export default defineConfig({
  publint: {
    level: 'error', // 'warning' | 'error' | 'suggestion'
  },
})

CLI

bash
tsdown --publint

attw(Are the types wrong?)

attw 验证你的 TypeScript 声明文件在不同模块解析策略(node10node16bundler)下是否正确。它能捕获错误的 ESM/CJS 类型声明等问题,这些问题可能导致使用者遇到运行时错误。

启用 attw

tsdown.config.ts
ts
import { defineConfig } from 'tsdown'

export default defineConfig({
  attw: true,
})

配置

tsdown.config.ts
ts
import { defineConfig } from 'tsdown'

export default defineConfig({
  attw: {
    // 解析配置:
    //   'strict'   - 要求所有解析方式通过(默认)
    //   'node16'   - 忽略 node10 解析失败
    //   'esm-only' - 忽略 CJS 解析失败
    profile: 'node16',

    // 级别:'warn'(默认)或 'error'(导致构建失败)
    level: 'error',

    // 忽略特定的问题类型
    ignoreRules: ['false-cjs', 'cjs-resolves-to-esm'],
  },
})

配置(Profiles)

配置说明
strict要求所有解析方式通过(默认)
node16忽略 node10 解析失败
esm-only忽略 node10node16-cjs 解析失败

忽略规则

你可以通过 ignoreRules 来抑制特定的问题类型:

规则说明
no-resolution模块无法解析
untyped-resolution解析成功但没有类型
false-cjs类型标注为 CJS 但实现是 ESM
false-esm类型标注为 ESM 但实现是 CJS
cjs-resolves-to-esmCJS 解析指向 ESM 模块
fallback-condition使用了 fallback/通配符条件
cjs-only-exports-defaultCJS 模块仅导出默认值
named-exports类型与实现的命名导出不匹配
false-export-default类型声明了不存在的默认导出
missing-export-equalsCJS 类型缺少 export =
unexpected-module-syntax文件使用了意外的模块语法
internal-resolution-error类型检查中的内部解析错误

CLI

bash
tsdown --attw

CI 集成

publintattw 都支持 CI 感知选项。这适用于仅在 CI 中运行包校验:

tsdown.config.ts
ts
import { defineConfig } from 'tsdown'

export default defineConfig({
  publint: 'ci-only',
  attw: {
    enabled: 'ci-only',
    profile: 'node16',
    level: 'error',
  },
})

NOTE

两个工具都需要项目目录中存在 package.json。如果未找到 package.json,会记录警告并跳过检查。

Released under the MIT License.