web-infra-dev / web-infra-dev/rslib

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

Open
#388 2 comments 0 reactions 1 assignee View on GitHub

@Timeless0911 is already working on this.

Since Nov 8, 2024.

🐞 bug api-extractor
Dominant language
TypeScript
Stars
1k
Forks
67
Avg merge
6h 9m
Merged PRs (30d)
57

Description

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/rsbuild-plugin-dts@0.0.16_@microsoft+api-extractor@7.47.11_@rsbuild+core@1.0.19_typescript@5.4.2/node_modules/rsbuild-plugin-dts/dist/tsc.js:104:23)
    at async generateDts (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/rsbuild-plugin-dts@0.0.16_@microsoft+api-extractor@7.47.11_@rsbuild+core@1.0.19_typescript@5.4.2/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/rsbuild-plugin-dts@0.0.16_@microsoft+api-extractor@7.47.11_@rsbuild+core@1.0.19_typescript@5.4.2/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/rsbuild-plugin-dts@0.0.16_@microsoft+api-extractor@7.47.11_@rsbuild+core@1.0.19_typescript@5.4.2/node_modules/rsbuild-plugin-dts/dist/tsc.js:104:23)
    at async generateDts (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/rsbuild-plugin-dts@0.0.16_@microsoft+api-extractor@7.47.11_@rsbuild+core@1.0.19_typescript@5.4.2/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/rsbuild-plugin-dts@0.0.16_@microsoft+api-extractor@7.47.11_@rsbuild+core@1.0.19_typescript@5.4.2/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/rsbuild-plugin-dts@0.0.16_@microsoft+api-extractor@7.47.11_@rsbuild+core@1.0.19_typescript@5.4.2/node_modules/rsbuild-plugin-dts/dist/index.js:61:57)
    at Object.call (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/@rsbuild+core@1.0.19/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/@rsbuild+core@1.0.19/node_modules/@rsbuild/core/dist/index.js:5613:9)
    at async Object.fn (file:///Users/bytedance/repositories/repro-rslib-bundle-typings/node_modules/.pnpm/@rsbuild+core@1.0.19/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.