From c70433514769750b2917742a7aec26b9cbc200fb Mon Sep 17 00:00:00 2001 From: Joash Mathew Date: Tue, 25 Jul 2023 16:58:38 -0400 Subject: [PATCH 1/2] feat: add support for processAssets optimizeChunkAssets has a deprecation notice for it, add support for processAssets to avoid viewing warnings everytime. This commit keeps compatibility for using optimizeChunkAssets if the end-user is using a version of webpack which doesn't have processAssets. --- wrapper-webpack-plugin.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/wrapper-webpack-plugin.js b/wrapper-webpack-plugin.js index d6036cf..969667a 100644 --- a/wrapper-webpack-plugin.js +++ b/wrapper-webpack-plugin.js @@ -38,10 +38,20 @@ class WrapperPlugin { wrapChunks(compilation, chunks, footer, header); }); } else { - compilation.hooks.optimizeChunkAssets.tapAsync('WrapperPlugin', (chunks, done) => { - wrapChunks(compilation, chunks, footer, header); - done(); - }); + // Keep support for optimizeChunkAssets on older Webpack versions + if (compilation.hooks.processAssets) { + compilation.hooks.processAssets.tapAsync({ + name: 'WrapperPlugin', + stage: 'compilation.PROCESS_ASSETS_STAGE_ADDITIONS' + }, (chunks) => { + processAssets(compilation, chunks); + }); + } else { + compilation.hooks.optimizeChunkAssets.tapAsync('WrapperPlugin', (chunks, done) => { + wrapChunks(compilation, chunks, footer, header); + done(); + }); + } } }); @@ -69,6 +79,20 @@ class WrapperPlugin { } } } // wrapChunks + + function processAssets(compilation, assets) { + for (const asset in assets) { + // @note There doesn't seem to be a way to get the hash from Source + const headerContent = (typeof header === 'function') ? header(fileName) : header; + const footerContent = (typeof footer === 'function') ? footer(fileName) : footer; + + compilation.assets[asset] = new ConcatSource( + String(headerContent), + compilation.assets[asset], + String(footerContent) + ); + } + } } } From 191a7a93958e91177d5940434c1f269f385200f5 Mon Sep 17 00:00:00 2001 From: Joash Mathew Date: Thu, 27 Jul 2023 10:01:17 -0400 Subject: [PATCH 2/2] feat: add tester functionality --- wrapper-webpack-plugin.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/wrapper-webpack-plugin.js b/wrapper-webpack-plugin.js index 969667a..c0be41b 100644 --- a/wrapper-webpack-plugin.js +++ b/wrapper-webpack-plugin.js @@ -82,15 +82,21 @@ class WrapperPlugin { function processAssets(compilation, assets) { for (const asset in assets) { - // @note There doesn't seem to be a way to get the hash from Source + /** + * @note There doesn't seem to be a way to get the hash from Source, so + * not using wrapFile function. + */ const headerContent = (typeof header === 'function') ? header(fileName) : header; const footerContent = (typeof footer === 'function') ? footer(fileName) : footer; - compilation.assets[asset] = new ConcatSource( - String(headerContent), - compilation.assets[asset], - String(footerContent) - ); + if (ModuleFilenameHelpers.matchObject(tester, asset)) + { + compilation.assets[asset] = new ConcatSource( + String(headerContent), + compilation.assets[asset], + String(footerContent) + ); + } } } }