Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: add bundle size reporter
- Loading branch information
Showing
8 changed files
with
116 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as colors from 'colorette' | ||
import { Logger } from '../log' | ||
|
||
const prettyBytes = (bytes: number) => { | ||
const unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] | ||
const exp = Math.floor(Math.log(bytes) / Math.log(1024)) | ||
return `${(bytes / Math.pow(1024, exp)).toFixed(2)} ${unit[exp]}` | ||
} | ||
|
||
const getLengthOfLongestString = (strings: string[]) => { | ||
return strings.reduce((max, str) => { | ||
return Math.max(max, str.length) | ||
}, 0) | ||
} | ||
|
||
const padRight = (str: string, maxLength: number) => { | ||
return str + ' '.repeat(maxLength - str.length) | ||
} | ||
|
||
export const reportSize = ( | ||
logger: Logger, | ||
format: string, | ||
files: { [name: string]: number } | ||
) => { | ||
const filenames = Object.keys(files) | ||
const maxLength = getLengthOfLongestString(filenames) + 1 | ||
for (const name of filenames) { | ||
logger.success( | ||
format, | ||
`${colors.bold(padRight(name, maxLength))}${colors.green( | ||
prettyBytes(files[name]) | ||
)}` | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Plugin } from '../plugin' | ||
import { reportSize } from '../lib/report-size' | ||
|
||
export const sizeReporter = (): Plugin => { | ||
return { | ||
name: 'size-reporter', | ||
|
||
buildEnd({ metafile }) { | ||
if (!metafile) return | ||
reportSize( | ||
this.logger, | ||
this.format, | ||
Object.keys(metafile.outputs).reduce((res, name) => { | ||
return { | ||
...res, | ||
[name]: metafile!.outputs[name].bytes, | ||
} | ||
}, {}) | ||
) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters