File: /home/frenchy/www/extracts/var/www/french-american.org/current/node_modules/gulp-uglyfly/index.js
'use strict';
var parallelize = require('parallel-transform'),
cpus = require('os').cpus().length,
buffer = require('vinyl-buffer'),
uglyfly = require('uglyfly-js'),
deepExtend = require('deep-extend'),
PluginError = require('gulp-util/lib/PluginError'),
pluginName = 'gulp-uglyfly';
function adjustOptions(opts, file) {
var preserveComments = opts && opts.preserveComments,
commentsOptionOverride,
defaultOptions,
options;
if (preserveComments === 'all') {
commentsOptionOverride = true;
} else if (preserveComments === 'some') {
commentsOptionOverride = /^!|@preserve|@license|@cc_on/i;
} else if (typeof preserveComments === 'function') {
commentsOptionOverride = preserveComments;
}
defaultOptions = {
sourceMapURL: false
};
if (file && file.sourceMap) {
if (file.sourceMap.mappings !== '') {
defaultOptions.inSourceMap = file.sourceMap;
}
if (file.relative) {
defaultOptions.outSourceMap = String(file.relative);
}
}
options = opts ? deepExtend(defaultOptions, opts) : defaultOptions;
delete options.parallelism;
delete options.preserveComments;
if (commentsOptionOverride) {
options.output = options.output || {};
options.output.comments = commentsOptionOverride;
}
return options;
}
function createError(file, err) {
var filePath,
msg;
filePath = file.path ? String(file.path) : '';
if(filePath.length === 0) {
filePath = 'ANONYMOUS FILE';
}
if (typeof err === 'string') {
return new PluginError(pluginName, filePath + ': ' + err, {
fileName: filePath,
showStack: false
});
}
msg = err.message || err.msg || /* istanbul ignore next */ 'unspecified error';
return new PluginError(pluginName, filePath + ': ' + msg, {
fileName: filePath,
lineNumber: err.line,
stack: err.stack,
showStack: false
});
}
function minify(opts) {
var parallelism = cpus;
opts = deepExtend({}, opts || {});
if (opts.parallelism && (typeof opts.parallelism === 'number')) {
parallelism = opts.parallelism;
}
return buffer().pipe(parallelize(parallelism, function (file, callback) {
var options,
inSourceMap,
dontAddSources;
if (file.isNull()) {
return callback(null, file);
}
dontAddSources = (opts.sourceMapIncludeSources === false);
if(typeof opts.getCustomFileOptions === 'function') {
opts = opts.getCustomFileOptions(file, opts) || {};
delete opts.getCustomFileOptions;
}
options = adjustOptions(opts, file);
inSourceMap = options.inSourceMap;
if (inSourceMap) {
options.sourceMapIncludeSources = false; //these would be overwritten anyway
}
try {
uglyfly.minify(file, options, function (err, results) {
var outSourceMap;
if (err) {
return callback(createError(file, err));
}
file.contents = new Buffer(results.code);
if ((typeof results.map === 'string') || (results.map instanceof String)) {
outSourceMap = JSON.parse(results.map);
if (outSourceMap) {
file.sourceMap = outSourceMap;
if (inSourceMap && !dontAddSources) {
file.sourceMap.sourcesContent = inSourceMap.sourcesContent;
file.sourceMap.sources = inSourceMap.sources;
}
}
}
return callback(null, file);
});
} catch (e) {
return callback(createError(file, e));
}
}));
}
module.exports = minify;