gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Dependency Extraction Webpack Plugin: Prettify the output of the development asset files

Open mattyrob opened this issue 3 years ago • 11 comments

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.

mattyrob avatar Feb 15 '23 17:02 mattyrob

Hey @mattyrob, I'd love to grab this issue!

derekhassan avatar Feb 20 '23 00:02 derekhassan

Hi @derekhassan - please go ahead if you can create a PR.

mattyrob avatar Feb 20 '23 08:02 mattyrob

hi, can I try on this?

xerpa43 avatar Mar 14 '23 12:03 xerpa43

so from what I've understood is to update the stringify function as proposed

xerpa43 avatar Mar 18 '23 13:03 xerpa43

@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.

mattyrob avatar Mar 18 '23 14:03 mattyrob

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 }

xerpa43 avatar Mar 18 '23 15:03 xerpa43

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]

tommusrhodus avatar Mar 24 '23 10:03 tommusrhodus

The other option would be to prepend the generated file with a comment for phpcs/wpcs to ignore it entirely

georgestephanis avatar Mar 24 '23 14:03 georgestephanis

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 );
}

mattyrob avatar Apr 06 '23 08:04 mattyrob

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.

gunjanjaswal avatar Jun 29 '26 18:06 gunjanjaswal

Is there a good reason not to always pretty print the asset files?

sirreal avatar Aug 24 '26 05:08 sirreal