-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Script Modules API: Backport - Add import map polyfill #5947
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
@@ -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); |
There was a problem hiding this comment.
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.
3fde764
to
cad52d7
Compare
There was a problem hiding this 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?
There was a problem hiding this 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 ) ) { |
There was a problem hiding this comment.
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.
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.