how to uglify JSON?
Closed this issue · 1 comments
I have some JSON config files in my project that I want to minify with my gulpfile script. It seems like gulp-uglify should be the tool for this, and I'm already using it for my js. If there is already another tool or method, please point me to that.
I'm trying to do this:
// Validate and minify JSON
const sources_conf_webapp = [
'./cfg/confc.json',
'./cfg/confc_blank.json',
'./cfg/confs.json'
];
const destdir_conf_webapp = './html/cfg/'
function build_conf_webapp() {
return src(sources_conf_webapp)
.pipe(preprocess({ context: context_webApp }))
.pipe(jsonlint())
.pipe(jsonlint.reporter())
.pipe(uglify())
.pipe(dest(destdir_conf_webapp));
}
which gives error:
'build_conf_webapp' errored after 324 ms
[18:53:19] GulpUglifyError: unable to minify JavaScript
Caused by: SyntaxError: Unexpected token: punc «:», expected: punc «;»
So I looked at the UglifyJS docs and it looks like the option is there on the CLI:
-p, --parse <options> Specify parser options:
`expression` Parse a single expression, rather than
a program (for parsing JSON).
(I also found this issue in UglifyJS which seems to indicate that it is not supported, but maybe it's old information. mishoo/UglifyJS#812 )
So I tried to add the parse option to my gulp script by adding the options: {parse:'expression'}
. But it doesn't look like that option is exposed in this gulp wrapper. gulp-uglify's index.d.ts only lists mangle, output, and compress in the Options.
Should it be as easy as exposing that option through gulp-uglify? If so I'll take a stab at it.
Oops, looks like there are quite a few other plugins that should work:
gulp-jsonmin
gulp-json-format
gulp-json-editor
gulp-json-fmt
I found them by searching 'json' on https://gulpjs.com/plugins. My earlier web search for 'gulp minify json' pointed me to this plugin. Sorry for the spam.