MCPcopy
hub / github.com/webpack/webpack / apply

Method apply

lib/BannerPlugin.js:84–143  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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 });

Callers

nothing calls this directly

Calls 8

tapMethod · 0.80
validateMethod · 0.80
canBeInitialMethod · 0.80
getPathMethod · 0.80
updateAssetMethod · 0.80
requireFunction · 0.50
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected