UNPKG

@aligov/retcodelog

Version:

retcode log上报npm包,适用于 browser|nodejs|weex|E应用

160 lines (140 loc) 5.08 kB
const cp = require('child_process'); const etag = require('etag'); const path = require('path'); const globby = require('globby'); const fs = require('fs-extra'); const strip = require('strip-comments'); // const stripCssComments = require('strip-css-comments'); const cwd = process.cwd(); const logJsFile = './build/log.js'; const logDebugJsFile = './build/log.debug.js'; const FILE_OPTS = { encoding: 'utf-8' }; module.exports = function (grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), parseunicode: { dist: { src : ['build/**/*.js'], //the path to the file options: { charset: 'utf-8', //default utf8 type : 'js' //default js } } }, uglify: { main: { options: { ie8: true }, files: [ { src : ['build/log.debug.js'], dest : 'build/log.js', charset: 'utf-8', ext : '.js' } ] }, }, copy: { main: { files: [ { expand: true, cwd : "build/", src : ['log.js', 'log.debug.js'], dest : 'build/<%= pkg.version%>/' } ] } }, browserify: { browser: { src : [ './src/log-browser.js' ], charset: 'utf-8', dest : 'build/log.debug.js', } }, conventionalChangelog: { options: { changelogOpts: { // conventional-changelog options go here preset: 'angular' } }, release: { src: 'CHANGELOG.md' } } }); grunt.loadNpmTasks('grunt-browserify'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-parseunicode'); grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-conventional-changelog'); // 同步版本号 grunt.registerTask('sync', 'sync package version', function () { try { let logJs = grunt.file.read(logDebugJsFile, FILE_OPTS); grunt.log.writeln(cp.execSync('fie git sync')); logJs = grunt.template.process(logJs); grunt.file.write(logDebugJsFile, logJs, FILE_OPTS); grunt.log.oklns('sync package version success', grunt.config.get('pkg').version); } catch (err) { grunt.log.errorlns('sync package version error', err); } }); // 设置log.js cache grunt.registerTask('cache', 'set log js cache', function () { try { const cacheSettingFile = './.assetsmetafile'; const logJsStr = grunt.file.read(logJsFile, FILE_OPTS); const eTag = etag(logJsStr); // log.js cache时间为7天 const cacheSettingStr = `Cache-Control:max-age=604800,s-maxage=60\nETag:${eTag}`; grunt.file.write(cacheSettingFile, cacheSettingStr, FILE_OPTS); grunt.log.oklns('set cache success\n\n', cacheSettingStr); } catch (err) { grunt.log.errorlns('set cache error', err); } }); // 设置删除comments grunt.registerTask('strip', 'strip comments in build', function () { const libPaths = globby.sync([ './build/*', './build/*/*' ], { cwd, }); libPaths.forEach((item) => { // min 文件不用处理 if (item.indexOf('.min.') > -1) { return; } try{ const filename = path.basename(item); const extname = path.extname(item); const dir = item.substr(0, item.lastIndexOf('/')); if (extname === '.js') { const newCode = strip(fs.readFileSync(path.join(cwd, item), 'utf8')); fs.writeFileSync(path.join(cwd, `${dir}/${filename}`), newCode, 'utf8'); } else if (extname === '.css') { // no css file // const newCode = stripCssComments(fs.readFileSync(path.join(cwd, item), 'utf8')); // fs.writeFileSync(path.join(cwd, `${dir}/${filename}`), newCode, 'utf8'); } } catch(ex) { console.log(item); console.log(ex) } }); }); grunt.registerTask('default', ['browserify', 'sync', 'uglify', 'parseunicode', 'copy', 'cache', 'strip']); grunt.registerTask('changelog', ['conventionalChangelog']); }