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]: Unable to bundle types from sub-directory module of dependency #388

Open
Asuka109 opened this issue Nov 8, 2024 · 2 comments
Open
Assignees
Labels
🐞 bug Something isn't working

Comments

@Asuka109
Copy link

Asuka109 commented Nov 8, 2024

Version

  System:
    OS: macOS 15.0
    CPU: (10) arm64 Apple M1 Pro
    Memory: 308.09 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Safari: 18.0
  npmPackages:
    @rslib/core: ^0.0.16 => 0.0.16

Details

There are two entries for comparing the differences of bunding types between root and sub-directory.

src/foo.ts

import { ClientDefinition } from 'kit';

export interface Foo extends ClientDefinition {
  bar: string;
}

export const foo: Foo = {} as any;

src/bar.ts

import { ClientDefinition } from 'kit/node';

export interface Foo extends ClientDefinition {
  bar: string;
}

export const foo: Foo = {} as any;

Actually, both of kit and kit/node exports same stuff.

After running rslib build we will get:

dist/foo.d.ts

declare interface ClientDefinition {
    name: string;
}

export declare interface Foo extends ClientDefinition {
    bar: string;
}

export declare const foo: Foo;

export { }

dist/bar.d.ts

import { ClientDefinition } from 'kit/node';

export declare interface Foo extends ClientDefinition {
    bar: string;
}

export declare const foo: Foo;

export { }

I thought that dist/foo.d.ts should be a correct output but dist/bar.d.ts not. I expect both them should bundle all the types from kit and without any import statements.

When I try to build again after removing typesVersions from kit/package.json, there are something getting error:

error   Failed to emit declaration files. (cjs0)
error   /Users/bytedance/repositories/repro-rslib-bundle-typings/src/bar.ts:1:34 - error TS2307: Cannot find module 'kit/node' or its corresponding type declarations.
  There are types at '/Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/kit/dist/node.d.ts', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'.
error   DTS generation failed
    at emitDts (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/rsbuild-plugin-dts/dist/tsc.js:104:23)
    at async generateDts (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/rsbuild-plugin-dts/dist/dts.js:116:5)
    at async process.<anonymous> (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/rsbuild-plugin-dts/dist/dts.js:130:9)
error   Failed to emit declaration files. (cjs1)
error   /Users/bytedance/repositories/repro-rslib-bundle-typings/src/bar.ts:1:34 - error TS2307: Cannot find module 'kit/node' or its corresponding type declarations.
  There are types at '/Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/kit/dist/node.d.ts', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'.
error   DTS generation failed
    at emitDts (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/rsbuild-plugin-dts/dist/tsc.js:104:23)
    at async generateDts (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/rsbuild-plugin-dts/dist/dts.js:116:5)
    at async process.<anonymous> (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/rsbuild-plugin-dts/dist/dts.js:130:9)
error   Failed to build.
error   Error occurred in cjs0 DTS generation
    at handler (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/rsbuild-plugin-dts/dist/index.js:61:57)
    at Object.call (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/@[email protected]/node_modules/@rsbuild/core/dist/index.js:5445:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async onDone (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/@[email protected]/node_modules/@rsbuild/core/dist/index.js:5613:9)
    at async Object.fn (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/@[email protected]/node_modules/@rsbuild/core/dist/index.js:5559:15)

So I modify tsconfig.json according to the logs.

  {
    "compilerOptions": {
      "lib": ["DOM", "ESNext"],
      "allowJs": true,
-     "module": "commonjs",
+     "module": "Preserve",
      "strict": true,
      "esModuleInterop": true,
      "skipLibCheck": true,
      "forceConsistentCasingInFileNames": true,
      "experimentalDecorators": true,
      "resolveJsonModule": true,
-     "moduleResolution": "node",
+     "moduleResolution": "Bundler",
      "target": "ES2019",
      "declaration": true,
      "outDir": "./dist",
      "jsx": "preserve",
      "baseUrl": "./",
      "isolatedModules": true
    },
    "include": ["src"]
  }

It works for me. Not sure about is it an expected behaviour?

Reproduce link

https://github.com/Asuka109/repro-rslib-bundle-typings

Reproduce Steps

  1. pnpm install
  2. pnpm build
@Asuka109 Asuka109 added the 🐞 bug Something isn't working label Nov 8, 2024
@Timeless0911
Copy link
Collaborator

Timeless0911 commented Nov 11, 2024

Set moduleResolution to node16 or nodenext and the same as module can also resolve this issue.

The typesVersions field seems not to be recognized by api-extractor which is different from tsserver.

@Timeless0911
Copy link
Collaborator

tracking microsoft/rushstack#5017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants