All files / scripts purge-css.ts

66.12% Statements 41/62
66.66% Branches 24/36
50% Functions 5/10
68.33% Lines 41/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287                                                                                          1x                                                   11x 11x       11x 15x 15x 3x             3x 12x 4x     11x                     2x                                                                                                                                 3x 3x 3x 3x             3x 3x 3x     3x       3x 1x                     2x 2x 2x 2x           2x 1x           1x 1x         1x 2x 2x         2x         2x 2x 2x 2x             1x                                                                   1x   1x                
/**
 * Purge unused CSS from the production build.
 *
 * Scans every `dist/**\/*.html` page (which already includes the ~3 500
 * generated news articles, dashboards, sitemaps and political-intelligence
 * pages emitted by `vite-plugin-static-pages.js`), all first-party JS
 * bundled into `dist/` **and** the source trees `js/` + `src/browser/`,
 * then rewrites each stylesheet shipped to S3 in-place โ€” keeping only the
 * selectors that those pages or their runtime JS reference.
 *
 * The source-tree scan is intentional: Vite tree-shakes / mangles class
 * strings during bundling, so a class that only appears in `src/browser`
 * source might not survive in the emitted JS even though the runtime
 * still toggles it (e.g. via `classList.add('hidden')`). Including the
 * unminified sources is a safety net against accidentally purging those
 * runtime-toggled classes; it is the right trade-off for a static-site
 * pipeline where the source corpus is small relative to the dist tree.
 *
 * Targets:
 *   - `dist/styles.css`           (legacy non-hashed root copy that
 *                                   `scripts/deploy-s3.sh` cache-busts on
 *                                   every push)
 *   - `dist/assets/styles-*.css`  (Vite-hashed bundle linked from every
 *                                   modern page; the static-pages plugin
 *                                   rewrites the `<link href>` to it)
 *
 * Filenames are preserved (PurgeCSS only mutates contents) so all
 * existing `<link>` hrefs and CloudFront URLs continue to work.
 *
 * The safelist captures classes/attributes that are added at runtime by
 * the theme switcher (`data-theme="dark"` / `light`), the lazy-loaded
 * dashboard / chart code, Mermaid diagrams (which inject `mermaid-*`
 * SVGs only after JS executes), and the article-type selectors that may
 * not appear in every sampled HTML file but are used by news articles
 * not yet in the corpus when this script runs locally.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
import { promises as fs } from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
import { PurgeCSS } from 'purgecss';
 
const projectRoot = path.resolve(
  path.dirname(fileURLToPath(import.meta.url)),
  '..',
);
 
interface PurgeStat {
  /** Stylesheet path relative to projectRoot. */
  file: string;
  /** Original byte size of the stylesheet on disk. */
  before: number;
  /** Byte size after PurgeCSS rewrite. */
  after: number;
}
 
/**
 * Recursively collect every file under `dir` whose basename matches one
 * of the supplied extensions. Skips `node_modules`, `.git` and
 * `.vite` directories so we never accidentally feed the purger huge
 * vendor blobs.
 */
async function walk(
  dir: string,
  exts: ReadonlySet<string>,
  out: string[] = [],
): Promise<string[]> {
  let entries: import('node:fs').Dirent[];
  try {
    entries = await fs.readdir(dir, { withFileTypes: true });
  } catch {
    return out;
  }
  for (const entry of entries) {
    const full = path.join(dir, entry.name);
    if (entry.isDirectory()) {
      Iif (
        entry.name === 'node_modules' ||
        entry.name === '.git' ||
        entry.name === '.vite'
      ) {
        continue;
      }
      await walk(full, exts, out);
    } else if (exts.has(path.extname(entry.name).toLowerCase())) {
      out.push(full);
    }
  }
  return out;
}
 
/**
 * Build the PurgeCSS safelist. Anything that is added to the DOM only
 * after JavaScript runs (theme switcher, Mermaid SVG IDs, Chart.js
 * tooltip nodes, lazy-loaded dashboard cards) MUST live here, otherwise
 * the purger will drop the matching CSS rules and the live site will
 * regress visually.
 */
function buildSafelist() {
  return {
    /* Selectors kept verbatim regardless of HTML scan */
    standard: [
      /^html$/,
      /^body$/,
      /:root/,
      /^dark-mode$/,
      /^light-mode$/,
      /^theme-transition$/,
      /^visible$/,
      /^hidden$/,
      /^loading$/,
      /^error$/,
      /^block$/,
      /^none$/,
      /aria-/,
    ],
    /* Pattern-matched selectors (and any descendants) preserved */
    deep: [
      /^mermaid/i,
      /^chartjs/i,
      /^chart-/i,
      /^hljs/i, // syntax highlighting on news articles
      /article-type-/,
      /data-theme/,
      /^dashboard-/,
      /^cia-/,
      /^cyberpunk/,
      /^swot/,
      /^mindmap/,
      /^reader-guide/,
    ],
    /* Greedy: keep entire selector chain if any token matches */
    greedy: [/article-type-/, /data-theme/],
    /* CSS custom properties โ€” `variables: false` below disables PurgeCSS
     * variable removal entirely; this entry is a defensive safety net in
     * case that flag is ever flipped on. */
    variables: [/--/],
    /* @keyframes โ€” `keyframes: false` below disables removal entirely;
     * this entry is a defensive safety net for the same reason. */
    keyframes: [/.*/],
  };
}
 
/**
 * Options for {@link purge}.
 */
interface PurgeOptions {
  /**
   * When `true` (default, production behaviour), also scan the
   * `js/` and `src/browser/` source trees in addition to the `dist/`
   * output. This is the safety net described in the module header
   * against tree-shaking eliminating runtime-toggled class strings.
   *
   * Tests against in-tree fixtures should pass `false` so the test
   * stays decoupled from the main repo source corpus and remains
   * deterministic if unrelated source files change.
   */
  scanSourceTree?: boolean;
}
 
async function purge(
  distDir: string,
  options: PurgeOptions = {},
): Promise<PurgeStat[]> {
  const { scanSourceTree = true } = options;
  const htmlExts: ReadonlySet<string> = new Set(['.html']);
  const jsExts: ReadonlySet<string> = new Set(['.js', '.mjs']);
  const cssExts: ReadonlySet<string> = new Set(['.css']);
 
  /* Collect content sources from the built output (HTML + emitted JS).
   * Adding the source `js/` and `src/browser/` directories captures any
   * class strings that survive minification but were rewritten in the
   * built bundles. Tests opt out via `scanSourceTree: false` so the
   * fixture is the only content corpus considered. */
  const htmlFiles = await walk(distDir, htmlExts);
  const jsFiles = await walk(distDir, jsExts);
  const srcJs = scanSourceTree
    ? await walk(path.join(projectRoot, 'js'), jsExts)
    : [];
  const srcTs = scanSourceTree
    ? await walk(path.join(projectRoot, 'src', 'browser'), new Set(['.ts']))
    : [];
 
  if (htmlFiles.length === 0) {
    throw new Error(
      `[purge-css] No HTML files found under ${distDir}. ` +
        `Run \`npm run build\` first.`,
    );
  }
 
  /* Stylesheets to purge.  We only purge the two targets actually
   * shipped to S3: the legacy root `dist/styles.css` and the Vite-hashed
   * `dist/assets/styles-*.css`.  Component sub-stylesheets are inlined
   * into one of those by Vite's CSS bundler, so they don't need a
   * separate pass. */
  const cssCandidates = await walk(distDir, cssExts);
  const cssTargets = cssCandidates.filter((file) => {
    const rel = path.relative(distDir, file);
    return (
      rel === 'styles.css' ||
      /^assets[/\\]styles-[A-Za-z0-9_-]+\.css$/.test(rel)
    );
  });
 
  if (cssTargets.length === 0) {
    throw new Error(
      `[purge-css] No styles.css targets found under ${distDir}. ` +
        `Expected dist/styles.css or dist/assets/styles-*.css.`,
    );
  }
 
  const safelist = buildSafelist();
  const stats: PurgeStat[] = [];
 
  /* PurgeCSS returns one result per `css` entry, in order, so we run
   * one pass per stylesheet to get accurate per-file size deltas and
   * to avoid cross-contamination if Vite ever emits more than one. */
  for (const cssPath of cssTargets) {
    const before = (await fs.stat(cssPath)).size;
    const result = await new PurgeCSS().purge({
      content: [...htmlFiles, ...jsFiles, ...srcJs, ...srcTs],
      css: [cssPath],
      safelist,
      defaultExtractor: (content) =>
        content.match(/[A-Za-z0-9_-]+/g) ?? [],
      keyframes: false, // do not attempt to remove unused @keyframes (Chart.js / Mermaid inject animation names at runtime)
      fontFace: true, // remove unused @font-face
      variables: false, // do not attempt to remove unused CSS variables (theme tokens are referenced from JS-set inline styles)
    });
    const purged = result[0]?.css ?? '';
    await fs.writeFile(cssPath, purged, 'utf8');
    const after = Buffer.byteLength(purged, 'utf8');
    stats.push({
      file: path.relative(projectRoot, cssPath),
      before,
      after,
    });
  }
 
  return stats;
}
 
function fmtKb(bytes: number): string {
  return `${(bytes / 1024).toFixed(1)} KiB`;
}
 
async function main(): Promise<void> {
  const distArg = process.argv[2] ?? path.join(projectRoot, 'dist');
  const distDir = path.resolve(distArg);
  console.log(`๐Ÿงน Purging unused CSS in ${distDir}โ€ฆ`);
 
  const stats = await purge(distDir);
  for (const s of stats) {
    const saved = s.before - s.after;
    const pct = s.before > 0 ? ((saved / s.before) * 100).toFixed(1) : '0.0';
    console.log(
      `  โ€ข ${s.file}: ${fmtKb(s.before)} โ†’ ${fmtKb(s.after)} ` +
        `(saved ${fmtKb(saved)}, ${pct}%)`,
    );
  }
  const totalBefore = stats.reduce((acc, s) => acc + s.before, 0);
  const totalAfter = stats.reduce((acc, s) => acc + s.after, 0);
  const totalSaved = totalBefore - totalAfter;
  const totalPct =
    totalBefore > 0 ? ((totalSaved / totalBefore) * 100).toFixed(1) : '0.0';
  console.log(
    `โœ… Purge complete: ${fmtKb(totalBefore)} โ†’ ${fmtKb(totalAfter)} ` +
      `(saved ${fmtKb(totalSaved)}, ${totalPct}%)`,
  );
}
 
/* Allow `import { purge }` from tests without triggering the CLI. */
const isMain =
  process.argv[1] !== undefined &&
  path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
Iif (isMain) {
  main().catch((err) => {
    console.error('[purge-css]', err);
    process.exit(1);
  });
}
 
export { purge, buildSafelist };