closure-compiler icon indicating copy to clipboard operation
closure-compiler copied to clipboard

Object spread causes polyfills even in modern language_out

Open L3P3 opened this issue 2 years ago • 3 comments

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. :wink:

L3P3 avatar Jun 07 '23 22:06 L3P3

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.

rishipal avatar Jun 09 '23 23:06 rishipal

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.

L3P3 avatar Jun 10 '23 00:06 L3P3

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

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... :wink:

L3P3 avatar Jun 10 '23 09:06 L3P3