developit / developit/microbundle

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

Đang mở
#1,094 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
JavaScript
Star
8.1k
Fork
358
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

Reproduce the issue with the package.json example using `microbundle --target node -f esm --generateTypes=true`, then compare the result with `tsc`. Inspect how the build handles the package.json `exports`, `main`, `module`, and `types` fields alongside the tsconfig.json `resolvePackageJsonExports` and `moduleResolution` settings. Done means conditional exports resolve the declared types without requiring a root `types` field.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, nodejs, rollup, typescript
Lĩnh vực
build-system, tooling
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.