-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Convert theme.json to theme-json.php for better performance. #4641
base: trunk
Are you sure you want to change the base?
Conversation
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 seems sensible, just a note about avoiding the need to commit additional built files.
From the Gutenberg issue:
This part is missing here. |
@swissspidy I commented elsewhere. But converting that file doesn't work, as this is an object with object. So you to convert array of array to objects, which can be expensive. Do you think I should do that as well? |
@spacedmonkey It's not about converting It's about using Let me explain: With your PHP, <?php
// theme-json.php
return = [
"version" => 2,
"settings" => [
// ...
"color" => [
"background" => true,
"custom" => true,
"customDuotone" => true,
"customGradient" => true,
"defaultDuotone" => true,
"defaultGradients" => true,
"defaultPalette" => true,
"duotone" => [
[
"name" => "Dark grayscale",
"colors" => [
"#000000",
"#7f7f7f"
],
"slug" => "dark-grayscale"
]
]
]
]
// ...
]; Then, That is slow. So to fix this, the Grunt command should create the PHP file with translations automatically applied. With the above example, it would look like: <?php
// theme-json.php
return = [
"version" => 2,
"settings" => [
// ...
"color" => [
"background" => true,
"custom" => true,
"customDuotone" => true,
"customGradient" => true,
"defaultDuotone" => true,
"defaultGradients" => true,
"defaultPalette" => true,
"duotone" => [
[
"name" => _x( "Dark grayscale", "Color name"), // See translation here
"colors" => [
"#000000",
"#7f7f7f"
],
"slug" => "dark-grayscale"
]
]
]
]
// ...
]; |
Is it, there profiling data for that? I feel like the I/O of |
It is slower than not doing any Hope my above example output makes sense. |
+1 to @swissspidy's feedback, while this PR is a great start, it would be more efficient to have the To be fair though, the existing One question I have though, shouldn't this be implemented in Gutenberg first (hence I created it as a Gutenberg issue)? Or is there something that prevents us from doing that? |
@@ -1462,6 +1462,17 @@ module.exports = function(grunt) { | |||
); | |||
} ); | |||
|
|||
grunt.registerTask( 'copy:theme-json', 'Copies theme.json file contents to theme-json.php.', function() { | |||
[ 'theme.json', 'theme-i18n.json' ].forEach( function( fileName ) { | |||
var themeData = grunt.file.readJSON(SOURCE_DIR + 'wp-includes/' + fileName); |
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.
Should we formally check if both JSON file are present before the readJSON
operation? It shouldn't make much difference as we know files will be always present.
Also, should we add a verification task to check if the files were created. ref (verify:old-files).
grunt.registerTask( 'verify:old-files', function() {
const file = `${ BUILD_DIR }wp-admin/includes/update-core.php`;
assert(
fs.existsSync( file ),
'The build/wp-admin/includes/update-core.php file does not exist.'
);
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.
Thanks @spacedmonkey. This looks good to me just added a small question in Gruntfile.js
@swissspidy @felixarntz Thanks for keeping the native english speakers honest. This PR is probably a case in which it's best to let the perfect be the enemy of the good so the performance gains for en_US installs aren't considered enough and the rest of the world (including other en_* sites) left with the performance cost. |
@peterwilsoncc I'm not sure I understand, are you saying we should hold off this change until we build in the translation support into the built file? If I understand you correctly, you're concerned this change would only benefit en_US sites. That's not the case though, it would be a performance enhancement for any WordPress site already in its current shape. It simply avoids parsing the JSON file. The translation behavior wouldn't change from how it is today. So there are only wins here, no losses for anyone. |
I don't think it makes sense to have some way of automated the merging and automatic translations of these two files. I think it is would hard to build and not make sense when this code is written. Translations can have complexities, like needing context parameters or plurral etc. Automating this conversation doesn't make sense to me. I think there is a two courses of action to go forward.
I also don't really understand why this is a json file at all. The point of json, is so that could be read outside of PHP, in javascript etc. But this file is only ever used in PHP. I would like the thoughts of @peterwilsoncc @swissspidy @felixarntz on this one. |
But we already do that though. The merging is already automated, just at runtime (in
This is a good start, but we can go even further. If we also automate the i18n stuff, we don't even need to read the
That only works for the While I am writing all this I noticed that we should really do this for |
Trac ticket:
Fixes: WordPress/gutenberg#45616
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.