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
10 changes: 1 addition & 9 deletions bin/packages/get-babel-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,11 @@ const babelConfigs = {
{},
babelDefaultConfig,
{
plugins: map(
plugins,
( plugin ) => overrideOptions( plugin, '@babel/plugin-transform-runtime', {
corejs: false,
} )
),
plugins,
presets: map(
babelDefaultConfig.presets,
( preset ) => overrideOptions( preset, '@babel/preset-env', {
modules: 'commonjs',
useBuiltIns: false,
} )
),
}
Expand All @@ -57,15 +51,13 @@ const babelConfigs = {
plugins: map(
plugins,
( plugin ) => overrideOptions( plugin, '@babel/plugin-transform-runtime', {
corejs: false,
useESModules: true,
} )
),
presets: map(
babelDefaultConfig.presets,
( preset ) => overrideOptions( preset, '@babel/preset-env', {
modules: false,
useBuiltIns: false,
} )
),
}
Expand Down
119 changes: 63 additions & 56 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,63 +102,63 @@ function gutenberg_register_scripts_and_styles() {
wp_register_script(
'wp-url',
gutenberg_url( 'build/url/index.js' ),
array(),
array( 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/url/index.js' ),
true
);
wp_register_script(
'wp-autop',
gutenberg_url( 'build/autop/index.js' ),
array(),
array( 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/autop/index.js' ),
true
);
wp_register_script(
'wp-wordcount',
gutenberg_url( 'build/wordcount/index.js' ),
array(),
array( 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/wordcount/index.js' ),
true
);
wp_register_script(
'wp-dom-ready',
gutenberg_url( 'build/dom-ready/index.js' ),
array(),
array( 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/dom-ready/index.js' ),
true
);
wp_register_script(
'wp-a11y',
gutenberg_url( 'build/a11y/index.js' ),
array( 'wp-dom-ready' ),
array( 'wp-dom-ready', 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/a11y/index.js' ),
true
);
wp_register_script(
'wp-hooks',
gutenberg_url( 'build/hooks/index.js' ),
array(),
array( 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/hooks/index.js' ),
true
);
wp_register_script(
'wp-i18n',
gutenberg_url( 'build/i18n/index.js' ),
array( 'wp-deprecated' ),
array( 'wp-deprecated', 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/i18n/index.js' ),
true
);
wp_register_script(
'wp-is-shallow-equal',
gutenberg_url( 'build/is-shallow-equal/index.js' ),
array(),
array( 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/is-shallow-equal/index.js' ),
true
);
wp_register_script(
'wp-token-list',
gutenberg_url( 'build/token-list/index.js' ),
array( 'lodash' ),
array( 'lodash', 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/token-list/index.js' ),
true
);
Expand All @@ -167,20 +167,10 @@ function gutenberg_register_scripts_and_styles() {
wp_register_script(
'wp-api-fetch',
gutenberg_url( 'build/api-fetch/index.js' ),
array( 'wp-hooks', 'wp-i18n' ),
array( 'wp-polyfill-ecmascript', 'wp-hooks', 'wp-i18n' ),
filemtime( gutenberg_dir_path() . 'build/api-fetch/index.js' ),
true
);
wp_add_inline_script(
'wp-api-fetch',
gutenberg_get_script_polyfill(
array(
'\'Promise\' in window' => 'wp-polyfill-promise',
'\'fetch\' in window' => 'wp-polyfill-fetch',
)
),
'before'
);
wp_add_inline_script(
'wp-api-fetch',
sprintf(
Expand All @@ -201,46 +191,47 @@ function gutenberg_register_scripts_and_styles() {
wp_register_script(
'wp-deprecated',
gutenberg_url( 'build/deprecated/index.js' ),
array( 'wp-hooks' ),
array( 'wp-polyfill-ecmascript', 'wp-hooks' ),
filemtime( gutenberg_dir_path() . 'build/deprecated/index.js' ),
true
);
wp_register_script(
'wp-blob',
gutenberg_url( 'build/blob/index.js' ),
array(),
array( 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/blob/index.js' ),
true
);
wp_register_script(
'wp-compose',
gutenberg_url( 'build/compose/index.js' ),
array( 'wp-element', 'wp-is-shallow-equal', 'lodash' ),
array( 'lodash', 'wp-element', 'wp-polyfill-ecmascript', 'wp-is-shallow-equal' ),
filemtime( gutenberg_dir_path() . 'build/compose/index.js' ),
true
);
wp_register_script(
'wp-keycodes',
gutenberg_url( 'build/keycodes/index.js' ),
array( 'lodash' ),
array( 'lodash', 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/keycodes/index.js' ),
true
);
wp_register_script(
'wp-html-entities',
gutenberg_url( 'build/html-entities/index.js' ),
array(),
array( 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/html-entities/index.js' ),
true
);
wp_register_script(
'wp-data',
gutenberg_url( 'build/data/index.js' ),
array(
'wp-element',
'lodash',
'wp-compose',
'wp-element',
'wp-polyfill-ecmascript',
'wp-is-shallow-equal',
'lodash',
'wp-redux-routine',
),
filemtime( gutenberg_dir_path() . 'build/data/index.js' ),
Expand All @@ -264,14 +255,14 @@ function gutenberg_register_scripts_and_styles() {
wp_register_script(
'wp-core-data',
gutenberg_url( 'build/core-data/index.js' ),
array( 'wp-data', 'wp-api-fetch', 'wp-url', 'lodash' ),
array( 'wp-polyfill-ecmascript', 'wp-data', 'wp-api-fetch', 'wp-url', 'lodash' ),
filemtime( gutenberg_dir_path() . 'build/core-data/index.js' ),
true
);
wp_register_script(
'wp-dom',
gutenberg_url( 'build/dom/index.js' ),
array( 'wp-tinymce', 'lodash' ),
array( 'lodash', 'wp-polyfill-ecmascript', 'wp-tinymce' ),
filemtime( gutenberg_dir_path() . 'build/dom/index.js' ),
true
);
Expand All @@ -285,7 +276,7 @@ function gutenberg_register_scripts_and_styles() {
wp_register_script(
'wp-block-serialization-spec-parser',
gutenberg_url( 'build/block-serialization-spec-parser/index.js' ),
array(),
array( 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/block-serialization-spec-parser/index.js' ),
true
);
Expand All @@ -301,21 +292,21 @@ function gutenberg_register_scripts_and_styles() {
wp_register_script(
'wp-shortcode',
gutenberg_url( 'build/shortcode/index.js' ),
array(),
array( 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/shortcode/index.js' ),
true
);
wp_register_script(
'wp-redux-routine',
gutenberg_url( 'build/redux-routine/index.js' ),
array(),
array( 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/redux-routine/index.js' ),
true
);
wp_register_script(
'wp-date',
gutenberg_url( 'build/date/index.js' ),
array( 'moment' ),
array( 'moment', 'wp-polyfill-ecmascript' ),
filemtime( gutenberg_dir_path() . 'build/date/index.js' ),
true
);
Expand Down Expand Up @@ -357,7 +348,7 @@ function gutenberg_register_scripts_and_styles() {
wp_register_script(
'wp-element',
gutenberg_url( 'build/element/index.js' ),
array( 'react', 'react-dom', 'lodash' ),
array( 'wp-polyfill-ecmascript', 'react', 'react-dom', 'lodash' ),
filemtime( gutenberg_dir_path() . 'build/element/index.js' ),
true
);
Expand All @@ -373,6 +364,7 @@ function gutenberg_register_scripts_and_styles() {
'wp-deprecated',
'wp-dom',
'wp-element',
'wp-polyfill-ecmascript',
'wp-hooks',
'wp-html-entities',
'wp-i18n',
Expand All @@ -398,6 +390,7 @@ function gutenberg_register_scripts_and_styles() {
'wp-deprecated',
'wp-dom',
'wp-element',
'wp-polyfill-ecmascript',
'wp-hooks',
'wp-i18n',
'wp-is-shallow-equal',
Expand All @@ -407,20 +400,10 @@ function gutenberg_register_scripts_and_styles() {
filemtime( gutenberg_dir_path() . 'build/blocks/index.js' ),
true
);

wp_add_inline_script(
'wp-blocks',
gutenberg_get_script_polyfill(
array(
'\'Promise\' in window' => 'wp-polyfill-promise',
)
),
'before'
);
wp_register_script(
'wp-viewport',
gutenberg_url( 'build/viewport/index.js' ),
array( 'wp-element', 'wp-data', 'wp-compose', 'lodash' ),
array( 'wp-polyfill-ecmascript', 'wp-element', 'wp-data', 'wp-compose', 'lodash' ),
filemtime( gutenberg_dir_path() . 'build/viewport/index.js' ),
true
);
Expand All @@ -437,8 +420,9 @@ function gutenberg_register_scripts_and_styles() {
'wp-blocks',
'wp-components',
'wp-compose',
'wp-data',
'wp-core-data',
'wp-data',
'wp-polyfill-ecmascript',
'wp-editor',
'wp-element',
'wp-html-entities',
Expand All @@ -453,10 +437,24 @@ function gutenberg_register_scripts_and_styles() {
wp_register_script(
'wp-nux',
gutenberg_url( 'build/nux/index.js' ),
array( 'wp-element', 'wp-components', 'wp-compose', 'wp-data', 'wp-i18n', 'lodash' ),
array(
'wp-element',
'wp-polyfill-ecmascript',
'wp-components',
'wp-compose',
'wp-data',
'wp-i18n',
'lodash',
),
filemtime( gutenberg_dir_path() . 'build/nux/index.js' ),
true
);
wp_register_script(
'wp-plugins',
gutenberg_url( 'build/plugins/index.js' ),
array( 'lodash', 'wp-element', 'wp-polyfill-ecmascript', 'wp-hooks', 'wp-compose' ),
filemtime( gutenberg_dir_path() . 'build/plugins/index.js' )
);
// Loading the old editor and its config to ensure the classic block works as expected.
wp_add_inline_script(
'editor',
Expand Down Expand Up @@ -579,6 +577,7 @@ function gutenberg_register_scripts_and_styles() {
'wp-deprecated',
'wp-dom',
'wp-element',
'wp-polyfill-ecmascript',
'wp-hooks',
'wp-html-entities',
'wp-i18n',
Expand Down Expand Up @@ -614,6 +613,7 @@ function gutenberg_register_scripts_and_styles() {
'wp-editor',
'wp-element',
'wp-embed',
'wp-polyfill-ecmascript',
'wp-i18n',
'wp-keycodes',
'wp-plugins',
Expand Down Expand Up @@ -714,13 +714,6 @@ function gutenberg_register_scripts_and_styles() {
);
wp_style_add_data( 'wp-block-library-theme', 'rtl', 'replace' );

wp_register_script(
'wp-plugins',
gutenberg_url( 'build/plugins/index.js' ),
array( 'lodash', 'wp-element', 'wp-hooks', 'wp-compose' ),
filemtime( gutenberg_dir_path() . 'build/plugins/index.js' )
);

if ( defined( 'GUTENBERG_LIVE_RELOAD' ) && GUTENBERG_LIVE_RELOAD ) {
$live_reload_url = ( GUTENBERG_LIVE_RELOAD === true ) ? 'http://localhost:35729/livereload.js' : GUTENBERG_LIVE_RELOAD;

Expand Down Expand Up @@ -830,11 +823,21 @@ function gutenberg_register_vendor_scripts() {
wp_add_inline_script( 'lodash', 'window.lodash = _.noConflict();' );
gutenberg_register_vendor_script(
'wp-polyfill-fetch',
'https://unpkg.com/whatwg-fetch/fetch.js'
'https://unpkg.com/whatwg-fetch/fetch.js',
array( 'wp-deprecated' )
);
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.

);
gutenberg_register_vendor_script(
'wp-polyfill-promise',
'https://unpkg.com/promise-polyfill@7.0.0/dist/promise' . $suffix . '.js'
'https://unpkg.com/promise-polyfill@7.0.0/dist/promise' . $suffix . '.js',
array( 'wp-deprecated' )
);
wp_add_inline_script(
'wp-polyfill-promise',
'wp.deprecated( "wp-polyfill-promise script handle", { plugin: "Gutenberg", version: "4.0" } );'
);
gutenberg_register_vendor_script(
'wp-polyfill-formdata',
Expand All @@ -844,6 +847,10 @@ function gutenberg_register_vendor_scripts() {
'wp-polyfill-node-contains',
'https://unpkg.com/polyfill-library@3.26.0-0/polyfills/Node/prototype/contains/polyfill.js'
);
gutenberg_register_vendor_script(
'wp-polyfill-ecmascript',
'https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.0.0/polyfill' . $suffix . '.js'
);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-preset-default/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 3.0.0 (Unreleased)

## Breaking Change

- The configured `@babel/preset-env` preset will no longer pass `useBuiltIns: 'usage'` as an option. It is therefore expected that a polyfill serve in its place, if necessary.

## 2.1.0 (2018-09-05)

### New Feature
Expand Down
Loading