|
| 1 | +import path from "path" |
| 2 | +import fs from "fs" |
| 3 | +import sizeLimit from "size-limit" |
| 4 | +import filePlugin from "@size-limit/file" |
| 5 | +import esbuildPlugin from "@size-limit/esbuild" |
| 6 | +import bytes from "bytes-iec" |
| 7 | + |
| 8 | +import * as funcs from "../../dist/index.cjs" |
| 9 | + |
| 10 | +const OUTPUT_FILE = "docs/assets/func-sizes.json" |
| 11 | + |
| 12 | +const formatBytes = (size) => { |
| 13 | + return bytes.format(size, { unitSeparator: " " }) |
| 14 | +} |
| 15 | + |
| 16 | +const getConfigFromIndexFile = (pathToIndexFile, functionNames) => { |
| 17 | + return functionNames |
| 18 | + .map((name) => { |
| 19 | + if (name === "default") { |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + return { |
| 24 | + path: pathToIndexFile, |
| 25 | + import: { [pathToIndexFile]: `{ ${name} }` }, |
| 26 | + name, |
| 27 | + files: [pathToIndexFile], |
| 28 | + } |
| 29 | + }) |
| 30 | + .filter(Boolean) |
| 31 | +} |
| 32 | + |
| 33 | +const baseDir = process.cwd() |
| 34 | +const esmPath = path.resolve(baseDir, "./dist/index.mjs") |
| 35 | +const cjsPath = path.resolve(baseDir, "./dist/index.cjs") |
| 36 | + |
| 37 | +const functionNames = Object.keys(funcs) |
| 38 | + |
| 39 | +const esmFiles = getConfigFromIndexFile(esmPath, functionNames) |
| 40 | +const cjsFiles = getConfigFromIndexFile(cjsPath, functionNames) |
| 41 | + |
| 42 | +const allImports = [ |
| 43 | + { |
| 44 | + path: esmPath, |
| 45 | + limit: "5.1 kb", |
| 46 | + import: { [esmPath]: "*" }, |
| 47 | + name: "all esm", |
| 48 | + files: [esmPath], |
| 49 | + sizeLimit: 5100, |
| 50 | + }, |
| 51 | + { |
| 52 | + path: cjsPath, |
| 53 | + limit: "5.4 kb", |
| 54 | + import: { [cjsPath]: "*" }, |
| 55 | + name: "all cjs", |
| 56 | + files: [cjsPath], |
| 57 | + sizeLimit: 5400, |
| 58 | + }, |
| 59 | +] |
| 60 | + |
| 61 | +const allFiles = [...esmFiles, ...cjsFiles, ...allImports] |
| 62 | + |
| 63 | +const getFuncSizes = async () => { |
| 64 | + const result = await sizeLimit([filePlugin, esbuildPlugin], { |
| 65 | + cwd: process.cwd(), |
| 66 | + configPath: ".size-limit.cjs", |
| 67 | + checks: allFiles, |
| 68 | + }) |
| 69 | + |
| 70 | + const funcs = {} |
| 71 | + |
| 72 | + result.forEach((funcSize, index) => { |
| 73 | + const funcItem = allFiles[index] |
| 74 | + const { size } = funcSize |
| 75 | + const { name, path } = funcItem |
| 76 | + |
| 77 | + const importType = path === esmPath ? "esm" : "cjs" |
| 78 | + |
| 79 | + if (!funcs[name]) funcs[name] = {} |
| 80 | + |
| 81 | + funcs[name][importType] = { size, formattedSize: formatBytes(size) } |
| 82 | + |
| 83 | + console.log("\n " + name, importType, size, formatBytes(size)) |
| 84 | + }) |
| 85 | + |
| 86 | + return funcs |
| 87 | +} |
| 88 | + |
| 89 | +getFuncSizes().then((sizes) => { |
| 90 | + const pathToSave = path.resolve(baseDir, OUTPUT_FILE) |
| 91 | + fs.writeFileSync(pathToSave, JSON.stringify(sizes, null, 2)) |
| 92 | +}) |
0 commit comments