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

Add Webpack plugin to ignore JS and PHP asset files when stylesheet used as entrypoint #6078

Merged
merged 7 commits into from Apr 21, 2021

Conversation

pierlon
Copy link
Contributor

@pierlon pierlon commented Apr 19, 2021

Summary

Follow up of #6057.

This PR adds a custom Webpack plugin that identifies stylesheet entries and prevents empty JS chunks and PHP asset files from being emitted for each entry. The plugin has been added to the shared config so that each Webpack config now makes use of it by default.

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@pierlon pierlon added the WS:Core Work stream for Plugin core label Apr 19, 2021
@pierlon pierlon added this to the v2.1 milestone Apr 19, 2021
@pierlon pierlon self-assigned this Apr 19, 2021
@pierlon pierlon added this to Ready for Review in Ongoing Apr 19, 2021
@@ -334,6 +335,8 @@ const settingsPage = {
}
},
} ),
// Ensure assets generated by `DependencyExtractionWebpackPlugin` are also ignored for styles entries.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should prevent the empty JS chunk and PHP asset file for the wp-components entry from being generated.

Copy link
Member

Choose a reason for hiding this comment

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

Success:

image

@github-actions
Copy link
Contributor

github-actions bot commented Apr 19, 2021

Plugin builds for 6c9e5da are ready 🛎️!

@pierlon
Copy link
Contributor Author

pierlon commented Apr 19, 2021

Investigating CI issues...

@codecov
Copy link

codecov bot commented Apr 19, 2021

Codecov Report

Merging #6078 (6c9e5da) into develop (c9c3784) will decrease coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@              Coverage Diff              @@
##             develop    #6078      +/-   ##
=============================================
- Coverage      74.92%   74.91%   -0.02%     
  Complexity      5775     5775              
=============================================
  Files            231      231              
  Lines          17484    17487       +3     
=============================================
  Hits           13100    13100              
- Misses          4384     4387       +3     
Flag Coverage Δ Complexity Δ
javascript 79.84% <ø> (ø) 0.00 <ø> (ø)
php 74.68% <ø> (-0.02%) 5775.00 <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ Complexity Δ
src/Cli/ValidationCommand.php 15.82% <0.00%> (-0.31%) 34.00% <0.00%> (ø%)

Comment on lines +53 to +54
$logger = new WP_CLI\Loggers\Regular( true );
WP_CLI::set_logger( $logger );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

PHPUnit tests were failing due to a call to WP_CLI::warning() failing to log a message:

amp-wp/amp.php

Line 217 in 1e7ea02

WP_CLI::warning( $message );

PHP Fatal error: Uncaught Error: Call to a member function warning() on null in /tmp/wordpress/src/wp-content/plugins/amp/vendor/wp-cli/wp-cli/php/class-wp-cli.php:808

It turns out the deafult WP CLI logger is set defined when WP_CLI::bootstrap() is called, but that isn't applicable in a testing environment. Instead, I've manually initialized and set a logger during the PHPUnit bootstrap process.

Copy link
Member

Choose a reason for hiding this comment

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

What is the warning that is being emitted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The amp-block-editor.js file was previously not being emitted by Webpack (which is now fixed in 4b47698). Because of that the following error was being emitted:

amp-wp/amp.php

Lines 160 to 169 in 1e7ea02

if ( ! file_exists( AMP__DIR__ . '/vendor/autoload.php' ) || ! file_exists( AMP__DIR__ . '/vendor/sabberworm/php-css-parser' ) || ! file_exists( AMP__DIR__ . '/assets/js/amp-block-editor.js' ) ) {
$_amp_load_errors->add(
'build_required',
sprintf(
/* translators: %s: composer install && npm install && npm run build:prod */
__( 'You appear to be running the AMP plugin from source. Please do %s to finish installation.', 'amp' ), // phpcs:ignore WordPress.Security.EscapeOutput
'<code>composer install &amp;&amp; npm install &amp;&amp; npm run build:prod</code>'
)
);
}

Copy link
Member

Choose a reason for hiding this comment

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

So now the warning is not being emitted? But if it were to be emitted again, then the logger change will ensure the warning will be emitted without a failure?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes and yes.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Output checks out!

I defer to @delawski for the webpack changes.

Copy link
Collaborator

@delawski delawski left a comment

Choose a reason for hiding this comment

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

@pierlon This looks really good!

Thank you for improving the workflow introduced in #6057.

I left one minor and optional suggestion.

bin/ignore-extraneous-style-assets.js Outdated Show resolved Hide resolved
Comment on lines -60 to -70
new TerserPlugin( {
parallel: true,
sourceMap: false,
cache: true,
terserOptions: {
output: {
comments: /translators:/i,
},
},
extractComments: false,
} ),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Out of curiosity: why are we no longer overriding the TerserPlugin config (or why have we done so before at all)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The config we're importing from @wordpress/scripts already configures the TerserPlugin the same way we do (with the additional config of not mangling translations).

As for why we were overriding it, we actually were not. The TerserPlugin config was only "recently" added to the default Webpack config (see WordPress/gutenberg#22990) (relative to when this plugin config was first added, that is).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the explanation 🙏

Comment on lines 349 to 356
entry: () => {
const entries = {};
const dir = './assets/css/src';
fs.readdirSync( dir ).forEach( ( fileName ) => {
entries[ fileName.replace( /\.[^/.]+$/, '' ) ] = `${ dir }/${ fileName }`;
} );

return entries;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like that!

pierlon and others added 2 commits April 21, 2021 13:10
Co-authored-by: Piotr Delawski <delawski@users.noreply.github.com>
@pierlon
Copy link
Contributor Author

pierlon commented Apr 21, 2021

Thanks for the review @delawski and @westonruter. This should be good to ship now 😄.

.flat()
.join( '|' );
.reduce( ( acc, entry ) => `${ acc }|${ entry }.js|${ entry }.asset.php`, '' )
.substr( 1 );
Copy link
Member

Choose a reason for hiding this comment

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

I was thinking this could be more clear:

Suggested change
.substr( 1 );
.trimStart( '|' );

But alas trimStart() doesn't take any arguments like ltrim() does in PHP.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also it seems that substr() is depracated and substring() should be used instead.

So ideally this would be something like:

Suggested change
.substr( 1 );
.substring( 1 ); // Remove a leading '|'

@westonruter to your point - I guess we can use a comment to clarify.

@westonruter
Copy link
Member

@pierlon There's a build error now:

ERROR in Entry module not found: Error: Can't resolve './assets/css/src/components' in '/home/runner/work/amp-wp/amp-wp'

pierlon and others added 2 commits April 21, 2021 13:54
Co-authored-by: Piotr Delawski <delawski@users.noreply.github.com>
@@ -349,7 +349,10 @@ const styles = {
const entries = {};
const dir = './assets/css/src';
fs.readdirSync( dir ).forEach( ( fileName ) => {
entries[ fileName.replace( /\.[^/.]+$/, '' ) ] = `${ dir }/${ fileName }`;
const fullPath = `${ dir }/${ fileName }`;
if ( ! fs.lstatSync( fullPath ).isDirectory() ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated this to ignore directories in assets/css/src, i.e. only root files in that directory will be used as entries.

@westonruter westonruter merged commit 7419cdf into develop Apr 21, 2021
@westonruter westonruter deleted the enhancement/ignore-style-assets-webpack-plugin branch April 21, 2021 19:44
@westonruter westonruter moved this from Ready for Review to Ready for QA in Ongoing Apr 21, 2021
@delawski
Copy link
Collaborator

QA passed. Found no issues in the "Network" tab in the Chrome dev tools.

@delawski delawski moved this from Ready for QA to QA Passed in Ongoing Apr 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WS:Core Work stream for Plugin core
Projects
Ongoing
  
QA Passed
Development

Successfully merging this pull request may close these issues.

None yet

3 participants