Skip to content

fix: add "types" field to subpath exports map generation - #307

Closed
YangJonghun wants to merge 7 commits into
rolldown:mainfrom
YangJonghun:fix/fix-subpath-exports-map
Closed

YangJonghun wants to merge 7 commits into
rolldown:mainfrom
YangJonghun:fix/fix-subpath-exports-map

Conversation

@YangJonghun

@YangJonghun YangJonghun commented Jun 8, 2025 •

Copy link
Copy Markdown

Description

When developers generate a bundle with multiple entry points, tsdown currently does not automatically generate the "types" field for subpath exports.

For example, if a developer provides the following entries:

["src/index.ts", "src/foo.ts"]

tsdown should generate the following exports map:

"exports": {
  ".": {
    "import": {
      "types": "./index.d.ts",
      "default": "./index.js"
    },
    "require": {
      "types": "./index.d.cts",
      "default": "./index.cjs"
    }
  },
  "./foo": {
    "import": {
      "types": "./foo.d.ts",
      "default": "./foo.js"
    },
    "require": {
      "types": "./foo.d.cts",
      "default": "./foo.cjs"
    }
  },
  "./package.json": "./package.json",
},
"main": "./index.cjs",
"module": "./index.js",
"types": "./index.d.cts",

Linked Issues

closes #253

Additional context

@netlify

netlify Bot commented Jun 8, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for tsdown ready!

Name Link
🔨 Latest commit 06aa373
🔍 Latest deploy log https://app.netlify.com/projects/tsdown/deploys/684c360456e0640008a46264
😎 Deploy Preview https://deploy-preview-307--tsdown.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Comment thread src/features/exports.ts Outdated
}
if (es) value[dualFormat ? 'import' : 'default'] = es
if (cjs) value[dualFormat ? 'require' : 'default'] = cjs
if (cjsTypes || esmTypes) value.types = cjsTypes || esmTypes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

types should precede import or require.

Comment thread src/features/exports.ts Outdated
if (chunk.facadeModuleId && !subExport.src) {
subExport.src = `./${slash(path.relative(pkgRoot, chunk.facadeModuleId))}`
}
} else if (!isIndex && (format === 'cjs' || format === 'es')) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only add types for non-index?

@sxzz

sxzz commented Jun 8, 2025

Copy link
Copy Markdown
Member

I think we'd better to have an option for this types generation, see #253 (comment)

@YangJonghun

YangJonghun commented Jun 9, 2025 •

Copy link
Copy Markdown
Author

@sxzz
Thank you for quick review. I've updated the code to separate the require and import cases, and reorder the "types" field to be declared earlier.

Comment thread src/features/exports.test.ts Resolved
},
"./package.json": "./package.json",
},
".": "./foo.js",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added dts with single entry test, and types field should be added here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense for the "types" setting to affect the root-level types too?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, setting exports.types itself doesn't make much difference, since TypeScript will look for .d.ts files based on the .js entry. However, explicitly specifying it may improve resolution speed. Regardless, since we have this option, we should set it for the main entry as well to maintain consistency.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your explanation.
If there's anything else I need to change, please feel free to let me know.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean types field should be added here.

@pkg-pr-new

pkg-pr-new Bot commented Jun 24, 2025

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/tsdown@307

commit: 06aa373

@osnoser1-slalom

osnoser1-slalom commented Jul 17, 2025 •

Copy link
Copy Markdown

I'm waiting this feature, it is really helpful, thank you for this work @YangJonghun and @sxzz

@sxzz
sxzz force-pushed the main branch 2 times, most recently from 13a67c0 to 8171b96 Compare August 10, 2025 00:54
@sxzz
sxzz force-pushed the main branch 3 times, most recently from 04e9d3d to 3b05923 Compare September 27, 2025 04:22
@sxzz
sxzz force-pushed the main branch 9 times, most recently from fc57efe to 4818111 Compare November 11, 2025 08:09
@sxzz
sxzz force-pushed the main branch 2 times, most recently from ecc60c6 to 597d921 Compare November 12, 2025 21:28
@sxzz
sxzz force-pushed the main branch 5 times, most recently from 10421f2 to 85c0e55 Compare December 1, 2025 17:36
@jadejr

jadejr commented Dec 5, 2025

Copy link
Copy Markdown

Will it be easy to add custom conditions to this later (not necessarily in this PR)? That's what I (currently) use to get live types. I've been managing it manually, but it'd be cool if tsdown could do it for me.

@sxzz

sxzz commented Dec 5, 2025

Copy link
Copy Markdown
Member

Closing this as stale, and types field can be omitted, and customized https://tsdown.dev/options/package-exports#customizing-exports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

when exports: true, subExport doesn't include type definition

4 participants