Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import chalk from 'chalk'
import Table = require('cli-table3')
import * as log from 'fancy-log'
import * as gzipSize from 'gzip-size'
import * as path from 'path'
import * as PluginError from 'plugin-error'
import * as prettyBytes from 'pretty-bytes'
import * as through from 'through2'
export const sizer = (options: any) => {
options = options || {}
options.gzip = options.gzip !== undefined ? options.gzip : false
options.minifier = options.minifier !== undefined ? options.minifier : null
options.total = options.total !== undefined ? options.total : true
options.fail = options.fail !== undefined ? options.fail : false
const tableHeadCols = ['File', 'Original']
let fail = false
Iif (options.gzip) {
tableHeadCols.push('Gzipped')
}
Iif (options.minifier) {
tableHeadCols.push('Minified')
if (options.gzip) {
tableHeadCols.push('Gzipped')
}
}
let fileCount = 0
let totalSize = 0
let totalGzippedSize = 0
let totalMinifiedSize = 0
let totalMinifiedGzippedSize = 0
let gzippedSize
let minifiedGzippedSize
let minified
const table = new Table({
chars: {
bottom: '═',
'bottom-left': '╚',
'bottom-mid': '╧',
'bottom-right': '╝',
left: '║',
'left-mid': '╟',
mid: '─',
'mid-mid': '┼',
middle: '│',
right: '║',
'right-mid': '╢',
top: '═',
'top-left': '╔',
'top-mid': '╤',
'top-right': '╗',
},
colAligns: ['left', 'right', 'right', 'right', 'right'],
head: tableHeadCols,
// colWidths: [ 30, 12, 12, 12, 12 ],
style: {
head: ['cyan'],
},
})
const getSizeToDisplay = (size: any, key: any, filename: any) => {
const max = options[filename] || options['*']
let value
value = max ? max[key] : options[key]
if (value && size > value) {
fail = true
return chalk.red(prettyBytes(size))
}
return prettyBytes(size)
}
return through.obj(
(file, enc, callback) => {
if (file.isNull()) {
callback(null, file)
return
}
if (file.isStream()) {
callback(new PluginError('gulp-sizereport', 'Streaming not supported'))
return
}
gzippedSize = gzipSize.sync(file.contents)
totalSize += file.contents.length
totalGzippedSize += gzippedSize
fileCount++
const row = [
path.join(options.dest, file.relative),
getSizeToDisplay(file.contents.length, 'maxSize', file.relative),
]
if (options.gzip) {
row.push(getSizeToDisplay(gzippedSize, 'maxGzippedSize', file.relative))
}
if (typeof options.minifier === 'function') {
minified = options.minifier('' + file.contents, file.relative)
totalMinifiedSize += minified.length
row.push(getSizeToDisplay(minified.length, 'maxMinifiedSize', file.relative))
if (options.gzip) {
minifiedGzippedSize = gzipSize.sync(minified)
totalMinifiedGzippedSize += minifiedGzippedSize
row.push(getSizeToDisplay(minifiedGzippedSize, 'maxMinifiedGzippedSize', file.relative))
}
}
table.push(row)
callback(null, file)
},
callback => {
if (options.title) {
// eslint-disable-next-line
log(':: ' + chalk.bold(options.title) + ' ::')
}
if (fileCount > 0) {
if (options.total === true) {
const row = ['', chalk.bold(getSizeToDisplay(totalSize, 'maxTotalSize', '*'))]
if (options.gzip) {
row.push(chalk.bold(getSizeToDisplay(totalGzippedSize, 'maxTotalGzippedSize', '*')))
}
if (options.minifier) {
row.push(chalk.bold(getSizeToDisplay(totalMinifiedSize, 'maxTotalMinifiedSize', '*')))
if (options.gzip) {
row.push(
chalk.bold(
getSizeToDisplay(totalMinifiedGzippedSize, 'maxTotalMinifiedGzippedSize', '*')
)
)
}
}
table.push(row)
}
options.logger('Size report')
console.log(table.toString())
if (options.fail && fail) {
callback()
}
}
callback()
}
)
}
|