Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for also displaying Brotli compressed size #47

Merged

Conversation

jonatanlinden
Copy link
Contributor

As the title says. It seems to be gaining ground, maybe someone else also will profit from such an option.

@sindresorhus
Copy link
Owner

Why not let the user display both if they want to?

@jonatanlinden
Copy link
Contributor Author

Yes, that would be better of course. To be honest, it is because of my very limited skills with javascript and promises. I'll have a look at it.

@jonatanlinden
Copy link
Contributor Author

I've done an attempt at letting the user show uncompressed, gzip and brotli size in any combination.

However, there seems to be some issues with the tests that rely on the finish event. After reading around and trying to understand, it seems that (at least one part of the problem) may be that the _flush function is not guaranteed to have finished before the finish event. So the size property of the stream may be undefined in those tests.

The 'finish' and 'end' events are from the parent Writable and Readable classes respectively. The 'finish' event is fired after stream.end() is called and all chunks have been processed by stream._transform(), 'end' is fired after all data has been output which is after the callback in stream._flush() has been called.

@jonatanlinden
Copy link
Contributor Author

Sorry, that was me following a red herring. Fixed.

index.js Outdated

function addPropWise(a, b) {
// eslint-disable-next-line guard-for-in
for (const k in b) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a for-of loop.

And use descriptive variable/function names. No abbreviations.

index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated
fancyLog(title + what + ' ' + strings.join(chalk.magenta(', ')));
}

function addPropWise(a, b) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a better name.

index.js Outdated
let fileCount = 0;
const totalSize = {};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's generally better to use a Map for this.

index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

Have you manually verified that all combinations of the options work correctly?

@jonatanlinden
Copy link
Contributor Author

No, I've just tried a few combinations. I guess the proper thing would be to add a couple of tests.

@jonatanlinden
Copy link
Contributor Author

jonatanlinden commented Apr 24, 2021

Speaking of which, I tried to test some more cases, and found something that I couldn't explain, which is also present in the original as well. In streaming mode, I can't manage to see that the data of the stream is actually piped on. The error could of course lie on my side.

For example, by modifying the test 'should handle stream contents' (and here I'm a bit on shaky ground), the second gulp-size invocation reports a size of 0.

it('should handle stream contents', callback => {
	const contents = through();
	const stream = size();
	const stream2 = size();

	stream2.on('finish', () => {
		assert.strictEqual(stream2.size, 100);
		callback();
	});

	stream.write(new Vinyl({
		path: path.join(__dirname, 'fixture.js'),
		contents
	}));

	contents.end(Buffer.alloc(100));
	stream.pipe(stream2);
	stream.end();
});

@sindresorhus
Copy link
Owner

I don't know, TBH. I haven't worked on this package for a long time.

@jonatanlinden
Copy link
Contributor Author

Regarding the issue mentioned above, it seems to be related to gulpjs/gulp#2380. I moved the callback (in the original gulp-size) from finish to directly after piping the file contents in case of it being a stream. After that it works, kind-of, in the sense that it doesn't empty the vinyl stream.

index.js Outdated Show resolved Hide resolved
@jonatanlinden
Copy link
Contributor Author

Sorry, it seems brotli-size requires node >= 10.16.

@sindresorhus sindresorhus changed the title Add support for displaying the brotli compressed size. Add support for displaying the brotli compressed size May 2, 2021
@sindresorhus sindresorhus changed the title Add support for displaying the brotli compressed size Add support for also displaying Brotli compressed size May 2, 2021
@sindresorhus sindresorhus merged commit e5200f1 into sindresorhus:main May 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants