@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 +29 -8
- package/index.d.ts +1 -0
- package/index.d.ts.map +1 -1
- package/index.js +39 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+

|
|
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
|
|
121
|
-
|
|
|
122
|
-
| `statsFile`
|
|
123
|
-
| `updateStats`
|
|
124
|
-
| `gzip`
|
|
125
|
-
| `brotli`
|
|
126
|
-
| `pluginName`
|
|
127
|
-
| `
|
|
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
package/index.d.ts.map
CHANGED
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
|
-
|
|
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
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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
|
-
|
|
244
|
+
try {
|
|
245
|
+
const statsPath = resolve(process.cwd(), statsFile);
|
|
222
246
|
|
|
223
|
-
|
|
247
|
+
const previousStats = await loadPreviousStats(statsPath);
|
|
224
248
|
|
|
225
|
-
|
|
249
|
+
printReport(currentStats, previousStats, logger);
|
|
226
250
|
|
|
227
|
-
|
|
228
|
-
|
|
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