File: /home/frenchy/www/french-american.org/current/node_modules/uglyfly-js/lib/get_uglyfly_api.js
/***********************************************************************
Copyright 2014 (c) Saair Quaderi <saair.quaderi@gmail.com>
Copyright 2012-2013 (c) Mihai Bazon <mihai.bazon@gmail.com>
UglyflyJS sourcecode can be found here:
https://github.com/quaderi/uglyflyjs
UglyflyJS (by Saair) is a fork of UglifyJS2 (by Mihai Bazon)
Both libraries are released under the BSD 2-Clause License.
***********************************************************************/
/*globals define, module, require */
((typeof define === "function") ? define :
function () { "use strict"; require('./nd').apply(module, arguments); })(
"get_uglyfly_api",
[
"get_uglyfly",
"defaults",
"get_promisify"
],
function (get_uglyfly, defaults, get_promisify) {
"use strict";
function get_uglyfly_api(dependencyHooks) {
var api,
getSourceMapPromise,
getFilePromise,
Buffer,
Promise,
promisify,
MOZ_SourceMap,
getFileContentsPromise,
warn_callback,
croak_callback,
noop,
UglyflyJS;
noop = function () {
return;
};
if (!dependencyHooks) {
throw "Dependency hooks must be provided.";
}
Buffer = dependencyHooks.Buffer;
Promise = dependencyHooks.Promise;
MOZ_SourceMap = dependencyHooks.MOZ_SourceMap;
getFileContentsPromise = dependencyHooks.getFileContentsPromise;
warn_callback = dependencyHooks.warn_callback || noop;
croak_callback = dependencyHooks.croak_callback || noop;
if (typeof Promise !== 'function') {
throw new TypeError('Promise dependency must be a function, specifically a promise constructor. Example Promise library: bluebird (https://github.com/petkaantonov/bluebird).');
}
promisify = get_promisify(Promise);
function asResolvedPromise(value) {
var args = [null].concat((arguments.length < 2) ? [value] : Array.prototype.slice(arguments));
return promisify(function (callback) {
callback.apply(null, args);
})();
}
UglyflyJS = get_uglyfly(MOZ_SourceMap, warn_callback, croak_callback);
function getFile(file, options, cb) {
var readpath, isBuffer;
if (typeof file === 'string') {
file = {
path: file
};
}
if (file && (typeof file.path === 'string')) {
if (file.contents) {
isBuffer = (typeof Buffer === 'function') && (file.contents instanceof Buffer);
if (isBuffer || (typeof file.contents === 'string')) {
return cb(null, {
path: file.path,
contents: isBuffer ? file.contents.toString() : file.contents
});
}
}
readpath = (typeof file.readpath === 'string') ? file.readpath : file.path;
if (!getFileContentsPromise) {
throw "Missing dependency hook. The getFileContentsPromise dependency function was not provided.";
}
return getFileContentsPromise(readpath).then(function (contents) {
return cb(null, {
path: file.path,
contents: contents
});
}).catch(function (err) {
return cb(err);
});
}
options = options || {};
if (!options.isRequired && !file) {
return cb();
}
return cb(new Error('The ' +
(options.isRequired ? 'required ' : '') +
(options.sourceDesc || 'input file') +
' was not specified in a recognized format'));
}
getFilePromise = promisify(getFile);
function getSourceMap(sourceRoot, outSourceMap, inSourceMap, shouldIncludeSources, fileSources, outFilename, cb) {
var inSourceMapPromise = null,
hasOwn = Object.prototype.hasOwnProperty;
if (!outSourceMap) {
return cb();
}
if (((typeof inSourceMap === "string") && (inSourceMap.charAt("0") !== "{")) ||
(inSourceMap && hasOwn.call(inSourceMap, "path") && hasOwn.call(inSourceMap, "contents"))) {
inSourceMapPromise = getFilePromise(inSourceMap, {
sourceDesc: 'input source map'
}).then(function (file) {
return file.contents;
});
} else {
inSourceMapPromise = asResolvedPromise(inSourceMap);
}
return inSourceMapPromise.then(function (inSourceMap) {
var source_map;
if (!MOZ_SourceMap) {
throw "Missing dependency hook. Mozilla's source map library must be provided. (https://github.com/mozilla/source-map/).";
}
source_map = new UglyflyJS.get_source_map({
file: outFilename,
orig: inSourceMap,
root: sourceRoot
});
if (shouldIncludeSources) {
fileSources.forEach(function (source) {
source_map.get().setSourceContent(source.path, source.content);
});
}
return cb(null, {
source_map: source_map
});
}).catch(function (err) {
return cb(err);
});
}
getSourceMapPromise = promisify(getSourceMap);
function process(input, options, cb) {
var toplevel = null,
fileSources = [];
if (!Array.isArray(input)) {
input = [ input ];
}
if (options.fromString) {
input = input.map(function (contents, i) {
return {
path: 'fakepath' + (i + 1) + '.js',
contents: contents
};
});
}
delete options.fromString;
if ((typeof options.sourceMapURL !== 'string') &&
(typeof options.outSourceMap === 'string') &&
(options.sourceMapURL !== false)) {
options.sourceMapURL = options.outSourceMap;
}
return promisify(function (cb) {
if (options.spidermonkey) {
toplevel = UglyflyJS.from_mozilla_ast(input);
return cb();
}
asResolvedPromise(input).each(
function (file, index) {
return getFilePromise(file, {isRequired: true}).then(function (source) {
if (options.outSourceMap && options.sourceMapIncludeSources && source.path) {
fileSources[index] = source;
}
toplevel = UglyflyJS.parse(source.contents, {
filename: source.path,
toplevel: toplevel
});
});
}
).then(function () {
return cb();
}).catch(function (err) {
return cb(err);
});
})().then(function () {
return getSourceMapPromise(
options.sourceRoot,
options.outSourceMap,
options.inSourceMap,
!!options.sourceMapIncludeSources,
fileSources,
options.sourcemapFileProperty
);
}).then(function (output) {
var results = UglyflyJS.process(toplevel, options, output);
return cb(null, {
code: results.js,
map: results.source_map
});
}).catch(function (err) {
return cb(err);
});
}
function minify(input, options, cb) {
if (typeof options === 'function') {
cb = options;
options = null;
}
if (typeof cb !== 'function') {
return;
}
options = defaults(options, {
spidermonkey: false,
sourceRoot: null,
outSourceMap: false,
sourcemapFileProperty: null,
inSourceMap : null,
sourceMapIncludeSources: false,
sourceMapURL: null,
warnings: false,
mangle: {},
output: {},
compress: {},
fromString: false
});
return process(input, options, cb);
}
function beautify(input, options, cb) {
if (typeof options === 'function') {
cb = options;
options = null;
}
if (typeof cb !== 'function') {
return;
}
options = defaults(options, {
spidermonkey: false,
sourceRoot: null,
outSourceMap: false,
sourcemapFileProperty: null,
inSourceMap : null,
sourceMapIncludeSources: false,
sourceMapURL: null,
warnings: false,
mangle: false,
output: {
beautify: true,
comments: true
},
compress: false,
fromString: false
});
return process(input, options, cb);
}
function parse($TEXT, options, cb) {
if (typeof options === 'function') {
cb = options;
options = null;
}
if (typeof cb !== 'function') {
return;
}
try {
return cb(null, UglyflyJS.parse($TEXT, options));
} catch (ex) {
return cb(ex);
}
}
function compress(toplevel, compressOptions, warningOptions, cb) {
if (typeof warningOptions === 'function') {
cb = warningOptions;
warningOptions = null;
}
if (typeof compressOptions === 'function') {
cb = compressOptions;
compressOptions = null;
}
if (typeof cb !== 'function') {
return;
}
try {
return cb(null, UglyflyJS.compress(toplevel, compressOptions, warningOptions));
} catch (ex) {
return cb(ex);
}
}
function from_mozilla_ast(mozilla_ast, cb) {
if (typeof cb !== 'function') {
return;
}
try {
return cb(null, UglyflyJS.from_mozilla_ast(mozilla_ast));
} catch (ex) {
return cb(ex);
}
}
api = {
beautify: beautify,
compress: compress,
from_mozilla_ast: from_mozilla_ast,
minify: minify,
parse: parse
};
return api;
}
return get_uglyfly_api;
}
);