'use strict'; function _worker_threads() { const data = require('worker_threads'); _worker_threads = function () { return data; }; return data; } function _jestUtil() { const data = require('jest-util'); _jestUtil = function () { return data; }; return data; } var _types = require('../types'); /** * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ let file = null; let setupArgs = []; let initialized = false; /** * This file is a small bootstrapper for workers. It sets up the communication * between the worker and the parent process, interpreting parent messages and * sending results back. * * The file loaded will be lazily initialized the first time any of the workers * is called. This is done for optimal performance: if the farm is initialized, * but no call is made to it, child Node processes will be consuming the least * possible amount of memory. * * If an invalid message is detected, the child will exit (by throwing) with a * non-zero exit code. */ const messageListener = request => { switch (request[0]) { case _types.CHILD_MESSAGE_INITIALIZE: const init = request; file = init[2]; setupArgs = init[3]; process.env.JEST_WORKER_ID = init[4]; break; case _types.CHILD_MESSAGE_CALL: const call = request; execMethod(call[2], call[3]); break; case _types.CHILD_MESSAGE_END: end(); break; case _types.CHILD_MESSAGE_MEM_USAGE: reportMemoryUsage(); break; default: throw new TypeError( `Unexpected request from parent process: ${request[0]}` ); } }; _worker_threads().parentPort.on('message', messageListener); function reportMemoryUsage() { if (_worker_threads().isMainThread) { throw new Error('Child can only be used on a forked process'); } const msg = [_types.PARENT_MESSAGE_MEM_USAGE, process.memoryUsage().heapUsed]; _worker_threads().parentPort.postMessage(msg); } function reportSuccess(result) { if (_worker_threads().isMainThread) { throw new Error('Child can only be used on a forked process'); } _worker_threads().parentPort.postMessage([_types.PARENT_MESSAGE_OK, result]); } function reportClientError(error) { return reportError(error, _types.PARENT_MESSAGE_CLIENT_ERROR); } function reportInitializeError(error) { return reportError(error, _types.PARENT_MESSAGE_SETUP_ERROR); } function reportError(error, type) { if (_worker_threads().isMainThread) { throw new Error('Child can only be used on a forked process'); } if (error == null) { error = new Error('"null" or "undefined" thrown'); } _worker_threads().parentPort.postMessage([ type, error.constructor && error.constructor.name, error.message, error.stack, typeof error === 'object' ? { ...error } : error ]); } function end() { const main = require(file); if (!main.teardown) { exitProcess(); return; } execFunction(main.teardown, main, [], exitProcess, exitProcess); } function exitProcess() { // Clean up open handles so the worker ideally exits gracefully _worker_threads().parentPort.removeListener('message', messageListener); } function execMethod(method, args) { const main = require(file); let fn; if (method === 'default') { fn = main.__esModule ? main.default : main; } else { fn = main[method]; } function execHelper() { execFunction(fn, main, args, reportSuccess, reportClientError); } if (initialized || !main.setup) { execHelper(); return; } initialized = true; execFunction(main.setup, main, setupArgs, execHelper, reportInitializeError); } function execFunction(fn, ctx, args, onResult, onError) { let result; try { result = fn.apply(ctx, args); } catch (err) { onError(err); return; } if ((0, _jestUtil().isPromise)(result)) { result.then(onResult, onError); } else { onResult(result); } } export = CssMinimizerPlugin; /** * @template [T=CssNanoOptionsExtended] */ declare class CssMinimizerPlugin { /** * @private * @param {any} input * @returns {boolean} */ private static isSourceMap; /** * @private * @param {Warning | WarningObject | string} warning * @param {string} file * @param {WarningsFilter} [warningsFilter] * @param {SourceMapConsumer} [sourceMap] * @param {Compilation["requestShortener"]} [requestShortener] * @returns {Error & { hideStack?: boolean, file?: string } | undefined} */ private static buildWarning; /** * @private * @param {Error | ErrorObject | string} error * @param {string} file * @param {SourceMapConsumer} [sourceMap] * @param {Compilation["requestShortener"]} [requestShortener] * @returns {Error} */ private static buildError; /** * @private * @param {Parallel} parallel * @returns {number} */ private static getAvailableNumberOfCores; /** * @param {BasePluginOptions & DefinedDefaultMinimizerAndOptions} [options] */ constructor( options?: | (BasePluginOptions & DefinedDefaultMinimizerAndOptions) | undefined ); /** * @private * @type {InternalPluginOptions} */ private options; /** * @private * @param {Compiler} compiler * @param {Compilation} compilation * @param {Record} assets * @param {{availableNumberOfCores: number}} optimizeOptions * @returns {Promise} */ private optimize; /** * @param {Compiler} compiler * @returns {void} */ apply(compiler: Compiler): void; } declare namespace CssMinimizerPlugin { export { cssnanoMinify, cssoMinify, cleanCssMinify, esbuildMinify, parcelCssMinify, lightningCssMinify, swcMinify, Schema, Compiler, Compilation, WebpackError, JestWorker, RawSourceMap, Asset, ProcessOptions, Syntax, Parser, Stringifier, CssNanoOptions, Warning, WarningObject, ErrorObject, MinimizedResult, Input, CustomOptions, InferDefaultType, BasicMinimizerImplementation, MinimizerImplementation, MinimizerOptions, InternalOptions, InternalResult, Parallel, Rule, Rules, WarningsFilter, BasePluginOptions, MinimizerWorker, ProcessOptionsExtender, CssNanoOptionsExtended, DefinedDefaultMinimizerAndOptions, InternalPluginOptions, }; } type CssNanoOptionsExtended = CssNanoOptions & { processorOptions?: ProcessOptionsExtender; }; type Compiler = import("webpack").Compiler; type BasePluginOptions = { test?: Rules | undefined; include?: Rules | undefined; exclude?: Rules | undefined; warningsFilter?: WarningsFilter | undefined; parallel?: Parallel; }; type DefinedDefaultMinimizerAndOptions = T extends CssNanoOptionsExtended ? { minify?: MinimizerImplementation | undefined; minimizerOptions?: MinimizerOptions | undefined; } : { minify: MinimizerImplementation; minimizerOptions?: MinimizerOptions | undefined; }; import { cssnanoMinify } from "./utils"; import { cssoMinify } from "./utils"; import { cleanCssMinify } from "./utils"; import { esbuildMinify } from "./utils"; import { parcelCssMinify } from "./utils"; import { lightningCssMinify } from "./utils"; import { swcMinify } from "./utils"; type Schema = import("schema-utils/declarations/validate").Schema; type Compilation = import("webpack").Compilation; type WebpackError = import("webpack").WebpackError; type JestWorker = import("jest-worker").Worker; type RawSourceMap = import("source-map").RawSourceMap; type Asset = import("webpack").Asset; type ProcessOptions = import("postcss").ProcessOptions; type Syntax = import("postcss").Syntax; type Parser = import("postcss").Parser; type Stringifier = import("postcss").Stringifier; type CssNanoOptions = { configFile?: string | undefined; preset?: [string, object] | string | undefined; }; type Warning = | (Error & { plugin?: string; text?: string; source?: string; }) | string; type WarningObject = { message: string; plugin?: string | undefined; text?: string | undefined; line?: number | undefined; column?: number | undefined; }; type ErrorObject = { message: string; line?: number | undefined; column?: number | undefined; stack?: string | undefined; }; type MinimizedResult = { code: string; map?: import("source-map").RawSourceMap | undefined; errors?: (string | Error | ErrorObject)[] | undefined; warnings?: (Warning | WarningObject)[] | undefined; }; type Input = { [file: string]: string; }; type CustomOptions = { [key: string]: any; }; type InferDefaultType = T extends infer U ? U : CustomOptions; type BasicMinimizerImplementation = ( input: Input, sourceMap: RawSourceMap | undefined, minifyOptions: InferDefaultType ) => Promise; type MinimizerImplementation = T extends any[] ? T extends infer T_1 ? { [P in keyof T_1]: BasicMinimizerImplementation } : never : BasicMinimizerImplementation; type MinimizerOptions = T extends any[] ? T extends infer T_1 ? { [P in keyof T_1]?: InferDefaultType | undefined } : never : InferDefaultType; type InternalOptions = { name: string; input: string; inputSourceMap: RawSourceMap | undefined; minimizer: { implementation: MinimizerImplementation; options: MinimizerOptions; }; }; type InternalResult = { outputs: Array<{ code: string; map: RawSourceMap | undefined; }>; warnings: Array; errors: Array; }; type Parallel = undefined | boolean | number; type Rule = RegExp | string; type Rules = Rule[] | Rule; type WarningsFilter = ( warning: Warning | WarningObject | string, file: string, source?: string ) => boolean; type MinimizerWorker = Worker & { transform: (options: string) => InternalResult; minify: (options: InternalOptions) => InternalResult; }; type ProcessOptionsExtender = | ProcessOptions | { from?: string; to?: string; parser?: string | Syntax | Parser; stringifier?: string | Syntax | Stringifier; syntax?: string | Syntax; }; type InternalPluginOptions = BasePluginOptions & { minimizer: { implementation: MinimizerImplementation; options: MinimizerOptions; }; }; import { Worker } from "jest-worker";