2023-03-07 20:27:20 +03:00
|
|
|
/* eslint-disable no-undef */
|
|
|
|
const assert = require('assert')
|
|
|
|
const chalk = require('chalk')
|
|
|
|
const fs = require('fs')
|
|
|
|
const gzipSize = require('gzip-size').sync
|
|
|
|
const path = require('path')
|
|
|
|
|
|
|
|
const buildDir = path.join(__dirname, '../build')
|
|
|
|
|
|
|
|
let entrypoints
|
|
|
|
try {
|
|
|
|
entrypoints = require(path.join(buildDir, 'asset-manifest.json')).entrypoints
|
|
|
|
} catch (e) {
|
|
|
|
console.log(chalk.yellow('You must build first: `yarn build`'))
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The last recorded size for these assets, as reported by `yarn build`.
|
2023-03-23 01:32:27 +03:00
|
|
|
const LAST_SIZE_MAIN_KB = 420
|
2023-03-07 20:27:20 +03:00
|
|
|
|
|
|
|
// This is the async-loaded js, called <number>.<hash>.js, with a matching css file.
|
2023-03-23 01:32:27 +03:00
|
|
|
const LAST_SIZE_ENTRY_KB = 1442
|
2023-03-07 20:27:20 +03:00
|
|
|
|
2023-03-14 22:38:39 +03:00
|
|
|
const SIZE_TOLERANCE_KB = 10
|
2023-03-07 20:27:20 +03:00
|
|
|
|
|
|
|
const jsEntrypoints = entrypoints.filter((entrypoint) => entrypoint.endsWith('js'))
|
|
|
|
assert(jsEntrypoints.length === 3)
|
|
|
|
|
|
|
|
let fail = false
|
|
|
|
console.log('File sizes after gzip:\n')
|
|
|
|
jsEntrypoints.forEach((entrypoint) => {
|
|
|
|
const name = entrypoint.match(/\/([\w\d-]*)\./)[1]
|
2023-03-10 01:49:39 +03:00
|
|
|
const size = gzipSize(fs.readFileSync(path.join(buildDir, entrypoint))) / 1024
|
2023-03-07 20:27:20 +03:00
|
|
|
|
2023-03-10 01:49:39 +03:00
|
|
|
let maxSize = LAST_SIZE_ENTRY_KB + SIZE_TOLERANCE_KB
|
2023-03-07 20:27:20 +03:00
|
|
|
if (name === 'runtime-main') {
|
|
|
|
return
|
|
|
|
} else if (name === 'main') {
|
2023-03-10 01:49:39 +03:00
|
|
|
maxSize = LAST_SIZE_MAIN_KB + SIZE_TOLERANCE_KB
|
2023-03-07 20:27:20 +03:00
|
|
|
}
|
|
|
|
|
2023-03-10 01:49:39 +03:00
|
|
|
const report = `\t${size.toFixed(2).padEnd(8)}kB\t${chalk.dim(
|
|
|
|
`max: ${maxSize.toFixed().padEnd(4)} kB`
|
|
|
|
)}\t${entrypoint}`
|
2023-03-07 20:27:20 +03:00
|
|
|
if (maxSize > size) {
|
2023-03-10 01:49:39 +03:00
|
|
|
console.log(chalk.green(report))
|
2023-03-07 20:27:20 +03:00
|
|
|
} else {
|
2023-03-10 01:49:39 +03:00
|
|
|
console.log(chalk.red(report), '\tdid you import an unnecessary dependency?')
|
2023-03-07 20:27:20 +03:00
|
|
|
fail = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (fail) {
|
|
|
|
console.log(chalk.yellow('\nOne or more of your files has grown too large.'))
|
|
|
|
console.log(chalk.yellow('Reduce the file size or update the size limit (in scripts/test-size.js)'))
|
|
|
|
process.exit(1)
|
|
|
|
}
|