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

Mismatch Between Type Declaration File Names and types Field in Generated package.json #29308

Open
1 of 4 tasks
ashkanhosseini opened this issue Dec 11, 2024 · 0 comments · May be fixed by #29366
Open
1 of 4 tasks

Mismatch Between Type Declaration File Names and types Field in Generated package.json #29308

ashkanhosseini opened this issue Dec 11, 2024 · 0 comments · May be fixed by #29366

Comments

@ashkanhosseini
Copy link

ashkanhosseini commented Dec 11, 2024

Current Behavior

When generating a publishable library using the @nx/js generator with the Rollup bundler, the nx build command produces a type declaration file (index.cjs.d.ts) that is not consistent with the types field in the generated package.json.

Specifically:

  1. The Rollup config generates index.cjs.d.ts in the dist directory.
  2. The types field in package.json is set to ./dist/index.d.ts.
  3. Similarly, the exports section refers to ./dist/index.d.ts for the types key
"exports": {
  "./package.json": "./package.json",
  ".": {
    "types": "./dist/index.d.ts",
    "import": "./dist/index.esm.js",
    "default": "./dist/index.cjs.js"
  },
"types": "./dist/index.d.ts"
}

As a result, when the package is consumed outside the workspace, TypeScript fails to locate the type declarations.

Error example:

Could not find a declaration file for module '@myorg/example'. 
Module implicitly has an 'any' type because 'index.cjs.d.ts' does not match the 'types' field.

Expected Behavior

The generated file names and package.json configuration should align. The following adjustment(s) could resolve the issue:

  1. The Rollup bundler should produce a type declaration file named index.d.ts (instead of index.cjs.d.ts).

GitHub Repo

No response

Steps to Reproduce

  1. Create a publishable library with Rollup bundler: npx nx g @nx/js:library packages/example-types --bundler=rollup --publishable --importPath=@myorg/example
  2. Build the library: npx nx build example-types
  3. Inspect the output directory and package.json:
  • Output includes index.cjs.d.ts in the dist folder.
  • package.json has a types field pointing to ./dist/index.d.ts and an exports entry for types pointing to the same file.
  1. Attempt to use the package in an external project:
  • Install the package and observe that TypeScript cannot resolve the type declarations.

resulting dist directory:

dist/
  index.cjs.js
  index.esm.js
  index.cjs.d.ts

generated package.json:

{
  "types": "./dist/index.d.ts",
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "types": "./dist/index.d.ts",
      "import": "./dist/index.esm.js",
      "default": "./dist/index.cjs.js"
    }
  }
}

Nx Report

NX   Report complete - copy this into the issue template

Node           : 22.11.0
OS             : darwin-arm64
Native Target  : aarch64-macos
yarn           : 4.5.3

nx                     : 20.2.2
@nx/js                 : 20.2.2
@nx/jest               : 20.2.2
@nx/eslint             : 20.2.2
@nx/workspace          : 20.2.2
@nx/devkit             : 20.2.2
@nx/eslint-plugin      : 20.2.2
@nx/module-federation  : 20.2.2
@nx/next               : 20.2.2
@nx/react              : 20.2.2
@nx/rollup             : 20.2.2
@nx/web                : 20.2.2
@nx/webpack            : 20.2.2
typescript             : 5.6.3
---------------------------------------
Registered Plugins:
@nx/js/typescript
@nx/eslint/plugin
@nx/jest/plugin
@nx/next/plugin
@nx/rollup/plugin

Failure Logs

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

ashkanhosseini added a commit to ashkanhosseini/nx that referenced this issue Dec 16, 2024
…le extensions for type definitions.

Updated the Rollup plugin's logic for generating type definition files to ensure compatibility with
additional file extensions, including .cjs.js and .mjs.js.

This change improves the handling of entry points and ensures that corresponding .d.ts files are
correctly named and emitted in all supported scenarios.

Added a comprehensive test case to validate the new behavior.

closed nrwl#29308
ashkanhosseini added a commit to ashkanhosseini/nx that referenced this issue Dec 16, 2024
…le extensions for type definitions.

Updated the Rollup plugin's logic for generating type definition files to ensure compatibility with
additional file extensions, including .cjs.js and .mjs.js.

This change improves the handling of entry points and ensures that corresponding .d.ts files are
correctly named and emitted in all supported scenarios.

Added a comprehensive test case to validate the new behavior.

closed nrwl#29308
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant