esbuild-minify-templates

raw JSON →
0.13.1 verified Mon Apr 27 auth: no javascript

A plugin for esbuild that minifies template literal strings by collapsing whitespace and removing whitespace around HTML tags. Version 0.13.1 is current; it supports node >=12 and requires esbuild >=0.13.0 <1.0.0 as a peer dependency. Features include source map support, the ability to ignore specific template literals with a comment, and options like `keepComments` (preserve HTML comments) and `taggedOnly` (minify only tagged templates). It ships TypeScript types. Unlike other minifiers, it works as two esbuild plugins that require the `write` option to be `false`, writing output files via a separate `writeFiles()` plugin.

error Error: Cannot find module 'esbuild-minify-templates'
cause CommonJS require used in an ESM-only package.
fix
Use import syntax: import { minifyTemplates } from 'esbuild-minify-templates';
error TypeError: writeFiles is not a function
cause Default import instead of named import.
fix
Use named import: import { writeFiles } from 'esbuild-minify-templates';
error Error: Build failed with 1 error: error: Cannot access 'write' before initialization
cause write option not set to false before build.
fix
Set the esbuild build option 'write' to false.
breaking The write option must be false to use esbuild-minify-templates.
fix Set esbuild build option 'write' to false.
gotcha writeFiles() must be included and placed after plugins that modify output.
fix Add writeFiles() to plugins list after minifyTemplates() and any other output-modifying plugins.
deprecated This package is ESM-only; CommonJS require() will not work.
fix Use import syntax or switch to ES modules in your project.
gotcha Ignoring a template literal requires a /*! minify-templates-ignore */ comment on the line directly before it; esbuild's minify and legalComments options must be set appropriately.
fix Set esbuild 'minify' to false and 'legalComments' to 'inline' for the ignore comment to work.
npm install esbuild-minify-templates
yarn add esbuild-minify-templates
pnpm add esbuild-minify-templates

Demonstrates the essential setup: import the two plugins, add them to the esbuild plugins array, and set write to false.

import esbuild from 'esbuild';
import { minifyTemplates, writeFiles } from 'esbuild-minify-templates';

await esbuild.build({
  entryPoints: ['src/index.ts'],
  outfile: 'dist/index.js',
  plugins: [minifyTemplates(), writeFiles()],
  bundle: true,
  sourcemap: true,
  write: false, // important!
});