kibana/docs/developer/best-practices/typescript.asciidoc

67 lines
5.9 KiB
Plaintext
Raw Normal View History

[[typescript]]
== Typescript
Although this is not a requirement, we encourage if all new code is developed in https://www.typescriptlang.org/[Typescript].
[discrete]
=== Project references
Kibana has crossed the 2m LoC mark. The current situation creates some scaling problems when the default out-of-the-box setup stops working. As a result, developers suffer from slow project compilation and IDE unresponsiveness. As a part of https://github.com/elastic/kibana/projects/63[Developer Experience project], we are migrating our tooling to use built-in TypeScript features addressing the scaling problems - https://www.typescriptlang.org/docs/handbook/project-references.html[project references] & https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#faster-subsequent-builds-with-the---incremental-flag[incremental builds]
In a nutshell - instead of compiling the whole Kibana codebase at once, this setup enforces splitting the code base into independent projects that form a directed acyclic graph (DAG). This allows the TypeScript compiler (`tsc`) to apply several advanced optimizations:
- Every project emits `public` interfaces in the form of `d.ts` type declarations generated by the TypeScript compiler
- These generated `d.ts` type declarations are used whenever a referenced project is imported in a depending project
- This makes it possible to determine which project needs rebuilding when the source code has changed to use a more aggressive caching strategy.
More details are available in the https://www.typescriptlang.org/docs/handbook/project-references.html[official docs]
[discrete]
==== Caveats
This architecture imposes several limitations to which we must comply:
chore(NA): tool to find plugins circular dependencies between plugins (#82867) * chore(NA): update gitignore to include first changes from moving into a single package.json * chore(NA): update gitignore * chore(NA): move all the dependencies into the single package.json and apply changes to bootstrap * chore(NA): fix types problems after the single package json * chore(NA): include code to find the dependencies used across the code * chore(NA): introduce pure lockfile for install dependencies on build * chore(NA): update clean task to not delete anything from xpack node_modules * chore(NA): update gitignore to remove development temporary rules * chore(NA): update notice file * chore(NA): update jest snapshots * chore(NA): fix whitelisted licenses to include a new specify form of an already included one * chore(NA): remove check lockfile symlinks from child projects * chore(NA): fix eslint and add missing declared deps on single pkg json * chore(NA): correctly update notice * chore(NA): fix failing jest test for storyshots.test.tsx * chore(NA): fix cypress multi reporter path * chore(NA): fix Project tests check * chore(NA): fix problem with logic to detect used dependes on oss build * chore(NA): include correct x-pack plugins dep discovery * chore(NA): discover entries under dynamic requires on vis_type_timelion * chore(NA): remove canvas * chore(NA): add initial code to find circular deps * chore(NA): ground work to integrate the circular deps scripts * chore(NA): add correct filtering to find circular dependenices feature * chore(NA): add ci mode flag into circular deps script * chore(NA): feature complete circular dependencies detect script * chore(NA): merge and solve conflicts with master * chore(NA): remove unwanted changes * chore(NA): remove unwanted changes on kbn storybook * chore(NA): hook find circular deps tool into ci * chore(NA): remove previous find plugin circular deps script * chore(NA): add type for circular dep list * chore(NA): add type for circular dep list for allowed list * chore(NA): allow CI to fail check * chore(NA): update deps allowed list * chore(NA): run search circular deps script over examples too * docs(NA): adds cli description * chore(NA): use plugin search paths to build entries to find circular deps * chore(NA): update allowed list * chore(NA): snapshot update for kbn optimizer test * chore(NA): update dpdm version * chore(NA): remove thirdParty flag * chore(NA): update docs to include info about the new tool * docs(NA): update to link PR instead of the issue * chore(NA): update debug logs to always output allowedList * fix(NA): correctly list found differences number * chore(NA): remove quiet flag * fix(NA): correctly fail the CI if circular deps are found * chore(NA): complete list of found circular deps * chore(NA): used named capturing group into the regex * docs(NA): update typescript best practices docs and styleguide * chore(NA): introduce quick filter option flag Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2020-11-30 23:19:32 +01:00
- Projects cannot have circular dependencies. Even though the Kibana platform doesn't support circular dependencies between Kibana plugins, TypeScript (and ES6 modules) does allow circular imports between files. So in theory, you may face a problem when migrating to the TS project references and you will have to resolve this circular dependency. We've built a tool that can be used to find such problems. Please read the prerequisites section below to know how to use it.
- A project must emit its type declaration. It's not always possible to generate a type declaration if the compiler cannot infer a type. There are two basic cases:
1. Your plugin exports a type inferring an internal type declared in Kibana codebase. In this case, you'll have to either export an internal type or to declare an exported type explicitly.
2. Your plugin exports something inferring a type from a 3rd party library that doesn't export this type. To fix the problem, you have to declare the exported type manually.
[discrete]
==== Prerequisites
Since project refs rely on generated `d.ts` files, the migration order does matter. You can migrate your plugin only when all the plugin dependencies already have migrated. It creates a situation where commonly used plugins (such as `data` or `kibana_react`) have to migrate first.
Run `node scripts/find_plugins_without_ts_refs.js --id your_plugin_id` to get a list of plugins that should be switched to TS project refs to unblock your plugin migration.
chore(NA): tool to find plugins circular dependencies between plugins (#82867) * chore(NA): update gitignore to include first changes from moving into a single package.json * chore(NA): update gitignore * chore(NA): move all the dependencies into the single package.json and apply changes to bootstrap * chore(NA): fix types problems after the single package json * chore(NA): include code to find the dependencies used across the code * chore(NA): introduce pure lockfile for install dependencies on build * chore(NA): update clean task to not delete anything from xpack node_modules * chore(NA): update gitignore to remove development temporary rules * chore(NA): update notice file * chore(NA): update jest snapshots * chore(NA): fix whitelisted licenses to include a new specify form of an already included one * chore(NA): remove check lockfile symlinks from child projects * chore(NA): fix eslint and add missing declared deps on single pkg json * chore(NA): correctly update notice * chore(NA): fix failing jest test for storyshots.test.tsx * chore(NA): fix cypress multi reporter path * chore(NA): fix Project tests check * chore(NA): fix problem with logic to detect used dependes on oss build * chore(NA): include correct x-pack plugins dep discovery * chore(NA): discover entries under dynamic requires on vis_type_timelion * chore(NA): remove canvas * chore(NA): add initial code to find circular deps * chore(NA): ground work to integrate the circular deps scripts * chore(NA): add correct filtering to find circular dependenices feature * chore(NA): add ci mode flag into circular deps script * chore(NA): feature complete circular dependencies detect script * chore(NA): merge and solve conflicts with master * chore(NA): remove unwanted changes * chore(NA): remove unwanted changes on kbn storybook * chore(NA): hook find circular deps tool into ci * chore(NA): remove previous find plugin circular deps script * chore(NA): add type for circular dep list * chore(NA): add type for circular dep list for allowed list * chore(NA): allow CI to fail check * chore(NA): update deps allowed list * chore(NA): run search circular deps script over examples too * docs(NA): adds cli description * chore(NA): use plugin search paths to build entries to find circular deps * chore(NA): update allowed list * chore(NA): snapshot update for kbn optimizer test * chore(NA): update dpdm version * chore(NA): remove thirdParty flag * chore(NA): update docs to include info about the new tool * docs(NA): update to link PR instead of the issue * chore(NA): update debug logs to always output allowedList * fix(NA): correctly list found differences number * chore(NA): remove quiet flag * fix(NA): correctly fail the CI if circular deps are found * chore(NA): complete list of found circular deps * chore(NA): used named capturing group into the regex * docs(NA): update typescript best practices docs and styleguide * chore(NA): introduce quick filter option flag Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2020-11-30 23:19:32 +01:00
Additionally, in order to migrate into project refs, you also need to make sure your plugin doesn't have circular dependencies with other plugins both on code and type imports. We run a job in the CI for each PR trying to find if new circular dependencies are being added which runs our tool with `node scripts/find_plugins_with_circular_deps`. However there are also a couple of circular dependencies already identified and that are in an allowed list to be solved. You also need to make sure your plugin don't rely in any other plugin into that allowed list. For a complete overview of the circular dependencies both found and in the allowed list as well as the complete circular dependencies path please run the following script locally with the debug flag `node scripts/find_plugins_with_circular_deps --debug` .
[discrete]
==== Implementation
2020-11-04 08:25:12 +01:00
- Make sure all the plugins listed as dependencies in *requiredPlugins*, *optionalPlugins* & *requiredBundles* properties of `kibana.json` manifest file have migrated to TS project references.
- Add `tsconfig.json` in the root folder of your plugin.
[source,json]
----
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./target/types",
"emitDeclarationOnly": true,
"declaration": true,
"declarationMap": true
},
"include": [
2021-10-07 20:30:32 +02:00
// add all the folders containing files to be compiled
],
"references": [
{ "path": "../../core/tsconfig.json" },
// add references to other TypeScript projects your plugin dependes on
]
}
----
If your plugin imports a file not listed in `include`, the build will fail with the next message `File ‘…’ is not listed within the file list of project …’. Projects must list all files or use an 'include' pattern.`
- Build you plugin `./node_modules/.bin/tsc -b src/plugins/my_plugin`. Fix errors if `tsc` cannot generate type declarations for your project.
- Add your project reference to `references` property of `tsconfig.refs.json`
- Add your plugin to `references` property and plugin folder to `exclude` property of the `tsconfig.json` it used to belong to (for example, for `src/plugins/**` it's `tsconfig.json`; for `x-pack/plugins/**` its `x-pack/tsconfig.json`).
- List the reference to your newly created project in all the Kibana `tsconfig.json` files that could import your project: `tsconfig.json`, `test/tsconfig.json`, `x-pack/tsconfig.json`, `x-pack/test/tsconfig.json`. And in all the plugin-specific `tsconfig.refs.json` for dependent plugins.
- You can measure how your changes affect `tsc` compiler performance with `node --max-old-space-size=4096 ./node_modules/.bin/tsc -p tsconfig.json --extendedDiagnostics --noEmit`. Compare with `master` branch.
You can use https://github.com/elastic/kibana/pull/79446 as an example.