Created
March 2, 2018 03:57
-
-
Save sijpkes/6bb74ea04f2ef874e8ddc5ac533353f3 to your computer and use it in GitHub Desktop.
Simple minify maker script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var UglifyJS = require('uglify-js'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var sys = require('util'); | |
/* | |
https://gist.github.com/kethinov/6658166#gistcomment-1921157 | |
*/ | |
var walkSync = function(dir, filelist) { | |
var path = path || require('path'); | |
var fs = fs || require('fs'), | |
files = fs.readdirSync(dir); | |
filelist = filelist || []; | |
files.forEach(function(file) { | |
if (fs.statSync(path.join(dir, file)).isDirectory()) { | |
filelist = walkSync(path.join(dir, file), filelist); | |
} | |
else { | |
if(path.extname(file) === '.js') { | |
filelist.push(path.join(dir, file)); | |
} | |
} | |
}); | |
return filelist; | |
}; | |
const pathDepth = (path) => { | |
var res = {depth: 0, segments: []}; | |
res.segments = path.split('/'); | |
res.depth = res.segments.length; | |
return res; | |
} | |
const prune = (depth, segments) => { | |
var seg = segments[depth > 2 ? depth-2 : 2]; | |
if(typeof seg === 'undefined') { | |
seg = ""; | |
} | |
return seg; | |
}; | |
const minify = (o) => { | |
new Promise((resolve, reject) => { | |
o.codeList.forEach((code, i) => { | |
var result = UglifyJS.minify(code , { | |
mangle: { | |
toplevel: true | |
}, | |
compress: { | |
sequences: true, | |
dead_code: true, | |
conditionals: true, | |
booleans: true, | |
unused: true, | |
if_return: true, | |
join_vars: true, | |
drop_console: true | |
} | |
}); | |
var file = Object.keys(code)[0]; | |
//console.log(o); | |
var __path = o.pathList[file]; | |
var pob = pathDepth(__path); | |
var depth = pob.depth; | |
var segments = pob.segments; | |
console.log(segments); | |
console.log(`${depth} path depth for ${file}`); | |
var shortPath = ""; | |
var output = o.output; | |
console.log(`${depth} path depth`); | |
output = output + "/" + prune(depth, segments); | |
console.log(`Check if ${output} exists`); | |
if(!fs.existsSync(output)) { | |
console.log(`Making ${output}`); | |
fs.mkdirSync(output); | |
} | |
if(!fs.statSync(output).isDirectory()) { | |
reject(`${output} not a directory!`); | |
} | |
fs.writeFileSync(path.join(output, file), result.code); | |
}); | |
resolve(o); | |
}).then((code) => { | |
console.log("Build files processed OK.") | |
} | |
).catch((err) => { | |
console.log(err); | |
}); | |
}; | |
const run = () => { | |
return new Promise((resolve, reject) => { | |
var o = {fileList: [], output: "", codeList: [], pathList: {}}; | |
var valid = 0; | |
process.argv.forEach((p,i) => { | |
if(p === '-i' || p === '--input') { | |
let d = process.argv[i+1]; | |
if(fs.statSync(d).isDirectory()) { | |
walkSync(d, o.fileList); | |
} else { | |
reject(`${p} requires a valid directory path.`); | |
} | |
valid ++; | |
} | |
if(p == '-o' || p === '--output') { | |
let d = process.argv[i+1]; | |
if(fs.statSync(d).isDirectory()) { | |
o.output = d; | |
} else { | |
reject(`${p} requires a valid directory path.`); | |
} | |
valid ++; | |
} | |
}); | |
if(valid === 2) { | |
resolve(o); | |
} else { | |
reject("Wrong number of parameters"); | |
} | |
}); | |
} | |
const buildCode = (fileList) => { | |
var codeList = []; | |
var pathList = {}; | |
fileList.forEach((f, i) => { | |
let code = {} | |
var file = path.basename(f); | |
code[file] = fs.readFileSync(f, {encoding: 'utf8'}); | |
pathList[file] = f; | |
codeList.push(code); | |
}); | |
return {codeList: codeList, pathList: pathList}; | |
}; | |
run().then((o)=> { | |
var bc = buildCode(o.fileList); | |
o.codeList = bc.codeList; | |
o.pathList = bc.pathList; | |
minify(o); | |
}).catch((err) => { | |
console.log(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment