| 87 | * @since 4.0.0 |
| 88 | */ |
| 89 | export class Reporter extends Context.Service<Reporter>()( |
| 90 | "@effect/bundle/Reporter", |
| 91 | { |
| 92 | make: Effect.gen(function*() { |
| 93 | const fs = yield* FileSystem.FileSystem |
| 94 | const path = yield* Path.Path |
| 95 | const { fixtures, fixturesDir } = yield* Fixtures |
| 96 | const rollup = yield* Rollup |
| 97 | const currentDirectory = path.resolve(fileURLToPath(new URL("../../../../", import.meta.url))) |
| 98 | |
| 99 | const calculateDifference = (current: BundleStats, previous: BundleStats) => { |
| 100 | const currSize = current.sizeInBytes |
| 101 | const prevSize = previous.sizeInBytes |
| 102 | const diff = currSize - prevSize |
| 103 | const diffPct = prevSize === 0 ? 0 : (Math.abs(diff) / prevSize) * 100 |
| 104 | const currKb = (currSize / 1000).toFixed(2) |
| 105 | const prevKb = (prevSize / 1000).toFixed(2) |
| 106 | const diffKb = (Math.abs(diff) / 1000).toFixed(2) |
| 107 | const filename = path.basename(current.path) |
| 108 | return { |
| 109 | diff, |
| 110 | diffPct, |
| 111 | currKb, |
| 112 | prevKb, |
| 113 | diffKb, |
| 114 | filename |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | const createComparisonReport = ( |
| 119 | entries: ReadonlyArray<{ |
| 120 | readonly current: BundleStats |
| 121 | readonly previous: BundleStats |
| 122 | readonly filename: string |
| 123 | }> |
| 124 | ): string => { |
| 125 | const lines: Array<string> = [ |
| 126 | "| File Name | Current Size | Previous Size | Difference |", |
| 127 | "|:----------|:------------:|:-------------:|:----------:|" |
| 128 | ] |
| 129 | for (const { current, previous, filename } of entries) { |
| 130 | const comparison = calculateDifference(current, previous) |
| 131 | const currKb = `${comparison.currKb} KB` |
| 132 | const prevKb = `${comparison.prevKb} KB` |
| 133 | const diffKb = `${comparison.diffKb} KB` |
| 134 | const diffPct = `${comparison.diffPct.toFixed(2)}%` |
| 135 | const sign = comparison.diff === 0 ? "" : comparison.diff > 0 ? "+" : "-" |
| 136 | const line = `| \`${filename}\` | ${currKb} | ${prevKb} | ${sign}${diffKb} (${sign}${diffPct}) |` |
| 137 | lines.push(line) |
| 138 | } |
| 139 | return lines.join("\n") + "\n" |
| 140 | } |
| 141 | |
| 142 | const createReport = (curr: ReadonlyArray<BundleStats>, prev: ReadonlyArray<BundleStats>): string => { |
| 143 | const entries: Array<{ |
| 144 | readonly current: BundleStats |
| 145 | readonly previous: BundleStats |
| 146 | readonly filename: string |