cssnano
Version:
A modular CSS minifier, built on top of the PostCSS ecosystem.
64 lines (56 loc) • 1.93 kB
JavaScript
import postcssDiscardComments from '#postcss-discard-comments';
import postcssNormalizeWhitespace from '#postcss-normalize-whitespace';
import postcssDiscardEmpty from '#postcss-discard-empty';
import cssnanoUtils from '#utils';
const { rawCache } = cssnanoUtils;
/**
* @template {object | void} [OptionsExtends=void]
* @typedef {false | OptionsExtends & {exclude?: true}} SimpleOptions
*/
/**
* @typedef {object} LiteOptions
* @property {SimpleOptions<import('#postcss-discard-comments').Options>} [discardComments]
* @property {SimpleOptions} [normalizeWhitespace]
* @property {SimpleOptions} [discardEmpty]
* @property {SimpleOptions} [rawCache]
*/
/**
* @param {[import('postcss').PluginCreator<any>, keyof LiteOptions][]} plugins
* @param {Parameters<typeof litePreset>[0]} opts
* @returns {ReturnType<typeof litePreset>["plugins"]}
*/
function configurePlugins(plugins, opts = {}) {
const defaults = /** @type {LiteOptions} */ ({});
// Merge option properties for each plugin
return plugins.map(([plugin, opt]) => {
const defaultProps = defaults[opt] ?? {};
const presetProps = opts[opt] ?? {};
return [
plugin,
presetProps !== false
? { ...defaultProps, ...presetProps }
: { exclude: true },
];
});
}
/**
* Safe and minimum transformation with just removing whitespaces, line breaks and comments
*
* @param {LiteOptions} opts
* @returns {{ plugins: [import('postcss').PluginCreator<any>, LiteOptions[keyof LiteOptions]][] }}
*/
function litePreset(opts = {}) {
return {
plugins: configurePlugins(
[
[postcssDiscardComments, 'discardComments'],
[postcssNormalizeWhitespace, 'normalizeWhitespace'],
[postcssDiscardEmpty, 'discardEmpty'],
[rawCache, 'rawCache'],
],
opts
),
};
}
const moduleExports = litePreset;
export { moduleExports as default, moduleExports as 'module.exports' };