Implement CLI wrapper for Node.js; fix Node.js TS defs

This commit is contained in:
Wilson Lin 2021-10-23 17:42:26 +11:00
parent 05e22f958f
commit ee9fc50741
4 changed files with 39 additions and 2 deletions

View File

@ -1,5 +1,10 @@
# minify-html changelog
## 0.6.10
- Fixed the Node.js library TypeScript definitions. `minifyJs` has been fixed to `minify_js` and `minifyCss` has been fixed to `minify_css`. This is not a breaking change the library itself only ever accepted the fixed names, so this is actually a typo fix.
- Implemented a basic CLI script for Node.js to allow using the library from the command line e.g. quick testing or sandboxing without needing to download and install the CLI separately. It accepts all configuration properties (all of which are currently booleans) using hyphen case e.g. `--do-not-minify-doctype`, as well as `--output [path]` and one default (i.e. not after an option switch) argument for the path to the input. It's only a few lines long and should not have a tangible effect on library size.
## 0.6.9
- Intrepret `type=module` on `<script>` tags as a JavaScript MIME eligible for its contents to be minified as JavaScript (previously it would not be and so its contents would be considered data and never minified as JavaScript).

26
nodejs/cli.js Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env node
const lib = require(".");
const fs = require("fs");
const cfgObj = {};
let input;
let output;
const args = process.argv.slice(2);
let arg;
while ((arg = args.shift()) !== undefined) {
if (arg.slice(0, 2) === "--") {
const name = arg.slice(2);
if (name === "output") {
output = args.shift();
} else {
cfgObj[name.replace(/-/g, "_")] = true;
}
} else {
input = arg;
}
}
const cfg = lib.createConfiguration(cfgObj);
const min = lib.minify(fs.readFileSync(input), cfg);
fs.writeFileSync(output, min);

4
nodejs/index.d.ts vendored
View File

@ -23,11 +23,11 @@ export function createConfiguration (options: {
/**
* 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 [esbuild-rs](https://github.com/wilsonzlin/esbuild-rs).
*/
minifyJs?: boolean;
minify_js?: boolean;
/**
* If enabled, CSS in `<style>` tags will be minified using [esbuild-rs](https://github.com/wilsonzlin/esbuild-rs).
*/
minifyCss?: boolean;
minify_css?: boolean;
/** Remove all bangs. */
remove_bangs?: boolean;
/** Remove all processing_instructions. */

View File

@ -40,10 +40,16 @@ const specifics = {
core: {
name: "@minify-html/core",
description: "Extremely fast and smart HTML minifier",
bin: {
"minify-html-core": "./cli.js",
},
},
js: {
name: "@minify-html/js",
description: "Extremely fast and smart HTML + JS + CSS minifier",
bin: {
"minify-html": "./cli.js",
},
},
}[process.argv[2]];