* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 350 | * @returns {void} |
| 351 | */ |
| 352 | apply(compiler) { |
| 353 | compiler.hooks.validate.tap(PLUGIN_NAME, () => { |
| 354 | compiler.validate( |
| 355 | () => { |
| 356 | const { definitions } = require("../schemas/WebpackOptions.json"); |
| 357 | |
| 358 | return { |
| 359 | definitions, |
| 360 | oneOf: [{ $ref: "#/definitions/CleanOptions" }] |
| 361 | }; |
| 362 | }, |
| 363 | this.options, |
| 364 | { |
| 365 | name: "Clean Plugin", |
| 366 | baseDataPath: "options" |
| 367 | } |
| 368 | ); |
| 369 | }); |
| 370 | |
| 371 | const { keep } = this.options; |
| 372 | |
| 373 | /** @type {boolean} */ |
| 374 | const dry = this.options.dry || false; |
| 375 | /** @type {KeepFn} */ |
| 376 | const keepFn = |
| 377 | typeof keep === "function" |
| 378 | ? keep |
| 379 | : typeof keep === "string" |
| 380 | ? (path) => path.startsWith(keep) |
| 381 | : typeof keep === "object" && keep.test |
| 382 | ? (path) => keep.test(path) |
| 383 | : () => false; |
| 384 | |
| 385 | // We assume that no external modification happens while the compiler is active |
| 386 | // So we can store the old assets and only diff to them to avoid fs access on |
| 387 | // incremental builds |
| 388 | /** @type {undefined | Assets} */ |
| 389 | let oldAssets; |
| 390 | |
| 391 | compiler.hooks.emit.tapAsync( |
| 392 | { |
| 393 | name: PLUGIN_NAME, |
| 394 | stage: 100 |
| 395 | }, |
| 396 | (compilation, callback) => { |
| 397 | const hooks = CleanPlugin.getCompilationHooks(compilation); |
| 398 | const logger = compilation.getLogger(`webpack.${PLUGIN_NAME}`); |
| 399 | const fs = /** @type {OutputFileSystem} */ (compiler.outputFileSystem); |
| 400 | |
| 401 | if (!fs.readdir) { |
| 402 | return callback( |
| 403 | new Error( |
| 404 | `${PLUGIN_NAME}: Output filesystem doesn't support listing directories (readdir)` |
| 405 | ) |
| 406 | ); |
| 407 | } |
| 408 | |
| 409 | /** @type {Assets} */ |
nothing calls this directly
no test coverage detected