async optimize(compiler, compilation, assets, optimizeOptions) {
    const cache = compilation.getCache("TerserWebpackPlugin");
    let numberOfAssets = 0;
    const assetsForMinify = await Promise.all(
      Object.keys(assets)
        .filter((name) => {
          const { info } = /** @type {Asset} */ (compilation.getAsset(name));

          if (
            // Skip double minimize assets from child compilation
            info.minimized ||
            // Skip minimizing for extracted comments assets
            info.extractedComments
          ) {
            return false;
          }

          if (
            !compiler.webpack.ModuleFilenameHelpers.matchObject.bind(
              // eslint-disable-next-line no-undefined
              undefined,
              this.options
            )(name)
          ) {
            return false;
          }

          return true;
        })
        .map(async (name) => {
          const { info, source } = /** @type {Asset} */ (
            compilation.getAsset(name)
          );

          const eTag = cache.getLazyHashedEtag(source);
          const cacheItem = cache.getItemCache(name, eTag);
          const output = await cacheItem.getPromise();

          if (!output) {
            numberOfAssets += 1;
          }

          return { name, info, inputSource: source, output, cacheItem };
        })
    );

    if (assetsForMinify.length === 0) {
      return;
    }

    /** @type {undefined | (() => MinimizerWorker<T>)} */
    let getWorker;
    /** @type {undefined | MinimizerWorker<T>} */
    let initializedWorker;
    /** @type {undefined | number} */
    let numberOfWorkers;

    if (optimizeOptions.availableNumberOfCores > 0) {
      // Do not create unnecessary workers when the number of files is less than the available cores, it saves memory
      numberOfWorkers = Math.min(
        numberOfAssets,
        optimizeOptions.availableNumberOfCores
      );
      // eslint-disable-next-line consistent-return
      getWorker = () => {
        if (initializedWorker) {
          return initializedWorker;
        }

        // eslint-disable-next-line global-require
        const { Worker } = require("jest-worker");

        initializedWorker =
          /** @type {MinimizerWorker<T>} */
          (
            new Worker(require.resolve("./minify"), {
              numWorkers: numberOfWorkers,
              enableWorkerThreads:
                typeof this.options.minimizer.implementation
                  .supportsWorkerThreads !== "undefined"
                  ? this.options.minimizer.implementation.supportsWorkerThreads() !==
                    false
                  : true,
            })
          );

        // https://github.com/facebook/jest/issues/8872#issuecomment-524822081
        const workerStdout = initializedWorker.getStdout();

        if (workerStdout) {
          workerStdout.on("data", (chunk) => process.stdout.write(chunk));
        }

        const workerStderr = initializedWorker.getStderr();

        if (workerStderr) {
          workerStderr.on("data", (chunk) => process.stderr.write(chunk));
        }

        return initializedWorker;
      };
    }

    const { SourceMapSource, ConcatSource, RawSource } =
      compiler.webpack.sources;

    /** @typedef {{ extractedCommentsSource : import("webpack").sources.RawSource, commentsFilename: string }} ExtractedCommentsInfo */
    /** @type {Map<string, ExtractedCommentsInfo>} */
    const allExtractedComments = new Map();
    const scheduledTasks = [];

    for (const asset of assetsForMinify) {
      scheduledTasks.push(async () => {
        const { name, inputSource, info, cacheItem } = asset;
        let { output } = asset;

        if (!output) {
          let input;
          /** @type {SourceMapInput | undefined} */
          let inputSourceMap;

          const { source: sourceFromInputSource, map } =
            inputSource.sourceAndMap();

          input = sourceFromInputSource;

          if (map) {
            if (!TerserPlugin.isSourceMap(map)) {
              compilation.warnings.push(
                /** @type {WebpackError} */
                (new Error(`${name} contains invalid source map`))
              );
            } else {
              inputSourceMap = /** @type {SourceMapInput} */ (map);
            }
          }

          if (Buffer.isBuffer(input)) {
            input = input.toString();
          }

          /**
           * @type {InternalOptions<T>}
           */
          const options = {
            name,
            input,
            inputSourceMap,
            minimizer: {
              implementation: this.options.minimizer.implementation,
              // @ts-ignore https://github.com/Microsoft/TypeScript/issues/10727
              options: { ...this.options.minimizer.options },
            },
            extractComments: this.options.extractComments,
          };

          if (typeof options.minimizer.options.module === "undefined") {
            if (typeof info.javascriptModule !== "undefined") {
              options.minimizer.options.module =
                /** @type {PredefinedOptions<T>["module"]} */
                (info.javascriptModule);
            } else if (/\.mjs(\?.*)?$/i.test(name)) {
              options.minimizer.options.module =
                /** @type {PredefinedOptions<T>["module"]} */ (true);
            } else if (/\.cjs(\?.*)?$/i.test(name)) {
              options.minimizer.options.module =
                /** @type {PredefinedOptions<T>["module"]} */ (false);
            }
          }

          if (typeof options.minimizer.options.ecma === "undefined") {
            options.minimizer.options.ecma =
              /** @type {PredefinedOptions<T>["ecma"]} */
              (
                TerserPlugin.getEcmaVersion(
                  compiler.options.output.environment || {}
                )
              );
          }

          try {
            output = await (getWorker
              ? getWorker().transform(getSerializeJavascript()(options))
              : minify(options));
          } catch (error) {
            const hasSourceMap =
              inputSourceMap && TerserPlugin.isSourceMap(inputSourceMap);

            compilation.errors.push(
              /** @type {WebpackError} */
              (
                TerserPlugin.buildError(
                  error,
                  name,
                  hasSourceMap
                    ? new (getTraceMapping().TraceMap)(
                        /** @type {SourceMapInput} */ (inputSourceMap)
                      )
                    : // eslint-disable-next-line no-undefined
                      undefined,
                  // eslint-disable-next-line no-undefined
                  hasSourceMap ? compilation.requestShortener : undefined
                )
              )
            );

            return;
          }

          if (typeof output.code === "undefined") {
            compilation.errors.push(
              /** @type {WebpackError} */
              (
                new Error(
                  `${name} from Terser plugin\nMinimizer doesn't return result`
                )
              )
            );

            return;
          }

          if (output.warnings && output.warnings.length > 0) {
            output.warnings = output.warnings.map(
              /**
               * @param {Error | string} item
               */
              (item) => TerserPlugin.buildWarning(item, name)
            );
          }

          if (output.errors && output.errors.length > 0) {
            const hasSourceMap =
              inputSourceMap && TerserPlugin.isSourceMap(inputSourceMap);

            output.errors = output.errors.map(
              /**
               * @param {Error | string} item
               */
              (item) =>
                TerserPlugin.buildError(
                  item,
                  name,
                  hasSourceMap
                    ? new (getTraceMapping().TraceMap)(
                        /** @type {SourceMapInput} */ (inputSourceMap)
                      )
                    : // eslint-disable-next-line no-undefined
                      undefined,
                  // eslint-disable-next-line no-undefined
                  hasSourceMap ? compilation.requestShortener : undefined
                )
            );
          }

          let shebang;

          if (
            /** @type {ExtractCommentsObject} */
            (this.options.extractComments).banner !== false &&
            output.extractedComments &&
            output.extractedComments.length > 0 &&
            output.code.startsWith("#!")
          ) {
            const firstNewlinePosition = output.code.indexOf("\n");

            shebang = output.code.substring(0, firstNewlinePosition);
            output.code = output.code.substring(firstNewlinePosition + 1);
          }

          if (output.map) {
            output.source = new SourceMapSource(
              output.code,
              name,
              output.map,
              input,
              /** @type {SourceMapInput} */ (inputSourceMap),
              true
            );
          } else {
            output.source = new RawSource(output.code);
          }

          if (output.extractedComments && output.extractedComments.length > 0) {
            const commentsFilename =
              /** @type {ExtractCommentsObject} */
              (this.options.extractComments).filename ||
              "[file].LICENSE.txt[query]";

            let query = "";
            let filename = name;

            const querySplit = filename.indexOf("?");

            if (querySplit >= 0) {
              query = filename.slice(querySplit);
              filename = filename.slice(0, querySplit);
            }

            const lastSlashIndex = filename.lastIndexOf("/");
            const basename =
              lastSlashIndex === -1
                ? filename
                : filename.slice(lastSlashIndex + 1);
            const data = { filename, basename, query };

            output.commentsFilename = compilation.getPath(
              commentsFilename,
              data
            );

            let banner;

            // Add a banner to the original file
            if (
              /** @type {ExtractCommentsObject} */
              (this.options.extractComments).banner !== false
            ) {
              banner =
                /** @type {ExtractCommentsObject} */
                (this.options.extractComments).banner ||
                `For license information please see ${path
                  .relative(path.dirname(name), output.commentsFilename)
                  .replace(/\\/g, "/")}`;

              if (typeof banner === "function") {
                banner = banner(output.commentsFilename);
              }

              if (banner) {
                output.source = new ConcatSource(
                  shebang ? `${shebang}\n` : "",
                  `/*! ${banner} */\n`,
                  output.source
                );
              }
            }

            const extractedCommentsString = output.extractedComments
              .sort()
              .join("\n\n");

            output.extractedCommentsSource = new RawSource(
              `${extractedCommentsString}\n`
            );
          }

          await cacheItem.storePromise({
            source: output.source,
            errors: output.errors,
            warnings: output.warnings,
            commentsFilename: output.commentsFilename,
            extractedCommentsSource: output.extractedCommentsSource,
          });
        }

        if (output.warnings && output.warnings.length > 0) {
          for (const warning of output.warnings) {
            compilation.warnings.push(/** @type {WebpackError} */ (warning));
          }
        }

        if (output.errors && output.errors.length > 0) {
          for (const error of output.errors) {
            compilation.errors.push(/** @type {WebpackError} */ (error));
          }
        }

        /** @type {Record<string, any>} */
        const newInfo = { minimized: true };
        const { source, extractedCommentsSource } = output;

        // Write extracted comments to commentsFilename
        if (extractedCommentsSource) {
          const { commentsFilename } = output;

          newInfo.related = { license: commentsFilename };

          allExtractedComments.set(name, {
            extractedCommentsSource,
            commentsFilename,
          });
        }

        compilation.updateAsset(name, source, newInfo);
      });
    }

    const limit =
      getWorker && numberOfAssets > 0
        ? /** @type {number} */ (numberOfWorkers)
        : scheduledTasks.length;
    await throttleAll(limit, scheduledTasks);

    if (initializedWorker) {
      await initializedWorker.end();
    }

    /** @typedef {{ source: import("webpack").sources.Source, commentsFilename: string, from: string }} ExtractedCommentsInfoWIthFrom */
    await Array.from(allExtractedComments)
      .sort()
      .reduce(
        /**
         * @param {Promise<unknown>} previousPromise
         * @param {[string, ExtractedCommentsInfo]} extractedComments
         * @returns {Promise<ExtractedCommentsInfoWIthFrom>}
         */
        async (previousPromise, [from, value]) => {
          const previous =
            /** @type {ExtractedCommentsInfoWIthFrom | undefined} **/ (
              await previousPromise
            );
          const { commentsFilename, extractedCommentsSource } = value;

          if (previous && previous.commentsFilename === commentsFilename) {
            const { from: previousFrom, source: prevSource } = previous;
            const mergedName = `${previousFrom}|${from}`;
            const name = `${commentsFilename}|${mergedName}`;
            const eTag = [prevSource, extractedCommentsSource]
              .map((item) => cache.getLazyHashedEtag(item))
              .reduce((previousValue, currentValue) =>
                cache.mergeEtags(previousValue, currentValue)
              );

            let source = await cache.getPromise(name, eTag);

            if (!source) {
              source = new ConcatSource(
                Array.from(
                  new Set([
                    .../** @type {string}*/ (prevSource.source()).split("\n\n"),
                    .../** @type {string}*/ (
                      extractedCommentsSource.source()
                    ).split("\n\n"),
                  ])
                ).join("\n\n")
              );

              await cache.storePromise(name, eTag, source);
            }

            compilation.updateAsset(commentsFilename, source);

            return { source, commentsFilename, from: mergedName };
          }

          const existingAsset = compilation.getAsset(commentsFilename);

          if (existingAsset) {
            return {
              source: existingAsset.source,
              commentsFilename,
              from: commentsFilename,
            };
          }

          compilation.emitAsset(commentsFilename, extractedCommentsSource, {
            extractedComments: true,
          });

          return { source: extractedCommentsSource, commentsFilename, from };
        },
        /** @type {Promise<unknown>} */ (Promise.resolve())
      );
  }
@private
@param {Compiler} compiler
@param {Compilation} compilation
@param {Record<string, import("webpack").sources.Source>} assets
@param {{availableNumberOfCores: number}} optimizeOptions
@returns {Promise<void>}