UNPKG

html-minifier

Version:

Highly effective, super-configurable, well-tested web page minifier

251 lines (247 loc) • 9.39 kB
/** * Single source of truth for minifier option names, descriptions, types, and shared defaults */ /** * @typedef {object} OptionDefinition * @property {string} description Help text, phrased for the option’s primary CLI form * @property {string} [descriptionAffirmative] Help text for what enabling the option does, where `description` describes the negated form * @property {string} type Key into the parser and JSON Schema type maps in cli.js and scripts/build-schema.js */ /** @type {Record<string, OptionDefinition>} */ const optionDefinitions = { cacheCSS: { description: 'Set CSS minification cache size (number of entries, default: 500)', type: 'int' }, cacheJS: { description: 'Set JavaScript minification cache size (number of entries, default: 500)', type: 'int' }, cacheSVG: { description: 'Set SVG minification cache size (number of entries, default: 500)', type: 'int' }, caseSensitive: { description: 'Keep element and attribute names as written (e.g., for framework components like `<MyButton>`)', type: 'boolean' }, collapseAttributeWhitespace: { description: 'Trim and collapse whitespace characters within attribute values', type: 'boolean' }, collapseBooleanAttributes: { description: 'Omit attribute values from boolean attributes', type: 'boolean' }, collapseEmptyAttributes: { description: 'Omit empty attribute values (e.g., `alt=""` → `alt`)', type: 'boolean' }, collapseInlineTagWhitespace: { description: 'Collapse whitespace more aggressively between inline elements—use with `--collapse-whitespace`', type: 'boolean' }, collapseNoBreakSpaces: { description: 'Remove whitespace next to no-break spaces, narrow no-break spaces, and figure spaces in text (e.g., `a &nbsp; b` → `a&nbsp;b`), keeping runs of them—use with `--collapse-whitespace`', type: 'boolean' }, collapseWhitespace: { description: 'Collapse whitespace that contributes to text nodes in a document tree', type: 'boolean' }, conservativeCollapse: { description: 'Always collapse to one space (never remove it entirely)—use with `--collapse-whitespace`', type: 'boolean' }, continueOnMinifyError: { description: 'Abort on minification errors', descriptionAffirmative: 'Continue on minification errors', type: 'invertedBoolean' }, continueOnParseError: { description: 'Handle parse errors instead of aborting', type: 'boolean' }, customAttrAssign: { description: 'Array of regexes that allow to support custom attribute assign expressions (e.g., `<div flex?="{{mode != cover}}"></div>`)', type: 'regexpArray' }, customAttrCollapse: { description: 'Regex that specifies custom attribute to strip newlines from (e.g., `/ng-class/`)', type: 'regexp' }, customAttrSurround: { description: 'Array of regexes that allow to support custom attribute surround expressions (e.g., `<input {{#if value}}checked="checked"{{/if}}>`)', type: 'regexpArray' }, customEventAttributes: { description: 'Array of regexes that allow to support custom event attributes (e.g., `ng-click`)—use with `--minify-js`', type: 'regexpArray' }, decodeEntities: { description: 'Use direct Unicode characters whenever possible', type: 'boolean' }, ignoreCustomComments: { description: 'Array of regexes that allow to ignore matching comments', type: 'regexpArray' }, ignoreCustomFragments: { description: 'Array of regexes that allow to ignore certain fragments, when matched (e.g., `<?php \u2026 ?>`, `{{ \u2026 }}`, etc.)', type: 'regexpArray' }, includeAutoGeneratedTags: { description: 'Insert elements generated by HTML parser', type: 'boolean' }, inlineCustomElements: { description: 'Array of names of custom elements which are inline, for whitespace handling—use with `--collapse-whitespace`', type: 'jsonArray' }, keepClosingSlash: { description: 'Keep the trailing slash on void elements and other start tags that carry one, and read it as closing the element', type: 'boolean' }, maxInputLength: { description: 'Maximum input length to prevent ReDoS attacks', type: 'int' }, maxLineLength: { description: 'Specify a maximum line length; output will be split by newlines in tags only (between attributes or before a tag’s `>`), where they can’t change the page, so lines may run longer', type: 'int' }, mergeScripts: { description: 'Merge consecutive `script` elements into one', type: 'boolean' }, mergeStyles: { description: 'Merge consecutive `style` elements into one', type: 'boolean' }, minifyCSS: { description: 'Minify CSS in `style` elements and attributes (uses Lightning CSS)', type: 'jsonObject' }, minifyJS: { description: 'Minify JavaScript in `script` elements and event attributes (uses Terser or SWC; pass `{"engine": "swc"}` for SWC)', type: 'jsonObject' }, minifyJSON: { description: 'Minify JSON in `script` elements (e.g., `application/ld+json`, `importmap`, etc.) by removing whitespace', type: 'boolean' }, minifySVG: { description: 'Minify SVG elements (uses SVGO)', type: 'jsonObject' }, minifyURLs: { description: 'Minify URLs in various attributes', type: 'json' }, noNewlinesBeforeTagClose: { description: 'Never split a line in a tag that closes an element—use with `--max-line-length`', type: 'boolean' }, partialMarkup: { description: 'Treat input as a partial HTML fragment, preserving stray end tags and unclosed tags', type: 'boolean' }, preserveLineBreaks: { description: 'Always collapse to one line break (never remove it entirely) when whitespace between tags includes a line break—use with `--collapse-whitespace`', type: 'boolean' }, preventAttributesEscaping: { description: 'Prevents the escaping of the values of attributes', type: 'boolean' }, processScripts: { description: 'Array of strings corresponding to types of `script` elements to process through minifier (e.g., `text/ng-template`, `text/x-handlebars-template`, etc.)', type: 'jsonArray' }, quoteCharacter: { description: 'Type of quote to use for attribute values (`\'` or `"`)', type: 'string' }, removeAttributeQuotes: { description: 'Remove quotes around attributes when possible', type: 'boolean' }, removeComments: { description: 'Strip HTML comments', type: 'boolean' }, removeDefaultTypeAttributes: { description: 'Remove default `type` attributes from `style`/`link` (e.g., `type="text/css"`) and `script` (e.g., `type="text/javascript"`) elements; other `type` attribute values are left intact', type: 'boolean' }, removeEmptyAttributes: { description: 'Remove all attributes with whitespace-only values', type: 'boolean' }, removeEmptyElements: { description: 'Remove all elements with empty contents', type: 'boolean' }, removeEmptyElementsExcept: { description: 'Array of elements to preserve (e.g., `td`, `<i class="test">`, etc.)—use with `--remove-empty-elements`', type: 'jsonArray' }, removeOptionalTags: { description: 'Remove optional tags', type: 'boolean' }, removeRedundantAttributes: { description: 'Remove attributes when value matches default or has no effect (as well as `rel` keywords and `meta` elements without effect)', type: 'boolean' }, removeTagWhitespace: { description: 'Remove space between attributes whenever possible; note that this will result in invalid HTML', type: 'boolean' }, removeUnusedCSS: { description: 'Remove CSS from `style` elements that name an element, class, or ID the document doesn’t reference—use with `--minify-css`, and `{"safelist": […]}` for names only external scripts use', type: 'jsonObject' }, selfCloseForeignElements: { description: 'Self-close empty SVG and MathML elements (e.g., `<path d="…"></path>` → `<path d="…"/>`)', type: 'boolean' }, sortAttributes: { description: 'Sort attributes by frequency', type: 'boolean' }, sortClassNames: { description: 'Sort style classes by frequency', type: 'boolean' }, trimCustomFragments: { description: 'Trim whitespace around custom fragments (`--ignore-custom-fragments`) where it doesn’t affect the output—use with `--collapse-whitespace`', type: 'boolean' }, strictCustomFragments: { description: 'Reject `ignoreCustomFragments` patterns that risk catastrophic backtracking (rather than warning about them)', type: 'boolean' }, // @@ Consider renaming (e.g., `convertDoctype`) with next major release, keeping an alias useShortDoctype: { description: 'Convert any doctype to the short HTML doctype (switches pages with legacy doctypes to standards mode)', type: 'boolean' } }; const optionDefaults = { continueOnMinifyError: true, ignoreCustomComments: [ /^!/, /^\s*#/ ], ignoreCustomFragments: [ /<%[\s\S]*?%>/, // Template code and XML declarations, not processing instructions HTML reads itself /<\?(?:php[\t\n\r ]|=|\$|xml(?:-stylesheet)?(?![\w-])|\s)[\s\S]*?\?>/i ], includeAutoGeneratedTags: false }; export { optionDefinitions, optionDefaults };