I'm in the middle of upgrading my personal plugins to Webpack5, and while things are going smooth; I've been trying to wrap my head around how to properly change the source of a asset mid build.
I'm aware of Webpack5's changes to asset hooks, and needing to be in the processAssets hook / appropriate stage to do changes.
compilation.hooks.processAssets.tap(
{
name: 'BobRoss',
stage: compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
additionalAssets: true
},
(assets) => {
// ... logic here ...
}
);
What I don't understand however is the actual asset's source prop, function or cached string?
For instance, to alter the source of a un-minified JS asset, I only find success with:
assets[i] = {
source: () => 'the change';
};
However if that asset is minified, I only find success with:
assets[i]._chachedSource = 'the change';
which is kind of weird, but I guess we can work with that..
But then in walks HTML assets, which regardless of being minified or not, only works if I:
assets[i]._value = 'the change';
So, I'm increasingly feeling like I'm missing some obvious API method used to change an asset's source consistently and I'm adding points of failure not solutions. Can anyone point me in the right direction or give me an idea of better approach?
Thank you!
processAssets.tap()
never gets called. Not sure if I'm doing something fundamentally wrong. Any ideas? – Boardman