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

[bundlephobia] Added a shield for calculating npm bundle sizes #1391

Closed
wants to merge 12 commits into from

Conversation

pastelsky
Copy link

A shield for bundle sizes of front end npm packages, as discussed over here - #941

@shields-ci
Copy link

shields-ci commented Dec 24, 2017

Messages
📖

✨ Thanks for your contribution to Shields, @pastelsky!

📖

Thanks for contributing to our documentation. We ❤️ our documentarians!

Generated by 🚫 dangerJS

@pastelsky
Copy link
Author

I'm not exactly sure why the CI check failed. Looks like some sort of an error related to svg2img library, which isn't related to this PR.

@paulmelnikow
Copy link
Member

Yea, you're right, it's unrelated. I opened #1392 to increase the timeout on that test. It times out fairly often.

@paulmelnikow paulmelnikow added the service-badge Accepted and actionable changes, features, and bugs label Dec 26, 2017
@pastelsky
Copy link
Author

Hey, are there any blockers for this to get merged?

Copy link
Member

@paulmelnikow paulmelnikow left a comment

Choose a reason for hiding this comment

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

Thanks for this! Here are a few comments for you.

'node',
'npm',
'bundle',
'size'
Copy link
Member

Choose a reason for hiding this comment

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

You can just include 'node' here and remove the others. Substrings of the title will be matched automatically.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

server.js Outdated
// C: /bundlephobia/(min|gzip)/@:scope/:package.:format
// D: /bundlephobia/(min|gzip)/@:scope/:package/:version.:format
const capturedResultType = match[1];
const capturedScopeWithoutAtSign = match[2];
Copy link
Member

Choose a reason for hiding this comment

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

Much as I love verbose names, I think you could drop captured from these, and shorten this one to scope.

Copy link
Author

Choose a reason for hiding this comment

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

Cool.

server.js Outdated
@@ -7715,6 +7715,53 @@ camp.route(/^\/nsp\/npm\/(?:@([^/]+)?\/)?([^/]+)?(?:\/([^/]+)?)?\.(svg|png|gif|j
}
}));

// bundle size for npm packages
camp.route(/^\/bundlephobia\/(min|gzip)\/(?:@([^/]+)?\/)?([^/]+)?(?:\/([^/]+)?)?\.(svg|png|gif|jpg|json)?$/,
Copy link
Member

Choose a reason for hiding this comment

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

Minified and gzipped aren't exclusive. Does gzip mean minified and gzipped?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, it does.

Copy link
Member

Choose a reason for hiding this comment

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

Then how about we make it min and min-gzip?

Copy link
Author

Choose a reason for hiding this comment

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

Another commonly used term for this is "minzip"

Copy link
Member

Choose a reason for hiding this comment

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

Sure, that works and seems clear.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

server.js Outdated
});
}

getBundlephobiaResults(capturedScopeWithoutAtSign, capturedPackageName, capturedVersion);
Copy link
Member

Choose a reason for hiding this comment

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

What advantage does making this basically an IIFE provide?

Copy link
Author

@pastelsky pastelsky Jan 4, 2018

Choose a reason for hiding this comment

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

Inlined the function

server.js Outdated
badgeData.colorscheme = 'red';
} else {
badgeData.text[1] = prettyBytes(showMin ? body.size : body.gzip);
badgeData.colorscheme = 'blue';
Copy link
Member

Choose a reason for hiding this comment

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

Could you put this in a try block to handle the case where the JSON is malformed?

Copy link
Author

Choose a reason for hiding this comment

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

Added a check for typeof body !== 'object' || body === null

.get(noExistPackage)
.expectJSON({
name: 'minified size',
value: 'PackageNotFoundError',
Copy link
Member

Choose a reason for hiding this comment

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

minified size | package not found or even bundlephobia | package not found would be more stylistically consistent with our error badges.

Copy link
Author

Choose a reason for hiding this comment

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

These error codes are returned by the bundlephobia service, and there are multiple such types - EntryPointError | PackageNotFoundError | VersionMismatchError etc. I'd prefer to keep them the same to enable easier debugging when issues are filed.

Copy link
Member

Choose a reason for hiding this comment

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

I think it's sufficient to re-run the failing request when an issue is filed. One of the goals of Shields is to provide consistent and concise badges, which applies to error messages too.

const withoutErrorSizesGzip = {
A: Joi.string().regex(/^\d+[.]?\d+ kB$/),
B: '3.58 kB',
C: Joi.string().regex(/^\d+[.]?\d+ kB$/),
Copy link
Member

Choose a reason for hiding this comment

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

You could use isFileSize for this.

const withoutErrorSizeMin = withoutErrorSizesMin[format]

const withoutErrorGzip = withoutErrorsGzip[format]
const withoutErrorSizeGzip = withoutErrorSizesGzip[format]
Copy link
Member

Choose a reason for hiding this comment

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

This is nice and DRY, though it's a bit difficult to follow what you're doing. Also not sure it's necessary to be exhaustive in testing these combinations.

Could I suggest a couple ways of making it easier to understand? One would be to reduce this to 5 success cases (four min and one gzip) and 1–3 failure cases, and lay out each test explicitly. The other would be to basically invert your data objects, making an array of objects with format, withoutErrorMin, etc. as keys.

@paulmelnikow
Copy link
Member

Are you willing to make the change to standardize the errors? If not I could pick this up from here.

@pastelsky
Copy link
Author

pastelsky commented Jan 16, 2018

Since I might have new error types in the future, I've added a regex based formatter that basically does this conversion => BuildError to build error. Unfortunately, I could not make time to refactor the tests. You can pick this up if you wish to, or I'll try to refactor them later when I find some time.

The CI seems to be tripping on something unrelated.

@paulmelnikow
Copy link
Member

paulmelnikow commented Jan 16, 2018

Okay, I'll pick that up.

I noticed the services-pr failure, which I think I fixed in #1454. If you merge master I think that should clear.

The failure in main should be fixable with npm run lint -- --fix.

@pastelsky
Copy link
Author

Lint errors / tests are passing now.

@tlaziuk tlaziuk mentioned this pull request Jan 28, 2018
@tlaziuk
Copy link
Contributor

tlaziuk commented Jan 31, 2018

@pastelsky @paulmelnikow - I want to help you with this PR, can I do that somehow?

@paulmelnikow
Copy link
Member

@tlaziuk That would be great! Would you like to take a shot at refactoring the tests? If so you could just grab these commits, merge in master, address the comments, and open a new PR.

@paulmelnikow
Copy link
Member

Let's finish this up in #1481.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
service-badge Accepted and actionable changes, features, and bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants