-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Document ts project references setup #78586
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6eac83d
document ts project references
mshustov dc5a6a7
Apply suggestions from code review
mshustov 90062e1
Merge remote-tracking branch 'origin/docuement-ts-project-refa' into …
mshustov aeec0f3
capitalize all the bullet points
mshustov 910cd09
address @rudolf comments
mshustov 225ec42
add a reference to example
mshustov da5d86e
fix identation
mshustov cc2ba37
Merge branch 'master' into docuement-ts-project-refa
mshustov e786249
convert into asciidoc
mshustov aa38635
fix numeration
mshustov 536b099
fix asciidoctor failures
mshustov abbe6eb
cleanup typescript.asciidoc
mshustov 6efc968
back to unordered list. asciidoctor fails validation
mshustov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Typescript | ||
| Although this is not a requirement, we encourage if all new code is developed in [Typescript](https://www.typescriptlang.org/). | ||
|
|
||
| - [Project references](#project-references) | ||
| - [Caveats](#caveats) | ||
| - [Prerequisitions](#prerequisitions) | ||
| - [Implementation](#implementation) | ||
|
|
||
| ## Project references | ||
| Kibana has crossed the 1.5m 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 [Developer Experience project](https://github.com/elastic/kibana/projects/63), we are migrating our tooling to use built-in TypeScript features addressing the scaling problems - [project references](https://www.typescriptlang.org/docs/handbook/project-references.html) & [incremental builds](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#faster-subsequent-builds-with-the---incremental-flag). | ||
|
|
||
| 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 [official docs](https://www.typescriptlang.org/docs/handbook/project-references.html) | ||
|
|
||
| ### Caveats | ||
| This architecture imposes several limitations to which we must comply: | ||
| - 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. | ||
| - 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. | ||
|
|
||
| ### Prerequisitions | ||
| Since `tsc` doesn't support circular project references, 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. | ||
|
|
||
| ### Implementation | ||
| 1. Make sure all the plugins listed as dependencies in `kibana.json` file have migrated to TS project references. | ||
| 2. Add `tsconfig.json` in the root folder of your plugin. | ||
| ```json | ||
| { | ||
| "extends": "../../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "composite": true, | ||
| "outDir": "./target/types", | ||
| "emitDeclarationOnly": true, | ||
| "declaration": true, | ||
| "declarationMap": true | ||
| }, | ||
| "include": [ | ||
| // add all the folders containg 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.` | ||
|
|
||
| 3. Build you plugin `./node_modules/.bin/tsc -b src/plugins/my_plugin`. Fix errors if `tsc` cannot generate type declarations for your project. | ||
| 4. Add your project reference to `references` property of `tsconfig.refs.json` | ||
| 5. 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/**` it’s `x-pack/tsconfig.json`). | ||
| 6. 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. | ||
| 7. 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. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.