html-minifier
Version:
Highly effective, super-configurable, well-tested web page minifier
79 lines • 3.52 kB
TypeScript
/**
* Custom fragment matching, and checks that minified CSS and JavaScript keep fragments intact
*
* `ignoreCustomFragments` patterns nearly always describe the same shape: a literal
* opening delimiter, an any-character or negated-class body, and a literal closing
* delimiter, as in `<%[\s\S]*?%>` or `\{\{[^}]*?\}\}`. Such a fragment can be found
* with `indexOf` in linear time, where running the pattern as a regex costs O(n²)
* on input that opens fragments it never closes—and can cost far more than that when
* the pattern itself backtracks. Patterns of other shapes keep running as regexes,
* one per pattern, so each keeps its own flags.
*/
export type DelimitedFragment = {
open: string;
close: string;
min: number;
max: number;
excluded: RegExp | null;
};
export type PatternFragment = {
search: RegExp;
anchored: RegExp;
close: string | null;
};
/**
* Describe a fragment pattern as literal delimiters around an any-character body
* @param {RegExp} pattern
* @returns {DelimitedFragment | null} Null when the pattern has another shape, which
* the caller answers by running it as a regex
*/
declare function toDelimitedFragment(pattern: RegExp): DelimitedFragment | null;
/**
* Prepare a fragment pattern for matching, by scanning where the shape allows it
* @param {RegExp} pattern
* @returns {DelimitedFragment | PatternFragment}
*/
declare function toFragment(pattern: RegExp): DelimitedFragment | PatternFragment;
/**
* Replace runs of custom fragments, and the whitespace padding them
* @param {string} value - Document to scan
* @param {(DelimitedFragment | PatternFragment)[]} fragments - In the order the patterns
* were given, since the earliest match wins and ties go to the pattern listed first
* @param {(match: string) => string} replacer - Called with each match, as `replace` would
* @returns {string}
*/
declare function replaceCustomFragments(value: string, fragments: (DelimitedFragment | PatternFragment)[], replacer: (match: string) => string): string;
export type Span = {
start: number;
end: number;
context: string;
};
export type Placement = {
text: string;
index: number;
context: string;
padding: string;
};
/**
* Whether minified CSS can stand in for its source; a fragment in an at-rule Lightning CSS
* cannot read would go with that at-rule
* @param {string} source
* @param {unknown} output - Anything but a string, from a minifier of one’s own, passes
* @param {RegExp} placeholder - Global pattern matching one fragment placeholder
* @returns {boolean}
*/
declare function isMinifiedCSSSafe(source: string, output: unknown, placeholder: RegExp): boolean;
/**
* Whether minified JavaScript can stand in for its source; code that doesn’t parse with
* its fragments in it stays as written, as does code whose output Acorn cannot read where
* it has to
* @param {string} source
* @param {unknown} output - Anything but a string, from a minifier of one’s own, passes
* @param {RegExp} placeholder - Global pattern matching one fragment placeholder
* @param {boolean} [inline] - Whether the code is an event handler
* @param {boolean} [isModule]
* @returns {Promise<boolean>}
*/
declare function isMinifiedJSSafe(source: string, output: unknown, placeholder: RegExp, inline?: boolean, isModule?: boolean): Promise<boolean>;
export { toDelimitedFragment, toFragment, replaceCustomFragments, isMinifiedCSSSafe, isMinifiedJSSafe };
//# sourceMappingURL=fragments.d.ts.map