Skip to content

Instantly share code, notes, and snippets.

@kyo-ago
Created May 15, 2012 13:30
Show Gist options
  • Save kyo-ago/2701795 to your computer and use it in GitHub Desktop.
Save kyo-ago/2701795 to your computer and use it in GitHub Desktop.
js minifier on NILScript(parse html)
var root = './htdocs/';
var html_path = root + 'index.html';
var output = cwd().file(root + 'js/minified.js').create();
var html = cwd().file(html_path).load('UTF-8');
var target_reg = /<!-- MINIFY_TARGET -->([\s\S]+?)<!-- \/MINIFY_TARGET -->/;
var block = html.match(target_reg).pop();
var minify = block.match(/'\/js\/.+?'/g).map(function (path) {
return cwd().file(root + path.replace(/'/g, '')).load('UTF-8');
}).join('');
var res = require('HTTP').HTTP.post('http://closure-compiler.appspot.com/compile', {
'js_code' : minify,
'compilation_level' : 'SIMPLE_OPTIMIZATIONS',
'output_format' : 'json',
'output_info' : 'compiled_code'
});
output.update(Object.fromJSON('('+res+')').compiledCode, 'UTF-8');
var timestamp = (new Date).getTime();
cwd().file(html_path+'_').update(html, 'UTF-8');
cwd().file(html_path).update(html.replace(target_reg, '<script type="text/javascript" src="/js/minified.js?'+timestamp+'"></script>'), 'UTF-8');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment