tonnage 0.0.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,41 @@
1
+ import fs from "node:fs/promises";
1
2
  import path from "node:path";
2
3
  import { gzipSync } from "node:zlib";
3
4
  import { build } from "esbuild";
4
- import fs from "node:fs/promises";
5
+ import os from "node:os";
6
+ //#region src/readme.ts
7
+ function tonnageMarkers(marker = `default`) {
8
+ validateMarker(marker);
9
+ return {
10
+ end: `<!-- tonnage:${marker}:end -->`,
11
+ start: `<!-- tonnage:${marker}:start -->`
12
+ };
13
+ }
14
+ function updateGeneratedSection(readme, markdown, marker) {
15
+ const { end, start } = tonnageMarkers(marker);
16
+ const startIndex = readme.indexOf(start);
17
+ const endIndex = readme.indexOf(end);
18
+ if (startIndex === -1 || endIndex === -1) throw new Error(`README must contain exactly one ${start} and one ${end} marker.`);
19
+ if (readme.slice(startIndex + start.length).includes(start) || readme.slice(endIndex + end.length).includes(end)) throw new Error(`README must contain exactly one ${start} and one ${end} marker.`);
20
+ if (endIndex < startIndex) throw new Error(`${end} must appear after ${start}.`);
21
+ const contentStart = startIndex + start.length;
22
+ return `${readme.slice(0, contentStart)}\n\n${markdown.trim()}\n\n${readme.slice(endIndex)}`;
23
+ }
24
+ function validateMarker(marker) {
25
+ if (!/^[a-z0-9][a-z0-9._-]*$/i.test(marker)) throw new Error(`Tonnage marker names may contain letters, numbers, dots, underscores, and hyphens.`);
26
+ }
27
+ //#endregion
5
28
  //#region src/measure.ts
6
29
  async function measureEntry(entry, options) {
30
+ return measureEntryWithPlugins(entry, options);
31
+ }
32
+ async function measureEntryWithPlugins(entry, options) {
7
33
  return measureBuild({ entryPoints: [path.resolve(options.resolveDirectory, entry)] }, options);
8
34
  }
9
35
  async function measureImports(imports, options) {
36
+ return measureImportsWithPlugins(imports, options);
37
+ }
38
+ async function measureImportsWithPlugins(imports, options) {
10
39
  if (imports.length === 0) throw new Error(`A tonnage entry must import at least one module.`);
11
40
  return measureBuild({ stdin: {
12
41
  contents: imports.map((specifier, index) => `export * as entry${index} from ${JSON.stringify(specifier)}`).join(`\n`),
@@ -24,9 +53,11 @@ async function measureBuild(entry, options) {
24
53
  format: `esm`,
25
54
  legalComments: `none`,
26
55
  logLevel: `silent`,
56
+ ...platform === `neutral` ? { mainFields: [`module`, `main`] } : {},
27
57
  minify: true,
28
58
  outdir: `out`,
29
59
  platform,
60
+ plugins: [...options.plugins ?? []],
30
61
  ...entry,
31
62
  target: options.target ?? `es2022`,
32
63
  treeShaking: true,
@@ -40,26 +71,40 @@ async function measureBuild(entry, options) {
40
71
  };
41
72
  }
42
73
  //#endregion
43
- //#region src/readme.ts
44
- function tonnageMarkers(marker = `default`) {
45
- validateMarker(marker);
46
- return {
47
- end: `<!-- tonnage:${marker}:end -->`,
48
- start: `<!-- tonnage:${marker}:start -->`
49
- };
74
+ //#region src/package-resolution.ts
75
+ async function withPackageResolver(packageName, packageDirectory, useResolver) {
76
+ const temporaryDirectory = await fs.mkdtemp(path.join(os.tmpdir(), `tonnage-package-`));
77
+ try {
78
+ const nodeModulesDirectory = path.join(temporaryDirectory, `node_modules`);
79
+ const packageLinkPath = path.resolve(nodeModulesDirectory, packageName);
80
+ if (!packageLinkPath.startsWith(`${nodeModulesDirectory}${path.sep}`)) throw new Error(`Invalid package name: ${packageName}`);
81
+ await fs.mkdir(path.dirname(packageLinkPath), { recursive: true });
82
+ await fs.symlink(packageDirectory, packageLinkPath, process.platform === `win32` ? `junction` : `dir`);
83
+ const filter = new RegExp(`^${escapeRegExp(packageName)}(?:/.*)?$`);
84
+ return await useResolver({
85
+ name: `tonnage-package-resolution`,
86
+ setup(build) {
87
+ build.onResolve({ filter }, async (args) => {
88
+ if (args.resolveDir === temporaryDirectory) return;
89
+ return build.resolve(args.path, {
90
+ importer: args.importer,
91
+ kind: args.kind,
92
+ namespace: args.namespace,
93
+ resolveDir: temporaryDirectory,
94
+ with: args.with
95
+ });
96
+ });
97
+ }
98
+ });
99
+ } finally {
100
+ await fs.rm(temporaryDirectory, {
101
+ force: true,
102
+ recursive: true
103
+ });
104
+ }
50
105
  }
51
- function updateGeneratedSection(readme, markdown, marker) {
52
- const { end, start } = tonnageMarkers(marker);
53
- const startIndex = readme.indexOf(start);
54
- const endIndex = readme.indexOf(end);
55
- if (startIndex === -1 || endIndex === -1) throw new Error(`README must contain exactly one ${start} and one ${end} marker.`);
56
- if (readme.slice(startIndex + start.length).includes(start) || readme.slice(endIndex + end.length).includes(end)) throw new Error(`README must contain exactly one ${start} and one ${end} marker.`);
57
- if (endIndex < startIndex) throw new Error(`${end} must appear after ${start}.`);
58
- const contentStart = startIndex + start.length;
59
- return `${readme.slice(0, contentStart)}\n\n${markdown.trim()}\n\n${readme.slice(endIndex)}`;
60
- }
61
- function validateMarker(marker) {
62
- if (!/^[a-z0-9][a-z0-9._-]*$/i.test(marker)) throw new Error(`Tonnage marker names may contain letters, numbers, dots, underscores, and hyphens.`);
106
+ function escapeRegExp(value) {
107
+ return value.replace(/[.*+?^${}()|[\]\\]/g, `\\$&`);
63
108
  }
64
109
  //#endregion
65
110
  //#region src/report.ts
@@ -70,26 +115,32 @@ async function createTonnageReport(config, configDirectory) {
70
115
  const resolveDirectory = path.dirname(packageJsonPath);
71
116
  const external = unique([...Object.keys(packageJson.peerDependencies ?? {}), ...config.external ?? []]);
72
117
  const exportImports = selectExportImports(packageJson.name, packageJson.exports, config.exports);
73
- const exportRows = await Promise.all(exportImports.map(async (specifier) => ({
74
- ...await measureImports([specifier], {
75
- external,
76
- platform: config.platform,
77
- resolveDirectory,
78
- target: config.target
79
- }),
80
- imports: [specifier],
81
- name: specifier
82
- })));
83
- const recipeRows = await Promise.all((config.recipes ?? []).map(async (recipe) => ({
84
- ...await measureEntry(recipe.entry, {
85
- external: unique([...external, ...recipe.external ?? []]),
86
- platform: recipe.platform ?? config.platform,
87
- resolveDirectory,
88
- target: config.target
89
- }),
90
- entry: recipe.entry,
91
- name: recipe.name
92
- })));
118
+ const { exportRows, recipeRows } = await withPackageResolver(packageJson.name, resolveDirectory, async (packageResolver) => {
119
+ return {
120
+ exportRows: await Promise.all(exportImports.map(async (specifier) => ({
121
+ ...await measureImportsWithPlugins([specifier], {
122
+ external,
123
+ platform: config.platform,
124
+ plugins: [packageResolver],
125
+ resolveDirectory,
126
+ target: config.target
127
+ }),
128
+ imports: [specifier],
129
+ name: specifier
130
+ }))),
131
+ recipeRows: await Promise.all((config.recipes ?? []).map(async (recipe) => ({
132
+ ...await measureEntryWithPlugins(recipe.entry, {
133
+ external: unique([...external, ...recipe.external ?? []]),
134
+ platform: recipe.platform ?? config.platform,
135
+ plugins: [packageResolver],
136
+ resolveDirectory,
137
+ target: config.target
138
+ }),
139
+ entry: recipe.entry,
140
+ name: recipe.name
141
+ })))
142
+ };
143
+ });
93
144
  return {
94
145
  exports: exportRows,
95
146
  packageName: packageJson.name,
@@ -97,12 +148,18 @@ async function createTonnageReport(config, configDirectory) {
97
148
  };
98
149
  }
99
150
  function renderTonnageMarkdown(report, config = {}) {
151
+ const hasExports = report.exports.length > 0;
152
+ const hasRecipes = report.recipes.length > 0;
153
+ const hasMultipleSections = hasExports && hasRecipes;
100
154
  const lines = [
101
155
  `## ${config.heading ?? `Bundle size`}`,
102
156
  ``,
103
- `Public-module rows retain complete runtime export surfaces. Recipe rows bundle their reviewable entry files and tree-shake unused exports. Both report exact minified and level-9 gzip JavaScript byte counts; declarations, source maps, CSS, and other assets are excluded. Peer dependencies stay external, and shared modules are counted once per bundle.`
157
+ ...methodologyLines({
158
+ hasExports,
159
+ hasRecipes
160
+ })
104
161
  ];
105
- if (report.exports.length > 0) lines.push(``, `### Public modules (whole export surface)`, ``, ...markdownTable([
162
+ if (hasExports) lines.push(``, ...hasMultipleSections ? [`### Package exports`, ``] : [], ...markdownTable([
106
163
  `Import`,
107
164
  `Minified JS`,
108
165
  `Gzip JS`
@@ -115,7 +172,7 @@ function renderTonnageMarkdown(report, config = {}) {
115
172
  `right`,
116
173
  `right`
117
174
  ]));
118
- if (report.recipes.length > 0) lines.push(``, `### Representative runtimes (tree-shaken)`, ``, ...markdownTable([
175
+ if (hasRecipes) lines.push(``, ...hasMultipleSections ? [`### Usage examples`, ``] : [], ...markdownTable([
119
176
  `Recipe`,
120
177
  `Entry`,
121
178
  `Minified JS`,
@@ -133,6 +190,13 @@ function renderTonnageMarkdown(report, config = {}) {
133
190
  ]));
134
191
  return lines.join(`\n`);
135
192
  }
193
+ function methodologyLines({ hasExports, hasRecipes }) {
194
+ const lines = [];
195
+ if (hasExports) lines.push(`Package export sizes include complete runtime export surfaces.`);
196
+ if (hasRecipes) lines.push(`Usage example sizes bundle their entry files and tree-shake unused exports.`);
197
+ lines.push(`Sizes are exact minified and level-9 gzip JavaScript byte counts. Declarations, source maps, CSS, and other assets are excluded. Peer dependencies stay external, and shared modules are counted once per bundle.`);
198
+ return lines;
199
+ }
136
200
  function selectExportImports(packageName, exportsValue, selection) {
137
201
  if (selection === false) return [];
138
202
  const exportKeys = getExportKeys(exportsValue);
@@ -194,6 +258,6 @@ async function runTonnage(config, options) {
194
258
  };
195
259
  }
196
260
  //#endregion
197
- export { updateGeneratedSection as a, tonnageMarkers as i, createTonnageReport as n, measureEntry as o, renderTonnageMarkdown as r, measureImports as s, runTonnage as t };
261
+ export { measureImports as a, measureEntry as i, createTonnageReport as n, tonnageMarkers as o, renderTonnageMarkdown as r, updateGeneratedSection as s, runTonnage as t };
198
262
 
199
- //# sourceMappingURL=run-DIk7L56w.js.map
263
+ //# sourceMappingURL=run-P2rhqYM3.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-P2rhqYM3.mjs","names":[],"sources":["../src/readme.ts","../src/measure.ts","../src/package-resolution.ts","../src/report.ts","../src/run.ts"],"sourcesContent":["export function tonnageMarkers(marker = `default`): {\n\tend: string\n\tstart: string\n} {\n\tvalidateMarker(marker)\n\treturn {\n\t\tend: `<!-- tonnage:${marker}:end -->`,\n\t\tstart: `<!-- tonnage:${marker}:start -->`,\n\t}\n}\n\nexport function updateGeneratedSection(\n\treadme: string,\n\tmarkdown: string,\n\tmarker?: string,\n): string {\n\tconst { end, start } = tonnageMarkers(marker)\n\tconst startIndex = readme.indexOf(start)\n\tconst endIndex = readme.indexOf(end)\n\n\tif (startIndex === -1 || endIndex === -1) {\n\t\tthrow new Error(\n\t\t\t`README must contain exactly one ${start} and one ${end} marker.`,\n\t\t)\n\t}\n\tif (\n\t\treadme.slice(startIndex + start.length).includes(start) ||\n\t\treadme.slice(endIndex + end.length).includes(end)\n\t) {\n\t\tthrow new Error(\n\t\t\t`README must contain exactly one ${start} and one ${end} marker.`,\n\t\t)\n\t}\n\tif (endIndex < startIndex) {\n\t\tthrow new Error(`${end} must appear after ${start}.`)\n\t}\n\n\tconst contentStart = startIndex + start.length\n\treturn `${readme.slice(0, contentStart)}\\n\\n${markdown.trim()}\\n\\n${readme.slice(endIndex)}`\n}\n\nfunction validateMarker(marker: string): void {\n\tif (!/^[a-z0-9][a-z0-9._-]*$/i.test(marker)) {\n\t\tthrow new Error(\n\t\t\t`Tonnage marker names may contain letters, numbers, dots, underscores, and hyphens.`,\n\t\t)\n\t}\n}\n","import path from \"node:path\"\nimport { gzipSync } from \"node:zlib\"\n\nimport { build, type BuildOptions, type Plugin } from \"esbuild\"\n\nimport type { BundleMeasurement, BundlePlatform } from \"./types.ts\"\n\nexport type MeasureImportsOptions = {\n\texternal?: readonly string[] | undefined\n\tplatform?: BundlePlatform | undefined\n\tresolveDirectory: string\n\ttarget?: string | undefined\n}\n\nexport type MeasureEntryOptions = MeasureImportsOptions\n\ntype InternalMeasureOptions = MeasureImportsOptions & {\n\tplugins?: readonly Plugin[]\n}\n\nexport async function measureEntry(\n\tentry: string,\n\toptions: MeasureEntryOptions,\n): Promise<BundleMeasurement> {\n\treturn measureEntryWithPlugins(entry, options)\n}\n\nexport async function measureEntryWithPlugins(\n\tentry: string,\n\toptions: InternalMeasureOptions,\n): Promise<BundleMeasurement> {\n\treturn measureBuild(\n\t\t{ entryPoints: [path.resolve(options.resolveDirectory, entry)] },\n\t\toptions,\n\t)\n}\n\nexport async function measureImports(\n\timports: readonly string[],\n\toptions: MeasureImportsOptions,\n): Promise<BundleMeasurement> {\n\treturn measureImportsWithPlugins(imports, options)\n}\n\nexport async function measureImportsWithPlugins(\n\timports: readonly string[],\n\toptions: InternalMeasureOptions,\n): Promise<BundleMeasurement> {\n\tif (imports.length === 0) {\n\t\tthrow new Error(`A tonnage entry must import at least one module.`)\n\t}\n\n\tconst entry = imports\n\t\t.map(\n\t\t\t(specifier, index) =>\n\t\t\t\t`export * as entry${index} from ${JSON.stringify(specifier)}`,\n\t\t)\n\t\t.join(`\\n`)\n\treturn measureBuild(\n\t\t{\n\t\t\tstdin: {\n\t\t\t\tcontents: entry,\n\t\t\t\tloader: `js`,\n\t\t\t\tresolveDir: options.resolveDirectory,\n\t\t\t\tsourcefile: `tonnage-entry.js`,\n\t\t\t},\n\t\t},\n\t\toptions,\n\t)\n}\n\nasync function measureBuild(\n\tentry: Pick<BuildOptions, `entryPoints` | `stdin`>,\n\toptions: InternalMeasureOptions,\n): Promise<BundleMeasurement> {\n\tconst platform = options.platform ?? `neutral`\n\n\tconst result = await build({\n\t\tbundle: true,\n\t\tcharset: `utf8`,\n\t\texternal: [\n\t\t\t...(options.external ?? []),\n\t\t\t...(platform === `browser` ? [] : [`node:*`]),\n\t\t],\n\t\tformat: `esm`,\n\t\tlegalComments: `none`,\n\t\tlogLevel: `silent`,\n\t\t...(platform === `neutral` ? { mainFields: [`module`, `main`] } : {}),\n\t\tminify: true,\n\t\toutdir: `out`,\n\t\tplatform,\n\t\tplugins: [...(options.plugins ?? [])],\n\t\t...entry,\n\t\ttarget: options.target ?? `es2022`,\n\t\ttreeShaking: true,\n\t\twrite: false,\n\t})\n\n\tconst outputFiles = [...(result.outputFiles ?? [])]\n\t\t.filter((output) => output.path.endsWith(`.js`))\n\t\t.sort((left, right) => left.path.localeCompare(right.path))\n\tif (outputFiles.length === 0) {\n\t\tthrow new Error(`The tonnage entry did not emit any runtime JavaScript.`)\n\t}\n\tconst rawBytes = outputFiles.reduce(\n\t\t(total, output) => total + output.contents.byteLength,\n\t\t0,\n\t)\n\tconst gzipBytes = outputFiles.reduce(\n\t\t(total, output) =>\n\t\t\ttotal + gzipSync(output.contents, { level: 9 }).byteLength,\n\t\t0,\n\t)\n\n\treturn { gzipBytes, rawBytes }\n}\n","import fs from \"node:fs/promises\"\nimport os from \"node:os\"\nimport path from \"node:path\"\n\nimport type { Plugin } from \"esbuild\"\n\nexport async function withPackageResolver<Result>(\n\tpackageName: string,\n\tpackageDirectory: string,\n\tuseResolver: (plugin: Plugin) => Promise<Result>,\n): Promise<Result> {\n\tconst temporaryDirectory = await fs.mkdtemp(\n\t\tpath.join(os.tmpdir(), `tonnage-package-`),\n\t)\n\ttry {\n\t\tconst nodeModulesDirectory = path.join(temporaryDirectory, `node_modules`)\n\t\tconst packageLinkPath = path.resolve(nodeModulesDirectory, packageName)\n\t\tif (!packageLinkPath.startsWith(`${nodeModulesDirectory}${path.sep}`)) {\n\t\t\tthrow new Error(`Invalid package name: ${packageName}`)\n\t\t}\n\n\t\tawait fs.mkdir(path.dirname(packageLinkPath), { recursive: true })\n\t\tawait fs.symlink(\n\t\t\tpackageDirectory,\n\t\t\tpackageLinkPath,\n\t\t\tprocess.platform === `win32` ? `junction` : `dir`,\n\t\t)\n\n\t\tconst filter = new RegExp(`^${escapeRegExp(packageName)}(?:/.*)?$`)\n\t\tconst plugin: Plugin = {\n\t\t\tname: `tonnage-package-resolution`,\n\t\t\tsetup(build) {\n\t\t\t\tbuild.onResolve({ filter }, async (args) => {\n\t\t\t\t\tif (args.resolveDir === temporaryDirectory) {\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\treturn build.resolve(args.path, {\n\t\t\t\t\t\timporter: args.importer,\n\t\t\t\t\t\tkind: args.kind,\n\t\t\t\t\t\tnamespace: args.namespace,\n\t\t\t\t\t\tresolveDir: temporaryDirectory,\n\t\t\t\t\t\twith: args.with,\n\t\t\t\t\t})\n\t\t\t\t})\n\t\t\t},\n\t\t}\n\n\t\treturn await useResolver(plugin)\n\t} finally {\n\t\tawait fs.rm(temporaryDirectory, { force: true, recursive: true })\n\t}\n}\n\nfunction escapeRegExp(value: string): string {\n\treturn value.replace(/[.*+?^${}()|[\\]\\\\]/g, `\\\\$&`)\n}\n","import fs from \"node:fs/promises\"\nimport path from \"node:path\"\n\nimport {\n\tmeasureEntryWithPlugins,\n\tmeasureImportsWithPlugins,\n} from \"./measure.ts\"\nimport { withPackageResolver } from \"./package-resolution.ts\"\nimport type {\n\tTonnageConfig,\n\tTonnageExports,\n\tTonnageReport,\n\tTonnageRow,\n} from \"./types.ts\"\n\ntype PackageJson = {\n\texports?: unknown\n\tname?: string\n\tpeerDependencies?: Record<string, string>\n}\n\nexport async function createTonnageReport(\n\tconfig: TonnageConfig,\n\tconfigDirectory: string,\n): Promise<TonnageReport> {\n\tconst packageJsonPath = path.resolve(\n\t\tconfigDirectory,\n\t\tconfig.packageJson ?? `package.json`,\n\t)\n\tconst packageJson = JSON.parse(\n\t\tawait fs.readFile(packageJsonPath, `utf8`),\n\t) as PackageJson\n\tif (!packageJson.name) {\n\t\tthrow new Error(`${packageJsonPath} does not declare a package name.`)\n\t}\n\n\tconst resolveDirectory = path.dirname(packageJsonPath)\n\tconst peerDependencies = Object.keys(packageJson.peerDependencies ?? {})\n\tconst external = unique([...peerDependencies, ...(config.external ?? [])])\n\tconst exportImports = selectExportImports(\n\t\tpackageJson.name,\n\t\tpackageJson.exports,\n\t\tconfig.exports,\n\t)\n\n\tconst { exportRows, recipeRows } = await withPackageResolver(\n\t\tpackageJson.name,\n\t\tresolveDirectory,\n\t\tasync (packageResolver) => {\n\t\t\tconst exportRows = await Promise.all(\n\t\t\t\texportImports.map(async (specifier): Promise<TonnageRow> => ({\n\t\t\t\t\t...(await measureImportsWithPlugins([specifier], {\n\t\t\t\t\t\texternal,\n\t\t\t\t\t\tplatform: config.platform,\n\t\t\t\t\t\tplugins: [packageResolver],\n\t\t\t\t\t\tresolveDirectory,\n\t\t\t\t\t\ttarget: config.target,\n\t\t\t\t\t})),\n\t\t\t\t\timports: [specifier],\n\t\t\t\t\tname: specifier,\n\t\t\t\t})),\n\t\t\t)\n\t\t\tconst recipeRows = await Promise.all(\n\t\t\t\t(config.recipes ?? []).map(async (recipe) => ({\n\t\t\t\t\t...(await measureEntryWithPlugins(recipe.entry, {\n\t\t\t\t\t\texternal: unique([...external, ...(recipe.external ?? [])]),\n\t\t\t\t\t\tplatform: recipe.platform ?? config.platform,\n\t\t\t\t\t\tplugins: [packageResolver],\n\t\t\t\t\t\tresolveDirectory,\n\t\t\t\t\t\ttarget: config.target,\n\t\t\t\t\t})),\n\t\t\t\t\tentry: recipe.entry,\n\t\t\t\t\tname: recipe.name,\n\t\t\t\t})),\n\t\t\t)\n\n\t\t\treturn { exportRows, recipeRows }\n\t\t},\n\t)\n\n\treturn {\n\t\texports: exportRows,\n\t\tpackageName: packageJson.name,\n\t\trecipes: recipeRows,\n\t}\n}\n\nexport function renderTonnageMarkdown(\n\treport: TonnageReport,\n\tconfig: TonnageConfig = {},\n): string {\n\tconst hasExports = report.exports.length > 0\n\tconst hasRecipes = report.recipes.length > 0\n\tconst hasMultipleSections = hasExports && hasRecipes\n\tconst lines = [\n\t\t`## ${config.heading ?? `Bundle size`}`,\n\t\t``,\n\t\t...methodologyLines({ hasExports, hasRecipes }),\n\t]\n\n\tif (hasExports) {\n\t\tlines.push(\n\t\t\t``,\n\t\t\t...(hasMultipleSections ? [`### Package exports`, ``] : []),\n\t\t\t...markdownTable(\n\t\t\t\t[`Import`, `Minified JS`, `Gzip JS`],\n\t\t\t\treport.exports.map((row) => [\n\t\t\t\t\tcode(row.name),\n\t\t\t\t\tformatBytes(row.rawBytes),\n\t\t\t\t\tformatBytes(row.gzipBytes),\n\t\t\t\t]),\n\t\t\t\t[`left`, `right`, `right`],\n\t\t\t),\n\t\t)\n\t}\n\n\tif (hasRecipes) {\n\t\tlines.push(\n\t\t\t``,\n\t\t\t...(hasMultipleSections ? [`### Usage examples`, ``] : []),\n\t\t\t...markdownTable(\n\t\t\t\t[`Recipe`, `Entry`, `Minified JS`, `Gzip JS`],\n\t\t\t\treport.recipes.map((row) => [\n\t\t\t\t\tescapeCell(row.name),\n\t\t\t\t\tcode(row.entry),\n\t\t\t\t\tformatBytes(row.rawBytes),\n\t\t\t\t\tformatBytes(row.gzipBytes),\n\t\t\t\t]),\n\t\t\t\t[`left`, `left`, `right`, `right`],\n\t\t\t),\n\t\t)\n\t}\n\n\treturn lines.join(`\\n`)\n}\n\nfunction methodologyLines({\n\thasExports,\n\thasRecipes,\n}: {\n\thasExports: boolean\n\thasRecipes: boolean\n}): string[] {\n\tconst lines: string[] = []\n\tif (hasExports) {\n\t\tlines.push(`Package export sizes include complete runtime export surfaces.`)\n\t}\n\tif (hasRecipes) {\n\t\tlines.push(\n\t\t\t`Usage example sizes bundle their entry files and tree-shake unused exports.`,\n\t\t)\n\t}\n\tlines.push(\n\t\t`Sizes are exact minified and level-9 gzip JavaScript byte counts. Declarations, source maps, CSS, and other assets are excluded. Peer dependencies stay external, and shared modules are counted once per bundle.`,\n\t)\n\treturn lines\n}\n\nfunction selectExportImports(\n\tpackageName: string,\n\texportsValue: unknown,\n\tselection: TonnageConfig[`exports`],\n): string[] {\n\tif (selection === false) {\n\t\treturn []\n\t}\n\n\tconst exportKeys = getExportKeys(exportsValue)\n\tconst options: TonnageExports = typeof selection === `object` ? selection : {}\n\tconst included = options.include\n\t\t? [...options.include]\n\t\t: exportKeys.filter((exportKey) => exportKey !== `./package.json`)\n\tconst excluded = new Set(options.exclude ?? [])\n\n\treturn included\n\t\t.filter((exportKey) => !excluded.has(exportKey))\n\t\t.map((exportKey) => toPackageImport(packageName, exportKey))\n}\n\nfunction getExportKeys(exportsValue: unknown): string[] {\n\tif (typeof exportsValue === `string` || Array.isArray(exportsValue)) {\n\t\treturn [`.`]\n\t}\n\tif (!exportsValue || typeof exportsValue !== `object`) {\n\t\treturn [`.`]\n\t}\n\n\tconst keys = Object.keys(exportsValue)\n\treturn keys.some((key) => key.startsWith(`.`)) ? keys : [`.`]\n}\n\nfunction toPackageImport(packageName: string, exportKey: string): string {\n\tif (exportKey === `.`) {\n\t\treturn packageName\n\t}\n\tif (!exportKey.startsWith(`./`)) {\n\t\tthrow new Error(`Invalid package export key: ${exportKey}`)\n\t}\n\treturn `${packageName}/${exportKey.slice(2)}`\n}\n\nfunction formatBytes(bytes: number): string {\n\treturn `${new Intl.NumberFormat(`en-US`).format(bytes)} B`\n}\n\nfunction code(value: string): string {\n\treturn `<code>${escapeCell(value)}</code>`\n}\n\nfunction escapeCell(value: string): string {\n\treturn value.replaceAll(`&`, `&amp;`).replaceAll(`|`, `&#124;`)\n}\n\nfunction unique(values: readonly string[]): string[] {\n\treturn [...new Set(values)]\n}\n\nfunction markdownTable(\n\theadings: readonly string[],\n\trows: readonly (readonly string[])[],\n\talignments: readonly (`left` | `right`)[],\n): string[] {\n\tconst widths = headings.map((heading, column) =>\n\t\tMath.max(heading.length, ...rows.map((row) => row[column]?.length ?? 0), 3),\n\t)\n\tconst renderRow = (cells: readonly string[]): string =>\n\t\t`| ${cells.map((cell, column) => padCell(cell, widths[column] ?? 3, alignments[column] ?? `left`)).join(` | `)} |`\n\tconst separator = widths.map((width, column) =>\n\t\talignments[column] === `right`\n\t\t\t? `${`-`.repeat(width - 1)}:`\n\t\t\t: `-`.repeat(width),\n\t)\n\n\treturn [renderRow(headings), renderRow(separator), ...rows.map(renderRow)]\n}\n\nfunction padCell(\n\tcell: string,\n\twidth: number,\n\talignment: `left` | `right`,\n): string {\n\treturn alignment === `right` ? cell.padStart(width) : cell.padEnd(width)\n}\n","import fs from \"node:fs/promises\"\nimport path from \"node:path\"\n\nimport { updateGeneratedSection } from \"./readme.ts\"\nimport { createTonnageReport, renderTonnageMarkdown } from \"./report.ts\"\nimport type { TonnageConfig, TonnageMode, TonnageRunResult } from \"./types.ts\"\n\nexport async function runTonnage(\n\tconfig: TonnageConfig,\n\toptions: {\n\t\tconfigDirectory?: string\n\t\tmode: TonnageMode\n\t},\n): Promise<TonnageRunResult> {\n\tconst configDirectory = path.resolve(options.configDirectory ?? process.cwd())\n\tconst readmePath = path.resolve(configDirectory, config.readme ?? `README.md`)\n\tconst oldReadme = await fs.readFile(readmePath, `utf8`)\n\tconst report = await createTonnageReport(config, configDirectory)\n\tconst markdown = renderTonnageMarkdown(report, config)\n\tconst newReadme = updateGeneratedSection(oldReadme, markdown, config.marker)\n\tconst changed = oldReadme !== newReadme\n\n\tif (options.mode === `write` && changed) {\n\t\tawait fs.writeFile(readmePath, newReadme)\n\t}\n\n\treturn { changed, readmePath, report }\n}\n"],"mappings":";;;;;;AAAA,SAAgB,eAAe,SAAS,WAGtC;CACD,eAAe,MAAM;CACrB,OAAO;EACN,KAAK,gBAAgB,OAAO;EAC5B,OAAO,gBAAgB,OAAO;CAC/B;AACD;AAEA,SAAgB,uBACf,QACA,UACA,QACS;CACT,MAAM,EAAE,KAAK,UAAU,eAAe,MAAM;CAC5C,MAAM,aAAa,OAAO,QAAQ,KAAK;CACvC,MAAM,WAAW,OAAO,QAAQ,GAAG;CAEnC,IAAI,eAAe,MAAM,aAAa,IACrC,MAAM,IAAI,MACT,mCAAmC,MAAM,WAAW,IAAI,SACzD;CAED,IACC,OAAO,MAAM,aAAa,MAAM,MAAM,CAAC,CAAC,SAAS,KAAK,KACtD,OAAO,MAAM,WAAW,IAAI,MAAM,CAAC,CAAC,SAAS,GAAG,GAEhD,MAAM,IAAI,MACT,mCAAmC,MAAM,WAAW,IAAI,SACzD;CAED,IAAI,WAAW,YACd,MAAM,IAAI,MAAM,GAAG,IAAI,qBAAqB,MAAM,EAAE;CAGrD,MAAM,eAAe,aAAa,MAAM;CACxC,OAAO,GAAG,OAAO,MAAM,GAAG,YAAY,EAAE,MAAM,SAAS,KAAK,EAAE,MAAM,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AAS,KAAK,SAAS,WACrC,KAAK,IAAI,QAAQ,QAAQ,GAAG,KAAK,KAAK,QAAQ,IAAI,OAAO,EAAE,UAAU,CAAC,GAAG,CAAC,CAC3E;CACA,MAAM,aAAa,UAClB,KAAK,MAAM,KAAK,MAAM,WAAW,QAAQ,MAAM,OAAO,WAAW,GAAG,WAAW,WAAW,MAAM,CAAC,CAAC,CAAC,KAAK,KAAK,EAAE;CAChH,MAAM,YAAY,OAAO,KAAK,OAAO,WACpC,WAAW,YAAY,UACpB,GAAG,IAAI,OAAO,QAAQ,CAAC,EAAE,KACzB,IAAI,OAAO,KAAK,CACpB;CAEA,OAAO;EAAC,UAAU,QAAQ;EAAG,UAAU,SAAS;EAAG,GAAG,KAAK,IAAI,SAAS;CAAC;AAC1E;AAEA,SAAS,QACR,MACA,OACA,WACS;CACT,OAAO,cAAc,UAAU,KAAK,SAAS,KAAK,IAAI,KAAK,OAAO,KAAK;AACxE;;;AC3OA,eAAsB,WACrB,QACA,SAI4B;CAC5B,MAAM,kBAAkB,KAAK,QAAQ,QAAQ,mBAAmB,QAAQ,IAAI,CAAC;CAC7E,MAAM,aAAa,KAAK,QAAQ,iBAAiB,OAAO,UAAU,WAAW;CAC7E,MAAM,YAAY,MAAM,GAAG,SAAS,YAAY,MAAM;CACtD,MAAM,SAAS,MAAM,oBAAoB,QAAQ,eAAe;CAEhE,MAAM,YAAY,uBAAuB,WADxB,sBAAsB,QAAQ,MACK,GAAU,OAAO,MAAM;CAC3E,MAAM,UAAU,cAAc;CAE9B,IAAI,QAAQ,SAAS,WAAW,SAC/B,MAAM,GAAG,UAAU,YAAY,SAAS;CAGzC,OAAO;EAAE;EAAS;EAAY;CAAO;AACtC"}
package/package.json CHANGED
@@ -1,53 +1,51 @@
1
1
  {
2
- "name": "tonnage",
3
- "version": "0.0.0",
4
- "description": "Keep deterministic bundle size reports in README files and CI.",
5
- "type": "module",
6
- "license": "MIT",
7
- "author": "Jeremy Banka",
8
- "engines": {
9
- "node": ">=22.18.0"
10
- },
11
- "publishConfig": {
12
- "access": "public"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "git+https://github.com/jeremybanka/tonnage.git"
17
- },
18
- "files": [
19
- "bin",
20
- "dist",
21
- "src",
22
- "examples",
23
- "README.md"
24
- ],
25
- "bin": {
26
- "tonnage": "bin/tonnage.bin.js"
27
- },
28
- "exports": {
29
- ".": {
30
- "import": "./dist/index.js",
31
- "types": "./dist/index.d.ts"
32
- }
33
- },
34
- "main": "./dist/index.js",
35
- "types": "./dist/index.d.ts",
36
- "scripts": {
37
- "build": "tsdown",
38
- "lint:eslint": "eslint",
39
- "lint:oxlint": "oxlint",
40
- "lint:types": "tsc",
41
- "test:once": "vitest run"
42
- },
43
- "dependencies": {
44
- "esbuild": "0.28.1"
45
- },
46
- "devDependencies": {
47
- "@types/node": "26.4.0",
48
- "eslint": "10.9.1",
49
- "tsdown": "0.22.14",
50
- "typescript": "7.0.2",
51
- "vitest": "4.1.11"
52
- }
53
- }
2
+ "name": "tonnage",
3
+ "version": "0.1.1",
4
+ "description": "Keep deterministic bundle size reports in README files and CI.",
5
+ "type": "module",
6
+ "license": "MPL-2.0",
7
+ "author": "Jeremy Banka",
8
+ "engines": {
9
+ "node": ">=22.18.0"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/jeremybanka/tonnage.git"
17
+ },
18
+ "files": [
19
+ "AGENTS.md",
20
+ "CHANGELOG.md",
21
+ "dist",
22
+ "src",
23
+ "examples",
24
+ "README.md",
25
+ "LICENSE"
26
+ ],
27
+ "bin": {
28
+ "tonnage": "dist/cli.mjs"
29
+ },
30
+ "exports": {
31
+ ".": {
32
+ "import": "./dist/index.mjs",
33
+ "types": "./dist/index.d.mts"
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "main": "./dist/index.mjs",
38
+ "types": "./dist/index.d.mts",
39
+ "dependencies": {
40
+ "comline": "0.5.2",
41
+ "esbuild": "0.28.2"
42
+ },
43
+ "devDependencies": {
44
+ "@types/node": "26.4.0",
45
+ "vitest": "npm:@voidzero-dev/vite-plus-test@0.1.24"
46
+ },
47
+ "scripts": {
48
+ "build": "vp pack",
49
+ "test": "vp test"
50
+ }
51
+ }
package/src/cli.ts CHANGED
@@ -4,8 +4,9 @@ import fs from "node:fs/promises"
4
4
  import path from "node:path"
5
5
  import { pathToFileURL } from "node:url"
6
6
 
7
+ import { parseTonnageCli, renderTonnageCliHelp } from "./command-line.ts"
7
8
  import { runTonnage } from "./run.ts"
8
- import type { TonnageConfig, TonnageMode } from "./types.ts"
9
+ import type { TonnageConfig } from "./types.ts"
9
10
 
10
11
  const DEFAULT_CONFIG_FILES = [
11
12
  `tonnage.config.ts`,
@@ -14,9 +15,14 @@ const DEFAULT_CONFIG_FILES = [
14
15
  ]
15
16
 
16
17
  async function main(): Promise<void> {
17
- const command = process.argv[2]
18
- const mode = parseMode(command)
19
- const configPath = await findConfig(process.argv[3])
18
+ const invocation = parseTonnageCli([`tonnage`, ...process.argv.slice(2)])
19
+ if (invocation.kind === `help`) {
20
+ process.stdout.write(renderTonnageCliHelp())
21
+ return
22
+ }
23
+
24
+ const { configArgument, mode } = invocation
25
+ const configPath = await findConfig(configArgument)
20
26
  const config = await loadConfig(configPath)
21
27
  const result = await runTonnage(config, {
22
28
  configDirectory: path.dirname(configPath),
@@ -39,20 +45,6 @@ async function main(): Promise<void> {
39
45
  )
40
46
  }
41
47
 
42
- function parseMode(command: string | undefined): TonnageMode {
43
- switch (command) {
44
- case `check`:
45
- case `test`:
46
- return `check`
47
- case `make`:
48
- case `write`:
49
- return `write`
50
- case undefined:
51
- default:
52
- throw new Error(`Usage: tonnage <write|check> [tonnage.config.ts]`)
53
- }
54
- }
55
-
56
48
  async function findConfig(configArgument: string | undefined): Promise<string> {
57
49
  if (configArgument) {
58
50
  return path.resolve(configArgument)
@@ -0,0 +1,52 @@
1
+ import { cli, help, helpOption, optional } from "comline"
2
+
3
+ import type { TonnageMode } from "./types.ts"
4
+
5
+ const ROOT_MANUAL = helpOption(`Show help for the tonnage command line.`)
6
+ const WRITE_MANUAL = helpOption(`Update the configured README report.`)
7
+ const CHECK_MANUAL = helpOption(
8
+ `Check that the configured README report is up to date.`,
9
+ )
10
+
11
+ const tonnageCli = cli({
12
+ cliName: `tonnage`,
13
+ cliDescription: `Keep deterministic bundle size reports in README files and CI.`,
14
+ routes: optional({
15
+ check: optional({ $configPath: null }),
16
+ write: optional({ $configPath: null }),
17
+ }),
18
+ routeOptions: {
19
+ "": ROOT_MANUAL,
20
+ check: CHECK_MANUAL,
21
+ "check/$configPath": CHECK_MANUAL,
22
+ write: WRITE_MANUAL,
23
+ "write/$configPath": WRITE_MANUAL,
24
+ },
25
+ discoverConfigPath: () => undefined,
26
+ })
27
+
28
+ export type TonnageCliInvocation =
29
+ | { kind: `help` }
30
+ | { configArgument?: string; kind: `run`; mode: TonnageMode }
31
+
32
+ export function parseTonnageCli(args: string[]): TonnageCliInvocation {
33
+ const { inputs } = tonnageCli(args)
34
+
35
+ if (inputs.case === `` || inputs.opts.help) {
36
+ return { kind: `help` }
37
+ }
38
+
39
+ const mode =
40
+ inputs.case === `write` || inputs.case === `write/$configPath`
41
+ ? `write`
42
+ : `check`
43
+ const configArgument = inputs.path[1]
44
+
45
+ return configArgument === undefined
46
+ ? { kind: `run`, mode }
47
+ : { configArgument, kind: `run`, mode }
48
+ }
49
+
50
+ export function renderTonnageCliHelp(): string {
51
+ return help(tonnageCli.definition)
52
+ }
package/src/measure.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import path from "node:path"
2
2
  import { gzipSync } from "node:zlib"
3
3
 
4
- import { build, type BuildOptions } from "esbuild"
4
+ import { build, type BuildOptions, type Plugin } from "esbuild"
5
5
 
6
6
  import type { BundleMeasurement, BundlePlatform } from "./types.ts"
7
7
 
@@ -14,9 +14,20 @@ export type MeasureImportsOptions = {
14
14
 
15
15
  export type MeasureEntryOptions = MeasureImportsOptions
16
16
 
17
+ type InternalMeasureOptions = MeasureImportsOptions & {
18
+ plugins?: readonly Plugin[]
19
+ }
20
+
17
21
  export async function measureEntry(
18
22
  entry: string,
19
23
  options: MeasureEntryOptions,
24
+ ): Promise<BundleMeasurement> {
25
+ return measureEntryWithPlugins(entry, options)
26
+ }
27
+
28
+ export async function measureEntryWithPlugins(
29
+ entry: string,
30
+ options: InternalMeasureOptions,
20
31
  ): Promise<BundleMeasurement> {
21
32
  return measureBuild(
22
33
  { entryPoints: [path.resolve(options.resolveDirectory, entry)] },
@@ -27,6 +38,13 @@ export async function measureEntry(
27
38
  export async function measureImports(
28
39
  imports: readonly string[],
29
40
  options: MeasureImportsOptions,
41
+ ): Promise<BundleMeasurement> {
42
+ return measureImportsWithPlugins(imports, options)
43
+ }
44
+
45
+ export async function measureImportsWithPlugins(
46
+ imports: readonly string[],
47
+ options: InternalMeasureOptions,
30
48
  ): Promise<BundleMeasurement> {
31
49
  if (imports.length === 0) {
32
50
  throw new Error(`A tonnage entry must import at least one module.`)
@@ -53,7 +71,7 @@ export async function measureImports(
53
71
 
54
72
  async function measureBuild(
55
73
  entry: Pick<BuildOptions, `entryPoints` | `stdin`>,
56
- options: MeasureEntryOptions,
74
+ options: InternalMeasureOptions,
57
75
  ): Promise<BundleMeasurement> {
58
76
  const platform = options.platform ?? `neutral`
59
77
 
@@ -67,9 +85,11 @@ async function measureBuild(
67
85
  format: `esm`,
68
86
  legalComments: `none`,
69
87
  logLevel: `silent`,
88
+ ...(platform === `neutral` ? { mainFields: [`module`, `main`] } : {}),
70
89
  minify: true,
71
90
  outdir: `out`,
72
91
  platform,
92
+ plugins: [...(options.plugins ?? [])],
73
93
  ...entry,
74
94
  target: options.target ?? `es2022`,
75
95
  treeShaking: true,
@@ -0,0 +1,57 @@
1
+ import fs from "node:fs/promises"
2
+ import os from "node:os"
3
+ import path from "node:path"
4
+
5
+ import type { Plugin } from "esbuild"
6
+
7
+ export async function withPackageResolver<Result>(
8
+ packageName: string,
9
+ packageDirectory: string,
10
+ useResolver: (plugin: Plugin) => Promise<Result>,
11
+ ): Promise<Result> {
12
+ const temporaryDirectory = await fs.mkdtemp(
13
+ path.join(os.tmpdir(), `tonnage-package-`),
14
+ )
15
+ try {
16
+ const nodeModulesDirectory = path.join(temporaryDirectory, `node_modules`)
17
+ const packageLinkPath = path.resolve(nodeModulesDirectory, packageName)
18
+ if (!packageLinkPath.startsWith(`${nodeModulesDirectory}${path.sep}`)) {
19
+ throw new Error(`Invalid package name: ${packageName}`)
20
+ }
21
+
22
+ await fs.mkdir(path.dirname(packageLinkPath), { recursive: true })
23
+ await fs.symlink(
24
+ packageDirectory,
25
+ packageLinkPath,
26
+ process.platform === `win32` ? `junction` : `dir`,
27
+ )
28
+
29
+ const filter = new RegExp(`^${escapeRegExp(packageName)}(?:/.*)?$`)
30
+ const plugin: Plugin = {
31
+ name: `tonnage-package-resolution`,
32
+ setup(build) {
33
+ build.onResolve({ filter }, async (args) => {
34
+ if (args.resolveDir === temporaryDirectory) {
35
+ return
36
+ }
37
+
38
+ return build.resolve(args.path, {
39
+ importer: args.importer,
40
+ kind: args.kind,
41
+ namespace: args.namespace,
42
+ resolveDir: temporaryDirectory,
43
+ with: args.with,
44
+ })
45
+ })
46
+ },
47
+ }
48
+
49
+ return await useResolver(plugin)
50
+ } finally {
51
+ await fs.rm(temporaryDirectory, { force: true, recursive: true })
52
+ }
53
+ }
54
+
55
+ function escapeRegExp(value: string): string {
56
+ return value.replace(/[.*+?^${}()|[\]\\]/g, `\\$&`)
57
+ }