diff --git a/src/native/cli.d b/src/native/cli.d index 6513420..0faffde 100644 --- a/src/native/cli.d +++ b/src/native/cli.d @@ -7,21 +7,20 @@ import argparse; # Minify the specified files minijson ./dist/**/*.json ./build/a.json + minijson file1_with_comment.json file2_with_comment.json - # Minify the specified files (supports comments) - minijson --comment file1_with_comment.json file2_with_comment.json + # Minify the specified files and disable comment support for faster minification + minijson --comment=false file1_no_comment.json file2_no_comment.json # Minify the specified json string minijson --str '{"some_json": "string_here"}' - - # Minify the specified json string (supports comments) - minijson --comment --str '{"some_json": "string_here"} //comment' + minijson --str '{"some_json": "string_here"} //comment' More information at https://github.com/aminya/minijson `)) struct Options { - bool comment = false; + bool comment = true; string[] str; // (Deprecated) A list of files to minify (for backwards compatiblitity with getopt) string[] file; diff --git a/src/native/lib.d b/src/native/lib.d index 2b0939c..bb512df 100644 --- a/src/native/lib.d +++ b/src/native/lib.d @@ -12,12 +12,12 @@ const tokenizerNoComment = ctRegex!(`[\n\r"[]]`, "g"); Params: jsonString = the json string you want to minify - hasComment = a boolean to support comments in json. Default: `false`. + hasComment = a boolean to support comments in json. Default: `true`. Return: the minified json string */ -string minifyString(in string jsonString, in bool hasComment = false) @trusted +string minifyString(in string jsonString, in bool hasComment = true) @trusted { return hasComment ? minifyStringWithComments(jsonString) : minifyStringNoComments(jsonString); } @@ -227,7 +227,7 @@ private bool hasNoSpace(const ref string str) @trusted Return: the minified json strings */ -string[] minifyStrings(in string[] jsonStrings, in bool hasComment = false) @trusted +string[] minifyStrings(in string[] jsonStrings, in bool hasComment = true) @trusted { import std.algorithm : map; import std.array : array; @@ -242,7 +242,7 @@ string[] minifyStrings(in string[] jsonStrings, in bool hasComment = false) @tru paths = the paths to the files. It could be glob patterns. hasComment = a boolean to support comments in json. Default: `false`. */ -void minifyFiles(in string[] paths, in bool hasComment = false) @trusted +void minifyFiles(in string[] paths, in bool hasComment = true) @trusted { import std.parallelism : parallel; import std.algorithm; diff --git a/src/native/libc.d b/src/native/libc.d index c8b19eb..78e6dcb 100644 --- a/src/native/libc.d +++ b/src/native/libc.d @@ -7,12 +7,12 @@ import minijson.lib : minifyString; Params: jsonString = the json string you want to minify - hasComment = a boolean to support comments in json. Default: `false`. + hasComment = a boolean to support comments in json. Default: `true`. Return: the minified json string */ -extern (C) auto c_minifyString(char* jsonCString, bool hasComment = false) +extern (C) auto c_minifyString(char* jsonCString, bool hasComment = true) { import std : fromStringz, toStringz; diff --git a/src/node/lib.ts b/src/node/lib.ts index c113f0c..2ad32c3 100644 --- a/src/node/lib.ts +++ b/src/node/lib.ts @@ -7,20 +7,18 @@ import { join } from "path" * Minify all the given JSON files in place. It minifies the files in parallel. * * @param files An array of paths to the files - * @param hasComment A boolean to support comments in json. Default `false`. + * @param hasComment A boolean to support comments in json. Default `true`. * @returns {Promise} Returns a void promise that resolves when all the files are minified * @throws {Promise} The promise is rejected with the reason for failure */ -export async function minifyFiles(files: string[], hasComment = false): Promise { +export async function minifyFiles(files: readonly string[], hasComment = true): Promise { try { const filesNum = files.length if (filesNum === 0) { return Promise.resolve() } - const minijsonArgs = hasComment ? ["--comment", ...files] : files - - await spawnMinijson(minijsonArgs) + await spawnMinijson([...files, hasComment ? "--comment=true" : "--comment=false"]) } catch (e) { console.error(e, "Falling back to jsonminify") await minifyFilesFallback(files) @@ -48,7 +46,7 @@ export function spawnMinijson(args: string[]): Promise { }) } -async function minifyFilesFallback(files: string[]) { +async function minifyFilesFallback(files: readonly string[]) { const jsonminify = require("jsonminify") await Promise.all( files.map(async (file) => { @@ -64,11 +62,11 @@ async function minifyFilesFallback(files: string[]) { * Minify the given JSON string * * @param jsonString The json string you want to minify - * @param hasComment A boolean to support comments in json. Default `false`. + * @param hasComment A boolean to support comments in json. Default `true`. * @returns {Promise} The minified json string * @throws {Promise} The promise is rejected with the reason for failure */ -export async function minifyString(jsonString: string, hasComment = false): Promise { +export async function minifyString(jsonString: string, hasComment = true): Promise { const args = ["--str", jsonString] if (hasComment) { args.push("--comment")