google / closure-compiler

A JavaScript checker and optimizer.

Home Page:https://developers.google.com/closure/compiler/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Object spread causes polyfills even in modern language_out

L3P3 opened this issue · comments

commented

Version: From at least mid 2022 up to latest.

compilation_level ADVANCED
language_out ECMASCRIPT_2020
rewrite_polyfills false

With ES2020 and up, using {x:foo, ...bar} causes this to be added to the head:

var ba="function"==typeof Object.defineProperties?Object.defineProperty:function(a,b,c){if(a==Array.prototype||a==Object.prototype)return a;a[b]=c.value;return a};function ca(a){a=["object"==typeof globalThis&&globalThis,a,"object"==typeof window&&window,"object"==typeof self&&self,"object"==typeof global&&global];for(var b=0;b<a.length;++b){var c=a[b];if(c&&c.Math==Math)return c}throw Error("Cannot find global object");}var da=ca(this);
function ea(a,b){if(b)a:{for(var c=da,d=a.split("."),f=0;f<d.length-1;f++){var e=d[f];if(!(e in c))break a;c=c[e]}d=d[d.length-1];f=c[d];e=b(f);e!=f&&null!=e&&ba(c,d,{configurable:!0,writable:!0,value:e})}}var fa="function"==typeof Object.assign?Object.assign:function(a,b){for(var c=1;c<arguments.length;c++){var d=arguments[c];if(d)for(var f in d)Object.prototype.hasOwnProperty.call(d,f)&&(a[f]=d[f])}return a};ea("Object.assign",function(a){return a||fa});

Summary: Abstract polyfill support, single Object.assign polyfill.

When I use Object.assign({x:foo}, bar), this disabled + useless polyfill stuff is not in there. sus
Since this adds almost a kilobyte, I deem this worth fixable. In ES2020, there is no point in having an Object.assign polyfill. 😉

How have you configured the compiler to run and how are you invoking it? In my IntegrationTest the polyfills are only getting injected if I set the language_out to ES3.

commented

Will post later, but can be found here: https://github.com/L3P3/lui/blob/master/build.js#L51

Also just noticed that the polyfill code has both defineProperty and defineProperties there, which seems very weird.

commented

Sorry, for ES2020, there are no polyfills, but for ES5, there is! My mistake in writing this issue! 😅

I am trying to nail it down. What I got so far is that the polyfill appears even with this code:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @language_out ECMASCRIPT5_STRICT
// @rewrite_polyfills false
// @output_file_name default.js
// ==/ClosureCompiler==

(() => {
  Object.assign;
  false ? { ...{} } : null;
})();

The polyfill code only appears when both lines inside the function are there. If you comment out one of those, it is not appearing. Note that I DISABLED polyfills explicitly but they still appear... 😉