{"id":11009,"library":"hast-util-minify-whitespace","title":"hast-util-minify-whitespace: HTML Whitespace Minification","description":"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.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/rehypejs/rehype-minify#main","tags":["javascript","hast","hast-util","html","minify","unist","util","utility","whitespace","typescript"],"install":[{"cmd":"npm install hast-util-minify-whitespace","lang":"bash","label":"npm"},{"cmd":"yarn add hast-util-minify-whitespace","lang":"bash","label":"yarn"},{"cmd":"pnpm add hast-util-minify-whitespace","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is ESM-only since v1.0.0; CommonJS `require()` is not supported.","wrong":"const minifyWhitespace = require('hast-util-minify-whitespace')","symbol":"minifyWhitespace","correct":"import { minifyWhitespace } from 'hast-util-minify-whitespace'"},{"note":"While not part of this package, `hastscript` is commonly used to create `hast` nodes and is often a companion dependency for testing or examples.","wrong":"const h = require('hastscript')","symbol":"h","correct":"import { h } from 'hastscript'"},{"note":"Import the TypeScript type definition for configuration options.","symbol":"Options","correct":"import type { Options } from 'hast-util-minify-whitespace'"}],"quickstart":{"code":"import { h } from 'hastscript';\nimport { minifyWhitespace } from 'hast-util-minify-whitespace';\n\nconst tree = h('p', [\n  '  ',\n  h('strong', 'foo'),\n  '  ',\n  h('em', 'bar'),\n  '  ',\n  h('meta', { itemProp: true }),\n  '  '\n]);\n\nconsole.log('Original tree:\\n', JSON.stringify(tree, null, 2));\n\nminifyWhitespace(tree);\n\nconsole.log('\\nMinified tree:\\n', JSON.stringify(tree, null, 2));","lang":"typescript","description":"Demonstrates how to import and use `minifyWhitespace` to process a `hast` tree created with `hastscript`, showing the transformation of internal whitespace."},"warnings":[{"fix":"Upgrade your Node.js environment to version 16 or later.","message":"This package, like all packages in the `rehypejs` monorepo, now requires Node.js 16 or newer. Older Node.js versions are no longer supported.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure your project uses ES modules (`import`) and configure your `package.json` with `\"type\": \"module\"` or use `.mjs` extensions for files importing this package.","message":"This package is ESM-only. Attempting to import it using CommonJS `require()` will result in errors.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure your build tooling and Node.js environment are up to date and correctly configured to handle the `exports` field. Avoid relying on undocumented subpath imports.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Always use `rehype-sanitize` or a similar sanitization utility on your `hast` tree before serializing it to HTML, especially if the input HTML is untrusted.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change `const minifyWhitespace = require('hast-util-minify-whitespace')` to `import { minifyWhitespace } from 'hast-util-minify-whitespace'` and ensure your project is configured for ESM.","cause":"Attempting to use `require()` to import an ESM-only package.","error":"ERR_REQUIRE_ESM: require() of ES Module ... not supported."},{"fix":"Confirm you are using `import { minifyWhitespace } from 'hast-util-minify-whitespace'` and that your environment supports ESM.","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.","error":"minifyWhitespace is not a function"},{"fix":"Install `hastscript` (`npm install hastscript`) and ensure you `import { h } from 'hastscript'` in your file.","cause":"The `h` function from `hastscript` was used in an example or your code without being installed or imported.","error":"ReferenceError: h is not defined"}],"ecosystem":"npm"}