-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[ENHANCEMENT] Improve readability of asset-size printer and include Brotli sizes #10044
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,46 +5,48 @@ const path = require('path'); | |
const walkSync = require('walk-sync'); | ||
const workerpool = require('workerpool'); | ||
|
||
module.exports = class AssetPrinterSize { | ||
module.exports = class AssetSizePrinter { | ||
constructor(options) { | ||
Object.assign(this, options); | ||
this.outputPath = options.outputPath; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes it clearer what's expected / passed in. |
||
this.ui = options.ui; | ||
} | ||
|
||
print() { | ||
const { filesize } = require('filesize'); | ||
let ui = this.ui; | ||
async print() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switched to "async - await" for both print functions. |
||
let files = await this.makeAssetSizesObject(); | ||
|
||
return this.makeAssetSizesObject().then((files) => { | ||
if (files.length !== 0) { | ||
ui.writeLine(chalk.green('File sizes:')); | ||
return files.forEach((file) => { | ||
let sizeOutput = filesize(file.size); | ||
if (file.showGzipped) { | ||
sizeOutput += ` (${filesize(file.gzipSize)} gzipped)`; | ||
} | ||
if (files.length !== 0) { | ||
const { filesize } = require('filesize'); | ||
const { getBorderCharacters, table } = require('table'); | ||
|
||
ui.writeLine(chalk.blue(` - ${file.name}: `) + sizeOutput); | ||
let tableHeaders = ['File', 'Size', 'Size (Gzip)', 'Size (Brotli)']; | ||
let tableData = [tableHeaders.map((header) => chalk.bold(header))]; | ||
|
||
files | ||
.sort((a, b) => (a.size > b.size ? -1 : 1)) | ||
.forEach((file) => { | ||
tableData.push([ | ||
chalk.green(file.name), | ||
filesize(file.size), | ||
file.size ? filesize(file.gzipSize) : '/', | ||
file.size ? filesize(file.brotliSize) : '/', | ||
]); | ||
}); | ||
} else { | ||
ui.writeLine(chalk.red(`No asset files found in the path provided: ${this.outputPath}`)); | ||
} | ||
}); | ||
|
||
this.ui.writeLine('File sizes:'); | ||
this.ui.write(table(tableData, { border: getBorderCharacters('norc') })); | ||
} else { | ||
this.ui.writeLine(chalk.red(`No asset files found in the provided path: ${this.outputPath}`)); | ||
} | ||
} | ||
|
||
printJSON() { | ||
let ui = this.ui; | ||
return this.makeAssetSizesObject().then((files) => { | ||
if (files.length !== 0) { | ||
let entries = files.map((file) => ({ | ||
name: file.name, | ||
size: file.size, | ||
gzipSize: file.gzipSize, | ||
})); | ||
ui.writeLine(JSON.stringify({ files: entries })); | ||
} else { | ||
ui.writeLine(chalk.red(`No asset files found in the path provided: ${this.outputPath}`)); | ||
} | ||
}); | ||
async printJSON() { | ||
let files = await this.makeAssetSizesObject(); | ||
|
||
if (files.length !== 0) { | ||
this.ui.writeLine(JSON.stringify({ files })); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} else { | ||
this.ui.writeLine(chalk.red(`No asset files found in the provided path: ${this.outputPath}`)); | ||
} | ||
} | ||
|
||
async makeAssetSizesObject() { | ||
|
@@ -83,7 +85,7 @@ module.exports = class AssetPrinterSize { | |
.map((x) => path.join(outputPath, x)); | ||
} catch (e) { | ||
if (e !== null && typeof e === 'object' && e.code === 'ENOENT') { | ||
throw new Error(`No asset files found in the path provided: ${outputPath}`); | ||
throw new Error(`No asset files found in the provided path: ${outputPath}`); | ||
} else { | ||
throw e; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. File was also renamed from |
||
|
||
const fs = require('fs'); | ||
const zlib = require('zlib'); | ||
|
||
module.exports = function (file) { | ||
let contentsBuffer = fs.readFileSync(file); | ||
|
||
return { | ||
brotliSize: zlib.brotliCompressSync(contentsBuffer).length, | ||
gzipSize: zlib.gzipSync(contentsBuffer).length, | ||
name: file, | ||
size: contentsBuffer.length, | ||
}; | ||
}; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use
AssetSizePrinter
everywhere else, and the filename isasset-size-printer
as well.