From a60b29484747aadba5320ae1a6838c2dd2c7399d Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Fri, 15 Mar 2024 20:55:59 +0300 Subject: [PATCH 1/2] feat: allow to customize the stage of BannerPlugin --- declarations/plugins/BannerPlugin.d.ts | 4 ++++ lib/BannerPlugin.js | 4 +++- lib/ExternalModuleFactoryPlugin.js | 4 ---- schemas/plugins/BannerPlugin.check.js | 2 +- schemas/plugins/BannerPlugin.json | 4 ++++ types.d.ts | 5 +++++ 6 files changed, 17 insertions(+), 6 deletions(-) diff --git a/declarations/plugins/BannerPlugin.d.ts b/declarations/plugins/BannerPlugin.d.ts index 53165456f..a2138db6b 100644 --- a/declarations/plugins/BannerPlugin.d.ts +++ b/declarations/plugins/BannerPlugin.d.ts @@ -50,6 +50,10 @@ export interface BannerPluginOptions { * If true, banner will not be wrapped in a comment. */ raw?: boolean; + /** + * Specifies the banner. + */ + stage?: number; /** * Include all modules that pass test assertion. */ diff --git a/lib/BannerPlugin.js b/lib/BannerPlugin.js index cf7c0530a..7b2704941 100644 --- a/lib/BannerPlugin.js +++ b/lib/BannerPlugin.js @@ -83,12 +83,14 @@ class BannerPlugin { options ); const cache = new WeakMap(); + const stage = + this.options.stage || Compilation.PROCESS_ASSETS_STAGE_ADDITIONS; compiler.hooks.compilation.tap("BannerPlugin", compilation => { compilation.hooks.processAssets.tap( { name: "BannerPlugin", - stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS + stage }, () => { for (const chunk of compilation.chunks) { diff --git a/lib/ExternalModuleFactoryPlugin.js b/lib/ExternalModuleFactoryPlugin.js index 649382f6d..6d11de5aa 100644 --- a/lib/ExternalModuleFactoryPlugin.js +++ b/lib/ExternalModuleFactoryPlugin.js @@ -71,10 +71,6 @@ class ExternalModuleFactoryPlugin { const dependency = data.dependencies[0]; const dependencyType = data.dependencyType; - console.log(dependency.request); - console.log(dependency.constructor); - console.log(dependency.assertions); - /** * @param {string|string[]|boolean|Record} value the external config * @param {string|undefined} type type of external diff --git a/schemas/plugins/BannerPlugin.check.js b/schemas/plugins/BannerPlugin.check.js index aa7cd0e95..69e04d21c 100644 --- a/schemas/plugins/BannerPlugin.check.js +++ b/schemas/plugins/BannerPlugin.check.js @@ -3,4 +3,4 @@ * DO NOT MODIFY BY HAND. * Run `yarn special-lint-fix` to update */ -"use strict";function n(t,{instancePath:l="",parentData:e,parentDataProperty:s,rootData:a=t}={}){let r=null,o=0;const u=o;let i=!1;const p=o;if(o===p)if(Array.isArray(t)){const n=t.length;for(let l=0;l Date: Fri, 15 Mar 2024 21:06:50 +0300 Subject: [PATCH 2/2] test: added for the stage option of BannerPlugin --- test/BannerPlugin.test.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/BannerPlugin.test.js b/test/BannerPlugin.test.js index 5024e61fc..69798d28e 100644 --- a/test/BannerPlugin.test.js +++ b/test/BannerPlugin.test.js @@ -79,3 +79,38 @@ it("can place banner as footer", done => { done(); }); }); + +it("should allow to change stage", done => { + const entryFile = path.join(pluginDir, "entry3.js"); + const outputFile = path.join(outputDir, "entry3.js"); + try { + fs.mkdirSync(path.join(pluginDir), { + recursive: true + }); + } catch (e) { + // empty + } + const compiler = webpack({ + mode: "production", + entry: { + entry3: entryFile + }, + output: { + path: outputDir + }, + plugins: [ + new webpack.BannerPlugin({ + raw: true, + banner: "/* banner is a string */", + stage: webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT + }) + ] + }); + fs.writeFileSync(entryFile, "console.log(1 + 1);", "utf-8"); + compiler.run(err => { + if (err) return done(err); + const fileResult = fs.readFileSync(outputFile, "utf8").split("\n"); + expect(fileResult[0]).toBe("/* banner is a string */"); + done(); + }); +});