Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

JSON cannot be minified by uglify-js #1

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

Comments

Projects
None yet
2 participants

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.

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!
  }));

@breeswish breeswish closed this in 2e969f2 Aug 26, 2013

Owner

breeswish commented Aug 26, 2013

@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 :)

Great, thanks. 👍

@zsoltszabo zsoltszabo referenced this issue in zsoltszabo/node-uglifier Mar 5, 2015

Closed

Parse error when requiring json file #3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment