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

Script Modules API: Backport - Add import map polyfill #5947

Conversation

cbravobernal
Copy link
Contributor

@cbravobernal cbravobernal commented Jan 25, 2024

Syncs the changes from WordPress/gutenberg#58263 to core.
Adds a polyfill to make import maps compatible with Safari old versions (under 15).

Trac ticket: https://core.trac.wordpress.org/ticket/60348

Screenshots:
Screenshot 2024-01-25 at 19 25 10
Screenshot 2024-01-25 at 18 57 42


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@luisherranz
Copy link
Member

luisherranz commented Jan 26, 2024

I requested changes in the Gutenberg's PR but the same should be applied here:

@cbravobernal
Copy link
Contributor Author

Done in 7dd5f5a

@cbravobernal cbravobernal changed the title Modules API: Backport - Add import map polyfill Script Modules API: Backport - Add import map polyfill Jan 26, 2024
@gziolo
Copy link
Member

gziolo commented Jan 26, 2024

Code-wise it seems to be ready to go. Do we have unit tests covering other polyfills? We could verify if the script prints when there are items in the import map.

@cbravobernal
Copy link
Contributor Author

Code-wise it seems to be ready to go. Do we have unit tests covering other polyfills? We could verify if the script prints when there are items in the import map.

I will take a look at it, if not, I will check if can add unit tests to cover them.

Gruntfile.js Outdated Show resolved Hide resolved
@cbravobernal cbravobernal force-pushed the add/modules-api-import-map-polyfill branch 2 times, most recently from 0ea9316 to 231c9fc Compare January 30, 2024 10:00
tools/webpack/packages.js Outdated Show resolved Hide resolved
Gruntfile.js Show resolved Hide resolved
@@ -1591,10 +1589,10 @@ module.exports = function(grunt) {
encoding: 'utf8',
} );
// `data:` URLs are allowed:
const match = contents.match( /sourceMappingURL=((?!data:).)/ );
const doesNotHaveSourceMap = ! /^\/\/# sourceMappingURL=((?!data:).)/m.test(contents);
Copy link
Member

@sirreal sirreal Jan 30, 2024

Choose a reason for hiding this comment

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

Sharing notes on this change that I co-authored with @c4rl0sbr4v0.

First, a minor change. We use RegExp.test() instead of String.match(). We don't care about the result of matching, we want to know "does this match," so RegExp.test() (with its boolean result) is ideal.

Second, and more important. The expression was not specific enough and was matching JavaScript source files that were, themselves, looking for sourcemap comments. In this PR is was matching this line from es-module-shims:

  const sourceMapURLCommentPrefix = '\n//# sourceMappingURL=';

We change to better match a source map comment by anchoring the expression to the start of the line and matching the comment opening //# . This fixes the false positives and allows us to remove several files from the ignore list.

@cbravobernal cbravobernal force-pushed the add/modules-api-import-map-polyfill branch from 3fde764 to cad52d7 Compare January 30, 2024 14:07
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

Everything looks good from my perspective. Thank you for all the iterations! @sirreal, is it ready to commit?

Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

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

I've only reviewed the small bit I was involved with around the build (verify that sourcemap URLs are not included in compiled JS filed).

Looks good! 👍

* @since 6.5.0
*/
public function print_import_map() {
$import_map = $this->get_import_map();
if ( ! empty( $import_map['imports'] ) ) {
global $wp_scripts;
if ( isset( $wp_scripts ) ) {
Copy link
Member

@gziolo gziolo Jan 31, 2024

Choose a reason for hiding this comment

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

What would be the case when $wp_scripts is not set? I'm asking because I don't see it guarded anywhere else in the codebase?

I see some ! empty( $wp_scripts ) checks.

@gziolo
Copy link
Member

gziolo commented Jan 31, 2024

Committed with https://core.trac.wordpress.org/changeset/57492.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants