Search


Search something to see results

MinifyOptions

Minify options

interface MinifyOptions {
    computeCalcExpression?: boolean;
    computeShorthand?: boolean;
    computeTransform?: boolean;
    evaluateMixins?: boolean;
    fastMode?: boolean;
    inlineCssVariables?: boolean;
    keepMixins?: boolean;
    mergeRules?: boolean | MergeRulesEnum;
    minify?: boolean;
    minifyAST?: boolean;
    nestingRules?: boolean;
    optimizeSelectors?: boolean;
    parseColor?: boolean;
    pass?: number;
    removeDuplicateDeclarations?: string | boolean | string[];
    removeEmpty?: boolean;
    removePrefix?: boolean;
}

Hierarchy (View Summary)

Index

Properties

computeCalcExpression?: boolean

Compute css math functions

computeShorthand?: boolean

Compute shorthand properties

computeTransform?: boolean

Compute css transform functions

evaluateMixins?: boolean

Evaluate css mixins and custom functions: the @apply rules are replaced by the body of the mixin they reference, the private variables (@private, mixin arguments and the custom properties of the rule) are substituted. the custom function calls (--fn(1px)) are replaced by the result of the function. see https://drafts.csswg.org/css-mixins-1/


import {transform} from '@tbela99/css-parser';

const css = `
@mixin --only-descs(--arg) {
color: var(--arg);
& * { color: var(--arg, red); }
}
.foo {
@apply --only-descs(green);
}
`;

const result = await transform(css, {evaluateMixins: true, expandNestingRules: true});

console.log(result.code);
// .foo,.foo *{color:green}
fastMode?: boolean

Fast mode: the ast is not minified and the rules are not merged, the values are minified when they are rendered. The output is larger and the minification is faster. It sets the default value of these options, an option given explicitly takes precedence:

{
minify: false,
mergeRules: MergeRulesEnum.None,
}
import {transformSync} from '@tbela99/css-parser';

const result = transformSync(css, {fastMode: true});
inlineCssVariables?: boolean

Inline css variables

keepMixins?: boolean

Keep the mixins and the custom functions once they are evaluated, see evaluateMixins. By default, the evaluated mixins and custom functions, and the mixins that are not referenced, are dropped and replaced with a comment

mergeRules?: boolean | MergeRulesEnum

Merge the adjacent rules (MergeRulesEnum, true is a shortcut for MergeRulesEnum.All, false for MergeRulesEnum.None).

  • MergeRulesEnum.All: the rules that share declarations are merged when the output is smaller
  • MergeRulesEnum.Identical: only the rules that have the same declarations are merged, the size of the output is not computed: .a{color:red}.b{color:red} is merged into .a,.b{color:red}
  • MergeRulesEnum.None: the rules that have different selectors are not merged

The default is MergeRulesEnum.All when the ast is minified, MergeRulesEnum.None otherwise. With minify: false, MergeRulesEnum.Identical and MergeRulesEnum.All merge the rules without minifying the ast: the adjacent rules and keyframes rules that have the same selector and the adjacent at-rules that have the same prelude are merged (@font-face is not merged), the last @keyframes of a name is kept, the rules that have different selectors are merged as above. The other minifications are not applied.

import {transformSync, MergeRulesEnum} from '@tbela99/css-parser';

// the adjacent rules that have the same declarations are merged, the ast is not minified
const result = transformSync(css, {minify: false, mergeRules: MergeRulesEnum.Identical});
minify?: boolean

Enable minification

minifyAST?: boolean

enable ast minification

nestingRules?: boolean

Generate nested rules

optimizeSelectors?: boolean

Optimize the selectors (default true): remove the redundant selectors, factor the selectors with :is(). false only applies when nestingRules is false: the nesting of the rules reads the optimized selectors.

parseColor?: boolean

Parse color tokens

pass?: number

Define minification passes.

removeDuplicateDeclarations?: string | boolean | string[]

Remove duplicate declarations from the same rule. if passed as a string array, duplicated declarations are removed, except for those in the array


import {transform} from '@tbela99/css-parser';

const css = `

.table {

width: 100%;
width: calc(100% + 40px);
margin-left: 20px;
margin-left: min(100% , 20px)
}

`;
const result = await transform(css, {

beautify: true,
validation: true,
removeDuplicateDeclarations: ['width']
}
);

console.log(result.code);
removeEmpty?: boolean

Remove empty ast nodes

removePrefix?: boolean

Remove css prefix