Skip to content

Commit eb68e95

Browse files
author
Spencer
authored
[ts] enable sourcemaps in summarized types of @kbn/crypto (elastic#126410)
* [ts] enable sourcemaps in summarized types of @kbn/crypto * update snapshots * remove unnecessary exports of @kbn/type-summarizer package * remove tsc from the build process * use `@kbn/type-summarizer` to summarize its own types * add tests for interface and function * switch to export type where necessary * ignore __tmp__ in global jest preset * ignore __tmp__ globally * remove `@kbn/crypto` types path
1 parent 1174ac0 commit eb68e95

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2783
-276
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
**/*.js.snap
2+
__tmp__
23
/.es
34
/.chromium
45
/build

.eslintrc.js

+30-22
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ const DEV_PACKAGES = [
104104
'kbn-storybook',
105105
'kbn-telemetry-tools',
106106
'kbn-test',
107+
'kbn-type-summarizer',
107108
];
108109

109110
/** Directories (at any depth) which include dev-only code. */
@@ -1632,28 +1633,6 @@ module.exports = {
16321633
},
16331634
},
16341635

1635-
/**
1636-
* Prettier disables all conflicting rules, listing as last override so it takes precedence
1637-
*/
1638-
{
1639-
files: ['**/*'],
1640-
rules: {
1641-
...require('eslint-config-prettier').rules,
1642-
...require('eslint-config-prettier/react').rules,
1643-
...require('eslint-config-prettier/@typescript-eslint').rules,
1644-
},
1645-
},
1646-
/**
1647-
* Enterprise Search Prettier override
1648-
* Lints unnecessary backticks - @see https://github.com/prettier/eslint-config-prettier/blob/main/README.md#forbid-unnecessary-backticks
1649-
*/
1650-
{
1651-
files: ['x-pack/plugins/enterprise_search/**/*.{ts,tsx}'],
1652-
rules: {
1653-
quotes: ['error', 'single', { avoidEscape: true, allowTemplateLiterals: false }],
1654-
},
1655-
},
1656-
16571636
/**
16581637
* Platform Security Team overrides
16591638
*/
@@ -1768,5 +1747,34 @@ module.exports = {
17681747
'@kbn/eslint/no_export_all': 'error',
17691748
},
17701749
},
1750+
1751+
{
1752+
files: ['packages/kbn-type-summarizer/**/*.ts'],
1753+
rules: {
1754+
'no-bitwise': 'off',
1755+
},
1756+
},
1757+
1758+
/**
1759+
* Prettier disables all conflicting rules, listing as last override so it takes precedence
1760+
*/
1761+
{
1762+
files: ['**/*'],
1763+
rules: {
1764+
...require('eslint-config-prettier').rules,
1765+
...require('eslint-config-prettier/react').rules,
1766+
...require('eslint-config-prettier/@typescript-eslint').rules,
1767+
},
1768+
},
1769+
/**
1770+
* Enterprise Search Prettier override
1771+
* Lints unnecessary backticks - @see https://github.com/prettier/eslint-config-prettier/blob/main/README.md#forbid-unnecessary-backticks
1772+
*/
1773+
{
1774+
files: ['x-pack/plugins/enterprise_search/**/*.{ts,tsx}'],
1775+
rules: {
1776+
quotes: ['error', 'single', { avoidEscape: true, allowTemplateLiterals: false }],
1777+
},
1778+
},
17711779
],
17721780
};

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ target
2020
*.iml
2121
*.log
2222
types.eslint.config.js
23+
__tmp__
2324

2425
# Ignore example plugin builds
2526
/examples/*/build

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@
479479
"@kbn/test": "link:bazel-bin/packages/kbn-test",
480480
"@kbn/test-jest-helpers": "link:bazel-bin/packages/kbn-test-jest-helpers",
481481
"@kbn/test-subj-selector": "link:bazel-bin/packages/kbn-test-subj-selector",
482+
"@kbn/type-summarizer": "link:bazel-bin/packages/kbn-type-summarizer",
482483
"@loaders.gl/polyfills": "^2.3.5",
483484
"@mapbox/vector-tile": "1.3.1",
484485
"@microsoft/api-documenter": "7.13.68",
@@ -869,6 +870,7 @@
869870
"simple-git": "1.116.0",
870871
"sinon": "^7.4.2",
871872
"sort-package-json": "^1.53.1",
873+
"source-map": "^0.7.3",
872874
"spawn-sync": "^1.0.15",
873875
"string-replace-loader": "^2.2.0",
874876
"strong-log-transformer": "^2.1.0",

packages/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ filegroup(
6666
"//packages/kbn-test-subj-selector:build",
6767
"//packages/kbn-timelion-grammar:build",
6868
"//packages/kbn-tinymath:build",
69+
"//packages/kbn-type-summarizer:build",
6970
"//packages/kbn-typed-react-router-config:build",
7071
"//packages/kbn-ui-framework:build",
7172
"//packages/kbn-ui-shared-deps-npm:build",
@@ -132,6 +133,7 @@ filegroup(
132133
"//packages/kbn-telemetry-tools:build_types",
133134
"//packages/kbn-test:build_types",
134135
"//packages/kbn-test-jest-helpers:build_types",
136+
"//packages/kbn-type-summarizer:build_types",
135137
"//packages/kbn-typed-react-router-config:build_types",
136138
"//packages/kbn-ui-shared-deps-npm:build_types",
137139
"//packages/kbn-ui-shared-deps-src:build_types",

packages/kbn-crypto/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ ts_project(
6161
srcs = SRCS,
6262
deps = TYPES_DEPS,
6363
declaration = True,
64+
declaration_map = True,
6465
emit_declaration_only = True,
6566
out_dir = "target_types",
6667
root_dir = "src",

packages/kbn-crypto/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "../../tsconfig.bazel.json",
33
"compilerOptions": {
44
"declaration": true,
5+
"declarationMap": true,
56
"emitDeclarationOnly": true,
67
"outDir": "./target_types",
78
"rootDir": "src",

packages/kbn-test/jest-preset.js

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// For a detailed explanation regarding each configuration property, visit:
1010
// https://jestjs.io/docs/en/configuration.html
1111

12+
/** @typedef {import("@jest/types").Config.InitialOptions} JestConfig */
13+
/** @type {JestConfig} */
1214
module.exports = {
1315
// The directory where Jest should output its coverage files
1416
coverageDirectory: '<rootDir>/target/kibana-coverage/jest',
@@ -128,4 +130,6 @@ module.exports = {
128130

129131
// A custom resolver to preserve symlinks by default
130132
resolver: '<rootDir>/node_modules/@kbn/test/target_node/jest/setup/preserve_symlinks_resolver.js',
133+
134+
watchPathIgnorePatterns: ['.*/__tmp__/.*'],
131135
};
+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
load("@npm//@bazel/typescript:index.bzl", "ts_config")
2+
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
3+
load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm", "pkg_npm_types", "ts_project")
4+
load("@build_bazel_rules_nodejs//internal/node:node.bzl", "nodejs_binary")
5+
load("@build_bazel_rules_nodejs//:index.bzl", "directory_file_path")
6+
7+
PKG_BASE_NAME = "kbn-type-summarizer"
8+
PKG_REQUIRE_NAME = "@kbn/type-summarizer"
9+
10+
SOURCE_FILES = glob(
11+
[
12+
"src/**/*.ts",
13+
],
14+
exclude = [
15+
"**/*.test.*"
16+
],
17+
)
18+
19+
SRCS = SOURCE_FILES
20+
21+
filegroup(
22+
name = "srcs",
23+
srcs = SRCS,
24+
)
25+
26+
NPM_MODULE_EXTRA_FILES = [
27+
"package.json",
28+
"README.md",
29+
]
30+
31+
RUNTIME_DEPS = [
32+
"@npm//@babel/runtime",
33+
"@npm//@microsoft/api-extractor",
34+
"@npm//source-map-support",
35+
"@npm//chalk",
36+
"@npm//getopts",
37+
"@npm//is-path-inside",
38+
"@npm//normalize-path",
39+
"@npm//source-map",
40+
"@npm//tslib",
41+
]
42+
43+
TYPES_DEPS = [
44+
"@npm//@microsoft/api-extractor",
45+
"@npm//@types/jest",
46+
"@npm//@types/node",
47+
"@npm//@types/normalize-path",
48+
"@npm//getopts",
49+
"@npm//is-path-inside",
50+
"@npm//normalize-path",
51+
"@npm//source-map",
52+
"@npm//tslib",
53+
]
54+
55+
ts_config(
56+
name = "tsconfig",
57+
src = "tsconfig.json",
58+
deps = [
59+
"//:tsconfig.base.json",
60+
"//:tsconfig.bazel.json",
61+
],
62+
)
63+
64+
ts_project(
65+
name = "tsc_types",
66+
args = ['--pretty'],
67+
srcs = SRCS,
68+
deps = TYPES_DEPS,
69+
declaration = True,
70+
declaration_map = True,
71+
emit_declaration_only = True,
72+
out_dir = "target_types",
73+
root_dir = "src",
74+
tsconfig = ":tsconfig",
75+
)
76+
77+
jsts_transpiler(
78+
name = "target_node",
79+
srcs = SRCS,
80+
build_pkg_name = package_name(),
81+
)
82+
83+
js_library(
84+
name = PKG_BASE_NAME,
85+
srcs = NPM_MODULE_EXTRA_FILES,
86+
deps = RUNTIME_DEPS + [":target_node"],
87+
package_name = PKG_REQUIRE_NAME,
88+
visibility = ["//visibility:public"],
89+
)
90+
91+
directory_file_path(
92+
name = "bazel-cli-path",
93+
directory = ":target_node",
94+
path = "bazel_cli.js",
95+
)
96+
97+
nodejs_binary(
98+
name = "bazel-cli",
99+
data = [
100+
":%s" % PKG_BASE_NAME
101+
],
102+
entry_point = ":bazel-cli-path",
103+
visibility = ["//visibility:public"],
104+
)
105+
106+
pkg_npm(
107+
name = "npm_module",
108+
deps = [
109+
":%s" % PKG_BASE_NAME,
110+
],
111+
)
112+
113+
filegroup(
114+
name = "build",
115+
srcs = [
116+
":npm_module",
117+
],
118+
visibility = ["//visibility:public"],
119+
)
120+
121+
pkg_npm_types(
122+
name = "npm_module_types",
123+
srcs = SRCS,
124+
deps = [":tsc_types"],
125+
package_name = PKG_REQUIRE_NAME,
126+
tsconfig = ":tsconfig",
127+
visibility = ["//visibility:public"],
128+
)
129+
130+
filegroup(
131+
name = "build_types",
132+
srcs = [
133+
":npm_module_types",
134+
],
135+
visibility = ["//visibility:public"],
136+
)
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# @kbn/type-summarizer
2+
3+
Consume the .d.ts files for a package, produced by `tsc`, and generate a single `.d.ts` file of the public types along with a source map that points back to the original source.
4+
5+
## You mean like API Extractor?
6+
7+
Yeah, except with source map support and without all the legacy features and other features we disable to generate our current type summaries.
8+
9+
I first attempted to implement this in api-extractor but I (@spalger) hit a wall when dealing with the `Span` class. This class handles all the text output which ends up becoming source code, and I wasn't able to find a way to associate specific spans with source locations without getting 12 headaches. Instead I decided to try implementing this from scratch, reducing our reliance on the api-extractor project and putting us in control of how we generate type summaries.
10+
11+
This package is missing some critical features for wider adoption, but rather than build the entire product in a branch I decided to implement support for a small number of TS features and put this to use in the `@kbn/crypto` module ASAP.
12+
13+
The plan is to expand to other packages in the Kibana repo, adding support for language features as we go.
14+
15+
## Something isn't working and I'm blocked!
16+
17+
If there's a problem with the implmentation blocking another team at any point we can move the package back to using api-extractor by removing the package from the `TYPE_SUMMARIZER_PACKAGES` list at the top of [packages/kbn-type-summarizer/src/lib/bazel_cli_config.ts](./src/lib/bazel_cli_config.ts).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
/** @typedef {import("@jest/types").Config.InitialOptions} JestConfig */
10+
/** @type {JestConfig} */
11+
module.exports = {
12+
preset: '@kbn/test/jest_node',
13+
rootDir: '../..',
14+
roots: ['<rootDir>/packages/kbn-type-summarizer'],
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
/** @typedef {import("@jest/types").Config.InitialOptions} JestConfig */
10+
/** @type {JestConfig} */
11+
module.exports = {
12+
preset: '@kbn/test/jest_integration_node',
13+
rootDir: '../..',
14+
roots: ['<rootDir>/packages/kbn-type-summarizer'],
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@kbn/type-summarizer",
3+
"version": "1.0.0",
4+
"license": "SSPL-1.0 OR Elastic License 2.0",
5+
"main": "./target_node/index.js",
6+
"private": true
7+
}

0 commit comments

Comments
 (0)