Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: generated .d.ts not handle imports' extensions correctly #678

Open
Jungzl opened this issue Jan 13, 2025 · 4 comments
Open

[Bug]: generated .d.ts not handle imports' extensions correctly #678

Jungzl opened this issue Jan 13, 2025 · 4 comments

Comments

@Jungzl
Copy link

Jungzl commented Jan 13, 2025

Version

System:                                                                                                                                                                                 
    OS: Windows 11 10.0.26100                                                                                                                                                             
    CPU: (24) x64 13th Gen Intel(R) Core(TM) i7-13700                                                                                                                                     
    Memory: 2.34 GB / 15.77 GB                                                                                                                                                            
  Browsers:
    Edge: Chromium (131.0.2903.63)
    Internet Explorer: 11.0.26100.1882
  npmPackages:
    @rslib/core: ^0.3.1 => 0.3.1

Details

dist/es/index.d.ts

expected:

export { foo } from './foo.js';

actual:

export { foo } from './foo';

dist/lib/index.d.cts

expected:

export { foo } from './foo.cjs';

actual:

export { foo } from './foo';

Reproduce link

https://github.com/Jungzl/rslib-project-issue-demo/tree/issue/dts

Reproduce Steps

  1. pnpm build
  2. pnpm attw
🥴 Import found in a type declaration file failed to resolve. Either this indicates that runtime resolution errors will occur, or (more likely) the types misrepresent the contents of the
 JavaScript files. Use -f json to see the imports that failed to resolve. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/InternalResolutionError.md


┌───────────────────┬──────────────────────────────┬──────────────────────────────┐
│                   │ "rslib-project""rslib-project/package.json" │
├───────────────────┼──────────────────────────────┼──────────────────────────────┤
│ node10            │ 🥴 Internal resolution error │ 🟢 (JSON)                    │
├───────────────────┼──────────────────────────────┼──────────────────────────────┤
│ node16 (from CJS) │ 🥴 Internal resolution error │ 🟢 (JSON)                    │
├───────────────────┼──────────────────────────────┼──────────────────────────────┤
│ node16 (from ESM) │ 🥴 Internal resolution error │ 🟢 (JSON)                    │
├───────────────────┼──────────────────────────────┼──────────────────────────────┤
│ bundler           │ 🟢                           │ 🟢 (JSON)                    │
└───────────────────┴──────────────────────────────┴──────────────────────────────┘

after manually add extensions from above:

 No problems found 🌟


┌───────────────────┬─────────────────┬──────────────────────────────┐
│                   │ "rslib-project""rslib-project/package.json" │
├───────────────────┼─────────────────┼──────────────────────────────┤
│ node10            │ 🟢              │ 🟢 (JSON)                    │
├───────────────────┼─────────────────┼──────────────────────────────┤
│ node16 (from CJS) │ 🟢 (CJS)        │ 🟢 (JSON)                    │
├───────────────────┼─────────────────┼──────────────────────────────┤
│ node16 (from ESM) │ 🟢 (ESM)        │ 🟢 (JSON)                    │
├───────────────────┼─────────────────┼──────────────────────────────┤
│ bundler           │ 🟢              │ 🟢 (JSON)                    │
└───────────────────┴─────────────────┴──────────────────────────────┘
@Timeless0911
Copy link
Contributor

This is a very complex topic.

Rslib generate DTS files as same as tsc, and Typescript would rewrite import * as foo from "./foo.ts" to import * as foo from "./foo.js" after TS 5.7 when emitting JS output files, see https://devblogs.microsoft.com/typescript/announcing-typescript-5-7/#path-rewriting-for-relative-paths.

But for DTS files, Typescript do not do such rewrite now.

Adding .ts and .cts extension for DTS files by Rslib is an optional choice, but this feature is better to have in Typescript when tsc generates DTS files. And we are glad to see the future plan of Typescript.

@Jungzl
Copy link
Author

Jungzl commented Jan 14, 2025

Thanks for your reply, I previously used tsup with the esbuild-plugin-file-path-extensions plugin, but encountered slow compilation times. So I tried to migrate to rslib. Seems like I need to write a plugin to modify output files by now.

@Timeless0911
Copy link
Contributor

Timeless0911 commented Jan 14, 2025

I remember that this plugin can only deal with Javascript output files. For DTS files, tsup use rollup-plugin-dts to bundle DTS files into one file which will not encounter this issue.

Currently, in Rslib, you can write an Rsbuild plugin to modify DTS files after emitting:

import type { RsbuildPlugin } from '@rsbuild/core';
import { defineConfig } from '@rslib/core';

const pluginFixDts: RsbuildPlugin = {
  name: 'fix-dts',
  setup(api) {
    api.onAfterBuild({
      handler: () => {
        // ...
      },
      // Set the order to 'post' to ensure that DTS files emitted after the build is complete.
      order: 'post',
    });
  },
};

export default defineConfig({
  plugins: [pluginFixDts]
  lib: [
    // ...
  ],
});

BTW, for dual packages, I prefer to provide one piece of DTS exports, you can refer @rsbuild/core for references and we have a workaround for making TypeScript work with moduleResolution node16+.

@Jungzl
Copy link
Author

Jungzl commented Jan 14, 2025

for dual packages, I prefer to provide one piece of DTS exports

I also think the double DTS file is redundant. I'll give it a try.

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

No branches or pull requests

2 participants