-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Dependency Extraction Webpack Plugin: Make the plugin work when using optimizations.runtimeChunk = 'single'
#26214
Dependency Extraction Webpack Plugin: Make the plugin work when using optimizations.runtimeChunk = 'single'
#26214
Conversation
Thanks for this @stefanfisk 👍 What do you think of adding a test that fails like in your example repository? You can add a directory here with a minimal example project: https://github.com/WordPress/gutenberg/tree/master/packages/dependency-extraction-webpack-plugin/test/fixtures |
3b3d69a
to
ce1e442
Compare
@sirreal I've added a test, but it is not failing since the PR already contained a fix. I also added a small change to which chunk's hash is used in the asset file. |
Oh, and I changed the snapshot format to contain the asset filenames, as that was part of the issue. |
ce1e442
to
d5b5042
Compare
I just cleaned the code up, and added use of the Webpack 5 I also added an option for specifying the asset output filename separately, so that one can use hashes in the js output filenames and still have predictable asset filenames. |
@stefanfisk, many thanks for opening this PR. It seems like a complex issue to address so I appreciate all the time invested on it since it works for you already 😃 I have one general question to better understand the context. What’s the purpose of |
This for example allows you to use an entrypoint for admin that is always loaded for admin pages, a block-editor entrypoint that is only loaded by the block editor, all while they share instances of stateful modules. |
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.
@slightlyfaulty left a comment on the #24352 confirming that this PR fixes the issue. I went through the proposed changes and they look good. All the newly added unit tests bring a lot of value. I think we can proceed with merging those changes as soon as the PR gets rebased with trunk
and all conflicts are resolved. @stefanfisk, thank you for keeping this PR opened for so long.
let assetFilename; | ||
|
||
if ( outputFilename ) { | ||
assetFilename = compilation.getPath( outputFilename, { |
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 enforce a matching outputFormat
based on the extension used in the file? At least when it ends with .php
or .json
. We could also leave it as is - the developer using the plugin still would have to pass two options to set everything correctly when using non-default output format.
expect( | ||
fs.readFileSync( assetFile, 'utf-8' ) | ||
).toMatchSnapshot( 'Asset file should match snapshot' ); | ||
).toMatchSnapshot( | ||
`Asset file '${ assetBasename }' should match snapshot` |
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.
Looks like a good idea in general 👍🏻
@@ -116,6 +116,13 @@ module.exports = { | |||
|
|||
The output format for the generated asset file. There are two options available: 'php' or 'json'. | |||
|
|||
##### `outputFilename` |
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.
With this new option proposed combinedOutputFile
plays a nearly identical role. At some point, we might want to deprecate combinedOutputFile
and consolidate the handling in outputFilename
.
optimizations.runtimeChunk = 'single'
This results in the asset files being named after the entry points, instead of the runtime chunk when using `runtimeChunk = 'single'`. Fixes WordPress#24352
Allows specifying the asset filename format separately from the output filename format. This is useful when the output filenames include a hash.
d5b5042
to
a6f01f2
Compare
@stefanfisk, thas was quick! CI checks are still green which means we are good to go 🎉 |
Congratulations on your first merged pull request, @stefanfisk! We'd like to credit you for your contribution in the post announcing the next WordPress release, but we can't find a WordPress.org profile associated with your GitHub account. When you have a moment, visit the following URL and click "link your GitHub account" under "GitHub Username" to link your accounts: https://profiles.wordpress.org/me/profile/edit/ And if you don't have a WordPress.org account, you can create one on this page: https://login.wordpress.org/register Kudos! |
@gziolo maintaining a local copy of the plugin in my site repos has been a thorn in my side for over a year now (npm keeps installing webpack 4 and 5 at the same time when I least expect it). At this point I am not missing any chance of getting this merged 😸 |
I'm not clear on the release schedule for the packages, do you have any idea of when this PR will be available? |
It's hard to tell because the WordPress 5.9 release was moved to January 25th: https://make.wordpress.org/core/2021/11/22/wordpress-5-9-revised-release-schedule/. We are now in feature freeze mode and the @noisysocks (the editor tech lead for WordPress 5.9 release) manually cherry-picks only bug fixes to the branch that is used for npm releases. It all depends on when he decides to create a |
Thanks for the work on this @stefanfisk and especially thank you for your patience and perseverance ❤️ |
I'll stick to the documented process which is to branch |
Hi @stefanfisk 👋 I noticed this PR now, long after it was merged, when working on something else in the dependency extraction plugin, and I wanted to ask: how does a practical project that runs into this issue look like? Suppose there is a project that produces two entrypoint scripts, So, if it's not WordPress enqueuing the scripts in a standard way, who else cares about the A similar issue will arise if there are two entrypoints that have any shared chunk in common, not just runtime. To load the entrypoint, the consumer is supposed to load 2+ JS files, but any |
@jsnajdr, the interesting part is that I was exploring using |
@jsnajdr I already had my own script loader that parsed a JSON manifest generated by The reason for using the custom script loader was that I was splitting the npm deps from the main bundle to minimize the impact of doing deploys that modified the site's JS code (which I was doing almost daily at the time). |
@stefanfisk checking back to see if you can confirm your WordPress.org username so we can ensure you are properly credited in the WordPress 6.0 release coming out on May 24th. You can use the links above or share information directly with me and I'll ensure it's included correctly within the release. And thanks again for your contribution, it's greatly appreciated! |
@jeffpaul done! |
Thanks @stefanfisk, that helps me a lot. In other words, you were looking at the |
@jsnajdr I'm no longer using this code, so I'm not sure if it works, but here's a gist describing exactly how it all worked: https://gist.github.com/stefanfisk/bb4e045f3d15829a377916dc5fee9acc Feel free to use it as you wish. |
Thanks, that gist shows very well the shortcomings of the current $assets = require( "$dir/assets.php" );
foreach ( $assets['scripts'] as $script ) {
wp_enqueue_script( $script['handle'], $script['src'], $script['deps'], $script['ver'] );
}
foreach ( $assets['styles'] as $style ) {
wp_enqueue_style( $style['handle'], $style['src'], $style['deps'], $style['ver'] );
} |
That's a simplified version because you don't want always to enqueue all those assets, but I agree on a principle that it would be great to have a way to register ( |
Not sure what you mean, because in general I need to enqueue all the scripts that webpack produced? There are some additional assets like sourcemaps or |
It might be correct for your use case. In general, when you have multiple entry points you might want to consume only a subset of them on different pages. That's why I suggested that you register all assets but enqueue only the entry points when necessary and WP would pick all dependencies out of the box. |
A basic example is splitting editor assets from frontend assets.
…On Wed, May 11, 2022 at 4:09 PM Greg Ziółkowski ***@***.***> wrote:
That's a simplified version because you don't want always to enqueue all
those assets
Not sure what you mean, because in general I need to enqueue all the
scripts that webpack produced? There are some additional assets like
sourcemaps or .LICENCE.txt files, but if something is a script or style,
it needs to be there.
It might be correct for your use case. In general, when you have multiple
entry points you might want to consume only a subset of them on different
pages. That's why I suggested that you register all assets but enqueue only
the entry points when necessary and WP would pick all dependencies out of
the box.
—
Reply to this email directly, view it on GitHub
<#26214 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGKC7I2Y6TJJILQBWND5BTVJO5RFANCNFSM4STVNTBQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
I see, there can be a big project with multiple entrypoints and it always registers all its assets, but when requesting "please enqueue stuff for entrypoint |
I created #41002 where I'm trying to solve some issues discussed here. |
Description
A quick proof of concept for fixing #24352, as requested by @sirreal.
How has this been tested?
I'm currently using this patch daily, and it works for me. Beyond that I don't know.
Types of changes
The asset filename logic has been modified, and the assets are now pushed to the entrypoint chunks instead of the runtime chunk.
Checklist: