Skip to content

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

Closed
cbravobernal wants to merge 21 commits into
WordPress:trunkfrom
cbravobernal:add/modules-api-import-map-polyfill
Closed

Script Modules API: Backport - Add import map polyfill#5947
cbravobernal wants to merge 21 commits into
WordPress:trunkfrom
cbravobernal:add/modules-api-import-map-polyfill

Conversation

@cbravobernal

@cbravobernal cbravobernal commented Jan 25, 2024

Copy link
Copy Markdown
Contributor

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.

@github-actions

Copy link
Copy Markdown

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

luisherranz commented Jan 26, 2024

Copy link
Copy Markdown
Member

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

@cbravobernal

Copy link
Copy Markdown
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
Comment thread src/wp-includes/class-wp-script-modules.php Outdated
Comment thread src/wp-includes/class-wp-script-modules.php Outdated
Comment thread src/wp-includes/.wp-env.override.json Outdated
Comment thread src/wp-includes/class-wp-script-modules.php Outdated
@gziolo

gziolo commented Jan 26, 2024

Copy link
Copy Markdown
Member

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
Copy Markdown
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.

Comment thread Gruntfile.js Outdated
@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
Comment thread src/wp-includes/class-wp-script-modules.php Outdated
Comment thread tests/phpunit/tests/dependencies/scripts.php Outdated
Comment thread tests/phpunit/tests/script-modules/wpScriptModules.php Outdated
Comment thread tools/webpack/packages.js Outdated
Comment thread tests/phpunit/tests/dependencies/scripts.php Outdated
Comment thread src/wp-includes/class-wp-script-modules.php Outdated
Comment thread src/wp-includes/class-wp-script-modules.php Outdated
Comment thread src/wp-includes/class-wp-script-modules.php Outdated
Comment thread Gruntfile.js
Comment thread Gruntfile.js
} );
// `data:` URLs are allowed:
const match = contents.match( /sourceMappingURL=((?!data:).)/ );
const doesNotHaveSourceMap = ! /^\/\/# sourceMappingURL=((?!data:).)/m.test(contents);

@sirreal sirreal Jan 30, 2024

Copy link
Copy Markdown
Member

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

@gziolo gziolo left a comment

Copy link
Copy Markdown
Member

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?

@sirreal sirreal left a comment

Copy link
Copy Markdown
Member

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! 👍

$import_map = $this->get_import_map();
if ( ! empty( $import_map['imports'] ) ) {
global $wp_scripts;
if ( isset( $wp_scripts ) ) {

@gziolo gziolo Jan 31, 2024

Copy link
Copy Markdown
Member

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

gziolo commented Jan 31, 2024

Copy link
Copy Markdown
Member

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