@speedy-js/depcost
Version:
[](https://npm.im/@speedy-js/depcost)
37 lines (30 loc) • 1.01 kB
JavaScript
const fs = require('fs')
const path = require('path')
/**
* @typedef {{ cwd?: string, filePath?: string, writeFile?: boolean }} HTMLReportOptions
*/
/**
* @param {any} results
* @param {HTMLReportOptions} options
* @returns {string}
*/
module.exports = function generateReportHTML(results, options = {}) {
results = results.sort((prev, next) => (prev.rawSize > next.rawSize
? 1 : prev.rawSize === next.rawSize
? 0 : -1))
const totalSize = results.reduce((memo, dep) => memo + dep.rawSize, 0)
const dataString = JSON.stringify(
results.map(ret => ({
item: ret.pkg,
count: ret.rawSize,
size: ret.size,
percent: Number((ret.rawSize / totalSize).toFixed(2)),
})),
)
const html = fs.readFileSync(path.join(__dirname, 'fixtures/template.html'), 'utf-8')
.replace('{{ data }}', dataString)
if (options.writeFile) {
fs.writeFileSync(path.join(options.cwd, options.filePath || 'depcost.html'), html, { encoding: 'utf-8' })
}
return html
}