Skip to content

Commit

Permalink
Require Node.js 18 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 31, 2023
1 parent b419330 commit eff5826
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 288 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 20
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
124 changes: 39 additions & 85 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
'use strict';
const fancyLog = require('fancy-log');
const PluginError = require('plugin-error');
const through = require('through2');
const chalk = require('chalk');
const prettyBytes = require('pretty-bytes');
const StreamCounter = require('stream-counter');
const gzipSize = require('gzip-size');
const brotliSize = require('brotli-size');

function hasSize(sizes) {
return [...sizes.values()].some(size => size > 0);
}
import fancyLog from 'fancy-log';
import chalk from 'chalk';
import prettyBytes from 'pretty-bytes';
import {gzipSize} from 'gzip-size';
import brotliSize from 'brotli-size';
import {gulpPlugin} from 'gulp-plugin-extras';

function promisify(stream, property, event = 'end') {
return new Promise((resolve, reject) => {
stream.on(event, () => resolve(stream[property]))
.on('error', error => reject(error));
});
}
const hasSize = sizes => [...sizes.values()].some(size => size > 0);

module.exports = (options = {}) => {
export default function gulpSize(options = {}) {
options = {
pretty: true,
showTotal: true,
uncompressed: options.uncompressed || !(options.gzip || options.brotli),
...options
...options,
};

let fileCount = 0;
Expand All @@ -33,10 +21,10 @@ module.exports = (options = {}) => {
const description = new Map([
['uncompressed', ''],
['gzip', ' (gzipped)'],
['brotli', ' (brotli)']
['brotli', ' (brotli)'],
]);

function log(what, sizes) {
const log = (what, sizes) => {
let {title} = options;
title = title ? chalk.cyan(title) + ' ' : '';
const sizeStrings = [...sizes].map(([key, size]) => {
Expand All @@ -45,79 +33,45 @@ module.exports = (options = {}) => {
});

fancyLog(title + what + ' ' + sizeStrings.join(chalk.magenta(', ')));
}

return through.obj((file, encoding, callback) => {
if (file.isNull()) {
callback(null, file);
return;
}

const finish = (error, sizes) => {
if (error) {
callback(new PluginError('gulp-size', error));
return;
}

for (const [key, size] of sizes) {
totalSize.set(key, size + (totalSize.get(key) || 0));
}

if (options.showFiles === true && hasSize(sizes)) {
log(chalk.blue(file.relative), sizes);
}

fileCount++;
callback(null, file);
};
};

return gulpPlugin('gulp-size', async file => {
const selectedSizes = new Map();
if (file.isStream()) {
if (options.uncompressed) {
selectedSizes.set('uncompressed', promisify(file.contents.pipe(new StreamCounter()), 'bytes', 'finish'));
}

if (options.gzip) {
selectedSizes.set('gzip', promisify(file.contents.pipe(gzipSize.stream()), 'gzipSize'));
}
if (options.uncompressed) {
selectedSizes.set('uncompressed', file.contents.length);
}

if (options.brotli) {
selectedSizes.set('brotli', promisify(file.contents.pipe(brotliSize.stream()), 'brotliSize'));
}
if (options.gzip) {
selectedSizes.set('gzip', gzipSize(file.contents));
}

if (file.isBuffer()) {
if (options.uncompressed) {
selectedSizes.set('uncompressed', file.contents.length);
}
if (options.brotli) {
selectedSizes.set('brotli', brotliSize.default(file.contents));
}

if (options.gzip) {
selectedSizes.set('gzip', gzipSize(file.contents));
}
let sizes = await Promise.all([...selectedSizes].map(async ([key, size]) => [key, await size]));
sizes = new Map(sizes);

if (options.brotli) {
selectedSizes.set('brotli', brotliSize.default(file.contents));
}
for (const [key, size] of sizes) {
totalSize.set(key, size + (totalSize.get(key) ?? 0));
}

(async () => {
try {
// We want to keep the names
const sizes = await Promise.all([...selectedSizes].map(async ([key, size]) => [key, await size]));
if (options.showFiles === true && hasSize(sizes)) {
log(chalk.blue(file.relative), sizes);
}

finish(null, new Map(sizes));
} catch (error) {
finish(error);
}
})();
}, function (callback) {
this.size = totalSize.values().next().value || 0;
this.prettySize = prettyBytes(this.size);
fileCount++;

if (!(fileCount === 1 && options.showFiles) && hasSize(totalSize) && fileCount > 0 && options.showTotal) {
log(chalk.green('all files'), totalSize);
}
return file;
}, {
async * onFinish(stream) { // eslint-disable-line require-yield
stream.size = totalSize.values().next().value ?? 0;
stream.prettySize = prettyBytes(stream.size);

callback();
if (!(fileCount === 1 && options.showFiles) && hasSize(totalSize) && fileCount > 0 && options.showTotal) {
log(chalk.green('all files'), totalSize);
}
},
});
};
}
28 changes: 16 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
"node": ">=18"
},
"scripts": {
"test": "xo && mocha"
"test": "xo && ava"
},
"files": [
"index.js"
Expand All @@ -33,18 +35,17 @@
],
"dependencies": {
"brotli-size": "^4.0.0",
"chalk": "^4.1.1",
"fancy-log": "^1.3.3",
"gzip-size": "^6.0.0",
"plugin-error": "^1.0.1",
"pretty-bytes": "^5.6.0",
"stream-counter": "^1.0.0",
"through2": "^4.0.2"
"chalk": "^5.3.0",
"fancy-log": "^2.0.0",
"gulp-plugin-extras": "^0.2.1",
"gzip-size": "^7.0.0",
"pretty-bytes": "^6.1.1"
},
"devDependencies": {
"mocha": "^8.3.2",
"vinyl": "^2.2.1",
"xo": "^0.39.1"
"ava": "^5.3.1",
"p-event": "^6.0.0",
"vinyl": "^3.0.0",
"xo": "^0.56.0"
},
"peerDependencies": {
"gulp": ">=4"
Expand All @@ -54,6 +55,9 @@
"optional": true
}
},
"ava": {
"serial": true
},
"xo": {
"env": [
"node",
Expand Down
24 changes: 13 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ Logs out the total size of files in the stream and optionally the individual fil

## Install

```
$ npm install --save-dev gulp-size
```sh
npm install --save-dev gulp-size
```

## Usage

```js
const gulp = require('gulp');
const size = require('gulp-size');
import gulp from 'gulp';
import size from 'gulp-size';

exports.default = () => (
export default () => (
gulp.src('fixture.js')
.pipe(size())
.pipe(gulp.dest('dist'))
Expand Down Expand Up @@ -101,19 +101,21 @@ Prettified version of `.size`.
You could, for example, use this to report the total project size with [`gulp-notify`](https://github.com/mikaelbr/gulp-notify):

```js
const gulp = require('gulp');
const size = require('gulp-size');
const notify = require('gulp-notify');
import gulp from 'gulp';
import size from 'gulp-size';
import notify from 'gulp-notify';

export default () => (

exports.default = () => {
const s = size();
const sizeInstance = size();

return gulp.src('fixture.js')
.pipe(s)
.pipe(sizeInstance)
.pipe(gulp.dest('dist'))
.pipe(notify({
onLast: true,
message: () => `Total size ${s.prettySize}`
message: () => `Total size ${sizeInstance.prettySize}`
}));
};
```

0 comments on commit eff5826

Please sign in to comment.