* Guard the bundle size * * @param limit size in kB
(limit: number)
| 378 | * @param limit size in kB |
| 379 | */ |
| 380 | function bundleSizeLimit(limit: number): Plugin { |
| 381 | let size = 0 |
| 382 | |
| 383 | return { |
| 384 | name: 'bundle-limit', |
| 385 | generateBundle(_, bundle) { |
| 386 | if (this.meta.watchMode) return |
| 387 | |
| 388 | size = Buffer.byteLength( |
| 389 | Object.values(bundle) |
| 390 | .map((i) => ('code' in i ? i.code : '')) |
| 391 | .join(''), |
| 392 | 'utf-8', |
| 393 | ) |
| 394 | }, |
| 395 | closeBundle() { |
| 396 | if (this.meta.watchMode) return |
| 397 | |
| 398 | const kb = size / 1000 |
| 399 | if (kb > limit) { |
| 400 | this.error( |
| 401 | `Bundle size exceeded ${limit} kB, current size is ${kb.toFixed( |
| 402 | 2, |
| 403 | )}kb.`, |
| 404 | ) |
| 405 | } |
| 406 | }, |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // #endregion |