diff --git a/src/config.js b/src/config.js index c1405a1b..796a5cab 100644 --- a/src/config.js +++ b/src/config.js @@ -20,6 +20,7 @@ program '-c, --compression [compression]', 'specify which compression algorithm to use' ) + .option('--report-json', 'output json') .parse(process.argv) let configFromCli diff --git a/src/report-json.js b/src/report-json.js new file mode 100644 index 00000000..95d41f1e --- /dev/null +++ b/src/report-json.js @@ -0,0 +1,12 @@ +const fs = require('fs') + +let report = false +if (process.argv.indexOf('--report-json') !== -1) report = true + +const reportJson = results => { + if(report){ + fs.writeFileSync('bundlesize-report.json', JSON.stringify(results,null,2)) + } +} + +module.exports = reportJson diff --git a/src/reporter.js b/src/reporter.js index 816c1a3a..d5c3d707 100644 --- a/src/reporter.js +++ b/src/reporter.js @@ -5,6 +5,7 @@ const build = require('./build') const api = require('./api') const debug = require('./debug') const shortener = require('./shortener') +const reportJson = require('./report-json') const setBuildStatus = ({ url, @@ -93,14 +94,14 @@ const analyse = ({ files, masterValues }) => { else if size > master, warn + pass else yay + pass */ - + if (size > maxSize) { fail = true if (prettySize) message += `> maxSize ${prettySize} ${compressionText}` - error(message, { fail: false, label: 'FAIL' }) + if (process.argv.indexOf('--report-json') === -1) error(message, { fail: false, label: 'FAIL' }) } else if (!master) { if (prettySize) message += `< maxSize ${prettySize} ${compressionText}` - info('PASS', message) + if (process.argv.indexOf('--report-json') === -1) info('PASS', message) } else { if (prettySize) message += `< maxSize ${prettySize} ${compressionText}` const diff = size - master @@ -159,8 +160,11 @@ const compare = (files, masterValues = {}) => { totalMaxSize: results.reduce((acc, result) => acc + result.maxSize, 0) }) - let fail = results.filter(result => result.fail).length > 0 + let fail; + if (process.argv.indexOf('--report-json') === -1) fail = results.filter(result => result.fail).length > 0 + reportJson(results); report({ files, globalMessage, fail }) + } const reporter = files => {