I'm building a JS-heavy WordPress plugin with babel/WebPack to transpile ES6 -> ES5 code. I'm also relying on some of the new features, like Promise
and Object.assign
, and I'm wondering a few things:
- Does WordPress ship any polyfills of its own for the global environment?
- What are best practices around handling/shipping polyfills in WordPress plugins?
I'm building a JS-heavy WordPress plugin with babel/WebPack to transpile ES6 -> ES5 code. I'm also relying on some of the new features, like Promise
and Object.assign
, and I'm wondering a few things:
- Does WordPress ship any polyfills of its own for the global environment?
- What are best practices around handling/shipping polyfills in WordPress plugins?
- Probably not to use JS at all ;) – Mark Kaplun Commented Feb 25, 2017 at 18:21
- on a more serious note, if you are talking about frontend, do whatever you want. backend, just avoid any complex JS – Mark Kaplun Commented Feb 25, 2017 at 18:22
- Lol but not really. wptavern/… – mAAdhaTTah Commented Feb 25, 2017 at 18:23
- 1 yes we hear the same BS for the last 7 years, and it always ends up the same way as "the year of the linux on desktop" ends. JS is a moving target which is hard to debug. The less you use it, the longer you will avoid white hair – Mark Kaplun Commented Feb 25, 2017 at 18:25
- WordPress still supports PHP 5.2 in 2017, REST API opens the door for everything, that's true - but IMHO manned mission to Mars will happen before JS becomes the 'future' of WP. – Fayaz Commented Feb 25, 2017 at 18:31
2 Answers
Reset to default 2Does WordPress ship any polyfills of its own for the global environment?
No, as far as I remember WP core doesn't ship with any polyfills.
What are best practices around handling/shipping polyfills in WordPress plugins?
The typical practice for JS in public WP extensions is to either:
stick with core–provided scripts/environment;
isolate your code and dependencies from impacting global state, by use of noConflict modes or otherwise.
If you have to deal with global state in public extension — be prepared for it to explode in most peculiar ways out there in the wild.
Update: since around September 2018, WordPress core does ship with a polyfill, wp-polyfill
, which appears to be babel-polyfill's browser library, and I expect is down to all the React in the Gutenberg editor. You can find it here:
wp-includes/js/dist/vendor/wp-polyfill.js
You can add wp-polyfill
as a dependency when enqueueing your own scripts, for example if you need jQuery and polyfills:
wp_enqueue_script(
'main_custom_script',
get_template_directory_uri() . '/dist/scripts.min.js',
array('jquery', 'wp-polyfill'),
'1.0',
true
);
Unfortunately, adding multiple copies of this polyfill ends in tears, so it still is sensible not to be including other copies of this polyfill with any plugins you develop, and to stick with this core script.
Another polyfilling alternative to consider is https://polyfill.io/v3/, where you can be selective about the polyfills you want.