Skip to content
This repository was archived by the owner on Nov 20, 2020. It is now read-only.

JSON cannot be minified by uglify-js #1

Closed
joeytwiddle opened this issue Aug 26, 2013 · 3 comments
Closed

JSON cannot be minified by uglify-js #1

joeytwiddle opened this issue Aug 26, 2013 · 3 comments

Comments

@joeytwiddle
Copy link

Because JSON is not quite valid Javascript. For example:

{ "success" : "true" }

is valid JSON but not valid JS! In JS that is considered to be a code block, not an object literal. To be valid JS it would need to be:

({ "success" : "true" })

The result is that a request with Accept: application/json fails to minify:

2013/08/26 20:35:45 [log] (express-minify/minify.js:39:10|minifyIt) Minifying some content of type '1' content={
  "retStatus": "success"
}
WARN: ERROR: Unexpected token: punc (:) [?:2,13]

node_modules/express-minify/node_modules/uglify-js/lib/parse.js:199
    throw new JS_Parse_Error(message, line, col, pos);
          ^
Error
    at new JS_Parse_Error (node_modules/express-minify/node_modules/uglify-js/lib/parse.js:185:18)
    at js_error (node_modules/express-minify/node_modules/uglify-js/lib/parse.js:199:11)
    at croak (node_modules/express-minify/node_modules/uglify-js/lib/parse.js:630:9)
    at token_error (node_modules/express-minify/node_modules/uglify-js/lib/parse.js:638:9)
    at unexpected (node_modules/express-minify/node_modules/uglify-js/lib/parse.js:644:9)
...

My suggestion is that JSON should not be marked for minifying. Alternatively, if you want to minify JSON, wrap it in (...) before passng to Uglify, and remove the parentheses afterwards.

@joeytwiddle
Copy link
Author

My current workaround for this is to override the default regexp to avoid minifying JSON:

  app.use(minify({
    js_match: /javascript/   // We drop the 'json|' from the original match, because JSON cannot always be parsed as JS!
  }));

@breezewish
Copy link
Owner

@joeytwiddle

Hello, I dropped json from js_match regexp. What's more, you can use res._no_minify = true to disable minifying specific file now. These changes has been published to version 0.0.5.

Enjoy :)

@joeytwiddle
Copy link
Author

Great, thanks. 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants