forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Packages: only add polyfills where needed (WordPress#65292)
* Packages: only add polyfills where needed * Attempt to fix interactivity-router * Switch to using magic comments * Remove previous experiment * Clean up changelog, comments and option name * Fix small typo in babel plugin comment * Tweak comment preservation regex * Simplify buffer check Co-authored-by: Jon Surrell <[email protected]> * Fix outdated comment * Use Internal header for babel preset changelog * Add unit test for DEWP * Add babel plugin test * Update babel plugin readme * Simplify DEWP test Co-authored-by: Jon Surrell <[email protected]> --------- Co-authored-by: sgomes <[email protected]> Co-authored-by: sirreal <[email protected]> Co-authored-by: gziolo <[email protected]> Co-authored-by: swissspidy <[email protected]> Co-authored-by: cbravobernal <[email protected]> Co-authored-by: tyxla <[email protected]> Co-authored-by: jsnajdr <[email protected]>
- Loading branch information
1 parent
594a948
commit bf63906
Showing
15 changed files
with
173 additions
and
16 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = [ | ||
// Ignore excessively strict polyfilling of `Array.prototype.push` to work | ||
// around an obscure bug involving non-writable arrays. | ||
// See https://issues.chromium.org/issues/42202623 for the details of the | ||
// bug that leads to the polyfilling, and which we are choosing to ignore. | ||
'es.array.push', | ||
// This is an IE-only feature which we don't use, and don't want to polyfill. | ||
// @see https://github.com/WordPress/gutenberg/pull/49234 | ||
'web.immediate', | ||
]; |
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,59 @@ | ||
// 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. | ||
function replacePolyfills() { | ||
return { | ||
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( 'core-js/' ) ) { | ||
// Remove import. | ||
path.remove(); | ||
this.hasAddedPolyfills = true; | ||
} | ||
}, | ||
|
||
// Handle `require` syntax. | ||
CallExpression( path ) { | ||
const callee = path?.node?.callee; | ||
const arg = path?.node?.arguments[ 0 ]; | ||
|
||
if ( | ||
! callee || | ||
! arg || | ||
callee.type !== 'Identifier' || | ||
callee.name !== 'require' | ||
) { | ||
return; | ||
} | ||
|
||
// Look for requires for `core-js`. | ||
if ( | ||
arg.type === 'StringLiteral' && | ||
arg.value.startsWith( 'core-js/' ) | ||
) { | ||
// Remove import. | ||
path.remove(); | ||
this.hasAddedPolyfills = true; | ||
} | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = replacePolyfills; |
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,6 @@ | ||
// Note: this fixture may need to be updated when the browserslist or the | ||
// core-js dependencies are updated. | ||
// It should always test a feature that is supported, but requires | ||
// a polyfill to work across all supported browsers. | ||
const foo = new URLSearchParams(); | ||
window.fooSize = foo.size; |
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
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
3 changes: 3 additions & 0 deletions
3
packages/dependency-extraction-webpack-plugin/test/fixtures/polyfill-magic-comment/index.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,3 @@ | ||
/* wp:polyfill */ | ||
|
||
// Nothing else, really. |
8 changes: 8 additions & 0 deletions
8
...pendency-extraction-webpack-plugin/test/fixtures/polyfill-magic-comment/webpack.config.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,8 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
const DependencyExtractionWebpackPlugin = require( '../../..' ); | ||
|
||
module.exports = { | ||
plugins: [ new DependencyExtractionWebpackPlugin() ], | ||
}; |
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