Enable TS decorators (#178873)

## Summary

Extracted from https://github.com/elastic/kibana/pull/175080

- Enable support for TS decorators (by enabling `experimentalDecorators`
and `emitDecoratorMetadata` on base TS config)
- Add plugins to babel configuration to support decorators 
  - `@babel/plugin-proposal-decorators`
  - `babel-plugin-transform-typescript-metadata`
- Import `reflect-metadata` to mimic `emitDecoratorMetadata`
  - on server-side in `setup_node_env` 
  - on browser-side from core's entrypoint
This commit is contained in:
Pierre Gayvallet 2024-03-20 12:02:41 +01:00 committed by GitHub
parent c8c3f19186
commit 07feb4f1f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 60 additions and 26 deletions

View file

@ -1097,6 +1097,7 @@
"redux-saga": "^1.1.3",
"redux-thunk": "^2.4.2",
"redux-thunks": "^1.0.0",
"reflect-metadata": "^0.2.1",
"remark-gfm": "1.0.0",
"remark-parse-no-trim": "^8.0.4",
"remark-stringify": "^8.0.3",
@ -1155,6 +1156,7 @@
"@babel/generator": "^7.23.6",
"@babel/helper-plugin-utils": "^7.24.0",
"@babel/parser": "^7.24.0",
"@babel/plugin-proposal-decorators": "^7.24.1",
"@babel/plugin-proposal-export-namespace-from": "^7.18.9",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
@ -1524,6 +1526,7 @@
"babel-plugin-require-context-hook": "^1.0.0",
"babel-plugin-styled-components": "^2.1.4",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
"babel-plugin-transform-typescript-metadata": "^0.3.2",
"backport": "^8.9.8",
"blob-polyfill": "^7.0.20220408",
"callsites": "^3.1.0",

View file

@ -26,6 +26,7 @@ NPM_MODULE_EXTRA_FILES = [
RUNTIME_DEPS = [
"@npm//@babel/plugin-transform-class-properties",
"@npm//@babel/plugin-proposal-decorators",
"@npm//@babel/plugin-proposal-export-namespace-from",
"@npm//@babel/plugin-proposal-nullish-coalescing-operator",
"@npm//@babel/plugin-proposal-optional-chaining",
@ -39,6 +40,7 @@ RUNTIME_DEPS = [
"@npm//babel-plugin-add-module-exports",
"@npm//babel-plugin-styled-components",
"@npm//babel-plugin-transform-react-remove-prop-types",
"@npm//babel-plugin-transform-typescript-metadata",
]
js_library(

View file

@ -58,5 +58,17 @@ module.exports = () => ({
allowDeclareFields: true,
},
],
// need to run before the typescript preset, else the param decorators
// are stripped from the imports
{
plugins: [
// Required for TypeScript decorators support
require.resolve('babel-plugin-transform-typescript-metadata'),
// Required for TypeScript decorators support
[require.resolve('@babel/plugin-proposal-decorators'), { version: 'legacy' }],
],
},
],
});

View file

@ -26,6 +26,8 @@
import './index.scss';
import 'reflect-metadata';
export type { DocLinksStart } from '@kbn/core-doc-links-browser';
export type { HttpSetup, HttpStart } from '@kbn/core-http-browser';
export type { I18nStart } from '@kbn/core-i18n-browser';

View file

@ -16,3 +16,4 @@ require('@kbn/babel-register').install();
require('./polyfill');
require('@kbn/security-hardening');
require('reflect-metadata');

View file

@ -1844,6 +1844,10 @@
"downlevelIteration": true,
// import tslib helpers rather than inlining helpers for iteration or spreading, for instance
"importHelpers": true,
// Allow usage of experimental decorators
"experimentalDecorators": true,
// Emits decorators metadata
"emitDecoratorMetadata": true,
// adding global typings
"types": [
"node",

View file

@ -247,17 +247,17 @@
lru-cache "^5.1.1"
semver "^6.3.1"
"@babel/helper-create-class-features-plugin@^7.18.6", "@babel/helper-create-class-features-plugin@^7.20.2", "@babel/helper-create-class-features-plugin@^7.22.15", "@babel/helper-create-class-features-plugin@^7.23.6":
version "7.23.6"
resolved "https://registry.yarnpkg.com/@babel/helper-create-class-features-plugin/-/helper-create-class-features-plugin-7.23.6.tgz#b04d915ce92ce363666f816a884cdcfc9be04953"
integrity sha512-cBXU1vZni/CpGF29iTu4YRbOZt3Wat6zCoMDxRF1MayiEc4URxOj31tT65HUM0CRpMowA3HCJaAOVOUnMf96cw==
"@babel/helper-create-class-features-plugin@^7.18.6", "@babel/helper-create-class-features-plugin@^7.22.15", "@babel/helper-create-class-features-plugin@^7.23.6", "@babel/helper-create-class-features-plugin@^7.24.1":
version "7.24.1"
resolved "https://registry.yarnpkg.com/@babel/helper-create-class-features-plugin/-/helper-create-class-features-plugin-7.24.1.tgz#db58bf57137b623b916e24874ab7188d93d7f68f"
integrity sha512-1yJa9dX9g//V6fDebXoEfEsxkZHk3Hcbm+zLhyu6qVgYFLvmTALTeV+jNU9e5RnYtioBrGEOdoI2joMSNQ/+aA==
dependencies:
"@babel/helper-annotate-as-pure" "^7.22.5"
"@babel/helper-environment-visitor" "^7.22.20"
"@babel/helper-function-name" "^7.23.0"
"@babel/helper-member-expression-to-functions" "^7.23.0"
"@babel/helper-optimise-call-expression" "^7.22.5"
"@babel/helper-replace-supers" "^7.22.20"
"@babel/helper-replace-supers" "^7.24.1"
"@babel/helper-skip-transparent-expression-wrappers" "^7.22.5"
"@babel/helper-split-export-declaration" "^7.22.6"
semver "^6.3.1"
@ -327,7 +327,7 @@
dependencies:
"@babel/types" "^7.22.5"
"@babel/helper-member-expression-to-functions@^7.22.15", "@babel/helper-member-expression-to-functions@^7.23.0":
"@babel/helper-member-expression-to-functions@^7.23.0":
version "7.23.0"
resolved "https://registry.yarnpkg.com/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.23.0.tgz#9263e88cc5e41d39ec18c9a3e0eced59a3e7d366"
integrity sha512-6gfrPwh7OuT6gZyJZvd6WbTfrqAo7vm4xCzAXOusKqq/vWdKXphTpj5klHKNmRUU6/QRGlBsyU9mAIPaWHlqJA==
@ -378,13 +378,13 @@
"@babel/helper-environment-visitor" "^7.22.20"
"@babel/helper-wrap-function" "^7.22.20"
"@babel/helper-replace-supers@^7.19.1", "@babel/helper-replace-supers@^7.22.20":
version "7.22.20"
resolved "https://registry.yarnpkg.com/@babel/helper-replace-supers/-/helper-replace-supers-7.22.20.tgz#e37d367123ca98fe455a9887734ed2e16eb7a793"
integrity sha512-qsW0In3dbwQUbK8kejJ4R7IHVGwHJlV6lpG6UA7a9hSa2YEiAib+N1T2kr6PEeUT+Fl7najmSOS6SmAwCHK6Tw==
"@babel/helper-replace-supers@^7.22.20", "@babel/helper-replace-supers@^7.24.1":
version "7.24.1"
resolved "https://registry.yarnpkg.com/@babel/helper-replace-supers/-/helper-replace-supers-7.24.1.tgz#7085bd19d4a0b7ed8f405c1ed73ccb70f323abc1"
integrity sha512-QCR1UqC9BzG5vZl8BMicmZ28RuUBnHhAMddD8yHFHDRH9lLTZ9uUPehX8ctVPT8l0TKblJidqcgUUKGVrePleQ==
dependencies:
"@babel/helper-environment-visitor" "^7.22.20"
"@babel/helper-member-expression-to-functions" "^7.22.15"
"@babel/helper-member-expression-to-functions" "^7.23.0"
"@babel/helper-optimise-call-expression" "^7.22.5"
"@babel/helper-simple-access@^7.22.5":
@ -401,7 +401,7 @@
dependencies:
"@babel/types" "^7.22.5"
"@babel/helper-split-export-declaration@^7.18.6", "@babel/helper-split-export-declaration@^7.22.6":
"@babel/helper-split-export-declaration@^7.22.6":
version "7.22.6"
resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.22.6.tgz#322c61b7310c0997fe4c323955667f18fcefb91c"
integrity sha512-AsUnxuLhRYsisFiaJwvp1QF+I3KjD5FOxut14q/GzovUe6orHLesW2C7d754kRm53h5gqrz6sFl6sxc4BVtE/g==
@ -487,16 +487,14 @@
"@babel/helper-create-class-features-plugin" "^7.18.6"
"@babel/helper-plugin-utils" "^7.18.6"
"@babel/plugin-proposal-decorators@^7.12.12":
version "7.20.2"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-decorators/-/plugin-proposal-decorators-7.20.2.tgz#1c6c32b2a44b154ebeec2bb534f9eaebdb541fb6"
integrity sha512-nkBH96IBmgKnbHQ5gXFrcmez+Z9S2EIDKDQGp005ROqBigc88Tky4rzCnlP/lnlj245dCEQl4/YyV0V1kYh5dw==
"@babel/plugin-proposal-decorators@^7.12.12", "@babel/plugin-proposal-decorators@^7.24.1":
version "7.24.1"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-decorators/-/plugin-proposal-decorators-7.24.1.tgz#bab2b9e174a2680f0a80f341f3ec70f809f8bb4b"
integrity sha512-zPEvzFijn+hRvJuX2Vu3KbEBN39LN3f7tW3MQO2LsIs57B26KU+kUc82BdAktS1VCM6libzh45eKGI65lg0cpA==
dependencies:
"@babel/helper-create-class-features-plugin" "^7.20.2"
"@babel/helper-plugin-utils" "^7.20.2"
"@babel/helper-replace-supers" "^7.19.1"
"@babel/helper-split-export-declaration" "^7.18.6"
"@babel/plugin-syntax-decorators" "^7.19.0"
"@babel/helper-create-class-features-plugin" "^7.24.1"
"@babel/helper-plugin-utils" "^7.24.0"
"@babel/plugin-syntax-decorators" "^7.24.1"
"@babel/plugin-proposal-export-default-from@^7.12.1":
version "7.18.10"
@ -602,12 +600,12 @@
dependencies:
"@babel/helper-plugin-utils" "^7.14.5"
"@babel/plugin-syntax-decorators@^7.19.0":
version "7.19.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-decorators/-/plugin-syntax-decorators-7.19.0.tgz#5f13d1d8fce96951bea01a10424463c9a5b3a599"
integrity sha512-xaBZUEDntt4faL1yN8oIFlhfXeQAWJW7CLKYsHTUqriCUbj8xOra8bfxxKGi/UwExPFBuPdH4XfHc9rGQhrVkQ==
"@babel/plugin-syntax-decorators@^7.24.1":
version "7.24.1"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-decorators/-/plugin-syntax-decorators-7.24.1.tgz#71d9ad06063a6ac5430db126b5df48c70ee885fa"
integrity sha512-05RJdO/cCrtVWuAaSn1tS3bH8jbsJa/Y1uD186u6J4C/1mnHFxseeuWpsqr9anvo7TUulev7tm7GDwRV+VuhDw==
dependencies:
"@babel/helper-plugin-utils" "^7.19.0"
"@babel/helper-plugin-utils" "^7.24.0"
"@babel/plugin-syntax-dynamic-import@^7.8.3":
version "7.8.3"
@ -12122,6 +12120,13 @@ babel-plugin-transform-react-remove-prop-types@^0.4.24:
resolved "https://registry.yarnpkg.com/babel-plugin-transform-react-remove-prop-types/-/babel-plugin-transform-react-remove-prop-types-0.4.24.tgz#f2edaf9b4c6a5fbe5c1d678bfb531078c1555f3a"
integrity sha512-eqj0hVcJUR57/Ug2zE1Yswsw4LhuqqHhD+8v120T1cl3kjg76QwtyBrdIk4WVwK+lAhBJVYCd/v+4nc4y+8JsA==
babel-plugin-transform-typescript-metadata@^0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-typescript-metadata/-/babel-plugin-transform-typescript-metadata-0.3.2.tgz#7a327842d8c36ffe07ee1b5276434e56c297c9b7"
integrity sha512-mWEvCQTgXQf48yDqgN7CH50waTyYBeP2Lpqx4nNWab9sxEpdXVeKgfj1qYI2/TgUPQtNFZ85i3PemRtnXVYYJg==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"
babel-polyfill@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-polyfill/-/babel-polyfill-6.26.0.tgz#379937abc67d7895970adc621f284cd966cf2153"
@ -26332,6 +26337,11 @@ redux@^4.0.0, redux@^4.0.4, redux@^4.2.0, redux@^4.2.1:
dependencies:
"@babel/runtime" "^7.9.2"
reflect-metadata@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.2.1.tgz#8d5513c0f5ef2b4b9c3865287f3c0940c1f67f74"
integrity sha512-i5lLI6iw9AU3Uu4szRNPPEkomnkjRTaVt9hy/bn5g/oSzekBSMeLZblcjP74AW0vBabqERLLIrz+gR8QYR54Tw==
refractor@^3.2.0, refractor@^3.6.0:
version "3.6.0"
resolved "https://registry.yarnpkg.com/refractor/-/refractor-3.6.0.tgz#ac318f5a0715ead790fcfb0c71f4dd83d977935a"