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

Method apply

lib/DotenvPlugin.js:230–287  ·  lib/DotenvPlugin.js::DotenvPlugin.apply

* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}

(compiler)

Source from the content-addressed store, hash-verified

228 * @returns {void}
229 */
230 apply(compiler) {
231 compiler.hooks.validate.tap(PLUGIN_NAME, () => {
232 compiler.validate(
233 () => {
234 const { definitions } = require(class="st">"../schemas/WebpackOptions.json");
235
236 return {
237 definitions,
238 oneOf: [{ $ref: class="st">"#/definitions/DotenvPluginOptions" }]
239 };
240 },
241 this.options,
242 {
243 name: class="st">"Dotenv Plugin",
244 baseDataPath: class="st">"options"
245 }
246 );
247 });
248 const definePlugin = new compiler.webpack.DefinePlugin({});
249 const prefixes = Array.isArray(this.options.prefix)
250 ? this.options.prefix
251 : [this.options.prefix || class="st">"WEBPACK_"];
252 /** @type {string | false} */
253 const dir =
254 typeof this.options.dir === class="st">"string"
255 ? this.options.dir
256 : typeof this.options.dir === class="st">"undefined"
257 ? compiler.context
258 : this.options.dir;
259
260 /** @type {undefined | Snapshot} */
261 let snapshot;
262
263 const cache = compiler.getCache(PLUGIN_NAME);
264 const identifier = JSON.stringify(
265 this.options.template || DEFAULT_TEMPLATE
266 );
267 const itemCache = cache.getItemCache(identifier, null);
268
269 compiler.hooks.beforeCompile.tapPromise(PLUGIN_NAME, async () => {
270 const { parsed, snapshot: newSnapshot } = dir
271 ? await this._loadEnv(compiler, itemCache, dir)
272 : { parsed: {} };
273 const env = this._getEnv(prefixes, parsed);
274
275 definePlugin.definitions = envToDefinitions(env || {});
276 snapshot = newSnapshot;
277 });
278
279 compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
280 if (snapshot) {
281 compilation.fileDependencies.addAll(snapshot.getFileIterable());
282 compilation.missingDependencies.addAll(snapshot.getMissingIterable());
283 }
284 });
285
286 definePlugin.apply(compiler);
287 }

Callers

nothing calls this directly

Calls 13

_loadEnvMethod · 0.95
_getEnvMethod · 0.95
applyMethod · 0.95
envToDefinitionsFunction · 0.85
tapMethod · 0.80
validateMethod · 0.80
isArrayMethod · 0.80
getItemCacheMethod · 0.80
getFileIterableMethod · 0.80
getMissingIterableMethod · 0.80
requireFunction · 0.50
getCacheMethod · 0.45

Tested by

no test coverage detected