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

Commit

Permalink
Merge pull request #25 from PeterDaveHelloKitchen/real_json_support
Browse files Browse the repository at this point in the history
Real json support
  • Loading branch information
breezewish committed May 17, 2015
2 parents 16df5ef + b3eb2b5 commit 81dfa97
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ app.use(minify(
less_match: /less/,
stylus_match: /stylus/,
coffee_match: /coffeescript/,
json_match: /json/,
cache: false
}));
```
Expand Down Expand Up @@ -65,6 +66,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
14 changes: 13 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var sass = require('node-sass');
var less = require('less');
var stylus = require('stylus');
var coffee = require('coffee-script');
JSON.minify = require("node-json-minify");

var expectation = {
'js': [
Expand All @@ -35,6 +36,9 @@ var expectation = {
],
'coffee': [
{src: 'square = (x) -> x * x'}
],
'json': [
{src: '{ "name" : "express-minify" , "author" : "Breezewish" , "description" : "An express middleware to automatically minify and cache your javascript and css files."}'}
]
};

Expand All @@ -44,7 +48,8 @@ var header = {
'sass': 'text/x-scss',
'less': 'text/less',
'stylus': 'text/stylus',
'coffee': 'text/coffeescript'
'coffee': 'text/coffeescript',
'json': 'application/json'
};

describe('minify()', function() {
Expand Down Expand Up @@ -264,6 +269,13 @@ function init(callback) {
});
}

minifyFunc.json = function(content, callback) {
callback({
processed: content,
minified: JSON.minify(content)
});
}

minifyFunc.css = function(content, callback) {
callback({
processed: content,
Expand Down

0 comments on commit 81dfa97

Please sign in to comment.