From 82961a17835f52845c3cd67808aaec751424a387 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Thu, 30 Sep 2021 13:16:13 +0100 Subject: [PATCH 01/22] chore(NA): auto creation of the package.json for the new types pkg rule --- packages/elastic-datemath/BUILD.bazel | 14 ++- src/dev/bazel/index.bzl | 2 + src/dev/bazel/types_pkg/BUILD.bazel | 19 ++++ src/dev/bazel/types_pkg/api_extractor.js | 108 +++++++++++++++++++++++ src/dev/bazel/types_pkg/package_json.tpl | 5 ++ src/dev/bazel/types_pkg/types_pkg.bzl | 82 +++++++++++++++++ 6 files changed, 228 insertions(+), 2 deletions(-) create mode 100644 src/dev/bazel/types_pkg/BUILD.bazel create mode 100644 src/dev/bazel/types_pkg/api_extractor.js create mode 100644 src/dev/bazel/types_pkg/package_json.tpl create mode 100644 src/dev/bazel/types_pkg/types_pkg.bzl diff --git a/packages/elastic-datemath/BUILD.bazel b/packages/elastic-datemath/BUILD.bazel index 04eb153bcc9e1..aa29ca03a6f45 100644 --- a/packages/elastic-datemath/BUILD.bazel +++ b/packages/elastic-datemath/BUILD.bazel @@ -1,6 +1,6 @@ load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project") load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") -load("//src/dev/bazel:index.bzl", "jsts_transpiler") +load("//src/dev/bazel:index.bzl", "jsts_transpiler", "types_pkg") PKG_BASE_NAME = "elastic-datemath" PKG_REQUIRE_NAME = "@elastic/datemath" @@ -57,10 +57,20 @@ ts_project( tsconfig = ":tsconfig", ) +types_pkg( + name = "npm_package_types", + package_name = "@types/elastic__datemath", + data = SRCS + DEPS + [ + "tsconfig.json", + "//:tsconfig.base.json", + ":tsc_types", + ] +) + js_library( name = PKG_BASE_NAME, srcs = NPM_MODULE_EXTRA_FILES, - deps = DEPS + [":target_node", ":tsc_types"], + deps = DEPS + [":target_node", ":tsc_types", ":npm_package_types"], package_name = PKG_REQUIRE_NAME, visibility = ["//visibility:public"], ) diff --git a/src/dev/bazel/index.bzl b/src/dev/bazel/index.bzl index e38d3d78c0071..cdc93d0b1fad4 100644 --- a/src/dev/bazel/index.bzl +++ b/src/dev/bazel/index.bzl @@ -11,5 +11,7 @@ Please do not import from any other files when looking to use a custom rule """ load("//src/dev/bazel:jsts_transpiler.bzl", _jsts_transpiler = "jsts_transpiler") +load("//src/dev/bazel/types_pkg:types_pkg.bzl", _types_pkg = "types_pkg") jsts_transpiler = _jsts_transpiler +types_pkg = _types_pkg diff --git a/src/dev/bazel/types_pkg/BUILD.bazel b/src/dev/bazel/types_pkg/BUILD.bazel new file mode 100644 index 0000000000000..6610a68a9be16 --- /dev/null +++ b/src/dev/bazel/types_pkg/BUILD.bazel @@ -0,0 +1,19 @@ +package(default_visibility = ["//visibility:public"]) + +load("@build_bazel_rules_nodejs//internal/node:node.bzl", "nodejs_binary") + +exports_files( + [ + "package_json.tpl", + "api_extractor.js" + ], + visibility = ["//visibility:public"] +) + +nodejs_binary( + name = "api_extractor", + data = [ + ":api_extractor.js", + ], + entry_point = ":api_extractor.js", +) diff --git a/src/dev/bazel/types_pkg/api_extractor.js b/src/dev/bazel/types_pkg/api_extractor.js new file mode 100644 index 0000000000000..4bb48854ef0ba --- /dev/null +++ b/src/dev/bazel/types_pkg/api_extractor.js @@ -0,0 +1,108 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +const { format, parseTsconfig } = require('typescript'); +const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor'); +const fs = require('fs'); +const path = require('path'); +const DEBUG = false; + +function runMain(tsConfig, entryPoint, dtsBundleOut, apiReviewFolder, acceptApiUpdates = false) { + const [parsedConfig, errors] = parseTsconfig(tsConfig); + if (errors && errors.length) { + console.error(format('', errors)); + return 1; + } + const pkgJson = path.resolve(path.dirname(entryPoint), 'package.json'); + if (!fs.existsSync(pkgJson)) { + fs.writeFileSync( + pkgJson, + JSON.stringify({ + name: 'GENERATED-BY-BAZEL', + version: '0.0.0', + description: 'This is a dummy package.json as API Extractor always requires one.', + }) + ); + } + // API extractor doesn't always support the version of TypeScript used in the repo + // example: at the moment it is not compatable with 3.2 + // to use the internal TypeScript we shall not create a program but rather pass a parsed tsConfig. + const parsedTsConfig = parsedConfig.config; + const extractorOptions = { + localBuild: acceptApiUpdates, + }; + const configObject = { + compiler: { + overrideTsconfig: parsedTsConfig, + }, + projectFolder: path.resolve(path.dirname(tsConfig)), + mainEntryPointFilePath: path.resolve(entryPoint), + apiReport: { + enabled: !!apiReviewFolder, + // TODO(alan-agius4): remove this folder name when the below issue is solved upstream + // See: https://github.com/microsoft/web-build-tools/issues/1470 + reportFileName: (apiReviewFolder && path.resolve(apiReviewFolder)) || 'invalid', + }, + docModel: { + enabled: false, + }, + dtsRollup: { + enabled: !!dtsBundleOut, + untrimmedFilePath: dtsBundleOut && path.resolve(dtsBundleOut), + }, + tsdocMetadata: { + enabled: false, + }, + }; + const options = { + configObject, + packageJson: undefined, + packageJsonFullPath: pkgJson, + configObjectFullPath: undefined, + }; + const extractorConfig = ExtractorConfig.prepare(options); + const { succeeded } = Extractor.invoke(extractorConfig, extractorOptions); + // API extractor errors are emitted by it's logger. + return succeeded ? 0 : 1; +} + +exports.runMain = runMain; + +// Entry point +if (require.main === module) { + if (DEBUG) { + console.error(` +api-extractor: running with + cwd: ${process.cwd()} + argv: + ${process.argv.join('\n ')} + `); + } + const [tsConfig, entryPoint, dtsBundleOut] = process.argv.slice(2); + const entryPoints = entryPoint.split(','); + const dtsBundleOuts = dtsBundleOut.split(','); + if (entryPoints.length !== entryPoints.length) { + throw new Error( + `Entry points count (${entryPoints.length}) does not match Bundle out count (${dtsBundleOuts.length})` + ); + } + for (let i = 0; i < entryPoints.length; i++) { + process.exitCode = runMain(tsConfig, entryPoints[i], dtsBundleOuts[i]); + if (process.exitCode !== 0) { + break; + } + } +} diff --git a/src/dev/bazel/types_pkg/package_json.tpl b/src/dev/bazel/types_pkg/package_json.tpl new file mode 100644 index 0000000000000..27b246e007876 --- /dev/null +++ b/src/dev/bazel/types_pkg/package_json.tpl @@ -0,0 +1,5 @@ +{ + "name": "{NAME}", + "description": "Generated by Bazel", + "types": "./index.d.ts" +} diff --git a/src/dev/bazel/types_pkg/types_pkg.bzl b/src/dev/bazel/types_pkg/types_pkg.bzl new file mode 100644 index 0000000000000..6a1c9774618fd --- /dev/null +++ b/src/dev/bazel/types_pkg/types_pkg.bzl @@ -0,0 +1,82 @@ +# +# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +# or more contributor license agreements. Licensed under the Elastic License +# 2.0 and the Server Side Public License, v 1; you may not use this file except +# in compliance with, at your election, the Elastic License 2.0 or the Server +# Side Public License, v 1. +# + +load("@build_bazel_rules_nodejs//:providers.bzl", "run_node") +#load("@build_bazel_rules_nodejs//internal/node:node.bzl", "nodejs_binary") +#load("@npm//typescript:index.bzl", "tsc") + +def _join(*elements): + segments = [f for f in elements if f] + if len(segments): + return "/".join(segments) + return "." + +def _types_pkg_impl(ctx): + out = ctx.actions.declare_file(_join(ctx.label.name, "package.json")) +# out = ctx.actions.declare_file("package.json") + inputs = ctx.files.data[:] + + ctx.actions.expand_template( + output = out, + template = ctx.file._template, + substitutions = {"{NAME}": ctx.attr.package_name}, + ) + + outputs = [] + outputs.append(out) +# # outputs.append(ctx.actions.declare_file(ctx.label.name + "index.d.ts")) +# js_out = ctx.actions.declare_directory("%s" % ctx.attr.name) +# outputs.append(js_out) +# +# extractor_args = ctx.actions.args() +# extractor_args.add_all([ +# ctx.expand_location("tsconfig.json"), +# ctx.expand_location("index.d.ts"), +# "index.d.ts" +# ]) +# +# ctx.actions.run( +# progress_message = "Running API Extractor", +# mnemonic = "APIExtractor", +# executable = ctx.executable._api_extractor, +# inputs = inputs, +# outputs = outputs, +# arguments = [extractor_args], +# ) + + # run_node( + # ctx, + # inputs = inputs, + # arguments = [extractor_args], + # outputs = outputs, + # mnemonic = "ApiExtractor", + # executable = "_api_extractor" + # execution_requirements = {}, + # ) + + return [DefaultInfo(files = depset(outputs))] + +types_pkg = rule( + implementation = _types_pkg_impl, + attrs = { + "package_name": attr.string(), + "_template": attr.label( + allow_single_file = True, + default = "package_json.tpl", + ), + "data": attr.label_list( + allow_files = True, + ), + "_api_extractor": attr.label( + doc = "Target that executes the api-extractor binary", + executable = True, + cfg = "host", + default = Label("//src/dev/bazel/types_pkg:api_extractor"), + ), + }, +) From 2edbb634f35da18e45ea059672a18b9b69d593e4 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Thu, 30 Sep 2021 18:57:21 +0100 Subject: [PATCH 02/22] chore(NA): first alpha api extractor working version --- package.json | 2 +- packages/elastic-datemath/BUILD.bazel | 5 +- src/dev/bazel/types_pkg/BUILD.bazel | 2 + src/dev/bazel/types_pkg/api_extractor.js | 5 +- src/dev/bazel/types_pkg/types_pkg.bzl | 59 ++++++++++-------------- yarn.lock | 44 +++++++++++++++--- 6 files changed, 71 insertions(+), 46 deletions(-) diff --git a/package.json b/package.json index c07efa4e607c3..708d50bc3723e 100644 --- a/package.json +++ b/package.json @@ -464,7 +464,7 @@ "@kbn/test-subj-selector": "link:bazel-bin/packages/kbn-test-subj-selector", "@loaders.gl/polyfills": "^2.3.5", "@microsoft/api-documenter": "7.7.2", - "@microsoft/api-extractor": "7.7.0", + "@microsoft/api-extractor": "7.7.2", "@octokit/rest": "^16.35.0", "@percy/agent": "^0.28.6", "@storybook/addon-a11y": "^6.1.20", diff --git a/packages/elastic-datemath/BUILD.bazel b/packages/elastic-datemath/BUILD.bazel index aa29ca03a6f45..33cab801130ea 100644 --- a/packages/elastic-datemath/BUILD.bazel +++ b/packages/elastic-datemath/BUILD.bazel @@ -58,11 +58,12 @@ ts_project( ) types_pkg( - name = "npm_package_types", + name = "npm_module_types", package_name = "@types/elastic__datemath", data = SRCS + DEPS + [ "tsconfig.json", "//:tsconfig.base.json", + "//:tsconfig.bazel.json", ":tsc_types", ] ) @@ -70,7 +71,7 @@ types_pkg( js_library( name = PKG_BASE_NAME, srcs = NPM_MODULE_EXTRA_FILES, - deps = DEPS + [":target_node", ":tsc_types", ":npm_package_types"], + deps = DEPS + [":target_node", ":tsc_types", ":npm_module_types"], package_name = PKG_REQUIRE_NAME, visibility = ["//visibility:public"], ) diff --git a/src/dev/bazel/types_pkg/BUILD.bazel b/src/dev/bazel/types_pkg/BUILD.bazel index 6610a68a9be16..27123072d97a7 100644 --- a/src/dev/bazel/types_pkg/BUILD.bazel +++ b/src/dev/bazel/types_pkg/BUILD.bazel @@ -13,6 +13,8 @@ exports_files( nodejs_binary( name = "api_extractor", data = [ + "@npm//@bazel/typescript", + "@npm//@microsoft/api-extractor", ":api_extractor.js", ], entry_point = ":api_extractor.js", diff --git a/src/dev/bazel/types_pkg/api_extractor.js b/src/dev/bazel/types_pkg/api_extractor.js index 4bb48854ef0ba..1df2e7ee02b8a 100644 --- a/src/dev/bazel/types_pkg/api_extractor.js +++ b/src/dev/bazel/types_pkg/api_extractor.js @@ -14,7 +14,10 @@ * found in the LICENSE file at https://angular.io/license */ -const { format, parseTsconfig } = require('typescript'); +console.log(process.argv); +console.log(process.cwd()); + +const { format, parseTsconfig } = require('@bazel/typescript'); const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor'); const fs = require('fs'); const path = require('path'); diff --git a/src/dev/bazel/types_pkg/types_pkg.bzl b/src/dev/bazel/types_pkg/types_pkg.bzl index 6a1c9774618fd..18814ae2566fc 100644 --- a/src/dev/bazel/types_pkg/types_pkg.bzl +++ b/src/dev/bazel/types_pkg/types_pkg.bzl @@ -7,8 +7,6 @@ # load("@build_bazel_rules_nodejs//:providers.bzl", "run_node") -#load("@build_bazel_rules_nodejs//internal/node:node.bzl", "nodejs_binary") -#load("@npm//typescript:index.bzl", "tsc") def _join(*elements): segments = [f for f in elements if f] @@ -16,48 +14,39 @@ def _join(*elements): return "/".join(segments) return "." +def _dts_inputs(files): + return [f for f in files if f.path.endswith(".d.ts") and not f.path.endswith(".map")] + def _types_pkg_impl(ctx): - out = ctx.actions.declare_file(_join(ctx.label.name, "package.json")) -# out = ctx.actions.declare_file("package.json") inputs = ctx.files.data[:] + package_json_output = ctx.actions.declare_file(_join(ctx.label.name, "package.json")) + outputs = [package_json_output] ctx.actions.expand_template( - output = out, + output = package_json_output, template = ctx.file._template, substitutions = {"{NAME}": ctx.attr.package_name}, ) - outputs = [] - outputs.append(out) -# # outputs.append(ctx.actions.declare_file(ctx.label.name + "index.d.ts")) -# js_out = ctx.actions.declare_directory("%s" % ctx.attr.name) -# outputs.append(js_out) -# -# extractor_args = ctx.actions.args() -# extractor_args.add_all([ -# ctx.expand_location("tsconfig.json"), -# ctx.expand_location("index.d.ts"), -# "index.d.ts" -# ]) -# -# ctx.actions.run( -# progress_message = "Running API Extractor", -# mnemonic = "APIExtractor", -# executable = ctx.executable._api_extractor, -# inputs = inputs, -# outputs = outputs, -# arguments = [extractor_args], -# ) + api_extracted_output = ctx.actions.declare_file(_join(ctx.label.name, "index.d.ts")) + outputs.append(api_extracted_output) - # run_node( - # ctx, - # inputs = inputs, - # arguments = [extractor_args], - # outputs = outputs, - # mnemonic = "ApiExtractor", - # executable = "_api_extractor" - # execution_requirements = {}, - # ) + extractor_args = ctx.actions.args() + package_path = ctx.label.package + + extractor_args.add(_join(package_path, "tsconfig.json")) + extractor_args.add_joined([s.path for s in _dts_inputs(ctx.files.data)], join_with = ",", omit_if_empty = False) + extractor_args.add(api_extracted_output.path) + + run_node( + ctx, + inputs = inputs, + arguments = [extractor_args], + outputs = [api_extracted_output], + mnemonic = "ApiExtractor", + executable = "_api_extractor", + execution_requirements = {}, + ) return [DefaultInfo(files = depset(outputs))] diff --git a/yarn.lock b/yarn.lock index bc0123c9f44ee..185e8b3f77262 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3160,14 +3160,22 @@ "@microsoft/node-core-library" "3.18.0" "@microsoft/tsdoc" "0.12.14" -"@microsoft/api-extractor@7.7.0": - version "7.7.0" - resolved "https://registry.yarnpkg.com/@microsoft/api-extractor/-/api-extractor-7.7.0.tgz#1550a5b88ca927d57e9c9698356a2f9375c5984c" - integrity sha512-1ngy95VA1s7GTE+bkS7QoYTg/TZs54CdJ46uAhl6HlyDJut4p/aH46W70g2XQs9VniIymW1Qe6fqNmcQUx5CVg== +"@microsoft/api-extractor-model@7.7.2": + version "7.7.2" + resolved "https://registry.yarnpkg.com/@microsoft/api-extractor-model/-/api-extractor-model-7.7.2.tgz#4872f2a3a4f90e57b82ade913edba728f7aefb02" + integrity sha512-USwWUPV3YLS8ZOS11vFh3nzEWXC2d8OZJ6CGp0nRnCXtbqmKqAq4Jg9J5gs1PCemo7JQEbzbHGGwycC0DbRJqw== dependencies: - "@microsoft/api-extractor-model" "7.7.0" - "@microsoft/node-core-library" "3.18.0" - "@microsoft/ts-command-line" "4.3.5" + "@microsoft/node-core-library" "3.18.2" + "@microsoft/tsdoc" "0.12.14" + +"@microsoft/api-extractor@7.7.2": + version "7.7.2" + resolved "https://registry.yarnpkg.com/@microsoft/api-extractor/-/api-extractor-7.7.2.tgz#cb802f08dd9d493596411f16188ebae1cf003a14" + integrity sha512-W75kcjWlONyB9kQKYAfSMiG3v2JMGlgUihny8PucZqdRatcYADeQiEcX5qE5sWdNRHD/J+5INiwlooZDd82sDQ== + dependencies: + "@microsoft/api-extractor-model" "7.7.2" + "@microsoft/node-core-library" "3.18.2" + "@microsoft/ts-command-line" "4.3.7" "@microsoft/tsdoc" "0.12.14" colors "~1.2.1" lodash "~4.17.15" @@ -3188,6 +3196,19 @@ timsort "~0.3.0" z-schema "~3.18.3" +"@microsoft/node-core-library@3.18.2": + version "3.18.2" + resolved "https://registry.yarnpkg.com/@microsoft/node-core-library/-/node-core-library-3.18.2.tgz#a5facc296256553d497a51813c0e9fbc55bad6de" + integrity sha512-IRoRmLwNvrR0rTNavYlfNObz9pr4Epo8Hd//0SNptt7adOySd735ur7YBO7SzafeijHsD3/dC4PXLLwhIsMU7Q== + dependencies: + "@types/node" "8.10.54" + colors "~1.2.1" + fs-extra "~7.0.1" + jju "~1.4.0" + semver "~5.3.0" + timsort "~0.3.0" + z-schema "~3.18.3" + "@microsoft/ts-command-line@4.3.5": version "4.3.5" resolved "https://registry.yarnpkg.com/@microsoft/ts-command-line/-/ts-command-line-4.3.5.tgz#78026d20244f39978d3397849ac8c40c0c2d4079" @@ -3197,6 +3218,15 @@ argparse "~1.0.9" colors "~1.2.1" +"@microsoft/ts-command-line@4.3.7": + version "4.3.7" + resolved "https://registry.yarnpkg.com/@microsoft/ts-command-line/-/ts-command-line-4.3.7.tgz#6c6dacc5582c54f123a276976fcac9799fcaa103" + integrity sha512-dl7j5E4Ly4vin0dFRNyDEmslpqLTeFkSvWi1Ux2OhTXbORpaRm2qivTQzUgbPSh8Mtc1LSZGekqEMNl0e0OMNw== + dependencies: + "@types/argparse" "1.0.33" + argparse "~1.0.9" + colors "~1.2.1" + "@microsoft/tsdoc@0.12.14": version "0.12.14" resolved "https://registry.yarnpkg.com/@microsoft/tsdoc/-/tsdoc-0.12.14.tgz#0e0810a0a174e50e22dfe8edb30599840712f22d" From 45160102d747b58f8c58a67b7bd511a42eb0734c Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Fri, 1 Oct 2021 12:48:25 +0100 Subject: [PATCH 03/22] chore(NA): support kbn-analytics --- packages/kbn-analytics/BUILD.bazel | 15 +++++++++++++-- src/dev/bazel/types_pkg/types_pkg.bzl | 6 +++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/kbn-analytics/BUILD.bazel b/packages/kbn-analytics/BUILD.bazel index cc65746e890ce..898af3dabf59e 100644 --- a/packages/kbn-analytics/BUILD.bazel +++ b/packages/kbn-analytics/BUILD.bazel @@ -1,6 +1,6 @@ load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project") load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") -load("//src/dev/bazel:index.bzl", "jsts_transpiler") +load("//src/dev/bazel:index.bzl", "jsts_transpiler", "types_pkg") PKG_BASE_NAME = "kbn-analytics" PKG_REQUIRE_NAME = "@kbn/analytics" @@ -78,10 +78,21 @@ ts_project( tsconfig = ":tsconfig", ) +types_pkg( + name = "npm_module_types", + package_name = "@types/kbn__analytics", + data = SRCS + TYPES_DEPS + [ + "tsconfig.json", + "//:tsconfig.base.json", + "//:tsconfig.bazel.json", + ":tsc_types", + ] +) + js_library( name = PKG_BASE_NAME, srcs = NPM_MODULE_EXTRA_FILES, - deps = RUNTIME_DEPS + [":target_node", ":target_web", ":tsc_types"], + deps = RUNTIME_DEPS + [":target_node", ":target_web", ":tsc_types", ":npm_module_types"], package_name = PKG_REQUIRE_NAME, visibility = ["//visibility:public"], ) diff --git a/src/dev/bazel/types_pkg/types_pkg.bzl b/src/dev/bazel/types_pkg/types_pkg.bzl index 18814ae2566fc..2c5c7b44e5cdd 100644 --- a/src/dev/bazel/types_pkg/types_pkg.bzl +++ b/src/dev/bazel/types_pkg/types_pkg.bzl @@ -14,8 +14,8 @@ def _join(*elements): return "/".join(segments) return "." -def _dts_inputs(files): - return [f for f in files if f.path.endswith(".d.ts") and not f.path.endswith(".map")] +def _dts_inputs(pkg_path, files): + return [f for f in files if f.path.endswith(_join(pkg_path, "target_types", "index.d.ts")) and not f.path.endswith(".map")] def _types_pkg_impl(ctx): inputs = ctx.files.data[:] @@ -35,7 +35,7 @@ def _types_pkg_impl(ctx): package_path = ctx.label.package extractor_args.add(_join(package_path, "tsconfig.json")) - extractor_args.add_joined([s.path for s in _dts_inputs(ctx.files.data)], join_with = ",", omit_if_empty = False) + extractor_args.add_joined([s.path for s in _dts_inputs(package_path, ctx.files.data)], join_with = ",", omit_if_empty = False) extractor_args.add(api_extracted_output.path) run_node( From d510f316e86ad528435229fc568d84e898218013 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Tue, 5 Oct 2021 18:44:27 +0100 Subject: [PATCH 04/22] chore(NA): correctly read tsconfig files and deps from ts_config rule --- packages/BUILD.bazel | 5 ++++- packages/elastic-datemath/BUILD.bazel | 15 +++++++++++---- packages/kbn-analytics/BUILD.bazel | 15 +++++++++++---- src/dev/bazel/types_pkg/types_pkg.bzl | 14 ++++++++++++++ 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/packages/BUILD.bazel b/packages/BUILD.bazel index 75c8d700e2843..41033b77c7436 100644 --- a/packages/BUILD.bazel +++ b/packages/BUILD.bazel @@ -3,7 +3,7 @@ filegroup( name = "build", srcs = [ - "//packages/elastic-datemath:build", + "//packages/elastic-datemath:build", "//packages/elastic-eslint-config-kibana:build", "//packages/elastic-safer-lodash-set:build", "//packages/kbn-ace:build", @@ -65,5 +65,8 @@ filegroup( "//packages/kbn-ui-shared-deps-src:build", "//packages/kbn-utility-types:build", "//packages/kbn-utils:build", + + "//packages/elastic-datemath:build_types", + "//packages/kbn-analytics:build_types", ], ) diff --git a/packages/elastic-datemath/BUILD.bazel b/packages/elastic-datemath/BUILD.bazel index 33cab801130ea..d9816cc7207d9 100644 --- a/packages/elastic-datemath/BUILD.bazel +++ b/packages/elastic-datemath/BUILD.bazel @@ -4,6 +4,7 @@ load("//src/dev/bazel:index.bzl", "jsts_transpiler", "types_pkg") PKG_BASE_NAME = "elastic-datemath" PKG_REQUIRE_NAME = "@elastic/datemath" +TYPES_PKG_REQUIRE_NAME = "@types/elastic__datemath" SOURCE_FILES = glob([ "src/index.ts", @@ -59,11 +60,9 @@ ts_project( types_pkg( name = "npm_module_types", - package_name = "@types/elastic__datemath", + package_name = TYPES_PKG_REQUIRE_NAME, + tsconfig = ":tsconfig", data = SRCS + DEPS + [ - "tsconfig.json", - "//:tsconfig.base.json", - "//:tsconfig.bazel.json", ":tsc_types", ] ) @@ -90,3 +89,11 @@ filegroup( ], visibility = ["//visibility:public"], ) + +filegroup( + name = "build_types", + srcs = [ + ":npm_module_types", + ], + visibility = ["//visibility:public"], +) diff --git a/packages/kbn-analytics/BUILD.bazel b/packages/kbn-analytics/BUILD.bazel index 898af3dabf59e..8e58e416b750e 100644 --- a/packages/kbn-analytics/BUILD.bazel +++ b/packages/kbn-analytics/BUILD.bazel @@ -4,6 +4,7 @@ load("//src/dev/bazel:index.bzl", "jsts_transpiler", "types_pkg") PKG_BASE_NAME = "kbn-analytics" PKG_REQUIRE_NAME = "@kbn/analytics" +TYPES_PKG_REQUIRE_NAME = "@types/kbn__analytics" SOURCE_FILES = glob( [ @@ -80,11 +81,9 @@ ts_project( types_pkg( name = "npm_module_types", - package_name = "@types/kbn__analytics", + package_name = TYPES_PKG_REQUIRE_NAME, + tsconfig = ":tsconfig", data = SRCS + TYPES_DEPS + [ - "tsconfig.json", - "//:tsconfig.base.json", - "//:tsconfig.bazel.json", ":tsc_types", ] ) @@ -111,3 +110,11 @@ filegroup( ], visibility = ["//visibility:public"], ) + +filegroup( + name = "build_types", + srcs = [ + ":npm_module_types", + ], + visibility = ["//visibility:public"], +) diff --git a/src/dev/bazel/types_pkg/types_pkg.bzl b/src/dev/bazel/types_pkg/types_pkg.bzl index 2c5c7b44e5cdd..96823014621b6 100644 --- a/src/dev/bazel/types_pkg/types_pkg.bzl +++ b/src/dev/bazel/types_pkg/types_pkg.bzl @@ -6,8 +6,18 @@ # Side Public License, v 1. # +load("@npm//@bazel/typescript/internal:ts_config.bzl", "TsConfigInfo") load("@build_bazel_rules_nodejs//:providers.bzl", "run_node") +def _tsconfig_inputs(ctx): + """Returns all transitively referenced tsconfig files from "tsconfig" """ + inputs = [] + if TsConfigInfo in ctx.attr.tsconfig: + inputs.extend(ctx.attr.tsconfig[TsConfigInfo].deps) + else: + inputs.append(ctx.file.tsconfig) + return inputs + def _join(*elements): segments = [f for f in elements if f] if len(segments): @@ -19,6 +29,8 @@ def _dts_inputs(pkg_path, files): def _types_pkg_impl(ctx): inputs = ctx.files.data[:] + tsconfig_inputs = _tsconfig_inputs(ctx) + inputs.extend(tsconfig_inputs) package_json_output = ctx.actions.declare_file(_join(ctx.label.name, "package.json")) outputs = [package_json_output] @@ -37,6 +49,7 @@ def _types_pkg_impl(ctx): extractor_args.add(_join(package_path, "tsconfig.json")) extractor_args.add_joined([s.path for s in _dts_inputs(package_path, ctx.files.data)], join_with = ",", omit_if_empty = False) extractor_args.add(api_extracted_output.path) + extractor_args.add_joined([s.path for s in inputs], join_with = ",", omit_if_empty = False) run_node( ctx, @@ -67,5 +80,6 @@ types_pkg = rule( cfg = "host", default = Label("//src/dev/bazel/types_pkg:api_extractor"), ), + "tsconfig": attr.label(mandatory = True, allow_single_file = [".json"]), }, ) From 78e9719b5cff273a0e1c5eb5efd7529c1a9cc436 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 6 Oct 2021 16:51:34 +0100 Subject: [PATCH 05/22] chore(NA): layed out pkg_npm_types tree artifact custom rule --- packages/elastic-datemath/BUILD.bazel | 22 +++--- packages/kbn-analytics/BUILD.bazel | 22 +++--- src/dev/bazel/index.bzl | 6 +- .../{types_pkg => pkg_npm_types}/BUILD.bazel | 6 +- .../api_extractor.js | 58 ++++++++++---- src/dev/bazel/pkg_npm_types/index.bzl | 15 ++++ .../package_json.mustache} | 2 +- .../pkg_npm_types.bzl} | 78 ++++++++++++------- 8 files changed, 138 insertions(+), 71 deletions(-) rename src/dev/bazel/{types_pkg => pkg_npm_types}/BUILD.bazel (82%) rename src/dev/bazel/{types_pkg => pkg_npm_types}/api_extractor.js (69%) create mode 100644 src/dev/bazel/pkg_npm_types/index.bzl rename src/dev/bazel/{types_pkg/package_json.tpl => pkg_npm_types/package_json.mustache} (74%) rename src/dev/bazel/{types_pkg/types_pkg.bzl => pkg_npm_types/pkg_npm_types.bzl} (51%) diff --git a/packages/elastic-datemath/BUILD.bazel b/packages/elastic-datemath/BUILD.bazel index d9816cc7207d9..0c6d720102c42 100644 --- a/packages/elastic-datemath/BUILD.bazel +++ b/packages/elastic-datemath/BUILD.bazel @@ -1,6 +1,6 @@ load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project") load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") -load("//src/dev/bazel:index.bzl", "jsts_transpiler", "types_pkg") +load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm_types") PKG_BASE_NAME = "elastic-datemath" PKG_REQUIRE_NAME = "@elastic/datemath" @@ -58,19 +58,10 @@ ts_project( tsconfig = ":tsconfig", ) -types_pkg( - name = "npm_module_types", - package_name = TYPES_PKG_REQUIRE_NAME, - tsconfig = ":tsconfig", - data = SRCS + DEPS + [ - ":tsc_types", - ] -) - js_library( name = PKG_BASE_NAME, srcs = NPM_MODULE_EXTRA_FILES, - deps = DEPS + [":target_node", ":tsc_types", ":npm_module_types"], + deps = DEPS + [":target_node", ":tsc_types"], package_name = PKG_REQUIRE_NAME, visibility = ["//visibility:public"], ) @@ -90,6 +81,15 @@ filegroup( visibility = ["//visibility:public"], ) +pkg_npm_types( + name = "npm_module_types", + package_name = TYPES_PKG_REQUIRE_NAME, + tsconfig = ":tsconfig", + data = SRCS + DEPS + [ + ":tsc_types", + ] +) + filegroup( name = "build_types", srcs = [ diff --git a/packages/kbn-analytics/BUILD.bazel b/packages/kbn-analytics/BUILD.bazel index 8e58e416b750e..17b6eb6dde5ce 100644 --- a/packages/kbn-analytics/BUILD.bazel +++ b/packages/kbn-analytics/BUILD.bazel @@ -1,6 +1,6 @@ load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project") load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") -load("//src/dev/bazel:index.bzl", "jsts_transpiler", "types_pkg") +load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm_types") PKG_BASE_NAME = "kbn-analytics" PKG_REQUIRE_NAME = "@kbn/analytics" @@ -79,19 +79,10 @@ ts_project( tsconfig = ":tsconfig", ) -types_pkg( - name = "npm_module_types", - package_name = TYPES_PKG_REQUIRE_NAME, - tsconfig = ":tsconfig", - data = SRCS + TYPES_DEPS + [ - ":tsc_types", - ] -) - js_library( name = PKG_BASE_NAME, srcs = NPM_MODULE_EXTRA_FILES, - deps = RUNTIME_DEPS + [":target_node", ":target_web", ":tsc_types", ":npm_module_types"], + deps = RUNTIME_DEPS + [":target_node", ":target_web", ":tsc_types"], package_name = PKG_REQUIRE_NAME, visibility = ["//visibility:public"], ) @@ -111,6 +102,15 @@ filegroup( visibility = ["//visibility:public"], ) +pkg_npm_types( + name = "npm_module_types", + package_name = TYPES_PKG_REQUIRE_NAME, + tsconfig = ":tsconfig", + data = SRCS + TYPES_DEPS + [ + ":tsc_types", + ] +) + filegroup( name = "build_types", srcs = [ diff --git a/src/dev/bazel/index.bzl b/src/dev/bazel/index.bzl index cdc93d0b1fad4..7d761f76aa862 100644 --- a/src/dev/bazel/index.bzl +++ b/src/dev/bazel/index.bzl @@ -6,12 +6,12 @@ # Side Public License, v 1. # -"""Public API interface for Bazel custom rules. +"""Public API interface for all Kibana Bazel custom rules. Please do not import from any other files when looking to use a custom rule """ load("//src/dev/bazel:jsts_transpiler.bzl", _jsts_transpiler = "jsts_transpiler") -load("//src/dev/bazel/types_pkg:types_pkg.bzl", _types_pkg = "types_pkg") +load("//src/dev/bazel/pkg_npm_types:index.bzl", _pkg_npm_types = "pkg_npm_types") jsts_transpiler = _jsts_transpiler -types_pkg = _types_pkg +pkg_npm_types = _pkg_npm_types diff --git a/src/dev/bazel/types_pkg/BUILD.bazel b/src/dev/bazel/pkg_npm_types/BUILD.bazel similarity index 82% rename from src/dev/bazel/types_pkg/BUILD.bazel rename to src/dev/bazel/pkg_npm_types/BUILD.bazel index 27123072d97a7..539a6dbf43088 100644 --- a/src/dev/bazel/types_pkg/BUILD.bazel +++ b/src/dev/bazel/pkg_npm_types/BUILD.bazel @@ -4,18 +4,18 @@ load("@build_bazel_rules_nodejs//internal/node:node.bzl", "nodejs_binary") exports_files( [ - "package_json.tpl", + "package_json.mustache", "api_extractor.js" ], visibility = ["//visibility:public"] ) nodejs_binary( - name = "api_extractor", + name = "_packager", data = [ "@npm//@bazel/typescript", "@npm//@microsoft/api-extractor", - ":api_extractor.js", + "@npm//mustache" ], entry_point = ":api_extractor.js", ) diff --git a/src/dev/bazel/types_pkg/api_extractor.js b/src/dev/bazel/pkg_npm_types/api_extractor.js similarity index 69% rename from src/dev/bazel/types_pkg/api_extractor.js rename to src/dev/bazel/pkg_npm_types/api_extractor.js index 1df2e7ee02b8a..a783a47604d3f 100644 --- a/src/dev/bazel/types_pkg/api_extractor.js +++ b/src/dev/bazel/pkg_npm_types/api_extractor.js @@ -20,6 +20,7 @@ console.log(process.cwd()); const { format, parseTsconfig } = require('@bazel/typescript'); const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor'); const fs = require('fs'); +const Mustache = require('mustache'); const path = require('path'); const DEBUG = false; @@ -84,6 +85,34 @@ function runMain(tsConfig, entryPoint, dtsBundleOut, apiReviewFolder, acceptApiU exports.runMain = runMain; +function generatePkgNpmTypesPackageJsonOutput( + outputBasePath, + packageJsonTemplatePath, + rawPackageJsonTemplateArgs +) { + const packageJsonTemplateArgsInTuples = rawPackageJsonTemplateArgs.reduce( + (a, v) => { + const lastTupleIdx = a.length - 1; + const lastTupleSize = a[lastTupleIdx].length; + + if (lastTupleSize < 2) { + a[lastTupleIdx].push(v); + + return a; + } + + return a.push([v]); + }, + [[]] + ); + const packageJsonTemplateArgs = Object.fromEntries(new Map(packageJsonTemplateArgsInTuples)); + console.log(packageJsonTemplateArgs); + const template = fs.readFileSync(packageJsonTemplatePath); + const renderedTemplate = Mustache.render(template.toString(), packageJsonTemplateArgs); + console.log(renderedTemplate); + fs.writeFileSync(path.resolve(outputBasePath, 'package.json'), renderedTemplate); +} + // Entry point if (require.main === module) { if (DEBUG) { @@ -94,18 +123,19 @@ api-extractor: running with ${process.argv.join('\n ')} `); } - const [tsConfig, entryPoint, dtsBundleOut] = process.argv.slice(2); - const entryPoints = entryPoint.split(','); - const dtsBundleOuts = dtsBundleOut.split(','); - if (entryPoints.length !== entryPoints.length) { - throw new Error( - `Entry points count (${entryPoints.length}) does not match Bundle out count (${dtsBundleOuts.length})` - ); - } - for (let i = 0; i < entryPoints.length; i++) { - process.exitCode = runMain(tsConfig, entryPoints[i], dtsBundleOuts[i]); - if (process.exitCode !== 0) { - break; - } - } + const [ + outputBasePath, + packageJsonTemplatePath, + stringifiedPackageJsonTemplateArgs, + tsConfig, + entryPoint, + ] = process.argv.slice(2); + const dtsBundleOutput = path.resolve(outputBasePath, 'index.d.ts'); + + generatePkgNpmTypesPackageJsonOutput( + outputBasePath, + packageJsonTemplatePath, + stringifiedPackageJsonTemplateArgs.split(',') + ); + process.exitCode = runMain(tsConfig, entryPoint, dtsBundleOutput); } diff --git a/src/dev/bazel/pkg_npm_types/index.bzl b/src/dev/bazel/pkg_npm_types/index.bzl new file mode 100644 index 0000000000000..578ecdd885d15 --- /dev/null +++ b/src/dev/bazel/pkg_npm_types/index.bzl @@ -0,0 +1,15 @@ +# +# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +# or more contributor license agreements. Licensed under the Elastic License +# 2.0 and the Server Side Public License, v 1; you may not use this file except +# in compliance with, at your election, the Elastic License 2.0 or the Server +# Side Public License, v 1. +# + +"""Public API interface for pkg_npm_types rule. +Please do not import from any other files when looking to this rule +""" + +load(":pkg_npm_types.bzl", _pkg_npm_types = "pkg_npm_types") + +pkg_npm_types = _pkg_npm_types diff --git a/src/dev/bazel/types_pkg/package_json.tpl b/src/dev/bazel/pkg_npm_types/package_json.mustache similarity index 74% rename from src/dev/bazel/types_pkg/package_json.tpl rename to src/dev/bazel/pkg_npm_types/package_json.mustache index 27b246e007876..094273a19e326 100644 --- a/src/dev/bazel/types_pkg/package_json.tpl +++ b/src/dev/bazel/pkg_npm_types/package_json.mustache @@ -1,5 +1,5 @@ { - "name": "{NAME}", + "name": "{{{NAME}}}", "description": "Generated by Bazel", "types": "./index.d.ts" } diff --git a/src/dev/bazel/types_pkg/types_pkg.bzl b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl similarity index 51% rename from src/dev/bazel/types_pkg/types_pkg.bzl rename to src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl index 96823014621b6..a91cb7388951b 100644 --- a/src/dev/bazel/types_pkg/types_pkg.bzl +++ b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl @@ -7,7 +7,7 @@ # load("@npm//@bazel/typescript/internal:ts_config.bzl", "TsConfigInfo") -load("@build_bazel_rules_nodejs//:providers.bzl", "run_node") +load("@build_bazel_rules_nodejs//:providers.bzl", "run_node", "LinkablePackageInfo") def _tsconfig_inputs(ctx): """Returns all transitively referenced tsconfig files from "tsconfig" """ @@ -27,59 +27,81 @@ def _join(*elements): def _dts_inputs(pkg_path, files): return [f for f in files if f.path.endswith(_join(pkg_path, "target_types", "index.d.ts")) and not f.path.endswith(".map")] -def _types_pkg_impl(ctx): +def _pkg_npm_types_impl(ctx): + # input declarations inputs = ctx.files.data[:] tsconfig_inputs = _tsconfig_inputs(ctx) inputs.extend(tsconfig_inputs) - package_json_output = ctx.actions.declare_file(_join(ctx.label.name, "package.json")) - outputs = [package_json_output] + inputs.extend([ctx.file._generated_package_json_template]) - ctx.actions.expand_template( - output = package_json_output, - template = ctx.file._template, - substitutions = {"{NAME}": ctx.attr.package_name}, - ) + # output dir declaration + package_dir = ctx.actions.declare_directory(ctx.label.name) + outputs = [package_dir] - api_extracted_output = ctx.actions.declare_file(_join(ctx.label.name, "index.d.ts")) - outputs.append(api_extracted_output) + # gathering template args + template_args = [ + "NAME", ctx.attr.package_name + ] + # layout api extractor arguments extractor_args = ctx.actions.args() package_path = ctx.label.package + # general args layout + ## [0] = base output dir + ## [1] = generated package json template input file path + ## [2] = stringified template args + ## [3] = tsconfig input file path + ## [4] = provided types to summarise entry point + extractor_args.add(package_dir.path) + extractor_args.add(ctx.file._generated_package_json_template.path) + extractor_args.add_joined(template_args, join_with = ",", omit_if_empty = False) extractor_args.add(_join(package_path, "tsconfig.json")) extractor_args.add_joined([s.path for s in _dts_inputs(package_path, ctx.files.data)], join_with = ",", omit_if_empty = False) - extractor_args.add(api_extracted_output.path) extractor_args.add_joined([s.path for s in inputs], join_with = ",", omit_if_empty = False) run_node( ctx, inputs = inputs, arguments = [extractor_args], - outputs = [api_extracted_output], - mnemonic = "ApiExtractor", - executable = "_api_extractor", - execution_requirements = {}, + outputs = outputs, + mnemonic = "AssembleNpmTypesPackage", + progress_message = "Assembling npm types package %s" % package_dir.short_path, + executable = "_packager", ) - return [DefaultInfo(files = depset(outputs))] + # this is a tree artifact, so correctly build the return + package_dir_depset = depset([package_dir]) -types_pkg = rule( - implementation = _types_pkg_impl, - attrs = { - "package_name": attr.string(), - "_template": attr.label( - allow_single_file = True, - default = "package_json.tpl", + return [ + DefaultInfo( + files = package_dir_depset, + runfiles = ctx.runfiles([package_dir]), ), + LinkablePackageInfo( + package_name = ctx.attr.package_name, + path = package_dir.path, + files = package_dir_depset, + ) + ] + +pkg_npm_types = rule( + implementation = _pkg_npm_types_impl, + attrs = { "data": attr.label_list( allow_files = True, ), - "_api_extractor": attr.label( - doc = "Target that executes the api-extractor binary", + "package_name": attr.string(), + "tsconfig": attr.label(mandatory = True, allow_single_file = [".json"]), + "_packager": attr.label( + doc = "Target that executes the npm types package assembler binary", executable = True, cfg = "host", - default = Label("//src/dev/bazel/types_pkg:api_extractor"), + default = Label("//src/dev/bazel/pkg_npm_types:_packager"), + ), + "_generated_package_json_template": attr.label( + allow_single_file = True, + default = "package_json.mustache", ), - "tsconfig": attr.label(mandatory = True, allow_single_file = [".json"]), }, ) From b1d9b7f8cfa7dc1c28cd8ce8279efbf64a537c7d Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 6 Oct 2021 16:54:55 +0100 Subject: [PATCH 06/22] chore(NA): missing todos --- src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl index a91cb7388951b..14ac723d3c9a7 100644 --- a/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl +++ b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl @@ -9,6 +9,14 @@ load("@npm//@bazel/typescript/internal:ts_config.bzl", "TsConfigInfo") load("@build_bazel_rules_nodejs//:providers.bzl", "run_node", "LinkablePackageInfo") + +#### TODO +###### improve provided types entry point to summarise logic +###### refact api_extractor file into packager folder with better js code architecture +###### link pkg npm types into node modules for migrated modules +###### study around completely split types and code trees for all packages (also test if we can symlink pkg npm into /npm_module instead of base folder) +###### source maps for api extractor + def _tsconfig_inputs(ctx): """Returns all transitively referenced tsconfig files from "tsconfig" """ inputs = [] From bdead3ef25ec185bbd2c0d4676d0471caae9ee82 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 6 Oct 2021 22:36:53 +0100 Subject: [PATCH 07/22] chore(NA): node modules link mapping --- src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl | 50 ++++++++++++++----- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl index 14ac723d3c9a7..83874ecae47cc 100644 --- a/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl +++ b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl @@ -7,7 +7,8 @@ # load("@npm//@bazel/typescript/internal:ts_config.bzl", "TsConfigInfo") -load("@build_bazel_rules_nodejs//:providers.bzl", "run_node", "LinkablePackageInfo") +load("@build_bazel_rules_nodejs//:providers.bzl", "run_node", "LinkablePackageInfo", "declaration_info") +load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module_mappings_aspect") #### TODO @@ -35,14 +36,30 @@ def _join(*elements): def _dts_inputs(pkg_path, files): return [f for f in files if f.path.endswith(_join(pkg_path, "target_types", "index.d.ts")) and not f.path.endswith(".map")] +def _deps_inputs(ctx): + """Returns all transitively referenced files on deps """ + deps_files_depsets = [] + for dep in ctx.attr.deps: + # Collect whatever is in the "data" + deps_files_depsets.append(dep.data_runfiles.files) + + # Only collect DefaultInfo files (not transitive) + deps_files_depsets.append(dep.files) + + deps_files = depset(transitive = deps_files_depsets).to_list() + return deps_files + def _pkg_npm_types_impl(ctx): # input declarations - inputs = ctx.files.data[:] + deps_inputs = _deps_inputs(ctx) tsconfig_inputs = _tsconfig_inputs(ctx) + inputs = ctx.files.srcs[:] inputs.extend(tsconfig_inputs) - inputs.extend([ctx.file._generated_package_json_template]) + inputs.extend(deps_inputs) + inputs.append(ctx.file._generated_package_json_template) # output dir declaration + package_path = ctx.label.package package_dir = ctx.actions.declare_directory(ctx.label.name) outputs = [package_dir] @@ -53,20 +70,19 @@ def _pkg_npm_types_impl(ctx): # layout api extractor arguments extractor_args = ctx.actions.args() - package_path = ctx.label.package - # general args layout - ## [0] = base output dir - ## [1] = generated package json template input file path - ## [2] = stringified template args - ## [3] = tsconfig input file path - ## [4] = provided types to summarise entry point + ## general args layout + ### [0] = base output dir + ### [1] = generated package json template input file path + ### [2] = stringified template args + ### [3] = tsconfig input file path + ### [4] = provided types to summarise entry point extractor_args.add(package_dir.path) extractor_args.add(ctx.file._generated_package_json_template.path) extractor_args.add_joined(template_args, join_with = ",", omit_if_empty = False) extractor_args.add(_join(package_path, "tsconfig.json")) extractor_args.add_joined([s.path for s in _dts_inputs(package_path, ctx.files.data)], join_with = ",", omit_if_empty = False) - extractor_args.add_joined([s.path for s in inputs], join_with = ",", omit_if_empty = False) + extractor_args.add_joined([s.path for s in deps_inputs], join_with = ",", omit_if_empty = False) run_node( ctx, @@ -86,8 +102,12 @@ def _pkg_npm_types_impl(ctx): files = package_dir_depset, runfiles = ctx.runfiles([package_dir]), ), + declaration_info( + declarations = depset([package_dir]) + ), LinkablePackageInfo( package_name = ctx.attr.package_name, + package_path = "", path = package_dir.path, files = package_dir_depset, ) @@ -96,10 +116,16 @@ def _pkg_npm_types_impl(ctx): pkg_npm_types = rule( implementation = _pkg_npm_types_impl, attrs = { - "data": attr.label_list( + "deps": attr.label_list( + doc = """Other targets which are the base types to summarise from""", allow_files = True, + aspects = [module_mappings_aspect], ), "package_name": attr.string(), + "srcs": attr.label_list( + doc = """Files inside this directory which are inputs for the types to summarise.""", + allow_files = True, + ), "tsconfig": attr.label(mandatory = True, allow_single_file = [".json"]), "_packager": attr.label( doc = "Target that executes the npm types package assembler binary", From ae054bbe36677b9e362db7a5f09ad9ddf7c05f93 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Thu, 7 Oct 2021 04:10:32 +0100 Subject: [PATCH 08/22] chore(NA): fully working pkg_npm_types rule --- package.json | 1 + packages/BUILD.bazel | 4 +- packages/elastic-datemath/BUILD.bazel | 12 ++-- packages/elastic-datemath/package.json | 1 - packages/kbn-analytics/BUILD.bazel | 20 +----- .../BUILD.bazel | 2 +- packages/kbn-ui-shared-deps-npm/BUILD.bazel | 2 +- packages/kbn-ui-shared-deps-src/BUILD.bazel | 2 +- src/dev/bazel/pkg_npm_types/BUILD.bazel | 13 +++- .../create_api_extraction.js} | 70 +++---------------- .../packager/generate_package_json.js | 43 ++++++++++++ src/dev/bazel/pkg_npm_types/packager/index.js | 46 ++++++++++++ src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl | 59 +++++++++------- yarn.lock | 4 ++ 14 files changed, 157 insertions(+), 122 deletions(-) rename src/dev/bazel/pkg_npm_types/{api_extractor.js => packager/create_api_extraction.js} (61%) create mode 100644 src/dev/bazel/pkg_npm_types/packager/generate_package_json.js create mode 100644 src/dev/bazel/pkg_npm_types/packager/index.js diff --git a/package.json b/package.json index c9de86c177ead..dff3a8eb26160 100644 --- a/package.json +++ b/package.json @@ -516,6 +516,7 @@ "@types/delete-empty": "^2.0.0", "@types/ejs": "^3.0.6", "@types/elasticsearch": "^5.0.33", + "@types/elastic__datemath": "link:bazel-bin/packages/elastic-datemath/npm_module_types", "@types/enzyme": "^3.10.8", "@types/eslint": "^7.28.0", "@types/extract-zip": "^1.6.2", diff --git a/packages/BUILD.bazel b/packages/BUILD.bazel index 41033b77c7436..832b5f745460f 100644 --- a/packages/BUILD.bazel +++ b/packages/BUILD.bazel @@ -4,6 +4,7 @@ filegroup( name = "build", srcs = [ "//packages/elastic-datemath:build", + "//packages/elastic-datemath:build_types", "//packages/elastic-eslint-config-kibana:build", "//packages/elastic-safer-lodash-set:build", "//packages/kbn-ace:build", @@ -65,8 +66,5 @@ filegroup( "//packages/kbn-ui-shared-deps-src:build", "//packages/kbn-utility-types:build", "//packages/kbn-utils:build", - - "//packages/elastic-datemath:build_types", - "//packages/kbn-analytics:build_types", ], ) diff --git a/packages/elastic-datemath/BUILD.bazel b/packages/elastic-datemath/BUILD.bazel index 0c6d720102c42..ce4fbd5035930 100644 --- a/packages/elastic-datemath/BUILD.bazel +++ b/packages/elastic-datemath/BUILD.bazel @@ -27,7 +27,7 @@ TYPES_DEPS = [ "@npm//@types/node", ] -DEPS = TYPES_DEPS +RUNTIME_DEPS = TYPES_DEPS jsts_transpiler( name = "target_node", @@ -48,7 +48,7 @@ ts_project( name = "tsc_types", args = ['--pretty'], srcs = SRCS, - deps = DEPS, + deps = TYPES_DEPS, declaration = True, declaration_map = True, emit_declaration_only = True, @@ -61,7 +61,7 @@ ts_project( js_library( name = PKG_BASE_NAME, srcs = NPM_MODULE_EXTRA_FILES, - deps = DEPS + [":target_node", ":tsc_types"], + deps = RUNTIME_DEPS + [":target_node"], package_name = PKG_REQUIRE_NAME, visibility = ["//visibility:public"], ) @@ -83,11 +83,11 @@ filegroup( pkg_npm_types( name = "npm_module_types", + srcs = SRCS, + deps = [":tsc_types"], package_name = TYPES_PKG_REQUIRE_NAME, tsconfig = ":tsconfig", - data = SRCS + DEPS + [ - ":tsc_types", - ] + visibility = ["//visibility:public"], ) filegroup( diff --git a/packages/elastic-datemath/package.json b/packages/elastic-datemath/package.json index b68a24d6cb7ae..af958e3730c3d 100644 --- a/packages/elastic-datemath/package.json +++ b/packages/elastic-datemath/package.json @@ -4,7 +4,6 @@ "description": "elasticsearch datemath parser, used in kibana", "license": "Apache-2.0", "main": "./target_node/index.js", - "types": "./target_types/index.d.ts", "peerDependencies": { "moment": "^2.24.0" } diff --git a/packages/kbn-analytics/BUILD.bazel b/packages/kbn-analytics/BUILD.bazel index 17b6eb6dde5ce..cc65746e890ce 100644 --- a/packages/kbn-analytics/BUILD.bazel +++ b/packages/kbn-analytics/BUILD.bazel @@ -1,10 +1,9 @@ load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project") load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") -load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm_types") +load("//src/dev/bazel:index.bzl", "jsts_transpiler") PKG_BASE_NAME = "kbn-analytics" PKG_REQUIRE_NAME = "@kbn/analytics" -TYPES_PKG_REQUIRE_NAME = "@types/kbn__analytics" SOURCE_FILES = glob( [ @@ -101,20 +100,3 @@ filegroup( ], visibility = ["//visibility:public"], ) - -pkg_npm_types( - name = "npm_module_types", - package_name = TYPES_PKG_REQUIRE_NAME, - tsconfig = ":tsconfig", - data = SRCS + TYPES_DEPS + [ - ":tsc_types", - ] -) - -filegroup( - name = "build_types", - srcs = [ - ":npm_module_types", - ], - visibility = ["//visibility:public"], -) diff --git a/packages/kbn-securitysolution-io-ts-utils/BUILD.bazel b/packages/kbn-securitysolution-io-ts-utils/BUILD.bazel index 24819bdd16a33..c827eb19f7a3d 100644 --- a/packages/kbn-securitysolution-io-ts-utils/BUILD.bazel +++ b/packages/kbn-securitysolution-io-ts-utils/BUILD.bazel @@ -37,7 +37,7 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/elastic-datemath", + "//packages/elastic-datemath:npm_module_types", "@npm//fp-ts", "@npm//io-ts", "@npm//moment", diff --git a/packages/kbn-ui-shared-deps-npm/BUILD.bazel b/packages/kbn-ui-shared-deps-npm/BUILD.bazel index ae3a6f05d456b..8d45afb6b8c44 100644 --- a/packages/kbn-ui-shared-deps-npm/BUILD.bazel +++ b/packages/kbn-ui-shared-deps-npm/BUILD.bazel @@ -71,7 +71,7 @@ WEBPACK_DEPS = [ ] TYPES_DEPS = [ - "//packages/elastic-datemath", + "//packages/elastic-datemath:npm_module_types", "//packages/kbn-utils", "@npm//@elastic/charts", "@npm//@elastic/eui", diff --git a/packages/kbn-ui-shared-deps-src/BUILD.bazel b/packages/kbn-ui-shared-deps-src/BUILD.bazel index f23c86769fb49..33aa6096bd3e0 100644 --- a/packages/kbn-ui-shared-deps-src/BUILD.bazel +++ b/packages/kbn-ui-shared-deps-src/BUILD.bazel @@ -42,7 +42,7 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/elastic-datemath", + "//packages/elastic-datemath:npm_module_types", "//packages/elastic-safer-lodash-set", "//packages/kbn-analytics", "//packages/kbn-babel-preset", diff --git a/src/dev/bazel/pkg_npm_types/BUILD.bazel b/src/dev/bazel/pkg_npm_types/BUILD.bazel index 539a6dbf43088..f30d0f8cb8324 100644 --- a/src/dev/bazel/pkg_npm_types/BUILD.bazel +++ b/src/dev/bazel/pkg_npm_types/BUILD.bazel @@ -2,10 +2,16 @@ package(default_visibility = ["//visibility:public"]) load("@build_bazel_rules_nodejs//internal/node:node.bzl", "nodejs_binary") +filegroup( + name = "packager_all_files", + srcs = glob([ + "packager/*", + ]), +) + exports_files( [ "package_json.mustache", - "api_extractor.js" ], visibility = ["//visibility:public"] ) @@ -15,7 +21,8 @@ nodejs_binary( data = [ "@npm//@bazel/typescript", "@npm//@microsoft/api-extractor", - "@npm//mustache" + "@npm//mustache", + ":packager_all_files" ], - entry_point = ":api_extractor.js", + entry_point = ":packager/index.js", ) diff --git a/src/dev/bazel/pkg_npm_types/api_extractor.js b/src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js similarity index 61% rename from src/dev/bazel/pkg_npm_types/api_extractor.js rename to src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js index a783a47604d3f..8d7e72677e11d 100644 --- a/src/dev/bazel/pkg_npm_types/api_extractor.js +++ b/src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js @@ -14,17 +14,18 @@ * found in the LICENSE file at https://angular.io/license */ -console.log(process.argv); -console.log(process.cwd()); - const { format, parseTsconfig } = require('@bazel/typescript'); const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor'); const fs = require('fs'); -const Mustache = require('mustache'); const path = require('path'); -const DEBUG = false; -function runMain(tsConfig, entryPoint, dtsBundleOut, apiReviewFolder, acceptApiUpdates = false) { +function createApiExtraction( + tsConfig, + entryPoint, + dtsBundleOut, + apiReviewFolder, + acceptApiUpdates = false +) { const [parsedConfig, errors] = parseTsconfig(tsConfig); if (errors && errors.length) { console.error(format('', errors)); @@ -83,59 +84,4 @@ function runMain(tsConfig, entryPoint, dtsBundleOut, apiReviewFolder, acceptApiU return succeeded ? 0 : 1; } -exports.runMain = runMain; - -function generatePkgNpmTypesPackageJsonOutput( - outputBasePath, - packageJsonTemplatePath, - rawPackageJsonTemplateArgs -) { - const packageJsonTemplateArgsInTuples = rawPackageJsonTemplateArgs.reduce( - (a, v) => { - const lastTupleIdx = a.length - 1; - const lastTupleSize = a[lastTupleIdx].length; - - if (lastTupleSize < 2) { - a[lastTupleIdx].push(v); - - return a; - } - - return a.push([v]); - }, - [[]] - ); - const packageJsonTemplateArgs = Object.fromEntries(new Map(packageJsonTemplateArgsInTuples)); - console.log(packageJsonTemplateArgs); - const template = fs.readFileSync(packageJsonTemplatePath); - const renderedTemplate = Mustache.render(template.toString(), packageJsonTemplateArgs); - console.log(renderedTemplate); - fs.writeFileSync(path.resolve(outputBasePath, 'package.json'), renderedTemplate); -} - -// Entry point -if (require.main === module) { - if (DEBUG) { - console.error(` -api-extractor: running with - cwd: ${process.cwd()} - argv: - ${process.argv.join('\n ')} - `); - } - const [ - outputBasePath, - packageJsonTemplatePath, - stringifiedPackageJsonTemplateArgs, - tsConfig, - entryPoint, - ] = process.argv.slice(2); - const dtsBundleOutput = path.resolve(outputBasePath, 'index.d.ts'); - - generatePkgNpmTypesPackageJsonOutput( - outputBasePath, - packageJsonTemplatePath, - stringifiedPackageJsonTemplateArgs.split(',') - ); - process.exitCode = runMain(tsConfig, entryPoint, dtsBundleOutput); -} +module.exports.createApiExtraction = createApiExtraction; diff --git a/src/dev/bazel/pkg_npm_types/packager/generate_package_json.js b/src/dev/bazel/pkg_npm_types/packager/generate_package_json.js new file mode 100644 index 0000000000000..d4a478a262e5b --- /dev/null +++ b/src/dev/bazel/pkg_npm_types/packager/generate_package_json.js @@ -0,0 +1,43 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +const fs = require('fs'); +const Mustache = require('mustache'); +const path = require('path'); + +function generatePackageJson(outputBasePath, packageJsonTemplatePath, rawPackageJsonTemplateArgs) { + const packageJsonTemplateArgsInTuples = rawPackageJsonTemplateArgs.reduce( + (a, v) => { + const lastTupleIdx = a.length - 1; + const lastTupleSize = a[lastTupleIdx].length; + + if (lastTupleSize < 2) { + a[lastTupleIdx].push(v); + + return a; + } + + return a.push([v]); + }, + [[]] + ); + const packageJsonTemplateArgs = Object.fromEntries(new Map(packageJsonTemplateArgsInTuples)); + + try { + const template = fs.readFileSync(packageJsonTemplatePath); + const renderedTemplate = Mustache.render(template.toString(), packageJsonTemplateArgs); + fs.writeFileSync(path.resolve(outputBasePath, 'package.json'), renderedTemplate); + } catch (e) { + console.error(e); + return 1; + } + + return 0; +} + +module.exports.generatePackageJson = generatePackageJson; diff --git a/src/dev/bazel/pkg_npm_types/packager/index.js b/src/dev/bazel/pkg_npm_types/packager/index.js new file mode 100644 index 0000000000000..cda299a99d76f --- /dev/null +++ b/src/dev/bazel/pkg_npm_types/packager/index.js @@ -0,0 +1,46 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +const { createApiExtraction } = require('./create_api_extraction'); +const { generatePackageJson } = require('./generate_package_json'); +const path = require('path'); + +const DEBUG = false; + +if (require.main === module) { + if (DEBUG) { + console.error(` +pkg_npm_types packager: running with + cwd: ${process.cwd()} + argv: + ${process.argv.join('\n ')} + `); + } + + // layout args + const [ + outputBasePath, + packageJsonTemplatePath, + stringifiedPackageJsonTemplateArgs, + tsConfig, + entryPoint, + ] = process.argv.slice(2); + const dtsBundleOutput = path.resolve(outputBasePath, 'index.d.ts'); + + // generate pkg json output + const generatePackageJsonRValue = generatePackageJson( + outputBasePath, + packageJsonTemplatePath, + stringifiedPackageJsonTemplateArgs.split(',') + ); + // create api extraction output + const createApiExtractionRValue = createApiExtraction(tsConfig, entryPoint, dtsBundleOutput); + + // setup correct exit code + process.exitCode = generatePackageJsonRValue || createApiExtractionRValue; +} diff --git a/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl index 83874ecae47cc..6e3eb27c6fdab 100644 --- a/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl +++ b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl @@ -12,30 +12,10 @@ load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module #### TODO -###### improve provided types entry point to summarise logic -###### refact api_extractor file into packager folder with better js code architecture -###### link pkg npm types into node modules for migrated modules +###### refact main build bazel file for packages ###### study around completely split types and code trees for all packages (also test if we can symlink pkg npm into /npm_module instead of base folder) ###### source maps for api extractor -def _tsconfig_inputs(ctx): - """Returns all transitively referenced tsconfig files from "tsconfig" """ - inputs = [] - if TsConfigInfo in ctx.attr.tsconfig: - inputs.extend(ctx.attr.tsconfig[TsConfigInfo].deps) - else: - inputs.append(ctx.file.tsconfig) - return inputs - -def _join(*elements): - segments = [f for f in elements if f] - if len(segments): - return "/".join(segments) - return "." - -def _dts_inputs(pkg_path, files): - return [f for f in files if f.path.endswith(_join(pkg_path, "target_types", "index.d.ts")) and not f.path.endswith(".map")] - def _deps_inputs(ctx): """Returns all transitively referenced files on deps """ deps_files_depsets = [] @@ -49,6 +29,31 @@ def _deps_inputs(ctx): deps_files = depset(transitive = deps_files_depsets).to_list() return deps_files +def _calculate_entrypoint_path(ctx): + return _join(ctx.bin_dir.path, ctx.label.package, _get_types_outdir_name(ctx), ctx.attr.entrypoint_name) + +def _get_types_outdir_name(ctx): + base_out_folder = _join(ctx.bin_dir.path, ctx.label.package) + type_dep_path = ctx.files.deps[0].path + type_dep_path_without_base_out = type_dep_path.replace(base_out_folder + "/", "", 1) + types_outdir_name = type_dep_path_without_base_out.split("/")[0] + return types_outdir_name + +def _join(*elements): + segments = [f for f in elements if f] + if len(segments): + return "/".join(segments) + return "." + +def _tsconfig_inputs(ctx): + """Returns all transitively referenced tsconfig files from "tsconfig" """ + all_inputs = [] + if TsConfigInfo in ctx.attr.tsconfig: + all_inputs.extend(ctx.attr.tsconfig[TsConfigInfo].deps) + else: + all_inputs.append(ctx.file.tsconfig) + return all_inputs + def _pkg_npm_types_impl(ctx): # input declarations deps_inputs = _deps_inputs(ctx) @@ -76,13 +81,13 @@ def _pkg_npm_types_impl(ctx): ### [1] = generated package json template input file path ### [2] = stringified template args ### [3] = tsconfig input file path - ### [4] = provided types to summarise entry point + ### [4] = entry point from provided types to summarise extractor_args.add(package_dir.path) extractor_args.add(ctx.file._generated_package_json_template.path) extractor_args.add_joined(template_args, join_with = ",", omit_if_empty = False) - extractor_args.add(_join(package_path, "tsconfig.json")) - extractor_args.add_joined([s.path for s in _dts_inputs(package_path, ctx.files.data)], join_with = ",", omit_if_empty = False) - extractor_args.add_joined([s.path for s in deps_inputs], join_with = ",", omit_if_empty = False) + extractor_args.add(tsconfig_inputs[0]) + extractor_args.add(_calculate_entrypoint_path(ctx)) + # extractor_args.add_joined([s.path for s in ctx.files.deps], join_with = ",", omit_if_empty = False) run_node( ctx, @@ -121,6 +126,10 @@ pkg_npm_types = rule( allow_files = True, aspects = [module_mappings_aspect], ), + "entrypoint_name": attr.string( + doc = """Entrypoint name of the types files group to summarise""", + default = "index.d.ts", + ), "package_name": attr.string(), "srcs": attr.label_list( doc = """Files inside this directory which are inputs for the types to summarise.""", diff --git a/yarn.lock b/yarn.lock index 75d264db7f537..13aa7bc30681b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6033,6 +6033,10 @@ resolved "https://registry.yarnpkg.com/@types/ejs/-/ejs-3.0.6.tgz#aca442289df623bfa8e47c23961f0357847b83fe" integrity sha512-fj1hi+ZSW0xPLrJJD+YNwIh9GZbyaIepG26E/gXvp8nCa2pYokxUYO1sK9qjGxp2g8ryZYuon7wmjpwE2cyASQ== +"@types/elastic__datemath@link:bazel-bin/packages/elastic-datemath/npm_module_types": + version "0.0.0" + uid "" + "@types/elasticsearch@^5.0.33": version "5.0.33" resolved "https://registry.yarnpkg.com/@types/elasticsearch/-/elasticsearch-5.0.33.tgz#b0fd37dc674f498223b6d68c313bdfd71f4d812b" From 5f5ed46508bda3c0277f32358c9d6816924779a9 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Tue, 26 Oct 2021 12:49:37 +0100 Subject: [PATCH 09/22] chore(NA): fix changes on new packages using elastic datemath pkgs --- packages/BUILD.bazel | 27 ++++++++++++++++++--- packages/elastic-apm-synthtrace/BUILD.bazel | 2 +- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/BUILD.bazel b/packages/BUILD.bazel index 20130a68560e3..0bbde05484690 100644 --- a/packages/BUILD.bazel +++ b/packages/BUILD.bazel @@ -1,11 +1,9 @@ -# Grouping target to call all underlying packages build -# targets so we can build them all at once +# It will build all declared code packages filegroup( - name = "build", + name = "build_pkg_code", srcs = [ "//packages/elastic-apm-synthtrace:build", "//packages/elastic-datemath:build", - "//packages/elastic-datemath:build_types", "//packages/elastic-eslint-config-kibana:build", "//packages/elastic-safer-lodash-set:build", "//packages/kbn-ace:build", @@ -69,3 +67,24 @@ filegroup( "//packages/kbn-utils:build", ], ) + +# It will build all declared package types +filegroup( + name = "build_pkg_types", + srcs = [ + "//packages/elastic-datemath:build_types", + ], +) + + + +# Grouping target to call all underlying packages build +# targets so we can build them all at once +# It will auto build all declared code packages and types packages +filegroup( + name = "build", + srcs = [ + ":build_pkg_code", + ":build_pkg_types" + ], +) diff --git a/packages/elastic-apm-synthtrace/BUILD.bazel b/packages/elastic-apm-synthtrace/BUILD.bazel index 5d9510c6a81d5..8ae1632ffee4c 100644 --- a/packages/elastic-apm-synthtrace/BUILD.bazel +++ b/packages/elastic-apm-synthtrace/BUILD.bazel @@ -37,7 +37,7 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/elastic-datemath", + "//packages/elastic-datemath:npm_module_types", "@npm//@elastic/elasticsearch", "@npm//moment", "@npm//p-limit", From e1b2ec05827f6c6a540c03399829c498d745af8e Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Tue, 26 Oct 2021 12:51:27 +0100 Subject: [PATCH 10/22] docs(NA): remove todo --- src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl index 6e3eb27c6fdab..f0df4c539cd1a 100644 --- a/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl +++ b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl @@ -12,7 +12,6 @@ load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module #### TODO -###### refact main build bazel file for packages ###### study around completely split types and code trees for all packages (also test if we can symlink pkg npm into /npm_module instead of base folder) ###### source maps for api extractor From ac72784c07d5005accb4f2920a9286fb17a861ab Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 27 Oct 2021 01:56:55 +0100 Subject: [PATCH 11/22] docs(NA): last todo text correction --- src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl index f0df4c539cd1a..e956b7a12058a 100644 --- a/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl +++ b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl @@ -12,8 +12,8 @@ load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module #### TODO -###### study around completely split types and code trees for all packages (also test if we can symlink pkg npm into /npm_module instead of base folder) -###### source maps for api extractor +# Implement a way to produce source maps for api extractor +# summarised types as referenced at (https://github.com/microsoft/rushstack/issues/1886#issuecomment-933997910) def _deps_inputs(ctx): """Returns all transitively referenced files on deps """ From a036832e1f3fff537cf04a9cc35df39da797301e Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Thu, 28 Oct 2021 16:43:04 +0100 Subject: [PATCH 12/22] chore(NA): removed commented lines --- src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl index e956b7a12058a..a40624d31e38b 100644 --- a/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl +++ b/src/dev/bazel/pkg_npm_types/pkg_npm_types.bzl @@ -86,7 +86,6 @@ def _pkg_npm_types_impl(ctx): extractor_args.add_joined(template_args, join_with = ",", omit_if_empty = False) extractor_args.add(tsconfig_inputs[0]) extractor_args.add(_calculate_entrypoint_path(ctx)) - # extractor_args.add_joined([s.path for s in ctx.files.deps], join_with = ",", omit_if_empty = False) run_node( ctx, From c3d6e43a9d66f8d34b885efb812f2921b7459a9c Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Thu, 28 Oct 2021 17:10:37 +0100 Subject: [PATCH 13/22] fix(NA): include missing package version --- src/dev/bazel/pkg_npm_types/package_json.mustache | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dev/bazel/pkg_npm_types/package_json.mustache b/src/dev/bazel/pkg_npm_types/package_json.mustache index 094273a19e326..9d1dc12c8f16e 100644 --- a/src/dev/bazel/pkg_npm_types/package_json.mustache +++ b/src/dev/bazel/pkg_npm_types/package_json.mustache @@ -1,5 +1,6 @@ { "name": "{{{NAME}}}", "description": "Generated by Bazel", - "types": "./index.d.ts" + "types": "./index.d.ts", + "version": "1.0.0" } From c9de76ff20e888676d0863e4a0038ba498f6d2ef Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Thu, 28 Oct 2021 21:28:43 +0100 Subject: [PATCH 14/22] chore(NA): include license keys --- src/dev/bazel/pkg_npm_types/package_json.mustache | 1 + src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/dev/bazel/pkg_npm_types/package_json.mustache b/src/dev/bazel/pkg_npm_types/package_json.mustache index 9d1dc12c8f16e..14dee45bf7de7 100644 --- a/src/dev/bazel/pkg_npm_types/package_json.mustache +++ b/src/dev/bazel/pkg_npm_types/package_json.mustache @@ -2,5 +2,6 @@ "name": "{{{NAME}}}", "description": "Generated by Bazel", "types": "./index.d.ts", + "license": "SSPL-1.0 OR Elastic License 2.0", "version": "1.0.0" } diff --git a/src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js b/src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js index 8d7e72677e11d..39f814e3325c4 100644 --- a/src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js +++ b/src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js @@ -37,8 +37,10 @@ function createApiExtraction( pkgJson, JSON.stringify({ name: 'GENERATED-BY-BAZEL', - version: '0.0.0', description: 'This is a dummy package.json as API Extractor always requires one.', + types: './index.d.ts', + license: 'SSPL-1.0 OR Elastic License 2.0', + version: '1.0.0', }) ); } From 051d25f91c5ed3c9586a7db12cf8d36a2170bc1c Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Fri, 29 Oct 2021 02:20:52 +0100 Subject: [PATCH 15/22] chore(NA): change mock types package into private --- src/dev/bazel/pkg_npm_types/package_json.mustache | 5 +++-- .../bazel/pkg_npm_types/packager/create_api_extraction.js | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/dev/bazel/pkg_npm_types/package_json.mustache b/src/dev/bazel/pkg_npm_types/package_json.mustache index 14dee45bf7de7..2229345252e3f 100644 --- a/src/dev/bazel/pkg_npm_types/package_json.mustache +++ b/src/dev/bazel/pkg_npm_types/package_json.mustache @@ -2,6 +2,7 @@ "name": "{{{NAME}}}", "description": "Generated by Bazel", "types": "./index.d.ts", - "license": "SSPL-1.0 OR Elastic License 2.0", - "version": "1.0.0" + "private": true, + "license": "MIT", + "version": "1.1.0" } diff --git a/src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js b/src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js index 39f814e3325c4..d5f7e0c33ff1c 100644 --- a/src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js +++ b/src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js @@ -39,6 +39,7 @@ function createApiExtraction( name: 'GENERATED-BY-BAZEL', description: 'This is a dummy package.json as API Extractor always requires one.', types: './index.d.ts', + private: true, license: 'SSPL-1.0 OR Elastic License 2.0', version: '1.0.0', }) From 3fb47d4c843c40084e9da5a84aee7e7b23c11d7c Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 3 Nov 2021 16:45:28 +0000 Subject: [PATCH 16/22] chore(NA): split types_pkg from code_pkg on @elastic/apm-synthtrace --- package.json | 1 + packages/BUILD.bazel | 1 + packages/elastic-apm-synthtrace/BUILD.bazel | 29 +++++++++++++++----- packages/elastic-apm-synthtrace/package.json | 1 - yarn.lock | 4 +++ 5 files changed, 28 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 63d2207674ecc..d6e4f220c4a7c 100644 --- a/package.json +++ b/package.json @@ -511,6 +511,7 @@ "@types/delete-empty": "^2.0.0", "@types/ejs": "^3.0.6", "@types/elasticsearch": "^5.0.33", + "@types/elastic__apm-synthtrace": "link:bazel-bin/packages/elastic-apm-synthtrace/npm_module_types", "@types/elastic__datemath": "link:bazel-bin/packages/elastic-datemath/npm_module_types", "@types/enzyme": "^3.10.8", "@types/eslint": "^7.28.0", diff --git a/packages/BUILD.bazel b/packages/BUILD.bazel index a03072fc64663..ea13727ce7615 100644 --- a/packages/BUILD.bazel +++ b/packages/BUILD.bazel @@ -73,6 +73,7 @@ filegroup( filegroup( name = "build_pkg_types", srcs = [ + "//packages/elastic-apm-synthtrace:build_types", "//packages/elastic-datemath:build_types", ], ) diff --git a/packages/elastic-apm-synthtrace/BUILD.bazel b/packages/elastic-apm-synthtrace/BUILD.bazel index 8ae1632ffee4c..660d866b3db50 100644 --- a/packages/elastic-apm-synthtrace/BUILD.bazel +++ b/packages/elastic-apm-synthtrace/BUILD.bazel @@ -1,9 +1,10 @@ load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project") load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") -load("//src/dev/bazel:index.bzl", "jsts_transpiler") +load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm_types") PKG_BASE_NAME = "elastic-apm-synthtrace" PKG_REQUIRE_NAME = "@elastic/apm-synthtrace" +TYPES_PKG_REQUIRE_NAME = "@types/elastic__apm-synthtrace" SOURCE_FILES = glob( [ @@ -31,21 +32,18 @@ RUNTIME_DEPS = [ "@npm//moment", "@npm//object-hash", "@npm//p-limit", - "@npm//utility-types", - "@npm//uuid", "@npm//yargs", ] TYPES_DEPS = [ "//packages/elastic-datemath:npm_module_types", "@npm//@elastic/elasticsearch", - "@npm//moment", - "@npm//p-limit", "@npm//@types/jest", "@npm//@types/lodash", "@npm//@types/node", - "@npm//@types/uuid", "@npm//@types/object-hash", + "@npm//moment", + "@npm//p-limit", ] jsts_transpiler( @@ -80,7 +78,7 @@ ts_project( js_library( name = PKG_BASE_NAME, srcs = NPM_MODULE_EXTRA_FILES, - deps = RUNTIME_DEPS + [":target_node", ":tsc_types"], + deps = RUNTIME_DEPS + [":target_node"], package_name = PKG_REQUIRE_NAME, visibility = ["//visibility:public"], ) @@ -99,3 +97,20 @@ filegroup( ], visibility = ["//visibility:public"], ) + +pkg_npm_types( + name = "npm_module_types", + srcs = SRCS, + deps = [":tsc_types"], + package_name = TYPES_PKG_REQUIRE_NAME, + tsconfig = ":tsconfig", + visibility = ["//visibility:public"], +) + +filegroup( + name = "build_types", + srcs = [ + ":npm_module_types", + ], + visibility = ["//visibility:public"], +) diff --git a/packages/elastic-apm-synthtrace/package.json b/packages/elastic-apm-synthtrace/package.json index 43699e4795586..d2f2016a891b3 100644 --- a/packages/elastic-apm-synthtrace/package.json +++ b/packages/elastic-apm-synthtrace/package.json @@ -4,6 +4,5 @@ "description": "Elastic APM trace data generator", "license": "SSPL-1.0 OR Elastic License 2.0", "main": "./target_node/index.js", - "types": "./target_types/index.d.ts", "private": true } diff --git a/yarn.lock b/yarn.lock index ce7dfffa4e475..86e10131fd10a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6095,6 +6095,10 @@ resolved "https://registry.yarnpkg.com/@types/ejs/-/ejs-3.0.6.tgz#aca442289df623bfa8e47c23961f0357847b83fe" integrity sha512-fj1hi+ZSW0xPLrJJD+YNwIh9GZbyaIepG26E/gXvp8nCa2pYokxUYO1sK9qjGxp2g8ryZYuon7wmjpwE2cyASQ== +"@types/elastic__apm-synthtrace@link:bazel-bin/packages/elastic-apm-synthtrace/npm_module_types": + version "0.0.0" + uid "" + "@types/elastic__datemath@link:bazel-bin/packages/elastic-datemath/npm_module_types": version "0.0.0" uid "" From 2f575876a213fcd786bf0e8bf3e2cab863b9aa0a Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 3 Nov 2021 19:21:07 +0000 Subject: [PATCH 17/22] chore(NA): disable validator on ts_project rule --- packages/elastic-apm-synthtrace/BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/elastic-apm-synthtrace/BUILD.bazel b/packages/elastic-apm-synthtrace/BUILD.bazel index 660d866b3db50..49c097656fe9f 100644 --- a/packages/elastic-apm-synthtrace/BUILD.bazel +++ b/packages/elastic-apm-synthtrace/BUILD.bazel @@ -73,6 +73,7 @@ ts_project( root_dir = "src", source_map = True, tsconfig = ":tsconfig", + validate = False, ) js_library( From 1492f533c175e1385d8b532d3fa6f10fdb75fc41 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Fri, 5 Nov 2021 16:14:50 +0000 Subject: [PATCH 18/22] chore(NA): move into ts_project wrapper --- packages/elastic-apm-synthtrace/BUILD.bazel | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/elastic-apm-synthtrace/BUILD.bazel b/packages/elastic-apm-synthtrace/BUILD.bazel index 49c097656fe9f..4b178880ffbe8 100644 --- a/packages/elastic-apm-synthtrace/BUILD.bazel +++ b/packages/elastic-apm-synthtrace/BUILD.bazel @@ -1,6 +1,6 @@ -load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project") +load("@npm//@bazel/typescript:index.bzl", "ts_config") load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") -load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm_types") +load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm_types", "ts_project") PKG_BASE_NAME = "elastic-apm-synthtrace" PKG_REQUIRE_NAME = "@elastic/apm-synthtrace" From 5d0953c66a96513f3ecb5e674b075438b3431a26 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 17 Nov 2021 03:13:01 +0000 Subject: [PATCH 19/22] chore(NA): complete types exports --- packages/elastic-apm-synthtrace/src/index.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/elastic-apm-synthtrace/src/index.ts b/packages/elastic-apm-synthtrace/src/index.ts index 70105438ff5ae..890b6593da2f7 100644 --- a/packages/elastic-apm-synthtrace/src/index.ts +++ b/packages/elastic-apm-synthtrace/src/index.ts @@ -6,14 +6,18 @@ * Side Public License, v 1. */ -export { service } from './lib/service'; -export { browser } from './lib/browser'; -export { timerange } from './lib/timerange'; +export { service, Service } from './lib/service'; +export { browser, Browser } from './lib/browser'; +export { timerange, Timerange } from './lib/timerange'; export { getTransactionMetrics } from './lib/utils/get_transaction_metrics'; export { getSpanDestinationMetrics } from './lib/utils/get_span_destination_metrics'; export { getObserverDefaults } from './lib/defaults/get_observer_defaults'; export { getChromeUserAgentDefaults } from './lib/defaults/get_chrome_user_agent_defaults'; -export { toElasticsearchOutput } from './lib/output/to_elasticsearch_output'; +export { + toElasticsearchOutput, + ElasticsearchOutput, + ElasticsearchOutputWriteTargets, +} from './lib/output/to_elasticsearch_output'; export { getBreakdownMetrics } from './lib/utils/get_breakdown_metrics'; export { cleanWriteTargets } from './lib/utils/clean_write_targets'; export { getWriteTargets } from './lib/utils/get_write_targets'; From f4c62871649a938d7afc286f7db34970f2176486 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 17 Nov 2021 12:46:53 +0000 Subject: [PATCH 20/22] chore(NA): export types as type only --- packages/elastic-apm-synthtrace/src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/elastic-apm-synthtrace/src/index.ts b/packages/elastic-apm-synthtrace/src/index.ts index a70be6f21a5da..7bd62bcab00c4 100644 --- a/packages/elastic-apm-synthtrace/src/index.ts +++ b/packages/elastic-apm-synthtrace/src/index.ts @@ -14,8 +14,8 @@ export { getTransactionMetrics } from './lib/utils/get_transaction_metrics'; export { getSpanDestinationMetrics } from './lib/utils/get_span_destination_metrics'; export { getObserverDefaults } from './lib/defaults/get_observer_defaults'; export { getChromeUserAgentDefaults } from './lib/defaults/get_chrome_user_agent_defaults'; -export { - toElasticsearchOutput, +export { toElasticsearchOutput } from './lib/output/to_elasticsearch_output'; +export type { ElasticsearchOutput, ElasticsearchOutputWriteTargets, } from './lib/output/to_elasticsearch_output'; From bb923aa022eb62c867aa2c3a4f7c82f55564bc28 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 17 Nov 2021 14:16:22 +0000 Subject: [PATCH 21/22] chore(NA): fix type exports --- packages/elastic-apm-synthtrace/src/index.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/elastic-apm-synthtrace/src/index.ts b/packages/elastic-apm-synthtrace/src/index.ts index 7bd62bcab00c4..19f0557ec3bb1 100644 --- a/packages/elastic-apm-synthtrace/src/index.ts +++ b/packages/elastic-apm-synthtrace/src/index.ts @@ -6,19 +6,22 @@ * Side Public License, v 1. */ -export { Exception } from './lib/entity'; -export { service, Service } from './lib/service'; -export { browser, Browser } from './lib/browser'; -export { timerange, Timerange } from './lib/timerange'; +export type { Exception } from './lib/entity'; +export type { + ElasticsearchOutput, + ElasticsearchOutputWriteTargets, +} from './lib/output/to_elasticsearch_output'; +export type { Service } from './lib/service'; +export type { Browser } from './lib/browser'; +export type { Timerange } from './lib/timerange'; +export { service } from './lib/service'; +export { browser } from './lib/browser'; +export { timerange } from './lib/timerange'; export { getTransactionMetrics } from './lib/utils/get_transaction_metrics'; export { getSpanDestinationMetrics } from './lib/utils/get_span_destination_metrics'; export { getObserverDefaults } from './lib/defaults/get_observer_defaults'; export { getChromeUserAgentDefaults } from './lib/defaults/get_chrome_user_agent_defaults'; export { toElasticsearchOutput } from './lib/output/to_elasticsearch_output'; -export type { - ElasticsearchOutput, - ElasticsearchOutputWriteTargets, -} from './lib/output/to_elasticsearch_output'; export { getBreakdownMetrics } from './lib/utils/get_breakdown_metrics'; export { cleanWriteTargets } from './lib/utils/clean_write_targets'; export { getWriteTargets } from './lib/utils/get_write_targets'; From 3de36fecc13efab4f75fc22b233530757e8397ca Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 17 Nov 2021 16:05:09 +0000 Subject: [PATCH 22/22] chore(NA): only include exception type --- packages/elastic-apm-synthtrace/src/index.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/elastic-apm-synthtrace/src/index.ts b/packages/elastic-apm-synthtrace/src/index.ts index 19f0557ec3bb1..931215c75fde4 100644 --- a/packages/elastic-apm-synthtrace/src/index.ts +++ b/packages/elastic-apm-synthtrace/src/index.ts @@ -7,13 +7,6 @@ */ export type { Exception } from './lib/entity'; -export type { - ElasticsearchOutput, - ElasticsearchOutputWriteTargets, -} from './lib/output/to_elasticsearch_output'; -export type { Service } from './lib/service'; -export type { Browser } from './lib/browser'; -export type { Timerange } from './lib/timerange'; export { service } from './lib/service'; export { browser } from './lib/browser'; export { timerange } from './lib/timerange';