-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
Guidance on porting a plugin to webpack v5? #11425
Comments
Yes, we need update this, {
apply(compiler) {
if (isWebpack4()) {
// Keep compatibility
compiler.emit('plugin-name', () => {})
} else {
// webpacks export `webpack-sources` to avoid cache problems
const { sources, Compilation } = require('webpack');
compiler.hooks.compilation.tap('plugin-name', (compilation) => {
compilation.hooks.processAssets.tapPromise(
{
name: 'plugin-name',
// https://github.com/webpack/webpack/blob/master/lib/Compilation.js#L3280
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
},
() => {
compilation.emitAsset('test.js', new sources.RawSource(data))
}
);
});
}
}
} If you need I can provide example for using webpack cache (persistent cache) |
For supporting webpack@4 and webpack@5 sources you can use this code: const webpack = require('webpack');
// webpack 5 exposes the sources property to ensure the right version of webpack-sources is used
const { RawSource } =
// eslint-disable-next-line global-require
webpack.sources || require('webpack-sources'); |
Thanks! I think it's working, at least at its most basic, but I will likely have some more questions 😄 First up, what's your recommended way of implementing |
You can use |
I'd prefer to use duck typing, but I don't have a reference to the current I guess I'll go with |
|
I'm working my way through our test suite, and running into an issue with https://github.com/GoogleChrome/workbox/blob/bb21e8208ec093dd66fb61b5fbcdfd67314eb2bc/test/workbox-webpack-plugin/node/generate-sw.js#L437-L487, which uses After updating to
I can see that the code in (I'm not sure if this code was run twice by webpack v4 as well, and it just couldn't detect creating multiple assets with the same name.) My goal is to ensure that my |
@jeffposnick Looks you adding assets, you need to use compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
compilation.hooks.processAssets.tapPromise(
{ name: pluginName, stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL},
() => {
// Code
}
)
}) But in same rare cases if you need to adding something for the child compilation you need to use |
This might be problematic once we release |
I tried tapping into As mentioned above, this is with |
/cc @jantimon Why @jeffposnick I think you need using this Maybe you can create simple example or provide link on the problem, I will investigate this |
I am generating an asset manifest for a service worker, so that stage does seem relevant 😄 Lines 3376 to 3381 in 9230acb
However, even when I set the stage to The latest copy of my work-in-progress code is at https://github.com/GoogleChrome/workbox/blob/webpack-v5/packages/workbox-webpack-plugin/src/generate-sw.js (i.e. in the Running it might be a bit annoying; try this after checking it out and switching to the right branch:
That should run just the failing test of the |
You can use |
I am in Italy right now but I am planning to upgrade to setAssets soon - what is needed to make it easier for you? The ugly name is only generated from the child generator and will be removed again - it's only used internally |
When using webpack v4, we rely on the code in Under the hood, the code calls:
to get both the full set of assets as well as figure out each asset's associated Will Is there any way to get that information about all assets, whether they were created in a parent or a child compilation, in a single call (as was possible in webpack v4), or do I have to iterate over the child compilations? It sounds like @jantimon is still in the progress of finalizing their work on |
Hm, we can do it, we can add assets |
No, assets are more than chunks, because developers can add assets using |
In webpack 5 you can still do |
For my use case, I need access to all of the assets that were created by a given invocation of
When I add One additional breaking change that I've noticed with the
code when run in webpack v5 gives assets in the Is there a reason why assets obtained from I've updated the code in the
|
I wonder why? That should only happen when there is not enough space for the assets to display. So only when |
I wonder why you want to use the Stats anyway. Usually one doesn't use them before the compilation finished. You can access all info from the Compilation directly:
Note that while webpack puts all assets somewhere in the graph, not all plugins do so. e. g. when using the copy-webpack-plugin there are some assets in the assets list which are only reachable from the chunk graph. More plugins do that. You probably also don't put the service-worker asset into the chunk graph. |
All of those Around the time of the webpack v4.0.0 release, when I was working on the changes needed to move from webpack v3.x to webpack v4.0.0, using I really don't want to have support two fundamentally different codepaths for obtaining and working with a list of assets within the same plugin, based on whether the user is running it in webpack v4 (prior to v4.40.0) or webpack v5. It sounds like the recommended course of action for the next |
For me this sounds like a good approach. Upgrading from 4.0 -> 4.40 should be straightforward for the user and I guess they already did so anyway. Anyway it wouldn't be a huge breaking change. Probably better than supporting two code paths, even while this isn't too bad. Many official plugins also support webpack 5 with two code paths... One could delete the old path in the next major version and there is no risk adding webpack 5 support when the old code path stays mostly untouched. Anyway you choice. |
Just a quick update: things appear to be mostly working based on our test suite. Thanks for everyone's help! We have an alpha of the next major Workbox release coming up in the near future, and I think we'll be good for getting this code out there and encourage developers to test with Two open questions that I am left with:
|
With Or you could calculate the publicPath yourself in the Service Worker from Or you can require that the user have to set the |
Thanks for the additional context around I don't think passing through The |
I'm going to close this issue since I think all of my open questions have been resolved. Thanks for everyone's help. I'll open follow-up issues with specific questions if anything unexpected comes up when users start testing with |
Fixes microsoft#27 Changes based on dialogue in webpack/webpack#11425 between webpack and workbook-webpack-plugin devs. Split tap and asset logic in `LicenseCheckerWebpackPlugin.apply()` by version of webpack. Use PROCESS_ASSETS_STAGE_ADDITIONAL for webpack 5. Update peerDependencies to `^4.4.0 || ^5.4.0`.
Fixes microsoft#27 Changes based on dialogue in webpack/webpack#11425 between webpack and workbook-webpack-plugin devs. Split tap and asset logic in `LicenseCheckerWebpackPlugin.apply()` by version of webpack. Use PROCESS_ASSETS_STAGE_ADDITIONAL for webpack 5. Keep existing logic for webpack 4 largely unchanged. Update peerDependencies to `^4.4.0 || ^5.4.0`.
Fixes #27 Changes based on dialogue in webpack/webpack#11425 between webpack and workbook-webpack-plugin devs. Split tap and asset logic in `LicenseCheckerWebpackPlugin.apply()` by version of webpack. Use PROCESS_ASSETS_STAGE_ADDITIONAL for webpack 5. Keep existing logic for webpack 4 largely unchanged. Update peerDependencies to `^4.4.0 || ^5.4.0`.
- Free-form extract `AdditionalAssetsPlugin` from static-site-generator-webpack-plugin, abstracting and modernizing the code as we go - Let client code specify the desired additional assets as a callback (rather than having knowledge about the URL mapping ahead of time) - Bestow “second-stage rocket” capabilities to the plugin, i.e. make it so that said client code can call a method-ish that retrieves already webpack'd JS - Use Webpack API to emit the things that the aforementioned client code returns, as figured out from webpack/webpack#11425 - In order to avoid Webpack whining about `DeprecationWarning: Compilation.assets will be frozen in future`, use an [`additionalPass` hook](https://webpack.js.org/api/compiler-hooks/#additionalpass), rather than an emit hook - Document my journey
I maintain
workbox-webpack-plugin
, and am trying to get a handle on the webpack v5 support story.The plugin has code that generates one or more new files and then adds them to the
compilation.assets
once it's done: https://github.com/GoogleChrome/workbox/blob/6d38919ebbc9664327e19ff00302d805c8166170/packages/workbox-webpack-plugin/src/generate-sw.js#L282-L285This work is done by tapping into
compiler.hooks.emit
.When I tried the existing plugin code against
webpack@next
, I encountered:I'm at a loss for how to handle this change, as I can't find anything in https://github.com/webpack/changelog-v5/blob/master/MIGRATION%20GUIDE.md or https://github.com/webpack/changelog-v5 that explains what the equivalent code in v5 would be, or what
PROCESS_ASSETS_STAGE_*
refers to.Is there an upgrade guide for plugin authors?
The text was updated successfully, but these errors were encountered: