@rollup-extras/plugin-size 0.1.0 → 0.2.0

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.
package/README.md CHANGED
@@ -1,14 +1,14 @@
1
1
  # @rollup-extras/plugin-size
2
2
 
3
- Rollup plugin that reports the size of generated artifacts — raw, minified (via [oxc-minify](https://www.npmjs.com/package/oxc-minify)), and compressed (gzip and/or brotli).
3
+ Rollup plugin that reports the size of generated artifacts — raw, optionally minified, and compressed (gzip and/or brotli).
4
4
 
5
5
  Output is summarised by category:
6
6
 
7
- | Category | Grouping | Sizes shown |
8
- | -------------------- | ------------------------------------- | ------------------------------ |
9
- | **Entry chunks** | output format (`es`, `cjs`, `umd`, …) | raw → minified → gzip / brotli |
10
- | **Non-entry chunks** | output format | raw → minified → gzip / brotli |
11
- | **Assets** | file extension (`.css`, `.svg`, …) | raw → gzip / brotli |
7
+ | Category | Grouping | Sizes shown |
8
+ | -------------------- | ------------------------------------- | ---------------------------------------------- |
9
+ | **Entry chunks** | output format (`es`, `cjs`, `umd`, …) | raw → minified (if configured) → gzip / brotli |
10
+ | **Non-entry chunks** | output format | raw → minified (if configured) → gzip / brotli |
11
+ | **Assets** | file extension (`.css`, `.svg`, …) | raw → gzip / brotli |
12
12
 
13
13
  A JSON stats file is maintained between builds so that size deltas are printed on subsequent runs. Only categories that appear in either the current or the previous build are shown — nothing irrelevant is printed.
14
14
 
@@ -67,6 +67,38 @@ size({ brotli: true });
67
67
  size({ gzip: false, brotli: true });
68
68
  ```
69
69
 
70
+ ### Enable minification reporting
71
+
72
+ Minification is optional. Pass a `minify` function to report minified sizes:
73
+
74
+ ```js
75
+ import { minify } from "oxc-minify";
76
+
77
+ size({
78
+ minify: async (fileName, code) => {
79
+ const result = await minify(fileName, code, {
80
+ compress: { target: "esnext" },
81
+ mangle: { toplevel: true },
82
+ codegen: { removeWhitespace: true },
83
+ });
84
+ return result.code;
85
+ },
86
+ });
87
+ ```
88
+
89
+ Or use any other minifier (terser, esbuild, etc.):
90
+
91
+ ```js
92
+ import { transform } from "esbuild";
93
+
94
+ size({
95
+ minify: async (_fileName, code) => {
96
+ const result = await transform(code, { minify: true });
97
+ return result.code;
98
+ },
99
+ });
100
+ ```
101
+
70
102
  ### Full options
71
103
 
72
104
  ```js
@@ -76,25 +108,30 @@ size({
76
108
  gzip: true,
77
109
  brotli: true,
78
110
  pluginName: "my-size",
111
+ minify: async (fileName, code) => {
112
+ // your minification logic
113
+ return minifiedCode;
114
+ },
79
115
  });
80
116
  ```
81
117
 
82
118
  ## Options
83
119
 
84
- | Option | Type | Default | Description |
85
- | ------------- | --------- | ------------------------------ | ------------------------------------------------------------------- |
86
- | `statsFile` | `string` | `'.stats.json'` | Path to the JSON stats file (relative to project root or absolute). |
87
- | `updateStats` | `boolean` | `true` | Whether to overwrite the stats file with the current build data. |
88
- | `gzip` | `boolean` | `true` | Report gzip-compressed sizes. |
89
- | `brotli` | `boolean` | `false` | Report brotli-compressed sizes. |
90
- | `pluginName` | `string` | `'@rollup-extras/plugin-size'` | Override the plugin name reported to Rollup. |
120
+ | Option | Type | Default | Description |
121
+ | ------------- | ---------- | ------------------------------ | ----------------------------------------------------------------------------------------------- |
122
+ | `statsFile` | `string` | `'.stats.json'` | Path to the JSON stats file (relative to project root or absolute). |
123
+ | `updateStats` | `boolean` | `true` | Whether to overwrite the stats file with the current build data. |
124
+ | `gzip` | `boolean` | `true` | Report gzip-compressed sizes. |
125
+ | `brotli` | `boolean` | `false` | Report brotli-compressed sizes. |
126
+ | `pluginName` | `string` | `'@rollup-extras/plugin-size'` | Override the plugin name reported to Rollup. |
127
+ | `minify` | `function` | `undefined` | Optional async function `(fileName, code) => minifiedCode` to minify chunks before compression. |
91
128
 
92
129
  ## How it works
93
130
 
94
131
  The plugin hooks into Rollup's `generateBundle` and `closeBundle` lifecycle:
95
132
 
96
133
  1. **`generateBundle`** — iterates over every chunk and asset in the output bundle.
97
- - **Chunks** (entry and non-entry) are minified with `oxc-minify` and then compressed with the enabled algorithms (gzip and/or brotli). Sizes are accumulated per output format.
134
+ - **Chunks** (entry and non-entry) are optionally minified (if a `minify` function is provided) and then compressed with the enabled algorithms (gzip and/or brotli). Sizes are accumulated per output format.
98
135
  - **Assets** are compressed with the enabled algorithms (no JS minification). Sizes are accumulated per file extension.
99
136
  2. **`closeBundle`** (via `multiConfigPluginBase`) — loads the previous stats file (if any), prints a comparison report using [`@niceties/ansi`](https://www.npmjs.com/package/@niceties/ansi) colours, and writes the updated stats file.
100
137
 
package/index.d.ts CHANGED
@@ -20,10 +20,11 @@ declare module '@rollup-extras/plugin-size' {
20
20
  updateStats?: boolean;
21
21
  gzip?: boolean;
22
22
  brotli?: boolean;
23
+ minify?: (code: string, fileName: string) => Promise<string>;
23
24
  };
24
25
  export type ChunkSizeStats = {
25
26
  raw: number;
26
- minified: number;
27
+ minified?: number;
27
28
  gzip?: number;
28
29
  brotli?: number;
29
30
  };
package/index.d.ts.map CHANGED
@@ -14,6 +14,6 @@
14
14
  "sourcesContent": [
15
15
  null
16
16
  ],
17
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;",
17
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;",
18
18
  "ignoreList": []
19
19
  }
package/index.js CHANGED
@@ -3,8 +3,6 @@ import { extname, resolve } from 'node:path';
3
3
  import { promisify } from 'node:util';
4
4
  import { brotliCompress as brotliCb, gzip as gzipCb } from 'node:zlib';
5
5
 
6
- import { minify } from 'oxc-minify';
7
-
8
6
  import { bold, cyan, dim, green, red, yellow } from '@niceties/ansi';
9
7
  import logger from '@rollup-extras/utils/logger';
10
8
  import { multiConfigPluginBase } from '@rollup-extras/utils/multi-config-plugin-base';
@@ -59,7 +57,8 @@ function printReport(current, previous, log) {
59
57
  const cur = current.entries?.[format];
60
58
  const prev = previous.entries?.[format];
61
59
  if (cur) {
62
- log(`${bold(cyan(format))} entry: ${formatSize(cur.raw)} → ${formatSize(cur.minified)}${formatCompressed(cur, prev)}`);
60
+ const minPart = cur.minified != null ? ` → ${formatSize(cur.minified)}` : '';
61
+ log(`${bold(cyan(format))} entry: ${formatSize(cur.raw)}${minPart}${formatCompressed(cur, prev)}`);
63
62
  } else {
64
63
  log(`${bold(cyan(format))} entry: ${dim('removed')}`);
65
64
  }
@@ -69,7 +68,8 @@ function printReport(current, previous, log) {
69
68
  const cur = current.chunks?.[format];
70
69
  const prev = previous.chunks?.[format];
71
70
  if (cur) {
72
- log(`${bold(cyan(format))} chunks: ${formatSize(cur.raw)} → ${formatSize(cur.minified)}${formatCompressed(cur, prev)}`);
71
+ const minPart = cur.minified != null ? ` → ${formatSize(cur.minified)}` : '';
72
+ log(`${bold(cyan(format))} chunks: ${formatSize(cur.raw)}${minPart}${formatCompressed(cur, prev)}`);
73
73
  } else {
74
74
  log(`${bold(cyan(format))} chunks: ${dim('removed')}`);
75
75
  }
@@ -111,7 +111,7 @@ async function compressSizes(buf, flags) {
111
111
  return result;
112
112
  }
113
113
 
114
- async function collectStats(currentStats, outputOptions, bundle, flags) {
114
+ async function collectStats(currentStats, outputOptions, bundle, flags, minifyFn) {
115
115
  const format = outputOptions.format;
116
116
 
117
117
  await Promise.all(
@@ -120,13 +120,17 @@ async function collectStats(currentStats, outputOptions, bundle, flags) {
120
120
  const code = (item).code;
121
121
  const raw = Buffer.byteLength(code, 'utf8');
122
122
 
123
- const minResult = await minify(fileName, code, {
124
- compress: { target: 'esnext' },
125
- mangle: { toplevel: true },
126
- codegen: { removeWhitespace: true },
127
- });
128
- const minifiedSize = Buffer.byteLength(minResult.code, 'utf8');
129
- const sizes = await compressSizes(Buffer.from(minResult.code, 'utf8'), flags);
123
+ let codeForCompression = code;
124
+
125
+ let minifiedSize;
126
+
127
+ if (minifyFn) {
128
+ const minifiedCode = await minifyFn(code, fileName);
129
+ minifiedSize = Buffer.byteLength(minifiedCode, 'utf8');
130
+ codeForCompression = minifiedCode;
131
+ }
132
+
133
+ const sizes = await compressSizes(Buffer.from(codeForCompression, 'utf8'), flags);
130
134
 
131
135
  const category = (item).isEntry ? 'entries' : 'chunks';
132
136
  if (!currentStats[category]) {
@@ -134,10 +138,12 @@ async function collectStats(currentStats, outputOptions, bundle, flags) {
134
138
  }
135
139
  const bucket = (currentStats[category]);
136
140
  if (!bucket[format]) {
137
- bucket[format] = { raw: 0, minified: 0 };
141
+ bucket[format] = { raw: 0 };
138
142
  }
139
143
  bucket[format].raw += raw;
140
- bucket[format].minified += minifiedSize;
144
+ if (minifiedSize != null) {
145
+ bucket[format].minified = (bucket[format].minified ?? 0) + minifiedSize;
146
+ }
141
147
  if (sizes.gzip != null) {
142
148
  bucket[format].gzip = (bucket[format].gzip ?? 0) + sizes.gzip;
143
149
  }
@@ -191,7 +197,7 @@ export default function (options) {
191
197
  factories
192
198
  );
193
199
 
194
- const { pluginName, statsFile, updateStats, logger } = normalizedOptions;
200
+ const { pluginName, statsFile, updateStats, logger, minify: minifyFn } = normalizedOptions;
195
201
 
196
202
  const flags = { gzip: normalizedOptions.gzip, brotli: normalizedOptions.brotli };
197
203
 
@@ -202,7 +208,7 @@ export default function (options) {
202
208
  return instance;
203
209
 
204
210
  async function onEachOutput(outputOptions, bundle) {
205
- await collectStats(currentStats, outputOptions, bundle, flags);
211
+ await collectStats(currentStats, outputOptions, bundle, flags, minifyFn);
206
212
  }
207
213
 
208
214
  async function reportAndSave() {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.1.0",
2
+ "version": "0.2.0",
3
3
  "license": "MIT",
4
4
  "name": "@rollup-extras/plugin-size",
5
5
  "author": "Konstantin Shutkin",
@@ -37,7 +37,6 @@
37
37
  "dependencies": {
38
38
  "@niceties/ansi": "^1.1.2",
39
39
  "@niceties/logger": "^2.1.0",
40
- "oxc-minify": "^0.125.0",
41
40
  "@rollup-extras/utils": "^2.0.0"
42
41
  },
43
42
  "peerDependencies": {