UNPKG

nyx_server

Version:

Node内容发布

90 lines (80 loc) 2.92 kB
var UglifyJS = require('uglify-js'); var JSHINT = require('jshint').JSHINT; var crypto = require("crypto"); var cleanspaceLine = module.exports.cleanspaceLine = function(content){ var lines = content.match(/.*[\r\n]/g); content = ''; if(typeof lines != 'undefined' && lines != null){ lines.forEach(function(line){ if(!/^\s*[\r\n]/.test(line)){ content += line; } }); } return content; } var cleancomment = module.exports.cleancomment = function(content){ var matchs = content.match(/<!--(.|\n|\r)*?-->/ig); if(typeof matchs != 'undefined' && matchs != null){ matchs.forEach(function(match){ content = content.replace(match,''); }); } return content; } var warpstr = module.exports.warpstr = function(content){ content = cleancomment(content); var lines = content.split(/\n/); var _content = ""; for(var i=0; i<lines.length; i++){ var _line = lines[i].replace(/(^\s*)|(\s*$)/g,''); if(_line=='') continue; _line = _line.replace(/<\/script>/ig, "<\\\/script>"); _content += "\n document.write('"+_line+"');"; } return _content; } module.exports.wrapScript = function(content , blockType , deviceType){ if((blockType == 'js' || blockType == 'css') && typeof deviceType !='undefined'){ var repc = warpstr(content); var ret = ""; if(repc != ""){ var ret = '\n<script>\nif(glue.device.type =="'+deviceType+'"){'; ret += repc; ret += "\n}\n<\/script>" } content = ret; }else if((blockType == 'requirejs') && typeof deviceType !='undefined'){ content = cleancomment(content); var repx = /(?:[\s\S]*)<script>([\s\S]*)<\/script>/; content = content.replace(repx , function(match , content , offset , str){ var ret = '\n<script>\nif(glue.device.type =="'+deviceType+'"){'; ret += content; ret += "\n}\n<\/script>"; return ret; }); } return cleancomment(content); } module.exports.md5Suffix = function(content, algorithm, encoding) { var hash = crypto.createHash(algorithm); hash.update(content); hash = hash.digest(encoding); return hash.slice(0, 8); }; module.exports.compressContent = function(content){ var topLevel = UglifyJS.parse(content); topLevel.figure_out_scope(); var compressor = UglifyJS.Compressor(); var compressed_ast = topLevel.transform(compressor); compressed_ast.figure_out_scope(); compressed_ast.compute_char_frequency(); compressed_ast.mangle_names(); var stream = UglifyJS.OutputStream({}); compressed_ast.print(stream); var code = stream.toString(); // this is your minified code return code; } module.exports.getWithoutMd5Suffix = function(baseName){ return baseName.substring(0 , baseName.length-9); }