Skip to content

Core Backend : ES Modules #7917

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

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft

Conversation

pankhur94
Copy link
Contributor

No description provided.

@pankhur94
Copy link
Contributor Author

We changed our core-bentley package to module system, while re building the repo, Bentley package builds successfully, however, some depenedent package fail to build with the following issue
image

I tried providing different types file for both cjs and esm build using export map,

"exports": {
    ".": {
      "require": {
        "types": "./lib/cjs/core-bentley",
        "default": "./lib/cjs/core-bentley.js"
      },
      "import": {
        "types": "./lib/esm/core-bentley",
        "default": "./lib/esm/core-bentley.js"
      }
    }
  }

, but the issue still persists.

@mayank99
Copy link

mayank99 commented Apr 2, 2025

I'm not familiar with this codebase, but it looks like a TypeScript issue. The packages that are consuming @itwin/core-bentley are expecting some types (likely CJS) that don't match what is being imported (likely ESM).

@grigasp
Copy link
Member

grigasp commented Apr 3, 2025

We changed our core-bentley package to module system, while re building the repo, Bentley package builds successfully, however, some depenedent package fail <..>

I'm not seeing this issue with presentation-backend, but I do with presentation-frontend package, and it's also related to BeEvent as yours:

src/test/PresentationManager.test.ts:97:63 - error TS2322: Type 'import("/home/grigas/itwinjs-core/core/bentley/lib/esm/BeEvent", { with: { "resolution-mode": "import" } }).BeEvent<(_imodel: import("/home/grigas/itwinjs-core/core/frontend/lib/cjs/IModelConnection").IModelConnection) => void>' is not assignable to type 'import("/home/grigas/itwinjs-core/core/bentley/lib/cjs/BeEvent").BeEvent<(_imodel: import("/home/grigas/itwinjs-core/core/frontend/lib/cjs/IModelConnection").IModelConnection) => void>'.
  Types have separate declarations of a private property '_listeners'.

From the paths in the error you can clearly see that BeEvent that tests use is from esm, but IModelConnection and its onClose BeEvent type are coming from cjs. And the tests are trying to assign an instance of ESM BeEvent to a property that is CJS BeEvent. From TS perspective these are 2 different types, and it rightfully complains.

I think this is happening because, with your changes, core-bentley package now properly supports ESM, and that makes our tests use ESM instead of CJS. On the other hand, core-frontend doesn't have proper ESM support, and the tests still use its CJS version, and all its dependencies are also from CJS, including BeEvent from core-bentley.

So I think you'll have to start from the top and then move deeper into the stack. E.g. core-bentley can be fixed only when you fix all packages that have dependency on it.

@pankhur94
Copy link
Contributor Author

pankhur94 commented Apr 3, 2025

We changed our core-bentley package to module system, while re building the repo, Bentley package builds successfully, however, some depenedent package fail <..>

I'm not seeing this issue with presentation-backend, but I do with presentation-frontend package, and it's also related to BeEvent as yours:

src/test/PresentationManager.test.ts:97:63 - error TS2322: Type 'import("/home/grigas/itwinjs-core/core/bentley/lib/esm/BeEvent", { with: { "resolution-mode": "import" } }).BeEvent<(_imodel: import("/home/grigas/itwinjs-core/core/frontend/lib/cjs/IModelConnection").IModelConnection) => void>' is not assignable to type 'import("/home/grigas/itwinjs-core/core/bentley/lib/cjs/BeEvent").BeEvent<(_imodel: import("/home/grigas/itwinjs-core/core/frontend/lib/cjs/IModelConnection").IModelConnection) => void>'.
  Types have separate declarations of a private property '_listeners'.

From the paths in the error you can clearly see that BeEvent that tests use is from esm, but IModelConnection and its onClose BeEvent type are coming from cjs. And the tests are trying to assign an instance of ESM BeEvent to a property that is CJS BeEvent. From TS perspective these are 2 different types, and it rightfully complains.

I think this is happening because, with your changes, core-bentley package now properly supports ESM, and that makes our tests use ESM instead of CJS. On the other hand, core-frontend doesn't have proper ESM support, and the tests still use its CJS version, and all its dependencies are also from CJS, including BeEvent from core-bentley.

So I think you'll have to start from the top and then move deeper into the stack. E.g. core-bentley can be fixed only when you fix all packages that have dependency on it.

We are actually moving other way round, for ex, we start with the package with least dependencies i.e. core-bentley, core-geometry and upwards. But yes, I do agree that the reason you explained is correct. As soon as we convert all the dependencies of presentation-backend i.e. core-bentley, core-backend etc to esm, the issue is resolved.

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

Successfully merging this pull request may close these issues.

3 participants