Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framework: Use babel-polyfill in place of runtime builtins #9643

Merged
merged 9 commits into from
Sep 6, 2018

Conversation

aduth
Copy link
Member

@aduth aduth commented Sep 5, 2018

Fixes #9640
Related: #9275
Alternative to: #9641

This pull request seeks to disable our use of the @babel/runtime useBuiltIns option, replaced instead by @babel/polyfill as a dependency of every script registered by Gutenberg. This could be further enhanced to conditionally load the script only for browsers where expected features are not supported, but at this time it is not evident how such a condition would be formed.

While this technically adds a new script to be loaded, there is a net decrease in bundle size, from 2011.96kb total to 1803.72kb (+89.6kb polyfill) (minified, pre-gzip), for an overall savings of 118.64kb. Full build comparison below:

Before
                         ./build/blocks/index.js    219 KiB      23  [emitted]         blocks
                      ./build/wordcount/index.js     10 KiB       0  [emitted]         wordcount
                            ./build/url/index.js    8.9 KiB       2  [emitted]         url
                     ./build/token-list/index.js     13 KiB       3  [emitted]         tokenList
                      ./build/shortcode/index.js   16.2 KiB       4  [emitted]         shortcode
                  ./build/redux-routine/index.js   18.6 KiB       5  [emitted]         reduxRoutine
                        ./build/plugins/index.js   13.3 KiB       6  [emitted]         plugins
                            ./build/nux/index.js   16.6 KiB       7  [emitted]         nux
                       ./build/keycodes/index.js   3.64 KiB       8  [emitted]         keycodes
               ./build/is-shallow-equal/index.js   1.15 KiB       9  [emitted]         isShallowEqual
                           ./build/i18n/index.js   27.7 KiB      10  [emitted]         i18n
                  ./build/html-entities/index.js  986 bytes      11  [emitted]         htmlEntities
                          ./build/hooks/index.js   19.3 KiB      12  [emitted]         hooks
                        ./build/element/index.js   34.3 KiB      13  [emitted]         element
                         ./build/editor/index.js    371 KiB      14  [emitted]  [big]  editor
                      ./build/dom-ready/index.js  778 bytes      15  [emitted]         domReady
                            ./build/dom/index.js   15.4 KiB      16  [emitted]         dom
                     ./build/deprecated/index.js   10.9 KiB      17  [emitted]         deprecated
                           ./build/date/index.js    196 KiB      18  [emitted]         date
                           ./build/data/index.js   52.1 KiB      19  [emitted]         data
                      ./build/core-data/index.js   57.8 KiB      20  [emitted]         coreData
                        ./build/compose/index.js     18 KiB      21  [emitted]         compose
./build/block-serialization-spec-parser/index.js   9.48 KiB      22  [emitted]         blockSerializationSpecParser
                       ./build/viewport/index.js   13.3 KiB       1  [emitted]         viewport
                           ./build/blob/index.js  909 bytes      24  [emitted]         blob
                          ./build/autop/index.js   24.7 KiB      25  [emitted]         autop
                      ./build/api-fetch/index.js   35.2 KiB      26  [emitted]         apiFetch
                           ./build/a11y/index.js   5.32 KiB      27  [emitted]         a11y
                  ./build/block-library/index.js    217 KiB      28  [emitted]         blockLibrary
                      ./build/edit-post/index.js   87.4 KiB      29  [emitted]         editPost
                     ./build/components/index.js    494 KiB      30  [emitted]  [big]  components
After
                         ./build/blocks/index.js    191 KiB      23  [emitted]         blocks
                      ./build/wordcount/index.js   2.53 KiB       0  [emitted]         wordcount
                            ./build/url/index.js    8.9 KiB       2  [emitted]         url
                     ./build/token-list/index.js   2.39 KiB       3  [emitted]         tokenList
                      ./build/shortcode/index.js   8.89 KiB       4  [emitted]         shortcode
                  ./build/redux-routine/index.js   18.6 KiB       5  [emitted]         reduxRoutine
                        ./build/plugins/index.js   11.7 KiB       6  [emitted]         plugins
                            ./build/nux/index.js   16.6 KiB       7  [emitted]         nux
                       ./build/keycodes/index.js   3.64 KiB       8  [emitted]         keycodes
               ./build/is-shallow-equal/index.js   1.15 KiB       9  [emitted]         isShallowEqual
                           ./build/i18n/index.js   24.6 KiB      10  [emitted]         i18n
                  ./build/html-entities/index.js  986 bytes      11  [emitted]         htmlEntities
                          ./build/hooks/index.js   10.2 KiB      12  [emitted]         hooks
                        ./build/element/index.js   24.3 KiB      13  [emitted]         element
                         ./build/editor/index.js    354 KiB      14  [emitted]  [big]  editor
                      ./build/dom-ready/index.js  778 bytes      15  [emitted]         domReady
                            ./build/dom/index.js   11.4 KiB      16  [emitted]         dom
                     ./build/deprecated/index.js   7.02 KiB      17  [emitted]         deprecated
                           ./build/date/index.js    191 KiB      18  [emitted]         date
                           ./build/data/index.js   43.3 KiB      19  [emitted]         data
                      ./build/core-data/index.js   48.9 KiB      20  [emitted]         coreData
                        ./build/compose/index.js   8.28 KiB      21  [emitted]         compose
./build/block-serialization-spec-parser/index.js   9.48 KiB      22  [emitted]         blockSerializationSpecParser
                       ./build/viewport/index.js   2.77 KiB       1  [emitted]         viewport
                           ./build/blob/index.js  909 bytes      24  [emitted]         blob
                          ./build/autop/index.js   11.8 KiB      25  [emitted]         autop
                      ./build/api-fetch/index.js   28.9 KiB      26  [emitted]         apiFetch
                           ./build/a11y/index.js    1.8 KiB      27  [emitted]         a11y
                  ./build/block-library/index.js    202 KiB      28  [emitted]         blockLibrary
                      ./build/edit-post/index.js   77.9 KiB      29  [emitted]         editPost
                     ./build/components/index.js    478 KiB      30  [emitted]  [big]  components

Testing instructions:

Repeat Steps to Reproduce from #9640, verifying both in Internet Explorer and in your preferred browser that there are no issues loading or using the editor.

@aduth aduth added [Type] Bug An existing feature does not function as intended Browser Issues Issues or PRs that are related to browser specific problems labels Sep 5, 2018
@aduth aduth added this to the 3.8 milestone Sep 5, 2018
@aduth aduth requested a review from gziolo September 5, 2018 21:39
} ],
isTestEnv && [ '@babel/preset-env' ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change for this package, should we update the changelog accordingly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do some cleanup to the get-babel-config.js file and remove the override of useBuiltIns: false,

@youknowriad
Copy link
Contributor

Nice work, I can't test and approve at the moment. It will be the first thing I'll do in the morning unless someone beats me to it.

@gziolo
Copy link
Member

gziolo commented Sep 6, 2018

5 unit tests failed, investigating.

@gziolo
Copy link
Member

gziolo commented Sep 6, 2018

This is very similar but simpler approach taken to what I was trying to do in #9275. I really like where it is going. Let's close #9275 and #9641 and focus to get this one in 💯

@gziolo gziolo added the [Priority] High Used to indicate top priority items that need quick attention label Sep 6, 2018
@youknowriad
Copy link
Contributor

I included a small CSS tweak to fix the block toolbars in 43cde18 (display flex elements in IE need flex-shrink: 0) if they are inside other flex elements :)

I didn't do a separate PR because people won't be able to test otherwise.

@youknowriad
Copy link
Contributor

Hey hey looks like the tests pass now 🎉

);
wp_add_inline_script(
'wp-polyfill-fetch',
'wp.deprecated( "wp-polyfill-fetch script handle", { plugin: "Gutenberg", version: "4.0" } );'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it mean that wp-polyfill-ecmascript contains also polyfill for fetch?

I can see some code for Promise in https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.0.0/polyfill.js, but can't see anything related to fetch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it relies on core-js I don't think it's included https://github.com/zloirock/core-js#missing-polyfills

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, occurred to me as well the polyfill is only going to provide language features (the specification). Since fetch is a browser standard, it needs to remain polyfilled. I'll add back.

@@ -8,7 +8,6 @@ module.exports = function( api ) {
targets: {
browsers: [ 'extends @wordpress/browserslist-config' ],
},
useBuiltIns: 'usage',
} ],
isTestEnv && [ '@babel/preset-env', {
useBuiltIns: 'usage',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to revert the change which removes:
useBuiltIns: 'usage',
from test environment because some tests were failing. Those which use Array.values method which isn't supported by Node.

@youknowriad suggested using babel-polyfill instead to have the setup closer to the browser but I found out that it can cause memory issues with Jest. See jestjs/jest#2755 and the line in the changelog:

Breaking Change: Replaced auto-loading of babel-polyfill with only regenerator-runtime, fixes a major memory leak.

@@ -16,6 +16,7 @@ describe( 'Babel preset default', () => {

const output = transform( input, {
configFile: false,
envName: 'production',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, we test again production configuration rather than test env ... It is more helpful now :)

@gziolo
Copy link
Member

gziolo commented Sep 6, 2018

We still need to test it against IE11, @jorgefilipecosta or @tofumatt can you help to ensure it works consistently?

Copy link
Member

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't working for me in IE11, it seems there is no fetch polypill. When the editor goes to autosave it gets stuck on "Autosaving…" with this error:

TypeError: Object doesn't support property or method 'fetch'

2018-09-06 12 21 23

youknowriad and others added 2 commits September 6, 2018 13:11
Not part of ECMAScript standard, so not provided through Babel polyfill
Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 Tested in IE and chrome. Works great.

@tofumatt
Copy link
Member

tofumatt commented Sep 6, 2018

I didn't re-test in IE but if @youknowriad says it works in IE11 that's good enough for me; dismissing my review 😄

@aduth aduth merged commit 400cba8 into master Sep 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Browser Issues Issues or PRs that are related to browser specific problems [Priority] High Used to indicate top priority items that need quick attention [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants