File size: 2,553 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { readFile, stat } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";

const scriptDirectory = dirname(fileURLToPath(import.meta.url));
const outputDirectory = join(scriptDirectory, "..", "dist");
const indexPath = join(outputDirectory, "index.html");
const maximumRawBytes = 10 * 1024 * 1024;
const maximumBrotliBytes = 3 * 1024 * 1024;

const html = await readFile(indexPath, "utf-8");
const assets = new Set(
  [...html.matchAll(/\/assets\/[^"' ]+\.(?:css|js)/g)].map(([asset]) => asset),
);

const initialJavaScriptAssets = [...assets].filter((asset) =>
  asset.endsWith(".js"),
);
const initialJavaScriptNames = new Set(
  initialJavaScriptAssets.map((asset) => asset.split("/").at(-1)),
);
const importGraph = new Map();

for (const asset of initialJavaScriptAssets) {
  const source = await readFile(join(outputDirectory, asset), "utf-8");
  const dependencies = new Set(
    [...source.matchAll(/\b(?:from|import)\s*\(?["']\.\/([^"']+\.js)["']/g)]
      .map((match) => match[1])
      .filter((dependency) => initialJavaScriptNames.has(dependency)),
  );
  importGraph.set(asset.split("/").at(-1), dependencies);
}

const visited = new Set();
const active = new Set();
const stack = [];

function findImportCycle(asset) {
  if (active.has(asset)) {
    return [...stack.slice(stack.indexOf(asset)), asset];
  }
  if (visited.has(asset)) {
    return null;
  }

  visited.add(asset);
  active.add(asset);
  stack.push(asset);
  for (const dependency of importGraph.get(asset) ?? []) {
    const cycle = findImportCycle(dependency);
    if (cycle) {
      return cycle;
    }
  }
  stack.pop();
  active.delete(asset);
  return null;
}

for (const asset of initialJavaScriptNames) {
  const cycle = findImportCycle(asset);
  if (cycle) {
    throw new Error(`Initial JavaScript import cycle: ${cycle.join(" -> ")}`);
  }
}

let rawBytes = 0;
let brotliBytes = 0;
for (const asset of assets) {
  const path = join(outputDirectory, asset);
  rawBytes += (await stat(path)).size;
  brotliBytes += (await stat(`${path}.br`)).size;
}

const toMiB = (bytes) => (bytes / 1024 / 1024).toFixed(2);
console.log(
  `Initial bundle: ${toMiB(rawBytes)} MiB raw, ` +
    `${toMiB(brotliBytes)} MiB Brotli across ${assets.size} assets.`,
);

if (rawBytes > maximumRawBytes) {
  throw new Error(`Initial raw bundle exceeds ${toMiB(maximumRawBytes)} MiB.`);
}
if (brotliBytes > maximumBrotliBytes) {
  throw new Error(
    `Initial Brotli bundle exceeds ${toMiB(maximumBrotliBytes)} MiB.`,
  );
}