-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
wp.hooks - enable the 'all' hook #12038
Conversation
What's the status of this one. Is it still something that it expected to be included in the library? |
Yes, this seems genuinely useful (maybe essential) for debugging hooks. I'll work on adding some tests and exploring the monkey patching approach forward. To introduce this feature, we need to insure it has minimal or no impact on performance. One other option I might explore instead of monkey patching is a debug version of the build that would include the 'all' hook support and would be loaded when SCRIPT_DEBUG is true. that would likely be a cleaner approach and would result in zero impact on production performance. Since this feature is intended for debugging only, this might make the most sense anyway. What do you think? |
Sounds great to be disabled by default but enabled in debug mode. I'm wondering how it could be distributed to npm to take advantage of it and ensure that dead code is never bundled for production. |
How about using the dev/prod build flag so we build the minified production bundle without the 'all' support, while building the unminified version with the 'all' hook support? I'll try to work on this. |
This small change may be all that is required. I am going to write some tests against this functionality to see if it works as expected. The performance penalty should be very small in practice, when no |
packages/hooks/src/createRunHook.js
Outdated
/* develblock:start */ | ||
// Handle any 'all' hooks registered. | ||
if ( hooks.all ) { | ||
handlers.push( hooks.all.handlers ); |
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.
You're pushing an array into the array, rather than the members of that array.
i.e.
handlers = [ 1 ];
hooks.all.handlers = [ 2 ];
handlers.push( hooks.all.handlers );
console.log( handlers );
// [ 1, [ 2 ] ]
You'd want one of either:
handlers = handlers.concat( hooks.all.handlers );
handlers.push( ...hooks.all.handlers );
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.
eagle eyes! i wouldn't have caught that error until i wrote the tests. will fix
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.
this is fixed.
packages/hooks/src/createRunHook.js
Outdated
@@ -30,6 +30,14 @@ function createRunHook( hooks, returnFirstArg ) { | |||
|
|||
const handlers = hooks[ hookName ].handlers; | |||
|
|||
// The following code can be stripped from production builds. | |||
/* develblock:start */ |
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.
Is this meant to be processed by a Webpack plugin we're not using?
https://www.npmjs.com/package/webpack-strip-block
We may consider instead a combination of Webpack's (built-in) DefinePlugin and dead-code elimination from its (built-in) minification:
https://webpack.js.org/plugins/define-plugin/
Prior art:
gutenberg/packages/blocks/src/api/validation.js
Lines 257 to 263 in 31b6b17
// In test environments, pre-process the sprintf message to improve // readability of error messages. We'd prefer to avoid pulling in this // dependency in runtime environments, and it can be dropped by a combo // of Webpack env substitution + UglifyJS dead code elimination. if ( process.env.NODE_ENV === 'test' ) { return ( ...args ) => logger( require( 'sprintf-js' ).sprintf( ...args ) ); } - Add feature flags for Phase 2 #13324
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.
very interesting.
i was actually imagining that the all hook handling would remain in the package by default and that we would strip it in the WordPress core build for the minified file.
In core we already build two versions of each script: one for SCRIPT_DEBUG and one for production. We can use these tags and the webpack-strip-block plugin to remove them in core's production build. This would completely eliminate the code for produciton and not introduce the overhead of checking the constant.
What do you think?
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.
The treatment of the environment variable is a bit of a facade, really. It actually achieves the same effect as what you're describing. The way the DefinePlugin works is to literally swap the identifier with some substitute. Thus, if you introduced a condition like this, the body of the condition would be kept for development, and removed for production.
Before (source):
/* a */
if ( process.env.NODE_ENV !== 'production' ) {
/* b */
}
/* c */
After (built with Webpack, unminified):
/* a */
if ( 'production' !== 'production' ) {
/* b */
}
/* c */
After (built with Webpack, minified, comments kept for demonstration only):
/* a *//* c */
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.
Ok, cool - so we don't need the or the tagging, just a conditional, got it. thanks for clarifying.
So in terms of what we ship in the package, we only ship source right? Do we ship debug scripts with the GB plugin or in core?
my ultimate goal is to have this depend of a wp-config constant, ideally SCRIPT_DEBUG.
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.
So in terms of what we ship in the package, we only ship source right? Do we ship debug scripts with the GB plugin or in core?
My expectation would have been that process.env.NODE_ENV
would be effectively "development" for the non-minified scripts we ship with core, and thus if SCRIPT_DEBUG
would cause the unminified form to load, it would take effect as you expect. However, considering an example where we console.error
a message, I see no reference to said message in the full unminified source for wp.components
. I'm not too sure if this is intended or not.
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 think that process.env.NODE_ENV
would work the way it's expected. WordPress core would have to generate two versions of the files: development
and production
. SCRIPT_DEBUG
would pick the proper version then. If this isn't the case in core, we should fix it.
Added some tests demonstrating the 'all' hook in 92920ff |
cc: @johnbillion this might be useful for query monitor |
@adamsilverstein, do you plan to use guard based on |
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.
This PR needs also rebase to trigger new Travis services.
Yes, thanks for the reminder about this ticket. I will work on it. |
5210e14
to
42659b9
Compare
Great, thanks for the tip - I'll change that. |
@aduth thanks, that fixed it. This is ready for review. |
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.
This all looks good now. Should we include docs for all
hook and add an entry to CHANGELOG file? It might be worth emphasizing that it works in non-production environments only.
Yes, I will work on adding that, thanks! |
packages/hooks/CHANGELOG.md
Outdated
@@ -1,3 +1,6 @@ | |||
## 2.1.0 (2019-05-16) |
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.
## 2.1.0 (2019-05-16) | |
## Master |
@@ -1,3 +1,9 @@ | |||
## Master |
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 updated CHANGELOG myself. We are still not set on the proper recommendation here, but there is new emerging pattern to list new changes as coming from master
. Probably something good to document. /cc @aduth
@adamsilverstein - I slightly modified CHANGELOG (see my comment #12038 (comment)). Great work, thanks for wrangling this enhancement 👍 |
Description
Fixes #8602
How has this been tested?
First pass: needs testing.
Types of changes
Checklist: