JSON cannot be minified by uglify-js
joeytwiddle opened this issue · 3 comments
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!
}));
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. 👍