Dependency Extraction Webpack Plugin: Prettify the output of the development asset files
What problem does this address?
During the build process of WordPress the script-loader-packages.php and script-loader-packages.min.php files are generated and list the included packages, and the associated the dependencies and versions.
The unminified version is currently not formatted to be very readable by humans.
What is your proposed solution?
Since the dependency-extraction-webpack-plugin package was updated to depend on json2php 0.0.7 there is now the option to use .make() to have greater control over the formatting of the output.
I propose that the stringify() function is updated to more prettily format the output for the unminified file.
A very rough POC is to update the function to:
stringify( asset ) {
if ( this.options.outputFormat === 'php' ) {
if ( this.options.combinedOutputFile.indexOf( ".min" ) > -1 ) {
return `<?php return ${ json2php(
JSON.parse( JSON.stringify( asset ) )
) };\n`;
} else{
const printer = json2php.make( { linebreak:'\n', indent:'\t' } );
return `<?php return ${ printer(
JSON.parse( JSON.stringify( asset ) )
) };\n`;
}
}
return JSON.stringify( asset );
}
This will leave the minified file as it is currently generated but add formatting to the unminified version.
Hey @mattyrob, I'd love to grab this issue!
Hi @derekhassan - please go ahead if you can create a PR.
hi, can I try on this?
so from what I've understood is to update the stringify function as proposed
@xerpa43 - that's the idea with the result being that the script-loader-packages.php file that is created should be a lot more human readable as opposed the the current output that is pretty much identical to the script-loader-packages.min.php` file and not very human-readable for figuring out the code.
so I think this should make it work:
stringify(asset) {
if (this.options.outputFormat === 'php') {
if (this.options.combinedOutputFile.indexOf('.min') > -1) {
return <?php return ${json2php(JSON.parse(JSON.stringify(asset)))};\n;
} else {
const printer = json2php.make({ linebreak: '\n', indent: '\t' });
return <?php return ${printer(JSON.parse(JSON.stringify(asset)))};\n;
}
}
return JSON.stringify(asset, null, 2); //2 for indentation
}
I've tested using the JSON2PHP make system to prettify output, here was the result:
<?php return array(
'dependencies' => array(
'wp-block-editor',
'wp-blocks',
'wp-element',
'wp-i18n',
'wp-server-side-render'
),
'version' => 'f7b62036d885e2304805'
);
This is much better, but unfortunately still doesn't pass WordPress-Extra PHPCS checks. Items remaining:
- Each array item in a multi-line array declaration must end in a comma [phpcs]
- Array double arrow not aligned correctly; expected 6 space(s) between "'version'" and double arrow, but found 1. [phpcs]
The other option would be to prepend the generated file with a comment for phpcs/wpcs to ignore it entirely
The code below will handle the additional [phpcs] issues by correctly spacing array declarations and adding trailing commas. There may be more elegant or efficient ways of doing this, but it works.
stringify( asset ) {
if ( this.options.outputFormat === 'php' ) {
if ( null !== this.options.combinedOutputFile && this.options.combinedOutputFile.indexOf( ".min" ) > -1 ) {
return `<?php return ${ json2php(
JSON.parse( JSON.stringify( asset ) )
) };\n`;
} else if ( null !== this.options.combinedOutputFile ) {
const printer = json2php.make( { linebreak:"\n", indent:'\t' } );
let out = '';
out += '<?php\n';
out += 'return ' + printer( JSON.parse( JSON.stringify( asset ) ) ) ;
out += ';\n';
// Add trailing comma where needed.
out = out.replace( /'\n/g, "',\n" ).replace( ')\n', '),\n' );
// Correctly space version declarations.
out = out.replace( /'version' =>/g, "'version' =>" );
// Correctly space array declarations for each package.
const regexp = /\n[\t]{1}'.*'\s=>/g;
let packages = out.match( regexp ).sort( ( a, b ) => b.length - a.length );
const longest = packages[0].length;
let newpkg = '';
for ( const pkg of packages ) {
if ( pkg.length < longest ) {
newpkg = pkg.replace( " ", " ".repeat( longest - ( pkg.length - 1 ) ) );
out = out.replace( pkg, newpkg );
}
}
return out;
}
}
return JSON.stringify( asset );
}
Opened #79650 for this. It pretty-prints the .asset.php files on dev builds (when optimization.minimize is false) and leaves the minified production output alone.
Is there a good reason not to always pretty print the asset files?