Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rollup-plugin-html] Expose html-minifier-terser options #2707

Open
JulianCataldo opened this issue Apr 20, 2024 · 1 comment
Open

[rollup-plugin-html] Expose html-minifier-terser options #2707

JulianCataldo opened this issue Apr 20, 2024 · 1 comment

Comments

@JulianCataldo
Copy link
Contributor

Hello,

I'm willing to open a PR for allowing finer control over HTML minification with @web/rollup-plugin-html.

Primary motivation for this feature, in my case, is preserving <!--lit-part EeTSS7FUtCM=--> comments in Declarative Shadow DOM (from Lit SSR output), thanks to the ignoreCustomComments matcher option.

But I'm sure some other peeps will find benefits by exposing the minifier API, versus a simple ON/OFF toggle.


Change:

if (pluginOptions.minify) {
  outputHtml = await minifyHTMLFunc(
    outputHtml,
    pluginOptions.minifyOptions
      ? pluginOptions.minifyOptions
      : {
          collapseWhitespace: true,
          removeComments: true,
          removeRedundantAttributes: true,
          removeScriptTypeAttributes: true,
          removeStyleLinkTypeAttributes: true,
          useShortDoctype: true,
          minifyCSS: true,
          minifyJS: true,
         // Or maybe with a spread here? Like `...pluginOptions.minifyOptions`
        },
  );
}

in:

if (pluginOptions.minify) {
outputHtml = await minifyHTMLFunc(outputHtml, {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyCSS: true,
minifyJS: true,
});
}

Change:

import type { Options as HTMLMinifierOptions } from 'html-minifier-terser';


  /** Whether to minify the output HTML. */
  minify?: boolean;
  /** Minification options for HTML Minifier. */
  minifyOptions?: HTMLMinifierOptions;

Association types from @types/html-minifier-terser.

in:

export interface RollupPluginHTMLOptions {
/** HTML file(s) to use as input. If not set, uses rollup input option. */
input?: string | InputHTMLOptions | (string | InputHTMLOptions)[];
/** HTML file glob pattern or patterns to ignore */
exclude?: string | string[];
/** Whether to minify the output HTML. */
minify?: boolean;
/** Whether to preserve or flatten the directory structure of the HTML file. */
flattenOutput?: boolean;
/** Directory to resolve absolute paths relative to, and to use as base for non-flatted filename output. */
rootDir?: string;
/** Path to load modules and assets from at runtime. */
publicPath?: string;
/** Transform asset source before output. */
transformAsset?: TransformAssetFunction | TransformAssetFunction[];
/** Transform HTML file before output. */
transformHtml?: TransformHtmlFunction | TransformHtmlFunction[];
/** Whether to extract and bundle assets referenced in HTML. Defaults to true. */
extractAssets?: boolean;
/** Whether to ignore assets referenced in HTML and CSS with glob patterns. */
externalAssets?: string | string[];
/** Define a full absolute url to your site (e.g. https://domain.com) */
absoluteBaseUrl?: string;
/** Whether to set full absolute urls for ['meta[property=og:image]', 'link[rel=canonical]', 'meta[property=og:url]'] or not. Requires a absoluteBaseUrl to be set. Default to true. */
absoluteSocialMediaUrls?: boolean;
/** Should a service worker registration script be injected. Defaults to false. */
injectServiceWorker?: boolean;
/** File system path to the generated service worker file */
serviceWorkerPath?: string;
/** Prefix to strip from absolute paths when resolving assets and scripts, for example when using a base path that does not exist on disk. */
absolutePathPrefix?: string;
/** When set to true, will insert meta tags for CSP and add script-src values for inline scripts by sha256-hashing the contents */
strictCSPInlineScripts?: boolean;
/** Bundle assets reference from CSS via `url` */
bundleAssetsFromCss?: boolean;
}


What do you think?

Thank you.

@bashmish
Copy link
Member

I'd rather expose a way to override the minify function with a simple interface, seems like it fits well with the current design of the plugin. Can even be the same option set to a callback instead of a boolean:

pluginOptions.minify = (html) => {
  const minifiedHtml = /* process html */
  return minifiedHtml;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants