2

So I am looping through files in a directory via Node and want to minify them with UglifyJs.

The API is dead easy for JavaScript files:

var UglifyJS = require("uglify-js")

// Looping here
UglifyJS.minify(listOfAllFiles[i])

However, the files I need to minify are JSON files, so this is producing an eval error. In the command line, if you are minifying JSON, you just pass --expr and it will evaluate as a single expression. Any idea how to pass this into the options object of the JavaScript API?

Cheers.

5
  • JSON =/= JavaScript. The syntax is different. There's very little about JSON that could be minified, even. Commented May 9, 2016 at 9:16
  • @Cerbrus Removal of whitespace. As I explicitly stated, UglifyJs supports JSON minification. Commented May 9, 2016 at 9:18
  • I don't see any expr parameter in it's documentation. Commented May 9, 2016 at 9:21
  • Take a look here. Commented May 9, 2016 at 9:30
  • I updated the tags a little. Commented May 9, 2016 at 9:41

2 Answers 2

12

If you're not dead set on UglifyJS, you could solve this with plain JavaScript, because JSON can't really be uglified that much. To remove all whitespace, use:

JSON.stringify(JSON.parse(listOfAllFiles[i]))

(Assuming listOfAllFiles[i] is the JSON string.)

Sign up to request clarification or add additional context in comments.

1 Comment

Ah very helpful! This will indeed do the trick and is super easy.
2

UglifyJS doesn't minify JSON.

You can minify using jsonminify instead.

Contrary to what others are saying, JSON minification is useful, as it allows you to use comments in JSON (which are not allowed in .json files).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.