Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[corejs3] Inject esnext.symbol.metadata for decorated classes #179

Merged
merged 3 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/babel-plugin-polyfill-corejs3/package.json
Expand Up @@ -32,10 +32,12 @@
"devDependencies": {
"@babel/core": "^7.22.6",
"@babel/helper-plugin-test-runner": "^7.22.5",
"@babel/plugin-proposal-decorators": "^7.22.15",
"@babel/plugin-transform-class-properties": "^7.22.5",
"@babel/plugin-transform-classes": "^7.22.6",
"@babel/plugin-transform-for-of": "^7.22.5",
"@babel/plugin-transform-modules-commonjs": "^7.22.5",
"@babel/plugin-transform-runtime": "^7.22.15",
"@babel/plugin-transform-spread": "^7.22.5",
"core-js": "^3.32.0",
"core-js-pure": "^3.32.0"
Expand Down
Expand Up @@ -204,6 +204,11 @@ const IteratorDependencies = [
"es.object.to-string",
];

export const DecoratorMetadataDependencies = [
"esnext.symbol.metadata",
"esnext.function.metadata",
];

const TypedArrayStaticMethods = {
from: define(null, ["es.typed-array.from"]),
fromAsync: define(null, [
Expand Down Expand Up @@ -640,10 +645,7 @@ export const StaticProperties: ObjectMap2<CoreJSPolyfillDescriptor> = {
"es.symbol.match-all",
"es.string.match-all",
]),
metadata: define("symbol/metadata", [
"esnext.symbol.metadata",
"esnext.function.metadata",
]),
metadata: define("symbol/metadata", DecoratorMetadataDependencies),
metadataKey: define("symbol/metadata-key", ["esnext.symbol.metadata-key"]),
observable: define("symbol/observable", ["esnext.symbol.observable"]),
patternMatch: define("symbol/pattern-match", [
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-plugin-polyfill-corejs3/src/index.ts
Expand Up @@ -8,6 +8,7 @@ import {
PromiseDependenciesWithIterators,
StaticProperties,
InstanceProperties,
DecoratorMetadataDependencies,
type CoreJSPolyfillDescriptor,
} from "./built-in-definitions";
import canSkipPolyfill from "./usage-filters";
Expand Down Expand Up @@ -375,6 +376,18 @@ export default defineProvider<Options>(function (
maybeInjectGlobal(CommonIterators, getUtils(path));
}
},

// Decorators metadata
Class(path: NodePath<t.Class>) {
const hasDecorators =
path.node.decorators?.length ||
path.node.body.body.some(
el => (el as t.ClassMethod).decorators?.length,
);
if (hasDecorators) {
maybeInjectGlobal(DecoratorMetadataDependencies, getUtils(path));
}
},
},
};
});
@@ -0,0 +1,3 @@
class A {
@dec foo = 2;
}
@@ -0,0 +1,7 @@
{
"plugins": [
["@@/polyfill-corejs3", { "method": "usage-global", "version": "3.31.0", "proposals": true }],
["@babel/plugin-proposal-decorators", { "version": "2023-05" }],
["@babel/plugin-transform-runtime", { "version": "7.100.0" }]
]
}
@@ -0,0 +1,14 @@
var _applyDecs = require("@babel/runtime-corejs3/helpers/applyDecs2305").default;
var _dec, _init_foo;
require("core-js/modules/esnext.function.metadata.js");
require("core-js/modules/esnext.symbol.metadata.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
_dec = dec;
class A {
static {
[_init_foo] = _applyDecs(this, [[_dec, 0, "foo"]], []).e;
}
foo = _init_foo(this, 2);
}