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

Utils: Default to empty object for previous defined utils #6077

Merged
merged 1 commit into from
Apr 10, 2018

Conversation

youknowriad
Copy link
Contributor

closes #6063

Depending on the order of the loaded scripts (which can change depending on the plugins used), we can see an error like this ReferenceError: Can't find variable: wp while depending on the wp-utils script because the global wp and wp.utils are not defined if this is the first script to be loaded.

This PR default to an empty object if one of these variables are not defined.

@youknowriad youknowriad added the [Type] Bug An existing feature does not function as intended label Apr 9, 2018
@youknowriad youknowriad added this to the 2.7 milestone Apr 9, 2018
@youknowriad youknowriad self-assigned this Apr 9, 2018
@youknowriad youknowriad requested a review from aduth April 9, 2018 10:20
@@ -97,7 +97,7 @@ function gutenberg_register_scripts_and_styles() {
filemtime( gutenberg_dir_path() . 'utils/build/index.js' ),
true
);
wp_add_inline_script( 'wp-utils', 'var originalUtils = wp.utils;', 'before' );
wp_add_inline_script( 'wp-utils', 'window.wp = window.wp || {}; var originalUtils = wp && wp.utils ? wp.utils : {};', 'before' );
Copy link
Member

Choose a reason for hiding this comment

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

Do we need the first part of this line? window.wp = window.wp || {};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well since the error is ReferenceError: Can't find variable: wp I believe so. If the wp-utils is the first script to be loaded, there will be no wp global defined at all before the script.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, right. Though then it begs the question do we need the second part, wp &&, since we know it'll be defined since we just defined it if it wasn't.

Maybe we just test against the property on window:

var originalUtils = window.wp && window.wp.utils ? window.wp.utils : {};

@youknowriad youknowriad merged commit 7022b76 into master Apr 10, 2018
@youknowriad youknowriad deleted the fix/wp-utils-deps branch April 10, 2018 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wp-utils console error in version 2.6
2 participants