@rollup-extras/plugin-size 0.2.1 → 0.3.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.
package/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ ![Coverage](https://raw.githubusercontent.com/kshutkin/rollup-extras/refs/heads/coverage/plugin-size/badge.svg)
2
+
1
3
  # @rollup-extras/plugin-size
2
4
 
3
5
  Rollup plugin that reports the size of generated artifacts — raw, optionally minified, and compressed (gzip and/or brotli).
@@ -108,6 +110,7 @@ size({
108
110
  gzip: true,
109
111
  brotli: true,
110
112
  pluginName: "my-size",
113
+ outputPlugin: false,
111
114
  minify: async (fileName, code) => {
112
115
  // your minification logic
113
116
  return minifiedCode;
@@ -115,16 +118,34 @@ size({
115
118
  });
116
119
  ```
117
120
 
121
+ ### Output plugin usage
122
+
123
+ The plugin can also be used as an output plugin (inside `output.plugins`). Because
124
+ Rollup rejects input-only hooks like `buildStart` in that position, pass
125
+ `outputPlugin: true` to skip attaching them:
126
+
127
+ ```js
128
+ export default {
129
+ input: "src/index.js",
130
+ output: {
131
+ dir: "dist",
132
+ format: "es",
133
+ plugins: [size({ outputPlugin: true })],
134
+ },
135
+ };
136
+ ```
137
+
118
138
  ## Options
119
139
 
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. |
140
+ | Option | Type | Default | Description |
141
+ | -------------- | ---------- | ------------------------------ | ----------------------------------------------------------------------------------------------- |
142
+ | `statsFile` | `string` | `'.stats.json'` | Path to the JSON stats file (relative to project root or absolute). |
143
+ | `updateStats` | `boolean` | `true` | Whether to overwrite the stats file with the current build data. |
144
+ | `gzip` | `boolean` | `true` | Report gzip-compressed sizes. |
145
+ | `brotli` | `boolean` | `false` | Report brotli-compressed sizes. |
146
+ | `pluginName` | `string` | `'@rollup-extras/plugin-size'` | Override the plugin name reported to Rollup. |
147
+ | `outputPlugin` | `boolean` | `false` | Set to `true` when using the plugin in `output.plugins` (see [Output plugin usage](#output-plugin-usage)). |
148
+ | `minify` | `function` | `undefined` | Optional async function `(fileName, code) => minifiedCode` to minify chunks before compression. |
128
149
 
129
150
  ## How it works
130
151
 
package/index.d.ts CHANGED
@@ -20,6 +20,7 @@ declare module '@rollup-extras/plugin-size' {
20
20
  updateStats?: boolean;
21
21
  gzip?: boolean;
22
22
  brotli?: boolean;
23
+ outputPlugin?: boolean;
23
24
  minify?: (code: string, fileName: string) => Promise<string>;
24
25
  };
25
26
  export type ChunkSizeStats = {
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
@@ -193,39 +193,66 @@ export default function (options) {
193
193
  updateStats: true,
194
194
  gzip: true,
195
195
  brotli: false,
196
+ outputPlugin: false,
196
197
  },
197
198
  factories
198
199
  );
199
200
 
200
- const { pluginName, statsFile, updateStats, logger, minify: minifyFn } = normalizedOptions;
201
+ const { pluginName, statsFile, updateStats, outputPlugin, logger, minify: minifyFn } = normalizedOptions;
201
202
 
202
203
  const flags = { gzip: normalizedOptions.gzip, brotli: normalizedOptions.brotli };
203
204
 
204
- const currentStats = {};
205
+ let currentStats = {};
206
+
207
+ let buildInputs = new WeakSet();
208
+ let reportComplete = false;
205
209
 
206
210
  const instance = multiConfigPluginBase(false, pluginName, reportAndSave, onEachOutput);
207
211
 
208
- instance.buildStart = () => {
209
- currentStats.entries = undefined;
210
- currentStats.chunks = undefined;
211
- currentStats.assets = undefined;
212
- };
212
+ if (outputPlugin) {
213
+ const addInstance = instance.api.addInstance;
214
+ instance.api.addInstance = () => trackBuild(addInstance());
215
+ trackBuild(instance);
216
+ } else {
217
+ instance.buildStart = () => {
218
+ currentStats = {};
219
+ };
220
+ }
213
221
 
214
222
  return instance;
215
223
 
224
+ function trackBuild(plugin) {
225
+ const renderStart = (plugin.renderStart);
226
+ plugin.renderStart = function (outputOptions, inputOptions) {
227
+
228
+ if (reportComplete && !buildInputs.has(inputOptions)) {
229
+ currentStats = {};
230
+ buildInputs = new WeakSet();
231
+ reportComplete = false;
232
+ }
233
+ buildInputs.add(inputOptions);
234
+ return renderStart.call(this, outputOptions, inputOptions);
235
+ };
236
+ return plugin;
237
+ }
238
+
216
239
  async function onEachOutput(outputOptions, bundle) {
217
240
  await collectStats(currentStats, outputOptions, bundle, flags, minifyFn);
218
241
  }
219
242
 
220
243
  async function reportAndSave() {
221
- const statsPath = resolve(process.cwd(), statsFile);
244
+ try {
245
+ const statsPath = resolve(process.cwd(), statsFile);
222
246
 
223
- const previousStats = await loadPreviousStats(statsPath);
247
+ const previousStats = await loadPreviousStats(statsPath);
224
248
 
225
- printReport(currentStats, previousStats, logger);
249
+ printReport(currentStats, previousStats, logger);
226
250
 
227
- if (updateStats) {
228
- await writeFile(statsPath, `${JSON.stringify(currentStats, null, 2)}\n`);
251
+ if (updateStats) {
252
+ await writeFile(statsPath, `${JSON.stringify(currentStats, null, 2)}\n`);
253
+ }
254
+ } finally {
255
+ reportComplete = true;
229
256
  }
230
257
  }
231
258
  }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.2.1",
2
+ "version": "0.3.1",
3
3
  "license": "MIT",
4
4
  "name": "@rollup-extras/plugin-size",
5
5
  "author": "Konstantin Shutkin",