Skip to content

Commit 40905d2

Browse files
authored
Merge pull request #4 from kuldeepkeshwar/master
move common logic to size-plugin-core & add support for size-plugin bot
2 parents 48db35d + 602f399 commit 40905d2

File tree

5 files changed

+37
-1341
lines changed

5 files changed

+37
-1341
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
> 🙋 Using Webpack? Check out the original [size-plugin](https://github.com/GoogleChromeLabs/size-plugin).
1717
18+
> 🙋‍♂ Using CI ? Check out [size-plugin-bot](https://github.com/kuldeepkeshwar/size-plugin-bot) 🤖 to automate intergation with CI
19+
1820
## Installation
1921

2022
Install `rollup-plugin-size` as a development dependency using npm:

package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@
1010
"author": "Wesley Luyten <me@wesleyluyten.com> (https://wesleyluyten.com)",
1111
"license": "Apache-2.0",
1212
"dependencies": {
13-
"brotli-size": "^0.0.3",
14-
"chalk": "^2.4.2",
15-
"glob": "^7.1.3",
16-
"gzip-size": "^5.0.0",
17-
"minimatch": "^3.0.4",
18-
"pretty-bytes": "^5.1.0"
13+
"size-plugin-core": "^0.0.6"
1914
},
2015
"devDependencies": {
2116
"eslint": "^5.14.0",

rollup-plugin-size.js

Lines changed: 34 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,51 @@
1-
/**
2-
* Copyright 2018 Google LLC
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5-
* use this file except in compliance with the License. You may obtain a copy of
6-
* the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12-
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13-
* License for the specific language governing permissions and limitations under
14-
* the License.
15-
*/
16-
171
const path = require('path');
18-
const chalk = require('chalk');
19-
const { promisify } = require('util');
20-
const globPromise = require('glob');
21-
const minimatch = require('minimatch');
22-
const gzipSize = require('gzip-size');
23-
const brotliSize = require('brotli-size');
24-
const prettyBytes = require('pretty-bytes');
25-
const { toMap, dedupe } = require('./utils.js');
26-
const glob = promisify(globPromise);
27-
28-
brotliSize.file = (path, options) => {
29-
return new Promise((resolve, reject) => {
30-
const stream = fs.createReadStream(path);
31-
stream.on('error', reject);
32-
33-
const brotliStream = stream.pipe(brotliSize.stream(options));
34-
brotliStream.on('error', reject);
35-
brotliStream.on('brotli-size', resolve);
36-
});
37-
};
38-
2+
const SizePluginCore = require('size-plugin-core');
393

404
const defaults = {
415
gzip: true,
426
brotli: false,
43-
pattern: '**/*.{mjs,js,css,html}',
7+
pattern: '**/*.{mjs,js,jsx,css,html}',
448
exclude: undefined,
45-
columnWidth: 20
9+
writeFile: true,
10+
publish: false
4611
};
47-
12+
/**
13+
* Size Plugin for Rollup
14+
* @param {Object} options
15+
* @param {string} [options.gzip] use gzip compression for files
16+
* @param {string} [options.brotli] use brotli compression for files
17+
* @param {string} [options.pattern] minimatch pattern of files to track
18+
* @param {string} [options.exclude] minimatch pattern of files NOT to track
19+
* @param {string} [options.filename] file name to save filesizes to disk
20+
* @param {boolean} [options.publish] option to publish filesizes to size-plugin-store
21+
* @param {boolean} [options.writeFile] option to save filesizes to disk
22+
*/
4823
function bundleSize(options) {
49-
const { pattern, exclude, columnWidth, brotli } = Object.assign(defaults, options);
50-
51-
let max = '';
52-
let firstTime = true;
53-
let initialSizes;
54-
55-
const compressionSize = brotli ? brotliSize : gzipSize;
56-
57-
function buildStart() {
58-
max = '';
59-
}
60-
61-
function renderChunk(code) {
62-
max = max += code;
63-
return null;
64-
}
24+
const coreOptions = Object.assign(defaults, options);
25+
coreOptions.compression = coreOptions.brotli ? 'brotli' : 'gzip';
6526

27+
const core = new SizePluginCore(coreOptions);
6628
async function generateBundle(outputOptions, bundle) {
67-
if (firstTime) {
68-
firstTime = false;
69-
initialSizes = await getSizes(path.dirname(outputOptions.file));
70-
}
71-
outputSizes(bundle).catch(console.error);
72-
}
73-
74-
async function outputSizes(assets) {
75-
// map of filenames to their previous size
76-
const sizesBefore = await Promise.resolve(initialSizes);
77-
const isMatched = minimatch.filter(pattern);
78-
const isExcluded = exclude ? minimatch.filter(exclude) : () => false;
79-
const assetNames = Object.keys(assets).filter(
80-
file => isMatched(file) && !isExcluded(file)
81-
);
82-
const sizes = await Promise.all(
83-
assetNames.map(name => compressionSize(assets[name].code))
84-
);
85-
86-
// map of de-hashed filenames to their final size
87-
initialSizes = toMap(assetNames, sizes);
88-
89-
// get a list of unique filenames
90-
const files = Object.keys(initialSizes).filter(dedupe);
91-
92-
const width = Math.max(...files.map(file => file.length));
93-
let output = '';
94-
for (const name of files) {
95-
const size = initialSizes[name] || 0;
96-
const delta = size - (sizesBefore[name] || 0);
97-
const msg =
98-
new Array(
99-
(width !== name.length ? width : columnWidth) - name.length + 2
100-
).join(' ') +
101-
name +
102-
' ⏤ ';
103-
const color =
104-
size > 100 * 1024
105-
? 'red'
106-
: size > 40 * 1024
107-
? 'yellow'
108-
: size > 20 * 1024
109-
? 'cyan'
110-
: 'green';
111-
let sizeText = chalk[color](prettyBytes(size));
112-
if (delta) {
113-
let deltaText = (delta > 0 ? '+' : '') + prettyBytes(delta);
114-
if (delta > 1024) {
115-
sizeText = chalk.bold(sizeText);
116-
deltaText = chalk.red(deltaText);
117-
} else if (delta < -10) {
118-
deltaText = chalk.green(deltaText);
119-
}
120-
sizeText += ` (${deltaText})`;
121-
}
122-
output += msg + sizeText;
123-
}
124-
if (output) {
125-
console.log(output);
29+
try {
30+
const assets = Object.keys(bundle).reduce((agg, key) => {
31+
agg[key] = {
32+
source: bundle[key].code
33+
};
34+
return agg;
35+
}, {});
36+
const outputPath = outputOptions.dir
37+
? path.resolve(outputOptions.dir)
38+
: path.dirname(outputOptions.file);
39+
outputOptions.file &&
40+
(core.options.pattern = `${Object.keys(assets).pop()}`);
41+
const output = await core.execute(assets, outputPath);
42+
output && console.log(output);
43+
} catch (error) {
44+
console.error(error);
12645
}
12746
}
128-
129-
async function getSizes(cwd) {
130-
const files = await glob(pattern, { cwd, ignore: exclude });
131-
132-
const sizes = await Promise.all(
133-
files.map(file => compressionSize.file(path.join(cwd, file)).catch(() => null))
134-
);
135-
136-
return toMap(files, sizes);
137-
}
138-
13947
return {
14048
name: 'rollup-plugin-size',
141-
buildStart,
142-
renderChunk,
14349
generateBundle
14450
};
14551
}

utils.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)