rollup-plugin-minify-html-literals
raw JSON → 1.2.6 verified Mon Apr 27 auth: no javascript
Rollup plugin that minifies HTML and CSS content inside JavaScript template literal strings. Version 1.2.6 is current and stable. It leverages the minify-html-literals library to process tagged templates (e.g., lit-html) and untagged templates (e.g., Polymer). Key differentiators: seamless Rollup integration, support for include/exclude patterns, fail-on-error mode, and the ability to customize the minification logic. Typical use cases: reducing bundle size for Web Component projects using lit-html or Polymer.
Common errors
error Error: Cannot find module 'rollup-plugin-minify-html-literals' ↓
cause Package not installed in node_modules.
fix
Run 'npm install rollup-plugin-minify-html-literals --save-dev'
error TypeError: (0 , _rollupPluginMinifyHtmlLiterals.default) is not a function ↓
cause Using CommonJS require() instead of ES import.
fix
Use 'import minifyHTML from "rollup-plugin-minify-html-literals"'
error Error: rollup-plugin-minify-html-literals: Must be installed with rollup ^0.65.2 || ^1.0.0 || ^2.0.0 ↓
cause Incompatible Rollup version (e.g., Rollup 3 or 4).
fix
Downgrade to Rollup 2.x or find an alternative plugin.
Warnings
breaking Rollup peer dependency was ^0.65.2 in v1.0.0, then updated to support ^2.0.0 in later 1.x releases. Plugin may not work with Rollup 3 or 4. ↓
fix Use Rollup 2 or lower, or switch to an alternative like @rollup/plugin-minify-html-literals.
gotcha The plugin must be placed before transpilers (Babel) and other minifiers (Uglify) in the plugins array, or template literals may be transformed before minification. ↓
fix Order plugins: minifyHTML first, then babel, then uglify.
gotcha By default, only tagged templates containing 'html' or 'css' (case insensitive) are minified. Untagged or differently tagged templates (e.g., Polymer) require custom shouldMinify option. ↓
fix Provide a shouldMinify function in options to match additional templates.
deprecated rollup-plugin-babel is deprecated; use @rollup/plugin-babel instead. ↓
fix Replace 'rollup-plugin-babel' with '@rollup/plugin-babel'.
Install
npm install rollup-plugin-minify-html-literals yarn add rollup-plugin-minify-html-literals pnpm add rollup-plugin-minify-html-literals Imports
- default wrong
const minifyHTML = require('rollup-plugin-minify-html-literals')correctimport minifyHTML from 'rollup-plugin-minify-html-literals' - default wrong
import { minifyHTML } from 'rollup-plugin-minify-html-literals'correctconst minifyHTML = require('rollup-plugin-minify-html-literals') - type definitions
import type { Options } from 'rollup-plugin-minify-html-literals'
Quickstart
import minifyHTML from 'rollup-plugin-minify-html-literals';
export default {
input: 'src/index.js',
output: { file: 'dist/bundle.js', format: 'iife' },
plugins: [
minifyHTML({
include: 'src/**/*.js',
options: {
shouldMinify: (template) => {
return template.parts.some(p => p.text.includes('<style') || p.text.includes('<dom-module'));
}
}
})
]
};