| 1 | import { promisify } from "util"; |
| 2 | import fs, { mkdir } from "fs"; |
| 3 | import path from "path"; |
| 4 | import { transform } from "@babel/core"; |
| 5 | import gzipSize from "gzip-size"; |
| 6 | import terser from "terser"; |
| 7 | import chalk from "chalk"; |
| 8 | |
| 9 | const options = { |
| 10 | babelrc: false, |
| 11 | configFile: false, |
| 12 | sourceType: "module", |
| 13 | compact: true, |
| 14 | }; |
| 15 | |
| 16 | export function dent(...args) { |
| 17 | const str = args[0].reduce((acc, str, index) => acc + args[index] + str); |
| 18 | const whitespace = str.match(/([\t ]*)[^\s\n]/m)[1]; |
| 19 | return str |
| 20 | .replace(new RegExp("^" + whitespace, "gm"), "") |
| 21 | .trim() |
| 22 | .replace(/\t/g, " "); |
| 23 | } |
| 24 | |
| 25 | export function babel(code, config) { |
| 26 | return transform(code, { |
| 27 | ...options, |
| 28 | ...config, |
| 29 | }) |
| 30 | .code.replace(/\n\n+/g, "\n") |
| 31 | .trim(); |
| 32 | } |
| 33 | |
| 34 | export function compressedSize(code, options = {}) { |
| 35 | return gzipSize( |
| 36 | terser.minify(code, { |
| 37 | mangle: true, |
| 38 | compress: true, |
| 39 | sourceMap: false, |
| 40 | module: true, |
| 41 | ecma: 8, |
| 42 | ...options, |
| 43 | }).code |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | export const readdir = promisify(fs.readdir); |
| 48 | export const readFile = promisify(fs.readFile); |
| 49 | export const writeFile = promisify(fs.writeFile); |
| 50 | export function mkdirp(f) { |
| 51 | return promisify(mkdir)(f).catch(() => {}); |
| 52 | } |
| 53 | |
| 54 | export async function compareFixturesToPresetEnv( |
| 55 | dir, |
| 56 | { compileBefore, compile, compileEnv }, |
| 57 | assertion |
| 58 | ) { |
| 59 | let summary = ""; |
| 60 | let count = 0, |
| 61 | total = 0, |
| 62 | sum = 0, |
| 63 | sumEnv = 0; |
| 64 | for (const f of await readdir(dir)) { |
| 65 | if (!/\.js$/.test(f)) continue; |
| 66 | const fixture = f.replace(/\.js$/, ""); |
| 67 | const context = { |
| 68 | fixture, |
| 69 | dir: path.join(dir, f), |
| 70 | }; |
| 71 | try { |
| 72 | context.file = await readFile(path.join(dir, f), "utf-8"); |
| 73 | } catch (e) {} |
| 74 | const before = compileBefore ? await compileBefore(context) : context.file; |
| 75 | const out = await compile(context); |
| 76 | const outEnv = await compileEnv(context); |
| 77 | count++; |
| 78 | const sizeBefore = (total += await compressedSize(before)); |
| 79 | const size = (sum += await compressedSize(out)); |
| 80 | const sizeEnv = (sumEnv += await compressedSize(outEnv)); |
| 81 | summary += |
| 82 | `\n ${fixture}: ${size}b` + |
| 83 | chalk.dim(` → ${sizeEnv - size}b smaller than env (`) + |
| 84 | chalk.green(`${(((sizeEnv - size) / sizeEnv) * 100) | 0}% savings`) + |
| 85 | chalk.dim`)`; |
| 86 | if (assertion) { |
| 87 | assertion({ fixture, size, sizeEnv, sizeBefore, before, out, outEnv }); |
| 88 | } |
| 89 | } |
| 90 | summary += chalk.magenta( |
| 91 | `\nAverage Savings of ${((sumEnv - sum) / count) | |
| 92 | 0}b compared to preset-env (${((sum / total - 1) * 100) | |
| 93 | 0}% overhead versus ${((sumEnv / total - 1) * 100) | 0}%)` |
| 94 | ); |
| 95 | return summary; |
| 96 | } |