hast-util-minify-whitespace: HTML Whitespace Minification
hast-util-minify-whitespace is a focused utility within the unified ecosystem, specifically designed to optimize HTML by minifying whitespace between elements in a `hast` (HTML Abstract Syntax Tree). It reduces multiple consecutive whitespace characters to a single space, or to a newline if configured, thereby improving the size of HTML fragments. The package is currently at version 1.0.1, with releases coordinated within the larger `rehypejs` collective, which emphasizes modern JavaScript practices. Key differentiators include its robust integration into the pluggable unified processing pipeline, full TypeScript support, and its commitment to WHATWG HTML parsing standards. It operates as an ESM-only module and requires Node.js 16 or newer, aligning with contemporary Node.js environments.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module ... not supported.
cause Attempting to use `require()` to import an ESM-only package.fixChange `const minifyWhitespace = require('hast-util-minify-whitespace')` to `import { minifyWhitespace } from 'hast-util-minify-whitespace'` and ensure your project is configured for ESM. -
minifyWhitespace is not a function
cause Trying to access `minifyWhitespace` as a property of the module object when using `require()` on an ESM-only package, or incorrectly destructuring named exports.fixConfirm you are using `import { minifyWhitespace } from 'hast-util-minify-whitespace'` and that your environment supports ESM. -
ReferenceError: h is not defined
cause The `h` function from `hastscript` was used in an example or your code without being installed or imported.fixInstall `hastscript` (`npm install hastscript`) and ensure you `import { h } from 'hastscript'` in your file.
Warnings
- breaking This package, like all packages in the `rehypejs` monorepo, now requires Node.js 16 or newer. Older Node.js versions are no longer supported.
- breaking This package is ESM-only. Attempting to import it using CommonJS `require()` will result in errors.
- breaking The `rehypejs` monorepo, which this package is part of, now uses the `exports` field in `package.json`. This may affect how bundlers and older Node.js versions resolve imports.
- gotcha Minifying whitespace within a `hast` tree does not inherently protect against Cross-Site Scripting (XSS) vulnerabilities. If the processed HTML is rendered without further sanitization, it could still be unsafe.
Install
-
npm install hast-util-minify-whitespace -
yarn add hast-util-minify-whitespace -
pnpm add hast-util-minify-whitespace
Imports
- minifyWhitespace
const minifyWhitespace = require('hast-util-minify-whitespace')import { minifyWhitespace } from 'hast-util-minify-whitespace' - h
const h = require('hastscript')import { h } from 'hastscript' - Options
import type { Options } from 'hast-util-minify-whitespace'
Quickstart
import { h } from 'hastscript';
import { minifyWhitespace } from 'hast-util-minify-whitespace';
const tree = h('p', [
' ',
h('strong', 'foo'),
' ',
h('em', 'bar'),
' ',
h('meta', { itemProp: true }),
' '
]);
console.log('Original tree:\n', JSON.stringify(tree, null, 2));
minifyWhitespace(tree);
console.log('\nMinified tree:\n', JSON.stringify(tree, null, 2));