File: /home/f/r/e/frenchy/www/french-american.org/current/node_modules/uglyfly-js/build/build.js
/***********************************************************************
Copyright 2014 (c) Saair Quaderi <saair.quaderi@gmail.com>
UglyflyJS sourcecode and BSD 2-clause license info can be found here:
https://github.com/quaderi/uglyflyjs
***********************************************************************/
(function () {
'use strict';
var util = require('util'),
path = require('path'),
fs = require('fs'),
requirejs = require('requirejs'),
promisify = require('bluebird').promisify,
mkdirp = promisify(require('mkdirp')),
rimraf = promisify(require('rimraf')),
readFile = promisify(fs.readFile),
writeFile = promisify(fs.writeFile),
appendFile = promisify(fs.appendFile),
ncp = promisify(require('ncp').ncp),
replace = promisify(require('replace')),
uglyfly = require('../index.js'),
minify = promisify(uglyfly.minify),
bundleModules = promisify(function (options, cb) {
requirejs.optimize(options, function (buildResponse) {
cb(null, buildResponse);
}, function (err) {
cb(err);
});
}),
preprocess = promisify(function stripNodeFriendlyDefines(dirpaths, cb) {
if (typeof dirpaths === 'string') {
dirpaths = [dirpaths];
}
try {
// note: the regex here is somewhat fragile to changes in the code
// also kind of lame that this is synchronous
replace({
regex: /\(\(typeof define === "function"\) \? define\s+:\s+function\s.+\{.+\}\)\(/g,
replacement: 'define(',
paths: dirpaths || [],
recursive: true,
silent: true
});
return cb();
} catch (ex) {
return cb(ex);
}
}),
licenseCommentsFilepath = path.resolve('build/lib/license_comment.js'),
inFilepath = path.resolve('build/get_uglyfly_api_built.js'),
outFilepath = path.resolve('build/get_uglyfly_api_built.min.js'),
outSourceMapFilepath = path.resolve('build/get_uglyfly_api_built.min.js.map'),
outSourceMapURL = path.relative(path.dirname(outFilepath), outSourceMapFilepath).split(path.sep).join('/'),
outFileURL = path.relative(path.dirname(outSourceMapFilepath), outFilepath).split(path.sep).join('/'),
minifyOptions = {
outSourceMap: true,
sourceMapURL: outSourceMapURL,
sourcemapFileProperty: outFileURL
};
mkdirp('build/lib').then(function () {
util.log('Clearing build library directory...');
return rimraf('build/lib');
}).then(function () {
util.log('Copying library files to build library directory...');
return ncp('lib', 'build/lib');
}).then(function () {
util.log('Preparing files for AMD bundling...');
return preprocess('build/lib');
}).then(function () {
util.log('Bundling AMD files...');
return bundleModules({
baseUrl: 'build/lib',
name: 'get_uglyfly_api',
out: 'build/get_uglyfly_api_built.js',
optimize: 'none' //don't uglify
});
}).then(function (buildResponse) {
util.log(buildResponse);
}).then(function () {
util.log('Writing comment/license info to "' + outFilepath + '"');
return readFile(licenseCommentsFilepath).then(function (contents) {
//we're not using minify's preserve comments output options because there
// would be redundant copyright/license comments from the many modules
return writeFile(outFilepath, contents);
});
}).then(function () {
util.log('Clearing build library directory...');
return rimraf('build/lib');
}).then(function () {
util.log('Starting bundle minification with UglyFly...');
return minify(inFilepath, minifyOptions).then(function (results) {
util.log('Writing minified code to "' + outFilepath + '"');
appendFile(outFilepath, results.code);
return results;
}).then(function (results) {
util.log('Writing minification sourcemap to "' + outSourceMapFilepath + '"');
writeFile(outSourceMapFilepath, results.map);
});
}).catch(function (err) {
util.error(err);
});
}());