Skip to content

Commit

Permalink
Add flag --silent for silent mode (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
binjospookie authored Mar 4, 2021
1 parent cf96128 commit 186684b
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ coverage/
.cache
dist/
node-warnings.logs
.idea/
17 changes: 13 additions & 4 deletions packages/size-limit/create-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function createJsonReporter (process) {
}
}

function createHumanReporter (process) {
function createHumanReporter (process, isSilentMode = false) {
function print (...lines) {
process.stdout.write(' ' + lines.join('\n ') + '\n')
}
Expand Down Expand Up @@ -77,7 +77,12 @@ function createHumanReporter (process) {
results (plugins, config) {
print('')
for (let check of config.checks) {
if (check.passed && config.hidePassed) continue
if (
(check.passed && config.hidePassed && !isSilentMode) ||
(isSilentMode && check.passed !== false)
) {
continue
}

let unlimited = typeof check.passed === 'undefined'
let rows = []
Expand Down Expand Up @@ -183,6 +188,10 @@ function createHumanReporter (process) {
}
}

module.exports = (process, isJSON) => {
return isJSON ? createJsonReporter(process) : createHumanReporter(process)
module.exports = (process, isJSON, isSilentMode) => {
if (isJSON) {
return createJsonReporter(process)
} else {
return createHumanReporter(process, isSilentMode)
}
}
3 changes: 3 additions & 0 deletions packages/size-limit/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = function parseArgs (plugins, argv) {
let args = { files: [] }
for (let i = 2; i < argv.length; i++) {
let arg = argv[i]

if (arg === '--limit') {
args.limit = argv[++i]
} else if (arg === '--debug') {
Expand Down Expand Up @@ -39,6 +40,8 @@ module.exports = function parseArgs (plugins, argv) {
args.highlightLess = true
} else if (arg[0] !== '-') {
args.files.push(arg)
} else if (arg === '--silent') {
args.isSilentMode = arg
} else if (arg !== '--json') {
throw new SizeLimitError('unknownArg', arg)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/size-limit/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ module.exports = async process => {
return process.argv.some(i => i === arg)
}
let isJsonOutput = hasArg('--json')
let reporter = createReporter(process, isJsonOutput)
let isSilentMode = hasArg('--silent')
let reporter = createReporter(process, isJsonOutput, isSilentMode)
let help = createHelp(process)
let config, args

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,41 @@ exports[`renders failed results 1`] = `
"
`;

exports[`renders list of failed and success checks in silent mode 1`] = `
"
small fail
Package size limit has exceeded by 1 B
Size limit: 102400 B
Size: 102401 B with all dependencies, minified and gzipped
big fail
Package size limit has exceeded by 100 B
Size limit: 100 KB
Size: 100.1 KB with all dependencies, minified and gzipped
"
`;

exports[`renders list of failed checks in silent mode 1`] = `
"
small fail
Package size limit has exceeded by 1 B
Size limit: 102400 B
Size: 102401 B with all dependencies, minified and gzipped
big fail
Package size limit has exceeded by 100 B
Size limit: 100 KB
Size: 100.1 KB with all dependencies, minified and gzipped
"
`;

exports[`renders list of success checks in silent mode 1`] = `
"
"
`;

exports[`renders result for file without gzip 1`] = `
"
Size limit: 99 B
Expand Down
18 changes: 18 additions & 0 deletions packages/size-limit/test/__snapshots__/run.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@ exports[`run shows error on time bigger than limit 1`] = `
"
`;

exports[`run shows error on time bigger than limit. in silent mode 1`] = `
"
Total time limit has exceeded
Time limit: 1 s
Size: 31 B  with all dependencies, minified and gzipped
Loading time: 10 ms on slow 3G
Running time: 1 s  on Snapdragon 410
Total time: 1.1 s
Try to reduce size or increase limit in \\"size-limit\\" section of package.json
"
`;

exports[`run shows error when using brotli without webpack 1`] = `
" ERROR  Config option brotli needs @size-limit/webpack plugin
"
Expand Down Expand Up @@ -308,6 +321,11 @@ Check out docs for more complicated cases
"
`;

exports[`run shows nothing in silent mode and success check 1`] = `
"
"
`;

exports[`run shows size-limit dependency warning 1`] = `""`;

exports[`run shows webpack-related help 1`] = `
Expand Down
104 changes: 102 additions & 2 deletions packages/size-limit/test/create-reporter.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let createReporter = require('../create-reporter')

function results (types, config, isJSON = false) {
function results (types, config, isJSON = false, isSilentMode = false) {
let stdout = ''
let plugins = {
has (type) {
Expand All @@ -14,7 +14,7 @@ function results (types, config, isJSON = false) {
}
}
}
let reporter = createReporter(process, isJSON)
let reporter = createReporter(process, isJSON, isSilentMode)
reporter.results(plugins, config)
return stdout
}
Expand Down Expand Up @@ -55,6 +55,106 @@ it('renders results', () => {
).toMatchSnapshot()
})

it('renders list of success checks in silent mode', () => {
expect(
results(
['webpack', 'time'],
{
checks: [
{
name: 'limitless',
size: 10,
config: 'a',
loadTime: 0.1,
runTime: 0.5,
time: 0.6
},
{
name: 'size',
size: 102400,
sizeLimit: 102400,
loadTime: 1,
runTime: 2,
time: 3,
passed: true
}
]
},
false,
true
)
).toMatchSnapshot()
})

it('renders list of failed checks in silent mode', () => {
expect(
results(
['webpack', 'time'],
{
checks: [
{
name: 'small fail',
size: 102401,
sizeLimit: 102400,
passed: false
},
{
name: 'big fail',
size: 102500,
sizeLimit: 102400,
passed: false
}
]
},
false,
true
)
).toMatchSnapshot()
})

it('renders list of failed and success checks in silent mode', () => {
expect(
results(
['webpack', 'time'],
{
checks: [
{
name: 'small fail',
size: 102401,
sizeLimit: 102400,
passed: false
},
{
name: 'limitless',
size: 10,
config: 'a',
loadTime: 0.1,
runTime: 0.5,
time: 0.6
},
{
name: 'size',
size: 102400,
sizeLimit: 102400,
loadTime: 1,
runTime: 2,
time: 3,
passed: true
},
{
name: 'big fail',
size: 102500,
sizeLimit: 102400,
passed: false
}
]
},
false,
true
)
).toMatchSnapshot()
})

it('renders failed results', () => {
expect(
results(['file'], {
Expand Down
16 changes: 16 additions & 0 deletions packages/size-limit/test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,22 @@ describe(`run`, () => {
expect(history.stdout).toMatchSnapshot()
})

it('shows error on time bigger than limit. in silent mode', async () => {
let [process, history] = createProcess('integration', [
'--silent',
'--limit',
'1 s'
])
await run(process)
expect(history.exitCode).toEqual(1)
expect(history.stderr).toEqual('')
expect(history.stdout).toMatchSnapshot()
})

it('shows nothing in silent mode and success check', async () => {
expect(await check('integration', ['--silent'])).toMatchSnapshot()
})

it('returns zero bytes for empty file', async () => {
expect(await check('zero')).toMatchSnapshot()
})
Expand Down

0 comments on commit 186684b

Please sign in to comment.