* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 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 | } |
nothing calls this directly
no test coverage detected