Skip to content

[Bug]: Unable to bundle types from sub-directory module of dependency #388

Open
@Asuka109

Description

@Asuka109

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

Metadata

Metadata

Assignees

Labels

api-extractor@microsoft/api-extractor related issue🐞 bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions