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

Commit

Permalink
move json minify to its own part, a better implement
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterDaveHello committed May 17, 2015
1 parent 1e18ceb commit 487c9eb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ app.use(minify(
less_match: /less/,
stylus_match: /stylus/,
coffee_match: /coffeescript/,
json_match: /json/,
cache: false
}));
```
Expand Down Expand Up @@ -62,6 +63,10 @@ app.use(minify(

matches CoffeeScript content-type.

- `json_match`: `RegExp`

matches JSON content-type.

- `cache`: `String | false`

the directory for cache storage (must be writeable). Pass `false` to cache in the memory (not recommended).
Expand Down
18 changes: 14 additions & 4 deletions minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var TYPE_SASS = 3;
var TYPE_LESS = 4;
var TYPE_STYLUS = 5;
var TYPE_COFFEE = 6;
var TYPE_JSON = 7;

function precompileError(err, type) {
return JSON.stringify(err);
Expand All @@ -40,10 +41,6 @@ function minifyIt(type, options, content, callback) {
result = uglifyjs.minify(result, opt).code;
}
} catch(err) {
try {
result = JSON.minify(content);
} catch(err) {
}
}
callback(result);
break;
Expand Down Expand Up @@ -134,6 +131,16 @@ function minifyIt(type, options, content, callback) {
}
callback(result);
break;
case TYPE_JSON:
var result = content;
try {
if (!options.noMinify) {
result = JSON.minify(content);
}
} catch(err) {
}
callback(result);
break;
default:
callback(content);
break;
Expand Down Expand Up @@ -205,6 +212,7 @@ module.exports = function express_minify(options) {
var less_match = options.less_match || /less/;
var stylus_match = options.stylus_match || /stylus/;
var coffee_match = options.coffee_match || /coffeescript/;
var json_match = options.json_match || /json/;
var cache = options.cache || false;

var cache_get = cacheGetMem;
Expand Down Expand Up @@ -266,6 +274,8 @@ module.exports = function express_minify(options) {
} else if (coffee_match.test(contentType)) {
type = TYPE_COFFEE;
res.setHeader('Content-Type', 'application/javascript');
} else if (json_match.test(contentType)) {
type = TYPE_JSON;
} else if (js_match.test(contentType)) {
type = TYPE_JS;
} else if (css_match.test(contentType)) {
Expand Down

0 comments on commit 487c9eb

Please sign in to comment.