Packages: Change how required built-ins are polyfilled with Babel 7#9171
Packages: Change how required built-ins are polyfilled with Babel 7#9171
Conversation
|
I still see imports like: require("core-js/modules/es6.array.find");My bet is that |
Would this be due to the supported |
I don't think it's an issue, it's rather |
55f0e02 to
932df9a
Compare
|
I was able to tweak Babel to ensure that we won't publish any references to |
|
Do you happen to know any good example of the message that could be included to inform people using our packages to install required polyfills themselves? It also raises the questions like:
|
Part of this will require identifying what features are actually in use which could require need for a polyfill.
It's tricky, since it's not only expected to be consumed by those browsers which support the script type, but also many transpilers like Webpack can use it for bundling from a dependency; in which case your package may include modern features inadvertently (because typically Babel won't be applied over an external dependency). |
|
After reading the docs for pkg.module from Rollup and checking a few popular libraries: I came to the conclusion that the following statement is valid:
In other words, we should leave the behavior as is and transpile all features but |
FYI this is already what we do in master. |
932df9a to
82f0fdf
Compare
I don't think this is an easy task to accomplish although it seems like a very expected feature to be included in
Yes, I just thought we can make it more up to date but it looks it was premature :( |
82f0fdf to
70d46ad
Compare
|
Added 70d46ad which adds the following note to all README files of packages that need them:
I also upgraded Babel to |
d70af05 to
2f25a0a
Compare
|
When we say "assumes that your code will run in an ES5 environment", are we including things like |
I think I mixed up a few things :) I think this message should be closer to:
|
|
What this PR does is that it changes the transpiled output from: import _Set from "@babel/runtime-corejs2/core-js/set";
var ATTRIBUTES_TYPES = new _Set(['string', 'boolean', 'number']);to just: var ATTRIBUTES_TYPES = new Set(['string', 'boolean', 'number']);The original version makes sure that Another consequence that is not always desired is that the The new version just uses the "naked" import 'core-js/library/fn/set'; // polyfills window.Setthey can use This is an approach that React takes: they use naked How can the required enviroment be described? I'm afraid there is no simple label like "ES2015+". The sources published as part of the module are still guaranteed to have the old ES5 syntax, but the expected buit-in classes and methods can be the latest ES2019+: The precise description of that enviroment is something like: only ES5 syntax is required, but the following ES2015+ builtins are expected to be present: [enumerated list of them]. How to construct that enumerated list? Not easy. By the way, |
Unfortunately, it is almost impossible to create and what's even worse to maintain the list of polyfills for 30+ packages...
We transpile code twice, so |
ece002a to
39f6b7d
Compare
| } ), | ||
| plugins: map( | ||
| plugins, | ||
| ( plugin ) => overrideOptions( plugin, '@babel/plugin-transform-runtime', { |
There was a problem hiding this comment.
I'm not certain we need this plugin anymore because we're using useBuiltIns: true. Any idea @aduth In my understanding the runtime is a subset of the polyfills (added by preset-env).
There was a problem hiding this comment.
Just read the docs https://babeljs.io/docs/en/next/babel-polyfill and https://babeljs.io/docs/en/next/babel-plugin-transform-runtime and I think it confirms my thoughts that we should just get rid of the runtime.
There was a problem hiding this comment.
We should be using useBuiltIns: false, not true and using the transform runtime. We should not be shipping polyfills that modify global prototypes.
There was a problem hiding this comment.
@blowery We are using useBuiltIns: false in the packages (npm) but I was referring to useBuiltIns: true used for Gutenberg.
There was a problem hiding this comment.
This raises the question of whether we should keep the runtime transform for the packages. I personally don't see any difference between those polyfills and the remaining ones not included in the runtime (aside from the way they are included), which means I think we should remove all the polyfills and not only the ones added by babel-preset-env
There was a problem hiding this comment.
I'm not quite sure. I followed the configuration from Calypso which was reviewed by @hzoo 😄
It has both @babel/preset-env with useBuiltIns: "entry" (https://github.com/Automattic/wp-calypso/blob/master/babel.config.js#L22) and @babel/plugin-transform-runtime with corejs: false (https://github.com/Automattic/wp-calypso/blob/master/babel.config.js#L51).
There was a problem hiding this comment.
Still not sure what's the point of @babel/plugin-transform-runtime if you do corejs: false. I guess there are some helpers but aren't these also polyfills?
There was a problem hiding this comment.
Yes, there are Babel helpers that are not polyfills. When transpiling classes, for example, Babel uses helper functions with names like _classCallCheck, _possibleConstructorReturn or _inherits. The runtime transform plugin prevents them from being duplicated all the time and imports them from @babel/runtime/helpers instead.
Another thing that is not a CoreJS polyfill is the regenerator runtime used when transpiling generators and async/await.
The CoreJS and regenerator are "polyfills" in a sense that they might be provided by the platform already. Babel helpers never are though.
There was a problem hiding this comment.
Thanks for the explanation @jsnajdr it makes sense 👍
39f6b7d to
6c2d121
Compare
Did we verify this assumption for Gutenberg itself? 😄 There appear to be some issues loading the editor in current master in IE11 which on preliminary testing seem to be attributable to changes here. |
|
I can confirm this appears to have broken the editor in IE11 due to the lack of a provided polyfill, verified via the following (very-naive-definitely-not-final) fix: diff --git a/lib/client-assets.php b/lib/client-assets.php
index 7ad3546c5..d2bcabd21 100644
--- a/lib/client-assets.php
+++ b/lib/client-assets.php
@@ -66,6 +66,11 @@ function gutenberg_get_script_polyfill( $tests ) {
return $polyfill;
}
+function gutenberg_ie11_ಥ_ಥ() {
+ ?><script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.0.0/polyfill.min.js"></script><?php
+}
+add_action( 'admin_enqueue_scripts', 'gutenberg_ie11_ಥ_ಥ', 0 );
+
/**
* Registers the main TinyMCE scripts.
*/In trying to come to a more solid solution, I'm left wondering what precisely we're expecting people to polyfill. Is it ES2015? How about things like |
|
Tracking IE11 issue for release candidate: #9640 |
We should 🤦♂️ I assumed this is going to be transparent for Gutenberg itself... |
To expand on this with an answer to my own question: Babel's polyfill intends to provide anything which is part of a published ECMAScript specification v2015 or newer.
https://babeljs.io/docs/en/babel-polyfill.html This is the reason why, in #9643, I chose the name |
As I learned yesterday based on the link shared by @youknowriad - it is meant to work that way but as always there are some exceptions. They are listed on the bottom of the page: https://github.com/zloirock/core-js#missing-polyfills In particular:
When doing review, I did quick search in the source file for |
|
Well, to be fair, if one assumes that https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf Rather, it's a WHATWG (and I assume W3) specification: |
Description
Fixes #8727.
Closes #9478.
Discussed during Core JS meeting: https://make.wordpress.org/core/2018/08/14/javascript-chat-summary-august-14/.
Problem: The current npm packages that being built for Gutenberg polyfill usage of some browser features that are not available on older browsers. This polyfill is global and affects any client who uses WordPress npm packages. This is unexpected and can alter the runtime environment in surprising ways.
Action items:
This PR also updates Babel to the latest RC version since we need to update all occurrences of
@babel/runtime-corejs2with@babel/runtimeanyways.I also changed the configuration for thebuild-moduledistribution output. I took advantage of the new setting for@babel/preset-env:"targets": { "esmodules": true }We use Babel twice to produce output for packages and then use Webpack to produce build files to consume inside WordPress, so we can take advantage of it and offerbuildversion of package which works with all browsers andbuild-moduleversion which works with all browsers that support<script type="module"></script>.How has this been tested?
npm run devnpm run buildnpm testChecklist:
/cc @blowery @jsnajdr