wilsonzlin / minify-html

Extremely fast and smart HTML + JS + CSS minifier, available for Rust, Deno, Java, Node.js, Python, Ruby, and WASM
MIT License
848 stars 36 forks source link

Feature request: Seperate exported type interface for cfg #97

Open jrson83 opened 2 years ago

jrson83 commented 2 years ago

I'm using the deno package and want to create a plugin for Lume static site builder.

It would be really great if there was a seperate export of the type interface for the cfg options, which is currently defined as parameter for the minify function.

Because there is no type export available and the index.js exported function cfg is type any, IDE type checking and auto-suggestions do not work.

Partial<typeof minify>

Or even worth:

Parameters<typeof minify>[1];

Would you consider to change the index.d.ts to export an interface CFG and also make it available on deno?

export interface CFG {
  /** Do not minify DOCTYPEs. Minified DOCTYPEs may not be spec compliant. */
  do_not_minify_doctype?: boolean;
  /** Ensure all unquoted attribute values in the output do not contain any characters prohibited by the WHATWG specification. */
  ensure_spec_compliant_unquoted_attribute_values?: boolean;
  /** Do not omit closing tags when possible. */
  keep_closing_tags?: boolean;
  /** Do not omit `<html>` and `<head>` opening tags when they don't have attributes. */
  keep_html_and_head_opening_tags?: boolean;
  /** Keep spaces between attributes when possible to conform to HTML standards. */
  keep_spaces_between_attributes?: boolean;
  /** Keep all comments. */
  keep_comments?: boolean;
  /**
   * If enabled, content in `<script>` tags with a JS or no [MIME type](https://mimesniff.spec.whatwg.org/#javascript-mime-type) will be minified using [minify-js](https://github.com/wilsonzlin/minify-js).
   */
  minify_js?: boolean;
  /**
   * If enabled, CSS in `<style>` tags and `style` attributes will be minified.
   */
  minify_css?: boolean;
  /** Remove all bangs. */
  remove_bangs?: boolean;
  /** Remove all processing_instructions. */
  remove_processing_instructions?: boolean;
}

export function minify(src: Buffer, cfg: CFG): Buffer;

Update:

The Lume plugin is commited, you can check in the experimental-plugins repo.

wilsonzlin commented 2 years ago

Yes the WASM library is currently missing the type declarations. Is copying the existing type declarations from the Node.js library to index.d.ts sufficient? I'm not sure how Deno detects type declarations.

jrson83 commented 2 years ago

@wilsonzlin thanks for replying. I have tested this locally. The index.d.ts for Deno should include the following code:

// See comment below
/** import type { Buffer } from "https://deno.land/std/node/buffer.ts"; */

export interface CFG {
  /** Do not minify DOCTYPEs. Minified DOCTYPEs may not be spec compliant. */
  do_not_minify_doctype?: boolean;
  /** Ensure all unquoted attribute values in the output do not contain any characters prohibited by the WHATWG specification. */
  ensure_spec_compliant_unquoted_attribute_values?: boolean;
  /** Do not omit closing tags when possible. */
  keep_closing_tags?: boolean;
  /** Do not omit `<html>` and `<head>` opening tags when they don't have attributes. */
  keep_html_and_head_opening_tags?: boolean;
  /** Keep spaces between attributes when possible to conform to HTML standards. */
  keep_spaces_between_attributes?: boolean;
  /** Keep all comments. */
  keep_comments?: boolean;
  /**
   * If enabled, content in `<script>` tags with a JS or no [MIME type](https://mimesniff.spec.whatwg.org/#javascript-mime-type) will be minified using [minify-js](https://github.com/wilsonzlin/minify-js).
   */
  minify_js?: boolean;
  /**
   * If enabled, CSS in `<style>` tags and `style` attributes will be minified.
   */
  minify_css?: boolean;
  /** Remove all bangs. */
  remove_bangs?: boolean;
  /** Remove all processing_instructions. */
  remove_processing_instructions?: boolean;
}

// Maybe we do not export the function at all, only the `CFG` interface, so we don't need to import/export `Buffer` at all
/** export function minify(src: Buffer, cfg: CFG): Buffer; */

Then inside your index.js, the index.d.ts file should be included using the triple-slash reference directive:

/// <reference types="./index.d.ts" />

It should be possible to re-export the type inside my plugins deps.ts file using (local path example):

export { default as init, minify } from "./minify-html/index.js";
export type { CFG } from "./minify-html/index.js"

Then inside my plugins mod.ts:

import { init, minify as minifyHTML } from "./deps.ts";
import type { CFG } from "./deps.ts";

...

page.content = decoder.decode(
  minifyHTML(encoder.encode(content), options.options),
);

It would be awesome if you have time to implement this. I would create a PR, but I never used Rust before.

Edit: Please see updated code

jrson83 commented 2 years ago

If the d.ts file needs just to be created I could do a PR?