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

Replace npmlog with consola #829

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Conversation

br0p0p
Copy link

@br0p0p br0p0p commented Jul 1, 2024

This PR replaces the deprecated and dependency-laden npmlog with a smaller, modern alternative: consola.

Module + Dependencies Size on Disk (Unpacked Size) Bundle size (minified)
npmlog 15 401 KB 65 KB
consola 1 222 KB 6 KB
𝛥 -14 (-93.3%) -179 KB (−44.63%) -59 KB (-90.7%)

This data was gathered from https://npmgraph.js.org/

Extrapolating based on the download stats for @mapbox/node-pre-gyp (5,730,435 downloads per week), this migration will have the following effects on the ecosystem as a whole:

  • 80,226,090 fewer dependencies downloaded per week
  • Reduced network traffic by ~1 TB per week
package-size-calculator output
Package size report
===================

Package info for "@mapbox/[email protected]": 2.0 MB
  Released: 2023-07-14 13:09:45.532 +0000 UTC (52w6d ago)
  Downloads last week: 4,706,924 (80.46%)
  Estimated traffic last week: 9.3 TB
  Subdependencies: 57

Removed dependencies:
  - [email protected]: 401 kB (20.27%)
    Downloads last week: 7,502,133 (N/A% from 5.0.1)
    Downloads last week from "@mapbox/[email protected]": 4,706,924 (N/A%)
    Traffic last week: N/A
    Traffic from "@mapbox/[email protected]": 9.3 TB (N/A%)
    Subdependencies: 22 (38.59%)

Added dependencies:
  + [email protected]: 228 kB (14.47%)
    Downloads last week: 4,630,250 (N/A% from 3.2.3)
    Estimated traffic last week: N/A
    Subdependencies: 1 (1.75%)

Estimated package size: 2.0 MB → 1.8 MB (91.26%)
Estimated traffic over a week: 9.3 TB → 8.5 TB (813 GB saved)
Estimated traffic over a week @ 100% downloads: 12 TB → 11 TB (1.0 TB saved)
Subdependencies: 57 → 36 (-21)

I will note that the output looks slightly different with these changes. Happy to update if the new format is not great

Before

Screenshot 2024-07-02 at 9 15 09 AM

After

Screenshot 2024-07-02 at 9 15 39 AM

@br0p0p br0p0p marked this pull request as ready for review July 2, 2024 03:08
Copy link
Contributor

@benmccann benmccann left a comment

Choose a reason for hiding this comment

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

Amazing! Thank you!!

@benmccann
Copy link
Contributor

@br0p0p would you be able to fix the merge conflict?

@br0p0p
Copy link
Author

br0p0p commented Jul 5, 2024

@benmccann done

@br0p0p br0p0p requested a review from benmccann July 5, 2024 01:46
// eslint-disable-next-line node/no-missing-require
const { createConsola } = require('consola/basic');

const log = createConsola({ stdout: process.stderr });
Copy link
Contributor

Choose a reason for hiding this comment

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

why put all output to stderr? that seems surprising. if we keep it we should at least add a comment to explain

Copy link
Author

Choose a reason for hiding this comment

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

I did this to maintain the default behavior of npmlog. See here and the README

Not exactly sure why this was the default

Probably a good call to add a comment at least. Maybe stderr makes sense here since the logger outputs more debugging type output and console.log() is used for user-facing output?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should remove this rather than trying to maintain the behavior of npmlog. I checked (see below) and consola logs warnings and errors to stderr and info type logs to stdout, which seems like what I'd want and expect out of a logging system. Let's just check that we're setting the log levels appropriately whenever we do logging - I left one comment where I think we should do log.error to make sure an error goes to stdout

https://github.com/unjs/consola/blob/713ec3454889bf967e9c694bf800955f1cd60e52/src/reporters/basic.ts#L70
https://github.com/unjs/consola/blob/713ec3454889bf967e9c694bf800955f1cd60e52/src/constants.ts#L23

Copy link
Author

@br0p0p br0p0p Jul 7, 2024

Choose a reason for hiding this comment

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

Sure thing. Updated.

My thinking was to prevent potentially breaking dependents which rely on the stderr output for whatever reason but I agree that not using stderr for everything makes sense

Copy link
Collaborator

Choose a reason for hiding this comment

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

The main reason that most logging from npm and node-gyp goes to stderr (even now that neither use npmlog) is that stdout is reserved for output that is expected to be used programmatically (usually json). We don't usually swap which output stream is used based on flags like --json so the default is to place all logging on stderr so that if a json command is piped to something like jq the logging won't inadvertently create invalid json.

There are a lot of gray areas to this rule and this is something I spent a lot of time figuring out for npm in the past. I haven't actually review this code (yet) so I'm not saying that all output should be put on stderr, just that as a default stderr is usually better than stdout for CLI logging output.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for this context @lukekarrys. Thoughts @benmccann?

Copy link
Contributor

Choose a reason for hiding this comment

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

I have no objection to stderr given that additional context. If @lukekarrys thinks it's the way to go I'll defer to him on that

Copy link
Contributor

Choose a reason for hiding this comment

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

@br0p0p do you want to change this back to use stderr by default? It might be the safer option to avoid changing that

Copy link
Author

Choose a reason for hiding this comment

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

@benmccann sure, no problem

lib/install.js Outdated Show resolved Hide resolved
Copy link
Contributor

@benmccann benmccann left a comment

Choose a reason for hiding this comment

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

lgtm except that it looks like the appveyor tests might be failing

@cclauss could you kick off the github actions tests on this PR?

@benmccann
Copy link
Contributor

@br0p0p I'm afraid all the CI jobs are failing with this PR. would you be able to take a look?

Copy link
Contributor

@benmccann benmccann left a comment

Choose a reason for hiding this comment

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

Thank you @br0p0p !

@cclauss this one is now ready to go. It would be great to merge before the next preview release as it fixes a few deprecation warnings (I believe all the remaining).

@br0p0p br0p0p requested a review from a team as a code owner September 10, 2024 21:41
@HummingMind
Copy link

Hoping this gets merged soon. Thank you for your work! 🍻

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.

4 participants