Skip to content

Commit

Permalink
Major bump to use ES Modules
Browse files Browse the repository at this point in the history
  • Loading branch information
kristian committed Jul 19, 2023
1 parent 24ee04e commit f410a25
Show file tree
Hide file tree
Showing 9 changed files with 2,569 additions and 2,711 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [12.x]
node-version: [18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules/
.nyc_output
coverage
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ npm install minify-xml -g
## Usage

```js
const minifyXML = require("minify-xml").minify;
import minifyXML from "minify-xml";

const xml = `<Tag xmlns:used = "used_ns" xmlns:unused = "unused_ns">
<!--
Expand Down Expand Up @@ -56,7 +56,7 @@ This outputs the minified XML:
Alternatively a [Node.js `Transform` stream](https://nodejs.org/api/stream.html#stream_class_stream_transform) can be provided to minify XML streams, which is especially helpful for very large files (> 2 GiB, which is the maximum `Buffer` size in Node.js on 64-bit machines):

```js
const minifyXMLStream = require("minify-xml").minifyStream;
import { minifyStream as minifyXMLStream } from "minify-xml";

fs.createReadStream("sitemap.xml", "utf8")
.pipe(minifyXMLStream())
Expand All @@ -68,8 +68,9 @@ fs.createReadStream("sitemap.xml", "utf8")
You may pass in the following options when calling minify:

```js
require("minify-xml").minify(`<tag/>`, { ... });
require("minify-xml").minifyStream({ ... });
import { minify as minifyXML, minifyStream as minifyXMLStream } from "minify-xml";
minifyXML(`<tag/>`, { ... });
minifyXMLStream({ ... });
```

- `removeComments` (default: `true`): Remove comments like `<!-- ... -->`.
Expand Down
31 changes: 17 additions & 14 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!/usr/bin/env node

const fs = require("fs");
const path = require("path");
const { constants: { MAX_STRING_LENGTH } } = require("buffer");
import fs from "node:fs";
import path from "node:path";
import constants from "buffer";
const { MAX_STRING_LENGTH } = constants;

const meow = require("meow");
const camelCase = require("camelcase");
const ProgressBar = require("progress");
const { minify, defaultOptions, minifyStream, defaultStreamOptions, debug: debugMinify } = require("./");
import meow from "meow";
import { default as camelCase } from "camelcase";
import ProgressBar from "progress";

import { default as minify, defaultOptions, minifyStream, defaultStreamOptions, debug as debugMinify } from "./index.js";

const cli = meow(`
Usage
Expand Down Expand Up @@ -44,20 +46,20 @@ const cli = meow(`
flags: {
stream: {
type: "boolean",
alias: "s"
shortFlag: "s"
},
output: {
type: "string",
alias: "o"
shortFlag: "o"
},
inPlace: {
type: "boolean",
alias: "i",
shortFlag: "i",
},

streamMaxMatchLength: {
type: "number",
alias: "streamMaximumMatchLength",
shortFlag: "streamMaximumMatchLength",
default: 256 * 1024 // 256 KiB
},

Expand Down Expand Up @@ -87,7 +89,7 @@ const cli = meow(`
},
collapseWhitespaceInDocType: {
type: "boolean",
alias: "collapse-whitespace-in-doctype"
shortFlag: "collapse-whitespace-in-doctype"
},
removeUnusedNamespaces: {
type: "boolean"
Expand All @@ -100,7 +102,7 @@ const cli = meow(`
},
ignoreCData: {
type: "boolean",
alias: "ignore-cdata"
shortFlag: "ignore-cdata"
},

debug: {
Expand All @@ -109,7 +111,8 @@ const cli = meow(`
}
},
booleanDefault: undefined,
allowUnknownFlags: false
allowUnknownFlags: false,
importMeta: import.meta
});

const input = cli.input[0], debug = cli.flags.debug.length && !cli.flags.debug.includes("false");
Expand Down
37 changes: 18 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict"; // we are overriding arguments, so this is important!

const strict = "strict", defaultOptions = module.exports.defaultOptions = {
const strict = "strict", strictOption = option => option === strict && { strict: true };
export const defaultOptions = {
removeComments: true,
removeWhitespaceBetweenTags: true, // true / false or 'strict' (will not consider prolog / doctype, as tags)
considerPreserveWhitespace: true,
Expand All @@ -14,7 +13,7 @@ const strict = "strict", defaultOptions = module.exports.defaultOptions = {
removeUnusedDefaultNamespace: true,
shortenNamespaces: true,
ignoreCData: true
}, strictOption = option => option === strict && { strict: true };
};

function trim(string) {
return string.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, String());
Expand Down Expand Up @@ -92,7 +91,7 @@ function ignoreCData(replacement) {
};
}

module.exports.minify = function(xml, options) {
export function minify(xml, options) {
// apply the default options
options = {
...defaultOptions,
Expand Down Expand Up @@ -239,20 +238,20 @@ module.exports.minify = function(xml, options) {
}

return xml.trim ? xml.trim() : trim(xml);
};
}; export default minify;

const pumpify = require("pumpify");
const replaceStream = require("replacestream"); // note that replacestream does NOT support zero-length regex matches!
import pumpify from "pumpify";
import replaceStream from "replacestream"; // note that replacestream does NOT support zero-length regex matches!

const unsupportedStreamOptions = ["removeUnusedNamespaces", "removeUnusedDefaultNamespace", "shortenNamespaces", "ignoreCData"],
defaultStreamOptions = module.exports.defaultStreamOptions = {
...defaultOptions,
streamMaxMatchLength: 256 * 1024, // 256 KiB, maximum size of matches between chunks
// all these options require prior knowledge about the stream, for instance if we are in a CData block, or what namespaces are present
...Object.fromEntries(unsupportedStreamOptions.map(option => [option, false]))
};
const unsupportedStreamOptions = ["removeUnusedNamespaces", "removeUnusedDefaultNamespace", "shortenNamespaces", "ignoreCData"];
export const defaultStreamOptions = {
...defaultOptions,
streamMaxMatchLength: 256 * 1024, // 256 KiB, maximum size of matches between chunks
// all these options require prior knowledge about the stream, for instance if we are in a CData block, or what namespaces are present
...Object.fromEntries(unsupportedStreamOptions.map(option => [option, false]))
};

module.exports.minifyStream = function(options) {
export function minifyStream(options) {
// apply the default options
options = {
...defaultStreamOptions,
Expand All @@ -277,13 +276,13 @@ module.exports.minifyStream = function(options) {
};

// called with the string-like object, it will create a chain of (replace)streams, which, if we pipe data into the first stream, apply all minifications
module.exports.minify(stringImposter, options);
minify(stringImposter, options);

// minify will always 'trim' the output, if more minification transformations have been applied, pumpify all streams into one
return streams.length > 1 ? pumpify(streams) : streams[0];
};

module.exports.debug = function(xml, options) {
export function debug(xml, options) {
xml && console.log(`\x1b[90m${xml}\x1b[0m`);

// the minify function accepts strings only, however only 'replace' is being called repeatedly, so we can take advantage of duck typing here
Expand All @@ -297,5 +296,5 @@ module.exports.debug = function(xml, options) {
};

// called with the string-like object, to dump all regular expressions into the console
module.exports.minify(stringImposter, options);
minify(stringImposter, options);
};
Loading

0 comments on commit f410a25

Please sign in to comment.