-
Notifications
You must be signed in to change notification settings - Fork 381
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
Changes from 5 commits
b01eafb
4b47698
0d15bdb
6d012dd
7ab7c82
f538053
6c9e5da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Webpack plugin to prevent extraneous assets from being emitted for stylesheets. | ||
* | ||
* JS files and PHP asset files generated by `DependencyExtractionWebpackPlugin` are currently ignored. | ||
*/ | ||
class IgnoreExtraneousStyleAssets { | ||
isCssFile( file ) { | ||
return /^(?!!!).+\.(?:sc|sa|c)ss$/.test( file ); | ||
} | ||
|
||
generateIgnoreRegex( entries ) { | ||
const expression = entries | ||
.reduce( ( acc, entry ) => `${ acc }|${ entry }.js|${ entry }.asset.php`, '' ) | ||
.substr( 1 ); | ||
pierlon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return new RegExp( expression ); | ||
} | ||
|
||
apply( compiler ) { | ||
const cssEntries = []; | ||
|
||
compiler.hooks.compilation.tap( 'IgnoreExtraneousStyleAssets', ( compilation ) => { | ||
// The `addEntry` compilation hook is undocumented. As for why, ¯\_(ツ)_/¯. | ||
compilation.hooks.addEntry.tap( 'IgnoreExtraneousStyleAssets', ( entry, name ) => { | ||
if ( entry.type === 'single entry' ) { | ||
if ( this.isCssFile( entry.request ) ) { | ||
cssEntries.push( name ); | ||
} | ||
} else if ( entry.type === 'multi entry' ) { | ||
const filteredDependencies = entry.dependencies.filter( | ||
( singleDependency ) => this.isCssFile( singleDependency.request ), | ||
); | ||
|
||
// If all dependencies of the entry are CSS files, add it to the list of entries to ignore. | ||
if ( entry.dependencies.length === filteredDependencies.length ) { | ||
cssEntries.push( name ); | ||
} | ||
} | ||
} ); | ||
} ); | ||
|
||
compiler.hooks.emit.tap( 'IgnoreExtraneousStyleAssets', ( compilation ) => { | ||
if ( cssEntries.length === 0 ) { | ||
return; | ||
} | ||
|
||
const ignoreRegex = this.generateIgnoreRegex( cssEntries ); | ||
|
||
Object.keys( compilation.assets ).forEach( ( assetName ) => { | ||
if ( ignoreRegex.test( assetName ) ) { | ||
delete compilation.assets[ assetName ]; | ||
} | ||
} ); | ||
} ); | ||
} | ||
} | ||
|
||
module.exports = IgnoreExtraneousStyleAssets; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -42,13 +42,16 @@ function amp_unit_test_load_plugin_file() { | |||||||||||||||||||||||
|
||||||||||||||||||||||||
tests_add_filter( 'muplugins_loaded', 'amp_unit_test_load_plugin_file' ); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// Start up the WP testing environment. | ||||||||||||||||||||||||
require $_test_root . '/includes/bootstrap.php'; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/* | ||||||||||||||||||||||||
* Load WP CLI. Its test bootstrap file can't be required as it will load | ||||||||||||||||||||||||
* duplicate class names which are already in use. | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
define( 'WP_CLI_ROOT', TESTS_PLUGIN_DIR . '/vendor/wp-cli/wp-cli' ); | ||||||||||||||||||||||||
define( 'WP_CLI_VENDOR_DIR', TESTS_PLUGIN_DIR . '/vendor' ); | ||||||||||||||||||||||||
require_once WP_CLI_ROOT . '/php/utils.php'; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
$logger = new WP_CLI\Loggers\Regular( true ); | ||||||||||||||||||||||||
WP_CLI::set_logger( $logger ); | ||||||||||||||||||||||||
Comment on lines
+53
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHPUnit tests were failing due to a call to Line 217 in 1e7ea02
It turns out the deafult WP CLI logger is set defined when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the warning that is being emitted? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Lines 160 to 169 in 1e7ea02
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes and yes. |
||||||||||||||||||||||||
|
||||||||||||||||||||||||
// Start up the WP testing environment. | ||||||||||||||||||||||||
require $_test_root . '/includes/bootstrap.php'; |
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 was thinking this could be more clear:
But alas
trimStart()
doesn't take any arguments likeltrim()
does in PHP.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.
Also it seems that
substr()
is depracated andsubstring()
should be used instead.So ideally this would be something like:
@westonruter to your point - I guess we can use a comment to clarify.