-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use DEWP's new auto-wp-polyfill support (#39629)
`@wordpress/dependency-extraction-webpack-plugin` has added the ability to detect a magic `/* wp:polyfill */` comment and only add `wp-polyfill` as a dependency if that is found. Since `wp-polyfill` is just a custom build of `core-js`, the intention is that `babel-plugin-polyfill-corejs3` (maybe via `@babel/preset-env`) would be used and then a custom Babel plugin would replace the `core-js` imports with the magic comment. This updates our Babel config to do just that, and removes the blanket addition of `wp-polyfill` via `injectPolyfill` or otherwise.
- Loading branch information
Showing
66 changed files
with
321 additions
and
135 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/js-packages/webpack-config/changelog/add-dependency-extraction-auto-polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: fixed | ||
|
||
Babel preset: Fix `pluginPreserveI18n` option. |
4 changes: 4 additions & 0 deletions
4
projects/js-packages/webpack-config/changelog/add-dependency-extraction-auto-polyfill#2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Babel preset: Add default for base `targets` option, replacing default `.presetEnv.targets`. |
4 changes: 4 additions & 0 deletions
4
projects/js-packages/webpack-config/changelog/add-dependency-extraction-auto-polyfill#3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Babel preset: Add `autoWpPolyfill` option. |
4 changes: 4 additions & 0 deletions
4
projects/js-packages/webpack-config/changelog/add-dependency-extraction-auto-polyfill#4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: fixed | ||
|
||
Update documentation for `DependencyExtractionPlugin` after #38877 and #38430. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
projects/js-packages/webpack-config/src/babel/replace-polyfills.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const npath = require( 'path' ); | ||
|
||
/** | ||
* Babel plugin that looks for `core-js` imports (or requires) | ||
* and replaces them with magic comments to mark the file as | ||
* depending on wp-polyfill. | ||
* | ||
* Based on https://github.com/WordPress/gutenberg/blob/28f1b5b308a62098f0e1e253cb734c83b2fa1356/packages/babel-preset-default/replace-polyfills.js | ||
* | ||
* @param {object} babel - Babel object. | ||
* @param {object} opts - Options from Babel config. | ||
* @return {object} Babel plugin. | ||
*/ | ||
module.exports = ( babel, opts ) => { | ||
const { types: t } = babel; | ||
const coreJsPrefix = opts.absoluteImports | ||
? npath.dirname( | ||
require.resolve( 'core-js/package.json', { paths: [ opts.absoluteImports ] } ) | ||
) + '/' | ||
: 'core-js/'; | ||
|
||
return { | ||
name: 'replacePolyfills', | ||
pre() { | ||
this.hasAddedPolyfills = false; | ||
}, | ||
visitor: { | ||
Program: { | ||
exit( path ) { | ||
if ( this.hasAddedPolyfills ) { | ||
// Add magic comment to top of file. | ||
path.addComment( 'leading', ' wp:polyfill ' ); | ||
} | ||
}, | ||
}, | ||
|
||
// Handle `import` syntax. | ||
ImportDeclaration( path ) { | ||
const source = path.node.source; | ||
const name = source.value || ''; | ||
|
||
// Look for imports from `core-js`. | ||
if ( name.startsWith( coreJsPrefix ) ) { | ||
// Replace import. | ||
path.replaceWith( t.noop() ); | ||
path.addComment( 'leading', ` wp:polyfill ${ npath.basename( name, '.js' ) } ` ); | ||
this.hasAddedPolyfills = true; | ||
} | ||
}, | ||
|
||
// Handle `require` syntax. | ||
ExpressionStatement( path ) { | ||
const expression = path.node.expression; | ||
if ( ! t.isCallExpression( expression ) ) { | ||
return; | ||
} | ||
|
||
const callee = expression.callee; | ||
const arg = expression.arguments[ 0 ]; | ||
if ( | ||
t.isIdentifier( callee ) && | ||
callee.name === 'require' && | ||
t.isStringLiteral( arg ) && | ||
arg.value.startsWith( coreJsPrefix ) | ||
) { | ||
// Replace require. | ||
path.replaceWith( t.noop() ); | ||
path.addComment( 'leading', ` wp:polyfill ${ npath.basename( arg.value, '.js' ) } ` ); | ||
this.hasAddedPolyfills = true; | ||
} | ||
}, | ||
}, | ||
}; | ||
}; |
4 changes: 4 additions & 0 deletions
4
projects/packages/backup/changelog/add-dependency-extraction-auto-polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: changed | ||
|
||
Only include `wp-polyfill` as a script dependency when needed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/packages/blaze/changelog/add-dependency-extraction-auto-polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: changed | ||
|
||
Only include `wp-polyfill` as a script dependency when needed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
projects/packages/classic-theme-helper/changelog/add-dependency-extraction-auto-polyfill#2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Significance: patch | ||
Type: removed | ||
Comment: Remove unnecessary JS dep on `@wordpress/dependency-extraction-webpack-plugin` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/packages/explat/changelog/add-dependency-extraction-auto-polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: changed | ||
|
||
Only include `wp-polyfill` as a script dependency when needed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/packages/forms/changelog/add-dependency-extraction-auto-polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: changed | ||
|
||
Only include `wp-polyfill` as a script dependency when needed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/packages/jetpack-mu-wpcom/changelog/add-dependency-extraction-auto-polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: changed | ||
|
||
Only include `wp-polyfill` as a script dependency when needed. |
Oops, something went wrong.