developit / developit/microbundle

package.json types/main/module take precedence over conditional exports

未關閉
#1,094 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
JavaScript
星號
8.1k
分支
358
PR 合併指標
30 天內沒有已合併 PR

描述

Possibly somewhat related to #1085.

Given a package.json:

```json
{
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"type": "module",
"dependencies": {
"some-client-with-matching-esm-and-tsconfig": "^x.x.x"
},
"exports": {
".": {
"require": {
"types": "./lib/index.d.ts",
"default": "./lib/index.cjs"
},
"import": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
}
},
"./package.json": "./package.json"
},
```
_without root `types` property, running:

```sh
microbundle --target node -f esm --generateTypes=true
```

results in error:

> (rpt2 plugin) Error: /Users/xxxx/dev/project/dependingclient.ts(197,32): semantic error TS2339: Property 'request' does not exist on type 'DependingClient.ts'.
> at RollupContext.error (/Users/xxxx/dev/project/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:20259:3

Indicating it cannot find the types specified in the conditional exports.

**Working solution**: Specify `"types": "./lib/index.d.ts"` at root-level.
**Also works**: Remove the `"main"` and `module` property

It seems to me package.json conditional exports are not taking precedence over main/module/types

Not sure if related but when in tsconfig.json `resolvePackageJsonExports` is explicitly set to `true` (even weirder is that it would be true by default in node16 moduleresolution) , regardless of whether `moduleResolution` is set to node16, it throws
> rpt2: options error TS5098: Option 'resolvePackageJsonExports' can only be used when 'moduleResolution' is set to 'node16', 'nodenext', or 'bundler'. Whereas running the build simple with `tsc` causes no issues.

貢獻指南

這個儲存庫沒有索引到貢獻指南

研究方向

使用 `microbundle --target node -f esm --generateTypes=true` 透過 package.json 範例重現問題,然後將結果與 `tsc` 比較。檢查建置如何處理 package.json 中的 `exports`、`main`、`module` 和 `types` 欄位,以及 tsconfig.json 中的 `resolvePackageJsonExports` 和 `moduleResolution` 設定。當條件式匯出能夠解析已宣告的型別,而不需要根層級的 `types` 欄位時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
javascript, nodejs, rollup, typescript
領域
build-system, tooling
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。