first
This commit is contained in:
685
node_modules/css-minimizer-webpack-plugin/dist/index.js
generated
vendored
Normal file
685
node_modules/css-minimizer-webpack-plugin/dist/index.js
generated
vendored
Normal file
@ -0,0 +1,685 @@
|
||||
"use strict";
|
||||
|
||||
const os = require("os");
|
||||
|
||||
const {
|
||||
SourceMapConsumer
|
||||
} = require("source-map");
|
||||
|
||||
const {
|
||||
validate
|
||||
} = require("schema-utils");
|
||||
|
||||
const serialize = require("serialize-javascript");
|
||||
|
||||
const {
|
||||
Worker
|
||||
} = require("jest-worker");
|
||||
|
||||
const {
|
||||
throttleAll,
|
||||
cssnanoMinify,
|
||||
cssoMinify,
|
||||
cleanCssMinify,
|
||||
esbuildMinify,
|
||||
parcelCssMinify
|
||||
} = require("./utils");
|
||||
|
||||
const schema = require("./options.json");
|
||||
|
||||
const {
|
||||
minify
|
||||
} = require("./minify");
|
||||
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
|
||||
|
||||
/** @typedef {import("webpack").Compiler} Compiler */
|
||||
|
||||
/** @typedef {import("webpack").Compilation} Compilation */
|
||||
|
||||
/** @typedef {import("webpack").WebpackError} WebpackError */
|
||||
|
||||
/** @typedef {import("jest-worker").Worker} JestWorker */
|
||||
|
||||
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
|
||||
|
||||
/** @typedef {import("webpack").Asset} Asset */
|
||||
|
||||
/** @typedef {import("postcss").ProcessOptions} ProcessOptions */
|
||||
|
||||
/** @typedef {import("postcss").Syntax} Syntax */
|
||||
|
||||
/** @typedef {import("postcss").Parser} Parser */
|
||||
|
||||
/** @typedef {import("postcss").Stringifier} Stringifier */
|
||||
|
||||
/**
|
||||
* @typedef {Object} CssNanoOptions
|
||||
* @property {string} [configFile]
|
||||
* @property {[string, object] | string | undefined} [preset]
|
||||
*/
|
||||
|
||||
/** @typedef {Error & { plugin?: string, text?: string, source?: string } | string} Warning */
|
||||
|
||||
/**
|
||||
* @typedef {Object} WarningObject
|
||||
* @property {string} message
|
||||
* @property {string} [plugin]
|
||||
* @property {string} [text]
|
||||
* @property {number} [line]
|
||||
* @property {number} [column]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} ErrorObject
|
||||
* @property {string} message
|
||||
* @property {number} [line]
|
||||
* @property {number} [column]
|
||||
* @property {string} [stack]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} MinimizedResult
|
||||
* @property {string} code
|
||||
* @property {RawSourceMap} [map]
|
||||
* @property {Array<Error | ErrorObject| string>} [errors]
|
||||
* @property {Array<Warning | WarningObject | string>} [warnings]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{ [file: string]: string }} Input
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{ [key: string]: any }} CustomOptions
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {T extends infer U ? U : CustomOptions} InferDefaultType
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @callback BasicMinimizerImplementation
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {InferDefaultType<T>} minifyOptions
|
||||
* @returns {Promise<MinimizedResult>}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {T extends any[] ? { [P in keyof T]: BasicMinimizerImplementation<T[P]>; } : BasicMinimizerImplementation<T>} MinimizerImplementation
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {T extends any[] ? { [P in keyof T]?: InferDefaultType<T[P]> } : InferDefaultType<T>} MinimizerOptions
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {Object} InternalOptions
|
||||
* @property {string} name
|
||||
* @property {string} input
|
||||
* @property {RawSourceMap | undefined} inputSourceMap
|
||||
* @property {{ implementation: MinimizerImplementation<T>, options: MinimizerOptions<T> }} minimizer
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef InternalResult
|
||||
* @property {Array<{ code: string, map: RawSourceMap | undefined }>} outputs
|
||||
* @property {Array<Warning | WarningObject | string>} warnings
|
||||
* @property {Array<Error | ErrorObject | string>} errors
|
||||
*/
|
||||
|
||||
/** @typedef {undefined | boolean | number} Parallel */
|
||||
|
||||
/** @typedef {RegExp | string} Rule */
|
||||
|
||||
/** @typedef {Rule[] | Rule} Rules */
|
||||
|
||||
/** @typedef {(warning: Warning | WarningObject | string, file: string, source?: string) => boolean} WarningsFilter */
|
||||
|
||||
/**
|
||||
* @typedef {Object} BasePluginOptions
|
||||
* @property {Rules} [test]
|
||||
* @property {Rules} [include]
|
||||
* @property {Rules} [exclude]
|
||||
* @property {WarningsFilter} [warningsFilter]
|
||||
* @property {Parallel} [parallel]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {JestWorker & { transform: (options: string) => InternalResult, minify: (options: InternalOptions<T>) => InternalResult }} MinimizerWorker
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef{ProcessOptions | { from?: string, to?: string, parser?: string | Syntax | Parser, stringifier?: string | Syntax | Stringifier, syntax?: string | Syntax } } ProcessOptionsExtender
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {CssNanoOptions & { processorOptions?: ProcessOptionsExtender }} CssNanoOptionsExtended
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {T extends CssNanoOptionsExtended ? { minify?: MinimizerImplementation<T> | undefined, minimizerOptions?: MinimizerOptions<T> | undefined } : { minify: MinimizerImplementation<T>, minimizerOptions?: MinimizerOptions<T> | undefined }} DefinedDefaultMinimizerAndOptions
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {BasePluginOptions & { minimizer: { implementation: MinimizerImplementation<T>, options: MinimizerOptions<T> } }} InternalPluginOptions
|
||||
*/
|
||||
|
||||
|
||||
const warningRegex = /\s.+:+([0-9]+):+([0-9]+)/;
|
||||
/**
|
||||
* @template [T=CssNanoOptionsExtended]
|
||||
*/
|
||||
|
||||
class CssMinimizerPlugin {
|
||||
/**
|
||||
* @param {BasePluginOptions & DefinedDefaultMinimizerAndOptions<T>} [options]
|
||||
*/
|
||||
constructor(options) {
|
||||
validate(
|
||||
/** @type {Schema} */
|
||||
schema, options || {}, {
|
||||
name: "Css Minimizer Plugin",
|
||||
baseDataPath: "options"
|
||||
});
|
||||
const {
|
||||
minify =
|
||||
/** @type {BasicMinimizerImplementation<T>} */
|
||||
cssnanoMinify,
|
||||
minimizerOptions =
|
||||
/** @type {MinimizerOptions<T>} */
|
||||
{},
|
||||
test = /\.css(\?.*)?$/i,
|
||||
warningsFilter = () => true,
|
||||
parallel = true,
|
||||
include,
|
||||
exclude
|
||||
} = options || {};
|
||||
/**
|
||||
* @private
|
||||
* @type {InternalPluginOptions<T>}
|
||||
*/
|
||||
|
||||
this.options = {
|
||||
test,
|
||||
warningsFilter,
|
||||
parallel,
|
||||
include,
|
||||
exclude,
|
||||
minimizer: {
|
||||
implementation:
|
||||
/** @type {MinimizerImplementation<T>} */
|
||||
minify,
|
||||
options: minimizerOptions
|
||||
}
|
||||
};
|
||||
}
|
||||
/**
|
||||
* @private
|
||||
* @param {any} input
|
||||
* @returns {boolean}
|
||||
*/
|
||||
|
||||
|
||||
static isSourceMap(input) {
|
||||
// All required options for `new SourceMapConsumer(...options)`
|
||||
// https://github.com/mozilla/source-map#new-sourcemapconsumerrawsourcemap
|
||||
return Boolean(input && input.version && input.sources && Array.isArray(input.sources) && typeof input.mappings === "string");
|
||||
}
|
||||
/**
|
||||
* @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}
|
||||
*/
|
||||
|
||||
|
||||
static buildWarning(warning, file, warningsFilter, sourceMap, requestShortener) {
|
||||
let warningMessage = typeof warning === "string" ? warning : `${warning.plugin ? `[${warning.plugin}] ` : ""}${warning.text || warning.message}`;
|
||||
let locationMessage = "";
|
||||
let source;
|
||||
|
||||
if (sourceMap) {
|
||||
let line;
|
||||
let column;
|
||||
|
||||
if (typeof warning === "string") {
|
||||
const match = warningRegex.exec(warning);
|
||||
|
||||
if (match) {
|
||||
line = +match[1];
|
||||
column = +match[2];
|
||||
}
|
||||
} else {
|
||||
({
|
||||
line,
|
||||
column
|
||||
} =
|
||||
/** @type {WarningObject} */
|
||||
warning);
|
||||
}
|
||||
|
||||
if (line && column) {
|
||||
const original = sourceMap.originalPositionFor({
|
||||
line,
|
||||
column
|
||||
});
|
||||
|
||||
if (original && original.source && original.source !== file && requestShortener) {
|
||||
({
|
||||
source
|
||||
} = original);
|
||||
warningMessage = `${warningMessage.replace(warningRegex, "")}`;
|
||||
locationMessage = `${requestShortener.shorten(original.source)}:${original.line}:${original.column}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (warningsFilter && !warningsFilter(warning, file, source)) {
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* @type {Error & { hideStack?: boolean, file?: string }}
|
||||
*/
|
||||
|
||||
|
||||
const builtWarning = new Error(`${file} from Css Minimizer plugin\n${warningMessage}${locationMessage ? ` ${locationMessage}` : ""}`);
|
||||
builtWarning.name = "Warning";
|
||||
builtWarning.hideStack = true;
|
||||
builtWarning.file = file; // eslint-disable-next-line consistent-return
|
||||
|
||||
return builtWarning;
|
||||
}
|
||||
/**
|
||||
* @private
|
||||
* @param {Error | ErrorObject | string} error
|
||||
* @param {string} file
|
||||
* @param {SourceMapConsumer} [sourceMap]
|
||||
* @param {Compilation["requestShortener"]} [requestShortener]
|
||||
* @returns {Error}
|
||||
*/
|
||||
|
||||
|
||||
static buildError(error, file, sourceMap, requestShortener) {
|
||||
/**
|
||||
* @type {Error & { file?: string }}
|
||||
*/
|
||||
let builtError;
|
||||
|
||||
if (typeof error === "string") {
|
||||
builtError = new Error(`${file} from Css Minimizer plugin\n${error}`);
|
||||
builtError.file = file;
|
||||
return builtError;
|
||||
}
|
||||
|
||||
if (
|
||||
/** @type {ErrorObject} */
|
||||
error.line &&
|
||||
/** @type {ErrorObject} */
|
||||
error.column) {
|
||||
const {
|
||||
line,
|
||||
column
|
||||
} =
|
||||
/** @type {ErrorObject & { line: number, column: number }} */
|
||||
error;
|
||||
const original = sourceMap && sourceMap.originalPositionFor({
|
||||
line,
|
||||
column
|
||||
});
|
||||
|
||||
if (original && original.source && requestShortener) {
|
||||
builtError = new Error(`${file} from Css Minimizer plugin\n${error.message} [${requestShortener.shorten(original.source)}:${original.line},${original.column}][${file}:${line},${column}]${error.stack ? `\n${error.stack.split("\n").slice(1).join("\n")}` : ""}`);
|
||||
builtError.file = file;
|
||||
return builtError;
|
||||
}
|
||||
|
||||
builtError = new Error(`${file} from Css Minimizer plugin\n${error.message} [${file}:${line},${column}]${error.stack ? `\n${error.stack.split("\n").slice(1).join("\n")}` : ""}`);
|
||||
builtError.file = file;
|
||||
return builtError;
|
||||
}
|
||||
|
||||
if (error.stack) {
|
||||
builtError = new Error(`${file} from Css Minimizer plugin\n${error.stack}`);
|
||||
builtError.file = file;
|
||||
return builtError;
|
||||
}
|
||||
|
||||
builtError = new Error(`${file} from Css Minimizer plugin\n${error.message}`);
|
||||
builtError.file = file;
|
||||
return builtError;
|
||||
}
|
||||
/**
|
||||
* @private
|
||||
* @param {Parallel} parallel
|
||||
* @returns {number}
|
||||
*/
|
||||
|
||||
|
||||
static getAvailableNumberOfCores(parallel) {
|
||||
// In some cases cpus() returns undefined
|
||||
// https://github.com/nodejs/node/issues/19022
|
||||
const cpus = os.cpus() || {
|
||||
length: 1
|
||||
};
|
||||
return parallel === true ? cpus.length - 1 : Math.min(Number(parallel) || 0, cpus.length - 1);
|
||||
}
|
||||
/**
|
||||
* @private
|
||||
* @param {Compiler} compiler
|
||||
* @param {Compilation} compilation
|
||||
* @param {Record<string, import("webpack").sources.Source>} assets
|
||||
* @param {{availableNumberOfCores: number}} optimizeOptions
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
|
||||
|
||||
async optimize(compiler, compilation, assets, optimizeOptions) {
|
||||
const cache = compilation.getCache("CssMinimizerWebpackPlugin");
|
||||
let numberOfAssetsForMinify = 0;
|
||||
const assetsForMinify = await Promise.all(Object.keys(typeof assets === "undefined" ? compilation.assets : assets).filter(name => {
|
||||
const {
|
||||
info
|
||||
} =
|
||||
/** @type {Asset} */
|
||||
compilation.getAsset(name);
|
||||
|
||||
if ( // Skip double minimize assets from child compilation
|
||||
info.minimized) {
|
||||
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) {
|
||||
numberOfAssetsForMinify += 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(numberOfAssetsForMinify, optimizeOptions.availableNumberOfCores);
|
||||
|
||||
getWorker = () => {
|
||||
if (initializedWorker) {
|
||||
return initializedWorker;
|
||||
}
|
||||
|
||||
initializedWorker =
|
||||
/** @type {MinimizerWorker<T>} */
|
||||
new Worker(require.resolve("./minify"), {
|
||||
numWorkers: numberOfWorkers,
|
||||
enableWorkerThreads: 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,
|
||||
RawSource
|
||||
} = compiler.webpack.sources;
|
||||
const scheduledTasks = [];
|
||||
|
||||
for (const asset of assetsForMinify) {
|
||||
scheduledTasks.push(async () => {
|
||||
const {
|
||||
name,
|
||||
inputSource,
|
||||
cacheItem
|
||||
} = asset;
|
||||
let {
|
||||
output
|
||||
} = asset;
|
||||
|
||||
if (!output) {
|
||||
let input;
|
||||
/** @type {RawSourceMap | undefined} */
|
||||
|
||||
let inputSourceMap;
|
||||
const {
|
||||
source: sourceFromInputSource,
|
||||
map
|
||||
} = inputSource.sourceAndMap();
|
||||
input = sourceFromInputSource;
|
||||
|
||||
if (map) {
|
||||
if (!CssMinimizerPlugin.isSourceMap(map)) {
|
||||
compilation.warnings.push(
|
||||
/** @type {WebpackError} */
|
||||
new Error(`${name} contains invalid source map`));
|
||||
} else {
|
||||
inputSourceMap =
|
||||
/** @type {RawSourceMap} */
|
||||
map;
|
||||
}
|
||||
}
|
||||
|
||||
if (Buffer.isBuffer(input)) {
|
||||
input = input.toString();
|
||||
}
|
||||
/**
|
||||
* @type {InternalOptions<T>}
|
||||
*/
|
||||
|
||||
|
||||
const options = {
|
||||
name,
|
||||
input,
|
||||
inputSourceMap,
|
||||
minimizer: {
|
||||
implementation: this.options.minimizer.implementation,
|
||||
options: this.options.minimizer.options
|
||||
}
|
||||
};
|
||||
let result;
|
||||
|
||||
try {
|
||||
result = await (getWorker ? getWorker().transform(serialize(options)) : minify(options));
|
||||
} catch (error) {
|
||||
const hasSourceMap = inputSourceMap && CssMinimizerPlugin.isSourceMap(inputSourceMap);
|
||||
compilation.errors.push(
|
||||
/** @type {WebpackError} */
|
||||
CssMinimizerPlugin.buildError(
|
||||
/** @type {any} */
|
||||
error, name, hasSourceMap ? new SourceMapConsumer(
|
||||
/** @type {RawSourceMap} */
|
||||
inputSourceMap) : // eslint-disable-next-line no-undefined
|
||||
undefined, // eslint-disable-next-line no-undefined
|
||||
hasSourceMap ? compilation.requestShortener : undefined));
|
||||
return;
|
||||
}
|
||||
|
||||
output = {
|
||||
warnings: [],
|
||||
errors: []
|
||||
};
|
||||
|
||||
for (const item of result.outputs) {
|
||||
if (item.map) {
|
||||
let originalSource;
|
||||
let innerSourceMap;
|
||||
|
||||
if (output.source) {
|
||||
({
|
||||
source: originalSource,
|
||||
map: innerSourceMap
|
||||
} = output.source.sourceAndMap());
|
||||
} else {
|
||||
originalSource = input;
|
||||
innerSourceMap = inputSourceMap;
|
||||
} // TODO need API for merging source maps in `webpack-source`
|
||||
|
||||
|
||||
output.source = new SourceMapSource(item.code, name, item.map, originalSource, innerSourceMap, true);
|
||||
} else {
|
||||
output.source = new RawSource(item.code);
|
||||
}
|
||||
}
|
||||
|
||||
if (result.errors && result.errors.length > 0) {
|
||||
const hasSourceMap = inputSourceMap && CssMinimizerPlugin.isSourceMap(inputSourceMap);
|
||||
|
||||
for (const error of result.errors) {
|
||||
output.warnings.push(CssMinimizerPlugin.buildError(error, name, hasSourceMap ? new SourceMapConsumer(
|
||||
/** @type {RawSourceMap} */
|
||||
inputSourceMap) : // eslint-disable-next-line no-undefined
|
||||
undefined, // eslint-disable-next-line no-undefined
|
||||
hasSourceMap ? compilation.requestShortener : undefined));
|
||||
}
|
||||
}
|
||||
|
||||
if (result.warnings && result.warnings.length > 0) {
|
||||
const hasSourceMap = inputSourceMap && CssMinimizerPlugin.isSourceMap(inputSourceMap);
|
||||
|
||||
for (const warning of result.warnings) {
|
||||
const buildWarning = CssMinimizerPlugin.buildWarning(warning, name, this.options.warningsFilter, hasSourceMap ? new SourceMapConsumer(
|
||||
/** @type {RawSourceMap} */
|
||||
inputSourceMap) : // eslint-disable-next-line no-undefined
|
||||
undefined, // eslint-disable-next-line no-undefined
|
||||
hasSourceMap ? compilation.requestShortener : undefined);
|
||||
|
||||
if (buildWarning) {
|
||||
output.warnings.push(buildWarning);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await cacheItem.storePromise({
|
||||
source: output.source,
|
||||
warnings: output.warnings,
|
||||
errors: output.errors
|
||||
});
|
||||
}
|
||||
|
||||
if (output.warnings && output.warnings.length > 0) {
|
||||
for (const warning of output.warnings) {
|
||||
compilation.warnings.push(warning);
|
||||
}
|
||||
}
|
||||
|
||||
if (output.errors && output.errors.length > 0) {
|
||||
for (const error of output.errors) {
|
||||
compilation.errors.push(error);
|
||||
}
|
||||
}
|
||||
|
||||
const newInfo = {
|
||||
minimized: true
|
||||
};
|
||||
const {
|
||||
source
|
||||
} = output;
|
||||
compilation.updateAsset(name, source, newInfo);
|
||||
});
|
||||
}
|
||||
|
||||
const limit = getWorker && numberOfAssetsForMinify > 0 ?
|
||||
/** @type {number} */
|
||||
numberOfWorkers : scheduledTasks.length;
|
||||
await throttleAll(limit, scheduledTasks);
|
||||
|
||||
if (initializedWorker) {
|
||||
await initializedWorker.end();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param {Compiler} compiler
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
|
||||
apply(compiler) {
|
||||
const pluginName = this.constructor.name;
|
||||
const availableNumberOfCores = CssMinimizerPlugin.getAvailableNumberOfCores(this.options.parallel);
|
||||
compiler.hooks.compilation.tap(pluginName, compilation => {
|
||||
compilation.hooks.processAssets.tapPromise({
|
||||
name: pluginName,
|
||||
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
|
||||
additionalAssets: true
|
||||
}, assets => this.optimize(compiler, compilation, assets, {
|
||||
availableNumberOfCores
|
||||
}));
|
||||
compilation.hooks.statsPrinter.tap(pluginName, stats => {
|
||||
stats.hooks.print.for("asset.info.minimized").tap("css-minimizer-webpack-plugin", (minimized, {
|
||||
green,
|
||||
formatFlag
|
||||
}) => // eslint-disable-next-line no-undefined
|
||||
minimized ?
|
||||
/** @type {Function} */
|
||||
green(
|
||||
/** @type {Function} */
|
||||
formatFlag("minimized")) : "");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
CssMinimizerPlugin.cssnanoMinify = cssnanoMinify;
|
||||
CssMinimizerPlugin.cssoMinify = cssoMinify;
|
||||
CssMinimizerPlugin.cleanCssMinify = cleanCssMinify;
|
||||
CssMinimizerPlugin.esbuildMinify = esbuildMinify;
|
||||
CssMinimizerPlugin.parcelCssMinify = parcelCssMinify;
|
||||
module.exports = CssMinimizerPlugin;
|
86
node_modules/css-minimizer-webpack-plugin/dist/minify.js
generated
vendored
Normal file
86
node_modules/css-minimizer-webpack-plugin/dist/minify.js
generated
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
|
||||
|
||||
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
|
||||
|
||||
/** @typedef {import("./index.js").InternalResult} InternalResult */
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param {import("./index.js").InternalOptions<T>} options
|
||||
* @returns {Promise<InternalResult>}
|
||||
*/
|
||||
const minify = async options => {
|
||||
const minifyFns = Array.isArray(options.minimizer.implementation) ? options.minimizer.implementation : [options.minimizer.implementation];
|
||||
/** @type {InternalResult} */
|
||||
|
||||
const result = {
|
||||
outputs: [],
|
||||
warnings: [],
|
||||
errors: []
|
||||
};
|
||||
let needSourceMap = false;
|
||||
|
||||
for (let i = 0; i <= minifyFns.length - 1; i++) {
|
||||
const minifyFn = minifyFns[i];
|
||||
const minifyOptions = Array.isArray(options.minimizer.options) ? options.minimizer.options[i] : options.minimizer.options;
|
||||
const prevResult = result.outputs.length > 0 ? result.outputs[result.outputs.length - 1] : {
|
||||
code: options.input,
|
||||
map: options.inputSourceMap
|
||||
};
|
||||
const {
|
||||
code,
|
||||
map
|
||||
} = prevResult; // eslint-disable-next-line no-await-in-loop
|
||||
|
||||
const minifyResult = await minifyFn({
|
||||
[options.name]: code
|
||||
}, map, minifyOptions);
|
||||
|
||||
if (typeof minifyResult.code !== "string") {
|
||||
throw new Error("minimizer function doesn't return the 'code' property or result is not a string value");
|
||||
}
|
||||
|
||||
if (minifyResult.map) {
|
||||
needSourceMap = true;
|
||||
}
|
||||
|
||||
if (minifyResult.errors) {
|
||||
result.errors = result.errors.concat(minifyResult.errors);
|
||||
}
|
||||
|
||||
if (minifyResult.warnings) {
|
||||
result.warnings = result.warnings.concat(minifyResult.warnings);
|
||||
}
|
||||
|
||||
result.outputs.push({
|
||||
code: minifyResult.code,
|
||||
map: minifyResult.map
|
||||
});
|
||||
}
|
||||
|
||||
if (!needSourceMap) {
|
||||
result.outputs = [result.outputs[result.outputs.length - 1]];
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
/**
|
||||
* @param {string} options
|
||||
* @returns {Promise<InternalResult>}
|
||||
*/
|
||||
|
||||
|
||||
async function transform(options) {
|
||||
// 'use strict' => this === undefined (Clean Scope)
|
||||
// Safer for possible security issues, albeit not critical at all here
|
||||
// eslint-disable-next-line no-new-func, no-param-reassign
|
||||
const evaluatedOptions = new Function("exports", "require", "module", "__filename", "__dirname", `'use strict'\nreturn ${options}`)(exports, require, module, __filename, __dirname);
|
||||
return minify(evaluatedOptions);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
minify,
|
||||
transform
|
||||
};
|
121
node_modules/css-minimizer-webpack-plugin/dist/options.json
generated
vendored
Normal file
121
node_modules/css-minimizer-webpack-plugin/dist/options.json
generated
vendored
Normal file
@ -0,0 +1,121 @@
|
||||
{
|
||||
"definitions": {
|
||||
"Rule": {
|
||||
"description": "Filtering rule as regex or string.",
|
||||
"anyOf": [
|
||||
{
|
||||
"instanceof": "RegExp",
|
||||
"tsType": "RegExp"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
"Rules": {
|
||||
"description": "Filtering rules.",
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "array",
|
||||
"items": {
|
||||
"description": "A rule condition.",
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/definitions/Rule"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/Rule"
|
||||
}
|
||||
]
|
||||
},
|
||||
"MinimizerOptions": {
|
||||
"additionalProperties": true,
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
"title": "CssMinimizerWebpackPluginOptions",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"test": {
|
||||
"description": "Include all modules that pass test assertion.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#test",
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/definitions/Rules"
|
||||
}
|
||||
]
|
||||
},
|
||||
"include": {
|
||||
"description": "Include all modules matching any of these conditions.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#include",
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/definitions/Rules"
|
||||
}
|
||||
]
|
||||
},
|
||||
"exclude": {
|
||||
"description": "Exclude all modules matching any of these conditions.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#exclude",
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/definitions/Rules"
|
||||
}
|
||||
]
|
||||
},
|
||||
"minimizerOptions": {
|
||||
"description": "Options for `cssMinimizerOptions`.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#minimizeroptions",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/MinimizerOptions"
|
||||
},
|
||||
{
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"items": {
|
||||
"$ref": "#/definitions/MinimizerOptions"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"parallel": {
|
||||
"description": "Use multi-process parallel running to improve the build speed.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#parallel",
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "boolean"
|
||||
},
|
||||
{
|
||||
"type": "integer"
|
||||
}
|
||||
]
|
||||
},
|
||||
"warningsFilter": {
|
||||
"description": "Allow to filter `css minimizer` warnings.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#warningsfilter",
|
||||
"instanceof": "Function"
|
||||
},
|
||||
"minify": {
|
||||
"description": "Allows you to override default minify function.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#minify",
|
||||
"anyOf": [
|
||||
{
|
||||
"instanceof": "Function"
|
||||
},
|
||||
{
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"items": {
|
||||
"instanceof": "Function"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
363
node_modules/css-minimizer-webpack-plugin/dist/utils.js
generated
vendored
Normal file
363
node_modules/css-minimizer-webpack-plugin/dist/utils.js
generated
vendored
Normal file
@ -0,0 +1,363 @@
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("./index.js").Input} Input */
|
||||
|
||||
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
|
||||
|
||||
/** @typedef {import("source-map").SourceMapGenerator} SourceMapGenerator */
|
||||
|
||||
/** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
|
||||
|
||||
/** @typedef {import("./index.js").CustomOptions} CustomOptions */
|
||||
|
||||
/** @typedef {import("postcss").ProcessOptions} ProcessOptions */
|
||||
|
||||
/** @typedef {import("postcss").Postcss} Postcss */
|
||||
const notSettled = Symbol(`not-settled`);
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {() => Promise<T>} Task
|
||||
*/
|
||||
|
||||
/**
|
||||
* Run tasks with limited concurency.
|
||||
* @template T
|
||||
* @param {number} limit - Limit of tasks that run at once.
|
||||
* @param {Task<T>[]} tasks - List of tasks to run.
|
||||
* @returns {Promise<T[]>} A promise that fulfills to an array of the results
|
||||
*/
|
||||
|
||||
function throttleAll(limit, tasks) {
|
||||
if (!Number.isInteger(limit) || limit < 1) {
|
||||
throw new TypeError(`Expected \`limit\` to be a finite number > 0, got \`${limit}\` (${typeof limit})`);
|
||||
}
|
||||
|
||||
if (!Array.isArray(tasks) || !tasks.every(task => typeof task === `function`)) {
|
||||
throw new TypeError(`Expected \`tasks\` to be a list of functions returning a promise`);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const result = Array(tasks.length).fill(notSettled);
|
||||
const entries = tasks.entries();
|
||||
|
||||
const next = () => {
|
||||
const {
|
||||
done,
|
||||
value
|
||||
} = entries.next();
|
||||
|
||||
if (done) {
|
||||
const isLast = !result.includes(notSettled);
|
||||
if (isLast) resolve(result);
|
||||
return;
|
||||
}
|
||||
|
||||
const [index, task] = value;
|
||||
/**
|
||||
* @param {T} x
|
||||
*/
|
||||
|
||||
const onFulfilled = x => {
|
||||
result[index] = x;
|
||||
next();
|
||||
};
|
||||
|
||||
task().then(onFulfilled, reject);
|
||||
};
|
||||
|
||||
Array(limit).fill(0).forEach(next);
|
||||
});
|
||||
}
|
||||
/* istanbul ignore next */
|
||||
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
|
||||
|
||||
async function cssnanoMinify(input, sourceMap, minimizerOptions = {
|
||||
preset: "default"
|
||||
}) {
|
||||
/**
|
||||
* @template T
|
||||
* @param {string} module
|
||||
* @returns {Promise<T>}
|
||||
*/
|
||||
const load = async module => {
|
||||
let exports;
|
||||
|
||||
try {
|
||||
// eslint-disable-next-line import/no-dynamic-require, global-require
|
||||
exports = require(module);
|
||||
return exports;
|
||||
} catch (requireError) {
|
||||
let importESM;
|
||||
|
||||
try {
|
||||
// eslint-disable-next-line no-new-func
|
||||
importESM = new Function("id", "return import(id);");
|
||||
} catch (e) {
|
||||
importESM = null;
|
||||
}
|
||||
|
||||
if (
|
||||
/** @type {Error & {code: string}} */
|
||||
requireError.code === "ERR_REQUIRE_ESM" && importESM) {
|
||||
exports = await importESM(module);
|
||||
return exports.default;
|
||||
}
|
||||
|
||||
throw requireError;
|
||||
}
|
||||
};
|
||||
|
||||
const [[name, code]] = Object.entries(input);
|
||||
/** @type {ProcessOptions} */
|
||||
|
||||
const postcssOptions = {
|
||||
from: name,
|
||||
...minimizerOptions.processorOptions
|
||||
};
|
||||
|
||||
if (typeof postcssOptions.parser === "string") {
|
||||
try {
|
||||
postcssOptions.parser = await load(postcssOptions.parser);
|
||||
} catch (error) {
|
||||
throw new Error(`Loading PostCSS "${postcssOptions.parser}" parser failed: ${
|
||||
/** @type {Error} */
|
||||
error.message}\n\n(@${name})`);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof postcssOptions.stringifier === "string") {
|
||||
try {
|
||||
postcssOptions.stringifier = await load(postcssOptions.stringifier);
|
||||
} catch (error) {
|
||||
throw new Error(`Loading PostCSS "${postcssOptions.stringifier}" stringifier failed: ${
|
||||
/** @type {Error} */
|
||||
error.message}\n\n(@${name})`);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof postcssOptions.syntax === "string") {
|
||||
try {
|
||||
postcssOptions.syntax = await load(postcssOptions.syntax);
|
||||
} catch (error) {
|
||||
throw new Error(`Loading PostCSS "${postcssOptions.syntax}" syntax failed: ${
|
||||
/** @type {Error} */
|
||||
error.message}\n\n(@${name})`);
|
||||
}
|
||||
}
|
||||
|
||||
if (sourceMap) {
|
||||
postcssOptions.map = {
|
||||
annotation: false
|
||||
};
|
||||
}
|
||||
/** @type {Postcss} */
|
||||
// eslint-disable-next-line global-require
|
||||
|
||||
|
||||
const postcss = require("postcss").default; // @ts-ignore
|
||||
// eslint-disable-next-line global-require
|
||||
|
||||
|
||||
const cssnano = require("cssnano"); // @ts-ignore
|
||||
// Types are broken
|
||||
|
||||
|
||||
const result = await postcss([cssnano(minimizerOptions)]).process(code, postcssOptions);
|
||||
return {
|
||||
code: result.css,
|
||||
map: result.map ? result.map.toJSON() : // eslint-disable-next-line no-undefined
|
||||
undefined,
|
||||
warnings: result.warnings().map(String)
|
||||
};
|
||||
}
|
||||
/* istanbul ignore next */
|
||||
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
|
||||
|
||||
async function cssoMinify(input, sourceMap, minimizerOptions) {
|
||||
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
|
||||
const csso = require("csso");
|
||||
|
||||
const [[filename, code]] = Object.entries(input);
|
||||
const result = csso.minify(code, {
|
||||
filename,
|
||||
sourceMap: Boolean(sourceMap),
|
||||
...minimizerOptions
|
||||
});
|
||||
return {
|
||||
code: result.css,
|
||||
map: result.map ?
|
||||
/** @type {SourceMapGenerator & { toJSON(): RawSourceMap }} */
|
||||
result.map.toJSON() : // eslint-disable-next-line no-undefined
|
||||
undefined
|
||||
};
|
||||
}
|
||||
/* istanbul ignore next */
|
||||
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
|
||||
|
||||
async function cleanCssMinify(input, sourceMap, minimizerOptions) {
|
||||
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
|
||||
const CleanCSS = require("clean-css");
|
||||
|
||||
const [[name, code]] = Object.entries(input);
|
||||
const result = await new CleanCSS({
|
||||
sourceMap: Boolean(sourceMap),
|
||||
...minimizerOptions,
|
||||
returnPromise: true
|
||||
}).minify({
|
||||
[name]: {
|
||||
styles: code
|
||||
}
|
||||
});
|
||||
const generatedSourceMap = result.sourceMap &&
|
||||
/** @type {SourceMapGenerator & { toJSON(): RawSourceMap }} */
|
||||
result.sourceMap.toJSON(); // workaround for source maps on windows
|
||||
|
||||
if (generatedSourceMap) {
|
||||
// eslint-disable-next-line global-require
|
||||
const isWindowsPathSep = require("path").sep === "\\";
|
||||
generatedSourceMap.sources = generatedSourceMap.sources.map(
|
||||
/**
|
||||
* @param {string} item
|
||||
* @returns {string}
|
||||
*/
|
||||
item => isWindowsPathSep ? item.replace(/\\/g, "/") : item);
|
||||
}
|
||||
|
||||
return {
|
||||
code: result.styles,
|
||||
map: generatedSourceMap,
|
||||
warnings: result.warnings
|
||||
};
|
||||
}
|
||||
/* istanbul ignore next */
|
||||
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
|
||||
|
||||
async function esbuildMinify(input, sourceMap, minimizerOptions) {
|
||||
/**
|
||||
* @param {import("esbuild").TransformOptions} [esbuildOptions={}]
|
||||
* @returns {import("esbuild").TransformOptions}
|
||||
*/
|
||||
const buildEsbuildOptions = (esbuildOptions = {}) => {
|
||||
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
|
||||
return {
|
||||
loader: "css",
|
||||
minify: true,
|
||||
legalComments: "inline",
|
||||
...esbuildOptions,
|
||||
sourcemap: false
|
||||
};
|
||||
}; // eslint-disable-next-line import/no-extraneous-dependencies, global-require
|
||||
|
||||
|
||||
const esbuild = require("esbuild"); // Copy `esbuild` options
|
||||
|
||||
|
||||
const esbuildOptions = buildEsbuildOptions(minimizerOptions); // Let `esbuild` generate a SourceMap
|
||||
|
||||
if (sourceMap) {
|
||||
esbuildOptions.sourcemap = true;
|
||||
esbuildOptions.sourcesContent = false;
|
||||
}
|
||||
|
||||
const [[filename, code]] = Object.entries(input);
|
||||
esbuildOptions.sourcefile = filename;
|
||||
const result = await esbuild.transform(code, esbuildOptions);
|
||||
return {
|
||||
code: result.code,
|
||||
// eslint-disable-next-line no-undefined
|
||||
map: result.map ? JSON.parse(result.map) : undefined,
|
||||
warnings: result.warnings.length > 0 ? result.warnings.map(item => {
|
||||
return {
|
||||
source: item.location && item.location.file,
|
||||
// eslint-disable-next-line no-undefined
|
||||
line: item.location && item.location.line ? item.location.line : undefined,
|
||||
// eslint-disable-next-line no-undefined
|
||||
column: item.location && item.location.column ? item.location.column : undefined,
|
||||
plugin: item.pluginName,
|
||||
message: `${item.text}${item.detail ? `\nDetails:\n${item.detail}` : ""}${item.notes.length > 0 ? `\n\nNotes:\n${item.notes.map(note => `${note.location ? `[${note.location.file}:${note.location.line}:${note.location.column}] ` : ""}${note.text}${note.location ? `\nSuggestion: ${note.location.suggestion}` : ""}${note.location ? `\nLine text:\n${note.location.lineText}\n` : ""}`).join("\n")}` : ""}`
|
||||
};
|
||||
}) : []
|
||||
};
|
||||
}
|
||||
/* istanbul ignore next */
|
||||
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
|
||||
|
||||
async function parcelCssMinify(input, sourceMap, minimizerOptions) {
|
||||
const [[filename, code]] = Object.entries(input);
|
||||
/**
|
||||
* @param {Partial<import("@parcel/css").TransformOptions>} [parcelCssOptions={}]
|
||||
* @returns {import("@parcel/css").TransformOptions}
|
||||
*/
|
||||
|
||||
const buildParcelCssOptions = (parcelCssOptions = {}) => {
|
||||
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
|
||||
return {
|
||||
minify: true,
|
||||
...parcelCssOptions,
|
||||
sourceMap: false,
|
||||
filename,
|
||||
code: Buffer.from(code)
|
||||
};
|
||||
}; // eslint-disable-next-line import/no-extraneous-dependencies, global-require
|
||||
|
||||
|
||||
const parcelCss = require("@parcel/css"); // Copy `esbuild` options
|
||||
|
||||
|
||||
const parcelCssOptions = buildParcelCssOptions(minimizerOptions); // Let `esbuild` generate a SourceMap
|
||||
|
||||
if (sourceMap) {
|
||||
parcelCssOptions.sourceMap = true;
|
||||
}
|
||||
|
||||
const result = await parcelCss.transform(parcelCssOptions);
|
||||
return {
|
||||
code: result.code.toString(),
|
||||
// eslint-disable-next-line no-undefined
|
||||
map: result.map ? JSON.parse(result.map.toString()) : undefined
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
throttleAll,
|
||||
cssnanoMinify,
|
||||
cssoMinify,
|
||||
cleanCssMinify,
|
||||
esbuildMinify,
|
||||
parcelCssMinify
|
||||
};
|
Reference in New Issue
Block a user