Skip to content

Instantly share code, notes, and snippets.

@sheutettz
Created August 18, 2015 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheutettz/113e30f8310d9ab3928d to your computer and use it in GitHub Desktop.
Save sheutettz/113e30f8310d9ab3928d to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// =================================================================================
// Copyright (c) 2014 sheutettz. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
// =================================================================================
'use strict';
var _ = require('lodash');
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var ejs = require('ejs');
var uglifyjs = require('uglify-js');
var args = process.argv.slice(2);
var production = (process.env.npm_config_production === 'true');
var minify = false;
var distclean = false;
var filePatterns = [];
args.forEach(function (val, index, array) {
if (val === '--minify') {
minify = true;
}
else if (val === '--no-minify') {
minify = false;
}
else if (val === '--production') {
production = true;
}
else if (val === '--no-production') {
production = false;
}
else if (val === '--distclean') {
distclean = true;
}
else if (_.isString(val) && val.length > 0) {
if (val.match(/^-+[a-zA-Z0-9][-a-zA-Z0-9]*$/)) {
console.warn('unknown command line option: ' + val);
}
else {
filePatterns.push(val);
}
}
});
if (filePatterns.length === 0) {
console.error('Error! No filename patterns specified!\n');
console.log('Usage: ejs-compiler.js [options] filename-patterns [filename-patterns ...]');
console.log('[Options]');
console.log(' --minify : enable minified compilation.');
console.log(' --no-minify : disable minified compilation.');
console.log(' --distclean : does not compile and generate the target js file(s) but removes instead.');
console.log(' --production : enable optimized compilation.');
console.log(' --no-production: disable optimized compilation.');
console.log();
}
var action = distclean ? unlinkFile : compileFile;
_.each(filePatterns, function (filename) {
glob(filename, {}, function (er, files) {
if (er) {
console.error(er);
return;
}
_.each(files, action);
});
});
function unlinkFile(srcFile) {
var dstFile = changeExt(srcFile, '.js');
if (fs.existsSync(srcFile) && fs.existsSync(dstFile)) {
console.warn('remove ' + dstFile);
fs.unlinkSync(dstFile);
}
}
function compileFile(srcFile) {
var src = fs.readFileSync(srcFile, 'utf8');
if (!src) {
console.error("failed to read file: " + srcFile);
return;
}
var opts = {
client: true,
compileDebug: !production,
filename: srcFile
};
var fn = ejs.compile(src, opts);
if (!_.isFunction(fn)) {
console.error("failed to compile: " + srcFile);
return;
}
var js = "module.exports = " + fn.toString();
{
js = uglifyjs.minify(js, {
fromString: true,
output: {
beautify: !minify
},
beautify: {
bracketize: true
},
mangle: {
toplevel: true,
eval: minify
},
compress: {
unsafe: false,
evaluate: true,
conditionals: true,
booleans: true,
if_return: true,
sequences: minify,
join_vars: minify,
dead_code: production,
drop_debugger: production,
unused: production,
drop_console: production
}
}).code;
}
var dstFile = changeExt(srcFile, '.js');
fs.writeFileSync(dstFile, js, 'utf8');
}
function changeExt(filename, newExt) {
var ext = path.extname(filename);
if (_.isString(ext) && ext.length > 0) {
var len = filename.length - ext.length;
return filename.substr(0, len) + newExt;
} else {
return filename + newExt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment