Skip to content

Commit

Permalink
feat(build): add size report
Browse files Browse the repository at this point in the history
  • Loading branch information
ferferga committed Nov 4, 2024
1 parent c2b174d commit 05a26fa
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
7 changes: 4 additions & 3 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { defineConfig } from 'vite';
* TODO: Replace with @jellyfin-vue/vite-plugins after https://github.com/vitejs/vite/issues/5370
* is fixed
*/
import { JellyfinVueAnalysis, JellyfinVueChunking } from '../packages/vite-plugins';
import { BundleAnalysis, BundleChunking, BundleSizeReport } from '../packages/vite-plugins';
import { entrypoints, localeFilesFolder, srcRoot } from './scripts/paths';
import virtualModules from './scripts/virtual-modules';

Expand All @@ -28,8 +28,9 @@ export default defineConfig({
base: './',
cacheDir: '../node_modules/.cache/vite',
plugins: [
JellyfinVueAnalysis(),
JellyfinVueChunking(),
BundleAnalysis(),
BundleChunking(),
BundleSizeReport(),
Virtual(virtualModules),
VueRouter({
dts: './types/global/routes.d.ts',
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/configs/eslint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { getTSVueConfig } from './rules/typescript-vue';
export { json } from './rules/json';
export { getNodeFiles, getWorkerFiles } from './rules/env';
export { i18n } from './rules/i18n';
export * from './shared';
4 changes: 2 additions & 2 deletions packages/vite-plugins/eslint.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Linter } from 'eslint';
import { getBaseConfig, getTSVueConfig, getNodeFiles } from '@jellyfin-vue/configs/eslint';
import { getBaseConfig, getTSVueConfig, getNodeFiles, tsFiles } from '@jellyfin-vue/configs/eslint';

export default [
...getBaseConfig('@jellyfin-vue/vite-plugins'),
...getTSVueConfig(false, import.meta.dirname),
...getNodeFiles()
...getNodeFiles(tsFiles)
] satisfies Linter.Config[];
57 changes: 52 additions & 5 deletions packages/vite-plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { basename } from 'node:path';
import { glob, lstat } from 'node:fs/promises';
import prettyBytes from 'pretty-bytes';
import { visualizer } from 'rollup-plugin-visualizer';
import { normalizePath, type Plugin } from 'vite';
import type { RollupLog } from 'rollup';
import type { Plugin } from 'vite';

/**
* This plugin extracts the logic for the analyze commands, so the main Vite config is cleaner.
*/
export function JellyfinVueAnalysis(): Plugin {
export function BundleAnalysis(): Plugin {
const warnings: RollupLog[] = [];

return {
name: 'Jellyfin_Vue:analysis',
name: 'Jellyfin_Vue:bundle_analysis',
enforce: 'post',
config: (_, env) => {
if (env.mode === 'analyze:bundle') {
Expand Down Expand Up @@ -54,9 +57,9 @@ export function JellyfinVueAnalysis(): Plugin {
/**
* Creates the Rollup's chunking strategy of the application (for code-splitting)
*/
export function JellyfinVueChunking(): Plugin {
export function BundleChunking(): Plugin {
return {
name: 'Jellyfin_Vue:chunking',
name: 'Jellyfin_Vue:bundle_chunking',
config: () => ({
build: {
rollupOptions: {
Expand Down Expand Up @@ -93,3 +96,47 @@ export function JellyfinVueChunking(): Plugin {
})
};
}

/**
* Reports the total siz and also per file type
*/
export function BundleSizeReport(): Plugin {
const files = new Map<string, number>();
const sizes = new Map<string, number>();
let outDir: string;
let totalSize = 0;
const convert = (bytes: number) => prettyBytes(bytes, { minimumFractionDigits: 2 });

return {
name: 'Jellyfin_Vue:bundle_size_report',
apply: 'build',
closeBundle: async () => {
for await (const file of glob(`${outDir}/**/**`)) {
const stat = await lstat(file);

if (stat.isFile()) {
const extension = basename(file).split('.').at(-1);
const filenum = files.get(extension!) ?? 0;
const size = sizes.get(extension!) ?? 0;

files.set(extension!, filenum + 1);
sizes.set(extension!, size + stat.size);
totalSize += stat.size;
}
}

for (const [key, val] of sizes) {
const num = files.get(key)!;

console.info(
`There are ${num} ${key} ${num > 1 ? 'files' : 'file'} (${convert(val)})`
);
}

console.info(`Total size of the bundle: ${convert(totalSize)}`);
},
configResolved: (config) => {
outDir = normalizePath(config.build.outDir);
}
};
}
1 change: 1 addition & 0 deletions packages/vite-plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"devDependencies": {
"@jellyfin-vue/configs": "*",
"pretty-bytes": "6.1.1",
"rollup-plugin-visualizer": "5.12.0",
"vite": "5.4.9"
}
Expand Down

0 comments on commit 05a26fa

Please sign in to comment.