UNPKG

4.01 kBMarkdownView Raw
1# gulp-vinyl-size
2
3[![Build Status](https://travis-ci.com/burntcustard/gulp-vinyl-size.svg?branch=master)](https://travis-ci.com/burntcustard/gulp-vinyl-size)
4[![codecov](https://codecov.io/gh/burntcustard/gulp-vinyl-size/branch/master/graph/badge.svg)](https://codecov.io/gh/burntcustard/gulp-vinyl-size)
5[![install size](https://packagephobia.com/badge?p=gulp-vinyl-size)](https://packagephobia.com/result?p=gulp-vinyl-size)
6
7[![NPM](https://nodei.co/npm/gulp-vinyl-size.png?compact=true)](https://nodei.co/npm/gulp-vinyl-size/)
8
9__[Gulp](https://www.npmjs.com/package/gulp) plugin to log the size of individual files (Vinyl objects) in the stream. A simpler, more flexible alternative to [gulp-size](https://www.npmjs.com/package/gulp-size).__
10
11## Install
12
13`$ npm install --save-dev gulp-vinyl-size`
14
15## Basic Usage
16
17```js
18const gulp = require('gulp');
19const size = require('gulp-vinyl-size');
20
21function copyScripts() {
22 return gulp
23 .src('assets/scripts/*.js')
24 .pipe(size())
25 .pipe(gulp.dest('/dist/js/'));
26}
27
28// [13:01:44] main.js: 3.13 kB
29// [13:01:45] demo.js: 1.09 kB
30```
31
32## Options
33
34The first parameter is an options object.
35
36### gzip (Default: `false`)
37
38Additionally log gzipped-size.
39
40```js
41.pipe(size({gzip: true}))
42
43// [12:32:22] main.js: 5.29 kB (2.18 kB gzipped)
44```
45
46### bytes (Default: `false`)
47
48Log size in bytes rather than converting to kilobytes, megabytes, etc.
49
50```js
51.pipe(size({bytes: true}))
52
53// [12:32:22] main.js: 5417 B
54```
55
56### filesize
57
58All options get passed _directly_ to the size-prettifying package filesize. See [it's npm page](https://www.npmjs.com/package/filesize) for a full list of options.
59
60```js
61.pipe(size({base: 2, spacer: '|'}))
62
63// [12:32:22] main.js: 5.29|KiB
64```
65
66## Callback function
67
68The second parameter is a callback function that lets you do whatever you want with the size info, instead of it being logged automtically.
69
70```js
71.pipe(size({}, size => console.log(`Minified CSS: ${size}`))
72
73// Minified CSS: 3.13 kB
74```
75
76[fancy-log](https://www.npmjs.com/package/fancy-log) can be used to keep the timestamp when logging via a callback.
77
78```js
79const log = require('fancy-log');
80// ...
81.pipe(size({}, size => log(`Minified CSS: ${size}`)))
82
83// [12:32:22] Minified CSS: 3.13 kB
84```
85
86## Advanced usage
87
88The parameter of the callback (which can be named anything, but `size` or `info` are recommended) is an object which outputs the size string when used within a template literal, but it also contains these properties:
89- `sizeString` - Size _with_ gzipped size (if `gzip: true`). E.g. `'24 B (gzipped: 8 B)'`. Same as `info` directly in the callback.
90- `size` - Size _without_ gzipped size. E.g. just `'24 B'`.
91- `gzip` - Gzipped size, _available even if the `gzip` option is false_. E.g. `'8 B'`
92- `filename` - `file.relative` of the vinyl. E.g. `'main.js'`.
93
94With these properties, multiple calls to gulp-vinyl-size, [fancy-log](https://www.npmjs.com/package/fancy-log) and [ansi-colors](https://www.npmjs.com/package/ansi-colors), detailed logging can be done, e.g.
95
96```js
97const gulp = require('gulp');
98const size = require('gulp-vinyl-size');
99const log = require('fancy-log');
100const c = require('ansi-colors');
101const sass = require('gulp-sass');
102const cleanCSS = require('gulp-clean-css');
103const purgecss = require('gulp-purgecss');
104
105function css() {
106 return gulp
107 .src('src/scss/style.scss')
108 .pipe(sass().on('error', sass.logError))
109 .on('data', () => log('CSS'))
110 .pipe(size({}, info => log(`└ transpiled ${color.magenta(info)}`)))
111 .pipe(purgecss({content: ['**/*.php']}))
112 .pipe(size({}, info => log(`└ purged ${color.magenta(info)}`)))
113 .pipe(cleanCSS())
114 .pipe(size({}, info => log(`└ minified ${color.magenta(info.size)} ${color.gray(`(gzipped: ${info.gzip})`)}`)))
115 .pipe(gulp.dest('dist/css'));
116}
117
118// [11:02:38] Starting 'css'...
119// [11:02:39] CSS
120// [11:02:39] └ transpiled 29.06 kB
121// [11:02:40] └ purged 6.31 kB
122// [11:02:40] └ minified 3.13 kB (gzipped: 1.04 kB)
123```