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

fix(ui5-tooling-transpile): d.ts files to be modules + generate index.d.ts #734

Merged
merged 1 commit into from
Apr 21, 2023

Conversation

petermuessig
Copy link
Member

@petermuessig petermuessig commented Apr 21, 2023

This fix ensures the correct d.ts generation. The transpile procees now creates an index.d.ts and all TS UI5 modules will declare its module with the fully qualified name:

declare module "${moduleName}" {
  ${source}
}

In addition, the generated control interfaces, the .gen.d.ts files need to also be converted to declare the module with the fully qualified name and put the imports into the module declaration.


The ui5-tooling-transpile tooling extension to properly generate the type definitions including the index.d.ts which ensures that e.g. TypeScript UI5 applications consuming UI5 libraries will get code completion by just adding the typeRoots configuration:

Snippet taken from: https://github.com/ui5-community/ui5-ecosystem-showcase/blob/223b9f54745c397f6bf7aec430d45d5a9a33c354/showcases/ui5-tsapp/tsconfig.json:

{
  "compilerOptions": {
    [...]
    "paths": {
      [...]
      "ui5/ecosystem/demo/tslib/*": ["./node_modules/ui5-tslib/src/ui5/ecosystem/demo/tslib/*"]
    },
    "typeRoots": [
      "./node_modules/@types",
      "./node_modules/ui5-tslib"
    ]
  },
  "include": ["./webapp/**/*.ts"]
}

The paths mapping is just included in case of the library hasn't been built yet in the monorepo structure. But this mapping can also be omitted. Important is the addition of the typeRoots configuration - keep in mind, as soon as you add your type root for e.g. node_modules/ui5-tslib, you need to also add the type roots to node_modules/@types so that the types from definitely typed can be found.

.map(
(dtsFile) =>
`/// <reference path=".${
/\.d\.ts$/.test(dtsFile) ? dtsFile : dtsFile.replace(/\.ts$/, ".d.ts")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's always a .d.ts file for each .ts file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct - and for controls there are even 2: the d.ts and the gen.d.ts

"integration/*": ["./webapp/test/integration/*"]
}
"integration/*": ["./webapp/test/integration/*"],
"ui5/ecosystem/demo/tslib/*": ["./node_modules/ui5-tslib/src/ui5/ecosystem/demo/tslib/*"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What' the difference here? Why do we still need a path into node_modules?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is this related to the following statement from the issue report??

"The paths mapping is just included in case of the library hasn't been built yet in the monorepo structure. But this mapping can also be omitted. "

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The paths mappings are still necessary as long as the library has not been built. Then this is used to lookup the type information from the ts files. Once built, the type definitions are retrieved from the dist folder, then the index.d.ts and the nested d.ts files are used and the typeRoots configuration wins.

Copy link

@akudev akudev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me overall

const source = sourcesMap[resourcePath];
let moduleName = /^\/resources\/(.*)\.ts$/.exec(resourcePath)?.[1];
if (moduleName?.endsWith(".gen.d")) {
moduleName = /^(.*)\.gen\.d$/.exec(moduleName)?.[1] || moduleName;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ummm... isn't the OR part in the end of this line (and the question mark) unneeded? You already checked that the module name ends with ".gen.d".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relict from previous logic - will adapt

for await (const resourcePath of Object.keys(sourcesMap)) {
const source = sourcesMap[resourcePath];
let moduleName = /^\/resources\/(.*)\.ts$/.exec(resourcePath)?.[1];
if (moduleName?.endsWith(".gen.d")) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm.. so the heuristics is: any module named "*.gen.d" is assumed to be one generated by the ts-interface-generator and hence already containing a module declaration, which has to be modified, though? Should maybe be written down somewhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding JSDoc

….d.ts

This fix ensures the correct d.ts generation. The transpile procees now
creates an index.d.ts and all TS UI5 modules will declare its module with
the fully qualified name:

```ts
declare module "${moduleName}" {
  ${source}
}
```

In addition, the generated control interfaces, the `.gen.d.ts` files
need to also be converted to declare the module with the fully qualified
name and put the imports into the module declaration.
@petermuessig petermuessig merged commit bc75c8c into main Apr 21, 2023
@petermuessig petermuessig deleted the fix/transpileDTS branch April 21, 2023 16:25
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.

2 participants