Skip to content

Commit 40430c2

Browse files
committed
inject polyfills in visitor in usage-global
1 parent b96b739 commit 40430c2

File tree

26 files changed

+222
-10
lines changed

26 files changed

+222
-10
lines changed

package-lock.json

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
},
6363
"devDependencies": {
6464
"@babel/core": "^7.28.3",
65+
"@babel/plugin-syntax-decorators": "^7.27.1",
6566
"@babel/plugin-transform-arrow-functions": "^7.27.1",
6667
"@babel/plugin-transform-block-scoped-functions": "^7.27.1",
6768
"@babel/plugin-transform-block-scoping": "^7.28.0",

packages/core-js-babel-plugin/index.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ function resolveHint(desc, meta) {
3737
}
3838

3939
module.exports = defineProvider(({
40+
babel,
4041
createMetaResolver,
4142
debug,
43+
getUtils,
4244
method,
4345
targets,
44-
babel: { types: t },
4546
}, {
4647
pkg,
4748
pkgs,
@@ -51,6 +52,9 @@ module.exports = defineProvider(({
5152
if (pkg === undefined) pkg = method === 'usage-pure' ? '@core-js/pure' : 'core-js';
5253
if (typeof pkg != 'string') throw new TypeError('Incorrect package name');
5354

55+
const t = babel.types;
56+
const isWebpack = babel.caller(caller => caller?.name === 'babel-loader');
57+
5458
const packages = pkgs ? [...defaultCoreJSPackages, ...pkgs] : [...defaultCoreJSPackages];
5559

5660
const modulesListForTargetVersion = getModulesListForTargetVersion(version);
@@ -157,5 +161,47 @@ module.exports = defineProvider(({
157161
return true;
158162
},
159163
usagePure() { /* empty */ },
164+
visitor: method === 'usage-global' && {
165+
// import('foo')
166+
CallExpression(path) {
167+
if (path.get('callee').isImport()) {
168+
const utils = getUtils(path);
169+
if (isWebpack) {
170+
// Webpack uses `Promise.all` to handle dynamic import.
171+
injectCoreJSModulesForEntry(`${ mode }/promise/all`, utils);
172+
} else {
173+
injectCoreJSModulesForEntry(`${ mode }/promise/constructor`, utils);
174+
}
175+
}
176+
},
177+
// (async function () { }).finally(...)
178+
Function(path) {
179+
if (path.node.async) {
180+
injectCoreJSModulesForEntry(`${ mode }/promise/constructor`, getUtils(path));
181+
}
182+
},
183+
// for-of, [a, b] = c
184+
'ForOfStatement|ArrayPattern'(path) {
185+
injectCoreJSModulesForEntry(`${ mode }/get-iterator`, getUtils(path));
186+
},
187+
// [...spread]
188+
SpreadElement(path) {
189+
if (!path.parentPath.isObjectExpression()) {
190+
injectCoreJSModulesForEntry(`${ mode }/get-iterator`, getUtils(path));
191+
}
192+
},
193+
// yield *
194+
YieldExpression(path) {
195+
if (path.node.delegate) {
196+
injectCoreJSModulesForEntry(`${ mode }/get-iterator`, getUtils(path));
197+
}
198+
},
199+
// decorators metadata
200+
Class(path) {
201+
if (path.node.decorators?.length || path.node.body.body.some(el => el.decorators?.length)) {
202+
injectCoreJSModulesForEntry(`${ mode }/symbol/metadata`, getUtils(path));
203+
}
204+
},
205+
},
160206
};
161207
});

scripts/bundle-tests/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
async function f() { /* empty */ }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"plugins": [
3+
[
4+
"@core-js",
5+
{
6+
"method": "usage-global",
7+
"version": "4.0",
8+
"targets": {
9+
"ie": 11
10+
}
11+
}
12+
]
13+
]
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "core-js/modules/es.object.to-string";
2+
import "core-js/modules/es.promise.constructor";
3+
import "core-js/modules/es.promise.catch";
4+
import "core-js/modules/es.promise.finally";
5+
async function f() {/* empty */}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class C {
2+
@dec foo = 2;
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"plugins": [
3+
[
4+
"@core-js",
5+
{
6+
"method": "usage-global",
7+
"version": "4.0",
8+
"targets": {
9+
"ie": 11
10+
}
11+
}
12+
],
13+
["@babel/plugin-syntax-decorators", { "version": "2023-11" }]
14+
]
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import "core-js/modules/esnext.function.metadata";
2+
import "core-js/modules/esnext.symbol.metadata";
3+
class C {
4+
@dec
5+
foo = 2;
6+
}

0 commit comments

Comments
 (0)