ffffng/app/bower_components/lodash/lib/common/minify.js

40 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2016-05-16 13:33:49 +02:00
'use strict';
2017-05-13 13:25:33 +02:00
const _ = require('lodash');
const fs = require('fs-extra');
const uglify = require('uglify-js');
2016-05-16 13:33:49 +02:00
2017-05-13 13:25:33 +02:00
const uglifyOptions = require('./uglify.options');
2016-05-16 13:33:49 +02:00
/*----------------------------------------------------------------------------*/
/**
* Asynchronously minifies the file at `srcPath`, writes it to `destPath`, and
* invokes `callback` upon completion. The callback is invoked with one argument:
* (error).
*
2017-05-13 13:25:33 +02:00
* If unspecified, `destPath` is `srcPath` with an extension of `.min.js`.
* (e.g. the `destPath` of `path/to/foo.js` would be `path/to/foo.min.js`)
2016-05-16 13:33:49 +02:00
*
* @param {string} srcPath The path of the file to minify.
* @param {string} [destPath] The path to write the file to.
* @param {Function} callback The function invoked upon completion.
* @param {Object} [option] The UglifyJS options object.
*/
function minify(srcPath, destPath, callback, options) {
if (_.isFunction(destPath)) {
if (_.isObject(callback)) {
options = callback;
}
callback = destPath;
destPath = undefined;
}
if (!destPath) {
destPath = srcPath.replace(/(?=\.js$)/, '.min');
}
2017-05-13 13:25:33 +02:00
const output = uglify.minify(srcPath, _.defaults(options || {}, uglifyOptions));
2016-05-16 13:33:49 +02:00
fs.writeFile(destPath, output.code, 'utf-8', callback);
}
module.exports = minify;