Lessons from open-source: Use picocolors to format terminal output with colors
This lessson is picked from Next.js source code. In this article, you will learn what a picocolors package is, how Next.js uses it to apply colors to terminal output.
It is important to mention the license and copyright when you copy open source code. Next.js authors even put the link to the file where this code is picked from.
L19 in the above image has ‘globalThis’. I have seen ‘this’, ‘self,’ ‘window’ but not ‘globalThis’.
The globalThis global property contains the global this value, which is usually akin to the global object. — MDN Docs
To access global this value consistently in window and non-window contexts without knowing the environment in which the code is being run, you can use globalThis.
Picocolors can be used to apply colors to your terminal output. I have found this import in Next.js build command related files. Because this package is so tiny, Next.js authors have picked the picocolors source code and placed it in packages/next/src/lib/picocolors.ts.
I have used ‘this’, ‘self,’ ‘window’ but not ‘globalThis’. Upon further reading the MDN docs, I learnt that to access global ‘this’ javascript context independant of environments (I am talking browser or non-browser environments), ‘globalThis’ can be used.
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.