What is gatsby-core-utils?
The gatsby-core-utils package provides a set of utility functions that are commonly used in Gatsby projects. These utilities help with various tasks such as file system operations, URL handling, and more.
What are gatsby-core-utils's main functionalities?
createContentDigest
This utility function creates a content digest (hash) for a given input. It is useful for generating unique identifiers for content.
const { createContentDigest } = require('gatsby-core-utils');
const data = { key: 'value' };
const digest = createContentDigest(data);
console.log(digest);
cpuCoreCount
This function returns the number of CPU cores available on the machine. It can be useful for optimizing parallel tasks.
const { cpuCoreCount } = require('gatsby-core-utils');
const coreCount = cpuCoreCount();
console.log(coreCount);
isCI
This utility checks if the code is running in a Continuous Integration (CI) environment. It can be used to conditionally execute code based on the environment.
const { isCI } = require('gatsby-core-utils');
if (isCI()) {
console.log('Running in a CI environment');
} else {
console.log('Not running in a CI environment');
}
Other packages similar to gatsby-core-utils
lodash
Lodash is a popular utility library that provides a wide range of functions for common programming tasks. While it is more general-purpose compared to gatsby-core-utils, it offers similar functionalities such as deep cloning, object manipulation, and more.
node-fetch
Node-fetch is a lightweight module that brings window.fetch to Node.js. It is similar to gatsby-core-utils in that it provides utility functions for making HTTP requests, but it is more focused on network operations.
fs-extra
Fs-extra is a module that extends the built-in Node.js file system module with additional features such as copying, moving, and removing files. It is similar to gatsby-core-utils in that it provides enhanced file system operations.
gatsby-core-utils
Utilities used in multiple Gatsby packages.
Usage
npm install gatsby-core-utils
createContentDigest
Encrypts an input using md5 hash of hexadecimal digest.
const { createContentDigest } = require("gatsby-core-utils")
const options = {
key: "value",
foo: "bar",
}
const digest = createContentDigest(options)
cpuCoreCount
Calculate the number of CPU cores on the current machine
This function can be controlled by an env variable GATSBY_CPU_COUNT
setting the first argument to true.
value | description |
---|
| Counts amount of real cores by running a shell command |
logical-cores | require("os").cpus() to count all virtual cores |
any number | Sets cpu count to that specific number |
const { cpuCoreCount } = require("gatsby-core-utils")
const coreCount = cpuCoreCount(false)
const { cpuCoreCount } = require("gatsby-core-utils")
process.env.GATSBY_CPU_COUNT = "logical-cores"
const coreCount = cpuCoreCount()
joinPath
A utility that joins paths with a /
on windows and unix-type platforms. This can also be used for URL concatenation.
const { joinPath } = require("gatsby-core-utils")
const BASEPATH = "/mybase/"
const pathname = "./gatsby/is/awesome"
const url = joinPath(BASEPATH, pathname)
isCI
A utility that enhances isCI
from 'ci-info` with support for Vercel and Heroku detection
const { isCI } = require("gatsby-core-utils")
if (isCI()) {
}
getCIName
A utility that returns the name of the current CI environment if available, null
otherwise
const { getCIName } = require("gatsby-core-utils")
const CI_NAME = getCIName()
console.log({ CI_NAME })
createRequireFromPath
A cross-version polyfill for Node's Module.createRequire
.
const { createRequireFromPath } = require("gatsby-core-utils")
const requireUtil = createRequireFromPath("../src/utils/")
requireUtil("./some-tool")