aboutsummaryrefslogtreecommitdiff
path: root/node_modules/express-minify/minifier.js
diff options
context:
space:
mode:
authorHumairAK <humair88@hotmail.com>2016-07-27 00:18:34 +0000
committerHumairAK <humair88@hotmail.com>2016-07-27 00:18:34 +0000
commit25706820837bd05cdc3b186eb1d5ebabf9688a61 (patch)
treeafbc83b902d3f0789d5174bd2e2798937ab57b26 /node_modules/express-minify/minifier.js
parent38594fd274a4bebee75ae71dbed457b39d253cae (diff)
parent714368c049f8749ed22c6d1d07bb3adc783dc349 (diff)
Merge branch 'master' of https://github.com/HumairAK/solutions_repo
Diffstat (limited to 'node_modules/express-minify/minifier.js')
-rw-r--r--node_modules/express-minify/minifier.js185
1 files changed, 185 insertions, 0 deletions
diff --git a/node_modules/express-minify/minifier.js b/node_modules/express-minify/minifier.js
new file mode 100644
index 0000000..504ed9d
--- /dev/null
+++ b/node_modules/express-minify/minifier.js
@@ -0,0 +1,185 @@
+var extend = require('util')._extend;
+
+/**
+ * Test whether a module exists.
+ * null for exists, false for not-exists.
+ */
+var testModule = function (name) {
+ var module = null;
+ try {
+ require.resolve(name);
+ } catch (ignore) {
+ module = false;
+ }
+ return module;
+};
+
+var Minifier = function (uglifyJS, cssmin, errorHandler) {
+ this.handleError = errorHandler || Minifier.defaultErrorHandler;
+ this.uglifyJS = uglifyJS || require('uglify-js');
+ this.cssmin = cssmin || require('cssmin');
+ this.sass = testModule('node-sass');
+ this.less = testModule('less');
+ this.stylus = testModule('stylus');
+ this.coffee = testModule('coffee-script');
+};
+
+Minifier.TYPE_TEXT = 0;
+Minifier.TYPE_JS = 1;
+Minifier.TYPE_CSS = 2;
+Minifier.TYPE_SASS = 3;
+Minifier.TYPE_LESS = 4;
+Minifier.TYPE_STYLUS = 5;
+Minifier.TYPE_COFFEE = 6;
+Minifier.TYPE_JSON = 7;
+
+Minifier.defaultErrorHandler = function (err, stage, assetType, minifyOptions, body, callback) {
+ if (stage === 'compile') {
+ callback(err, JSON.stringify(err));
+ return;
+ }
+ callback(err, body);
+};
+
+Minifier.prototype.compileAndMinify = function (assetType, minifyOptions, body, callback) {
+ if (typeof callback !== 'function') {
+ return;
+ }
+
+ var self = this;
+ var result, opt;
+
+ switch (assetType) {
+ case Minifier.TYPE_JS:
+ result = body;
+ try {
+ if (!minifyOptions.noMinify) {
+ opt = extend({fromString: true}, minifyOptions.uglifyOpt);
+ result = self.uglifyJS.minify(result, opt).code;
+ }
+ } catch (err) {
+ self.handleError(err, 'minify', assetType, minifyOptions, result, callback);
+ return;
+ }
+ callback(null, result);
+ break;
+ case Minifier.TYPE_CSS:
+ result = body;
+ try {
+ if (!minifyOptions.noMinify) {
+ result = self.cssmin(result);
+ }
+ } catch (err) {
+ self.handleError(err, 'minify', assetType, minifyOptions, result, callback);
+ return;
+ }
+ callback(null, result);
+ break;
+ case Minifier.TYPE_SASS:
+ if (!self.sass) {
+ self.sass = require('node-sass');
+ }
+ result = body;
+ try {
+ result = self.sass.renderSync({
+ data: result
+ }).css.toString();
+ } catch (err) {
+ self.handleError(err, 'compile', assetType, minifyOptions, result, callback);
+ return;
+ }
+ try {
+ if (!minifyOptions.noMinify) {
+ result = self.cssmin(result);
+ }
+ } catch (err) {
+ self.handleError(err, 'minify', assetType, minifyOptions, result, callback);
+ return;
+ }
+ callback(null, result);
+ break;
+ case Minifier.TYPE_LESS:
+ if (!self.less) {
+ self.less = require('less');
+ }
+ result = body;
+ self.less.render(result, function (err, output) {
+ if (err) {
+ self.handleError(err, 'compile', assetType, minifyOptions, result, callback);
+ return;
+ }
+ result = output.css;
+ try {
+ if (!minifyOptions.noMinify) {
+ result = self.cssmin(result);
+ }
+ } catch (err) {
+ self.handleError(err, 'minify', assetType, minifyOptions, result, callback);
+ return;
+ }
+ callback(null, result);
+ });
+ break;
+ case Minifier.TYPE_STYLUS:
+ if (!self.stylus) {
+ self.stylus = require('stylus');
+ }
+ result = body;
+ self.stylus.render(result, function (err, css) {
+ if (err) {
+ self.handleError(err, 'compile', assetType, minifyOptions, result, callback);
+ return;
+ }
+ result = css;
+ try {
+ if (!minifyOptions.noMinify) {
+ result = self.cssmin(result);
+ }
+ } catch (err) {
+ self.handleError(err, 'minify', assetType, minifyOptions, result, callback);
+ return;
+ }
+ callback(null, result);
+ });
+ break;
+ case Minifier.TYPE_COFFEE:
+ if (!self.coffee) {
+ self.coffee = require('coffee-script');
+ }
+ result = body;
+ try {
+ result = self.coffee.compile(result);
+ } catch (err) {
+ self.handleError(err, 'compile', assetType, minifyOptions, result, callback);
+ return;
+ }
+ try {
+ if (!minifyOptions.noMinify) {
+ opt = extend({fromString: true}, minifyOptions.uglifyOpt);
+ result = self.uglifyJS.minify(result, opt).code;
+ }
+ } catch (err) {
+ self.handleError(err, 'minify', assetType, minifyOptions, result, callback);
+ return;
+ }
+ callback(null, result);
+ break;
+ case Minifier.TYPE_JSON:
+ result = body;
+ try {
+ if (!minifyOptions.noMinify) {
+ result = JSON.stringify(JSON.parse(result));
+ }
+ } catch (err) {
+ self.handleError(err, 'minify', assetType, minifyOptions, result, callback);
+ return;
+ }
+ callback(null, result);
+ break;
+ default:
+ callback(null, body);
+ break;
+ }
+};
+
+module.exports = Minifier;