Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option --report-json #323

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ program
'-c, --compression [compression]',
'specify which compression algorithm to use'
)
.option('--report-json', 'output json')
.parse(process.argv)

let configFromCli
Expand Down
12 changes: 12 additions & 0 deletions src/report-json.js
Original file line number Diff line number Diff line change
@@ -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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to customize the report filename?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course!
It's possible, if I add args--report-json-name.

}
}

module.exports = reportJson
12 changes: 8 additions & 4 deletions src/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 => {
Expand Down