3 var _ = require('lodash'),
4 fs = require('fs-extra'),
5 uglify = require('uglify-js');
7 var uglifyOptions = require('./uglify.options');
9 /*----------------------------------------------------------------------------*/
12 * Asynchronously minifies the file at `srcPath`, writes it to `destPath`, and
13 * invokes `callback` upon completion. The callback is invoked with one argument:
16 * If unspecified, `destPath` is `srcPath` with an extension of `.min.js`. For
17 * example, a `srcPath` of `path/to/foo.js` would have a `destPath` of `path/to/foo.min.js`.
19 * @param {string} srcPath The path of the file to minify.
20 * @param {string} [destPath] The path to write the file to.
21 * @param {Function} callback The function invoked upon completion.
22 * @param {Object} [option] The UglifyJS options object.
24 function minify(srcPath, destPath, callback, options) {
25 if (_.isFunction(destPath)) {
26 if (_.isObject(callback)) {
33 destPath = srcPath.replace(/(?=\.js$)/, '.min');
35 var output = uglify.minify(srcPath, _.defaults(options || {}, uglifyOptions));
36 fs.writeFile(destPath, output.code, 'utf-8', callback);
39 module.exports = minify;