Skip to content

Commit 873c54b

Browse files
feat(vite): get rid of unnecessary polyfills (#3242)
1 parent 27c2910 commit 873c54b

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

packages/plugin-vite/src/plugins/patches.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { cjsPlugin } from "./patches/commonjs.ts";
44
import { jsxComments } from "./patches/jsx_comment.ts";
55
import babelReact from "@babel/preset-react";
66
import { inlineEnvVarsPlugin } from "./patches/inline_env_vars.ts";
7+
import { removePolyfills } from "./patches/remove_polyfills.ts";
78

89
export function patches(): Plugin {
910
let isDev = false;
@@ -33,6 +34,7 @@ export function patches(): Plugin {
3334
babelrc: false,
3435
plugins: [
3536
cjsPlugin,
37+
removePolyfills,
3638
jsxComments,
3739
inlineEnvVarsPlugin(
3840
isDev ? "development" : "production",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { PluginObj, types } from "@babel/core";
2+
3+
export function removePolyfills(
4+
{ types: t }: { types: typeof types },
5+
): PluginObj {
6+
return {
7+
name: "fresh-remove-polyfills",
8+
visitor: {
9+
IfStatement(path) {
10+
if (
11+
t.isUnaryExpression(path.node.test) &&
12+
path.node.test.operator === "!" &&
13+
t.isMemberExpression(path.node.test.argument) &&
14+
t.isIdentifier(path.node.test.argument.object) &&
15+
((path.node.test.argument.object.name === "String" &&
16+
t.isIdentifier(path.node.test.argument.property) &&
17+
path.node.test.argument.property.name === "fromCodePoint") ||
18+
(path.node.test.argument.object.name === "Object" &&
19+
t.isIdentifier(path.node.test.argument.property) &&
20+
(path.node.test.argument.property.name === "keys" ||
21+
path.node.test.argument.property.name === "create")))
22+
) {
23+
if (path.node.alternate) {
24+
path.replaceWith(t.cloneNode(path.node.alternate, true));
25+
} else {
26+
path.remove();
27+
}
28+
}
29+
},
30+
},
31+
};
32+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { expect } from "@std/expect/expect";
2+
import * as babel from "@babel/core";
3+
import { removePolyfills } from "./remove_polyfills.ts";
4+
5+
function runTest(options: { input: string; expected: string }) {
6+
const res = babel.transformSync(options.input, {
7+
filename: "foo.js",
8+
babelrc: false,
9+
plugins: [removePolyfills],
10+
});
11+
12+
const output = res?.code ?? "";
13+
expect(output).toEqual(options.expected);
14+
}
15+
16+
Deno.test("remove polyfills - Object.keys", () => {
17+
runTest({
18+
input: `if (!Object.keys) {
19+
Object.keys = function (o) {
20+
var a = []
21+
for (var i in o) if (o.hasOwnProperty(i)) a.push(i)
22+
return a
23+
}
24+
}
25+
26+
Object.keys({ foo: "bar" });`,
27+
expected: `Object.keys({
28+
foo: "bar"
29+
});`,
30+
});
31+
});
32+
33+
Deno.test("remove polyfills - Object.create", () => {
34+
runTest({
35+
input: `if (!Object.create) {
36+
Object.create = function (o) {
37+
function F () {}
38+
F.prototype = o
39+
var newf = new F()
40+
return newf
41+
}
42+
}
43+
44+
Object.create({});`,
45+
expected: `Object.create({});`,
46+
});
47+
});
48+
49+
Deno.test("remove polyfills - keep alternate", () => {
50+
runTest({
51+
input: `if (!Object.create) {
52+
Object.create = function (o) {
53+
function F () {}
54+
F.prototype = o
55+
var newf = new F()
56+
return newf
57+
}
58+
} else {
59+
console.log("foo")
60+
}`,
61+
expected: `{
62+
console.log("foo");
63+
}`,
64+
});
65+
});
66+
67+
Deno.test("remove polyfills - String.fromCodePoint", () => {
68+
runTest({
69+
input: `if (!String.fromCodePoint) {
70+
foo
71+
}
72+
73+
String.fromCodePoint(42);`,
74+
expected: `String.fromCodePoint(42);`,
75+
});
76+
});
77+
78+
Deno.test("remove polyfills - Keep unrelated unary statements", () => {
79+
runTest({
80+
input: `if (+String.fromCodePoint) {
81+
foo;
82+
}`,
83+
expected: `if (+String.fromCodePoint) {
84+
foo;
85+
}`,
86+
});
87+
});

0 commit comments

Comments
 (0)