feat(vite): get rid of unnecessary polyfills (#3242)

This commit is contained in:
Marvin Hagemeister
2025-08-22 11:23:57 +02:00
committed by GitHub
parent 27c2910b1d
commit 873c54bd5f
3 changed files with 121 additions and 0 deletions
@@ -0,0 +1,32 @@
import type { PluginObj, types } from "@babel/core";
export function removePolyfills(
{ types: t }: { types: typeof types },
): PluginObj {
return {
name: "fresh-remove-polyfills",
visitor: {
IfStatement(path) {
if (
t.isUnaryExpression(path.node.test) &&
path.node.test.operator === "!" &&
t.isMemberExpression(path.node.test.argument) &&
t.isIdentifier(path.node.test.argument.object) &&
((path.node.test.argument.object.name === "String" &&
t.isIdentifier(path.node.test.argument.property) &&
path.node.test.argument.property.name === "fromCodePoint") ||
(path.node.test.argument.object.name === "Object" &&
t.isIdentifier(path.node.test.argument.property) &&
(path.node.test.argument.property.name === "keys" ||
path.node.test.argument.property.name === "create")))
) {
if (path.node.alternate) {
path.replaceWith(t.cloneNode(path.node.alternate, true));
} else {
path.remove();
}
}
},
},
};
}