Script Modules API: Backport - Add import map polyfill#5947
Script Modules API: Backport - Add import map polyfill#5947cbravobernal wants to merge 21 commits intoWordPress:trunkfrom
Conversation
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
I requested changes in the Gutenberg's PR but the same should be applied here: |
|
Done in 7dd5f5a |
|
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. |
0ea9316 to
231c9fc
Compare
| } ); | ||
| // `data:` URLs are allowed: | ||
| const match = contents.match( /sourceMappingURL=((?!data:).)/ ); | ||
| const doesNotHaveSourceMap = ! /^\/\/# sourceMappingURL=((?!data:).)/m.test(contents); |
There was a problem hiding this comment.
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.
3fde764 to
cad52d7
Compare
sirreal
left a comment
There was a problem hiding this comment.
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 ) ) { |
There was a problem hiding this comment.
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.
|
Committed with https://core.trac.wordpress.org/changeset/57492. |
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:


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.