I run Google PageSpeed Insights on my site, and it reports an issue that is “Avoid serving legacy JavaScript to modern browsers”, as below:
I have no idea of what is @babel/plugin-transform-classes
, so I search online and it seems babel is a JavaScript compiler and @babel/plugin-transform-classes is a plugin.
But I don’t know or install the compiler and the plugin, why there will be such an issue?
Advertisement
Answer
Short answer
- A babel plugin that fix some Javascript errors for legacy browser
- You cannot fix this error on your own, not if you’re using WordPress or any framework at least.
Long answer
What is babel
Babel is a JavaScript compiler which is used by framework to make code understandable for older browser.
What is @babel/plugin-transform-classes
This is a babel plugin, which is included in @babel/preset-env
.
It does
- Transpiles classes using
SuperClass.apply(/* ... */)
, but native classes aren’t callable and thus throw in this case.- Some built-in functions (like
Array
) always return a new object. Instead of returning it, Babel should treat it as the new this.
In
JavaScript
x
10
10
1
class Test {
2
constructor(name) {
3
this.name = name;
4
}
5
6
logger() {
7
console.log("Hello", this.name);
8
}
9
}
10
Out
JavaScript
1
20
20
1
function _classCallCheck(instance, Constructor) {
2
if (!(instance instanceof Constructor)) {
3
throw new TypeError("Cannot call a class as a function");
4
}
5
}
6
7
var Test = (function() {
8
function Test(name) {
9
_classCallCheck(this, Test);
10
11
this.name = name;
12
}
13
14
Test.prototype.logger = function logger() {
15
console.log("Hello", this.name);
16
};
17
18
return Test;
19
})();
20
Can I fix this problem on my own?
No, since a lot of library are still using babel, you’ll have to wait for them to move on.
So you’re safe to ignore this issue.