add bundle size check

This commit is contained in:
Xuan Son Nguyen 2024-12-13 12:22:07 +01:00
parent c0e5046acc
commit 41f04470d0

View file

@ -3,6 +3,8 @@ import { viteSingleFile } from 'vite-plugin-singlefile';
import path from 'path';
import fs from 'fs';
const MAX_BUNDLE_SIZE = 1024 * 1024; // only increase when absolutely necessary
const GUIDE_FOR_FRONTEND = `
<!--
This is a single file build of the frontend.
@ -26,6 +28,13 @@ const BUILD_PLUGINS = [
const outputIndexHtml = path.join(config.build.outDir, 'index.html');
const content = fs.readFileSync(outputIndexHtml, 'utf-8');
if (content.length > MAX_BUNDLE_SIZE) {
throw new Error(
`Bundle size is too large (${Math.ceil(content.length / 1024)} KB).\n` +
`Please reduce the size of the frontend or increase MAX_BUNDLE_SIZE in vite.config.js.\n`,
);
}
const targetOutputFile = path.join(config.build.outDir, '../../public/index.html');
fs.writeFileSync(targetOutputFile, GUIDE_FOR_FRONTEND + '\n' + content);
}