* Validates the configured options and injects rendered banner comments into * matching compilation assets at the configured process-assets stage. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 82 | * @returns {void} |
| 83 | */ |
| 84 | apply(compiler) { |
| 85 | compiler.hooks.validate.tap(PLUGIN_NAME, () => { |
| 86 | compiler.validate( |
| 87 | () => require("../schemas/plugins/BannerPlugin.json"), |
| 88 | this.options, |
| 89 | { |
| 90 | name: "Banner Plugin", |
| 91 | baseDataPath: "options" |
| 92 | }, |
| 93 | (options) => require("../schemas/plugins/BannerPlugin.check")(options) |
| 94 | ); |
| 95 | }); |
| 96 | const options = this.options; |
| 97 | const banner = this.banner; |
| 98 | const matchObject = ModuleFilenameHelpers.matchObject.bind( |
| 99 | undefined, |
| 100 | options |
| 101 | ); |
| 102 | /** @type {WeakMap<Source, { source: ConcatSource, comment: string }>} */ |
| 103 | const cache = new WeakMap(); |
| 104 | const stage = |
| 105 | this.options.stage || Compilation.PROCESS_ASSETS_STAGE_ADDITIONS; |
| 106 | |
| 107 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { |
| 108 | compilation.hooks.processAssets.tap({ name: PLUGIN_NAME, stage }, () => { |
| 109 | for (const chunk of compilation.chunks) { |
| 110 | if (options.entryOnly && !chunk.canBeInitial()) { |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | for (const file of chunk.files) { |
| 115 | if (!matchObject(file)) { |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | /** @type {PathDataChunk} */ |
| 120 | const data = { chunk, filename: file }; |
| 121 | |
| 122 | const comment = compilation.getPath( |
| 123 | /** @type {string | import("./TemplatedPathPlugin").TemplatePathFn<PathDataChunk>} */ |
| 124 | (banner), |
| 125 | data |
| 126 | ); |
| 127 | |
| 128 | compilation.updateAsset(file, (old) => { |
| 129 | const cached = cache.get(old); |
| 130 | if (!cached || cached.comment !== comment) { |
| 131 | const source = options.footer |
| 132 | ? new ConcatSource(old, "\n", comment) |
| 133 | : new ConcatSource(comment, "\n", old); |
| 134 | cache.set(old, { source, comment }); |
| 135 | return source; |
| 136 | } |
| 137 | return cached.source; |
| 138 | }); |
| 139 | } |
| 140 | } |
| 141 | }); |
nothing calls this directly
no test coverage detected