CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-gatsby-core-utils

A collection of core utility functions for the Gatsby ecosystem including content hashing, path manipulation, system information, and process coordination

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

path-utilities.mddocs/

Path Utilities

Cross-platform path manipulation utilities ensuring consistent path handling across Windows, macOS, and Linux systems. These functions handle platform-specific path separators and edge cases.

Capabilities

Path Joining

Joins path segments with proper handling of Windows path separators.

/**
 * Joins path segments and handles Windows path separators by escaping backslashes
 * @param paths - Path segments to join
 * @returns Joined path string with properly escaped separators
 */
function joinPath(...paths: Array<string>): string;

Usage Examples:

import { joinPath } from "gatsby-core-utils";

// Cross-platform path joining
const configPath = joinPath(process.cwd(), "src", "gatsby-config.js");
console.log(configPath); 
// Unix: "/home/user/project/src/gatsby-config.js"
// Windows: "C:\\\\Users\\\\user\\\\project\\\\src\\\\gatsby-config.js"

// Build system paths
const publicPath = joinPath(siteRoot, "public", "static", "images");
const componentPath = joinPath("src", "components", "Layout.tsx");

Path Normalization

Converts Windows backslash paths to forward slash paths for URLs and cross-platform consistency.

/**
 * Converts Windows backslash paths to forward slash paths
 * Handles extended-length paths (\\\\?\\ prefix) by leaving them unchanged
 * @param path - Path to convert
 * @returns Path with forward slashes, preserving extended-length paths
 */
function slash(path: string): string;

Usage Examples:

import { slash } from "gatsby-core-utils";

// Normalize Windows paths for URLs
const windowsPath = "C:\\Users\\user\\project\\src\\pages\\index.js";
const normalizedPath = slash(windowsPath);
console.log(normalizedPath); // "C:/Users/user/project/src/pages/index.js"

// Safe for extended-length paths
const extendedPath = "\\\\?\\C:\\VeryLongPath\\...";
const safeNormalized = slash(extendedPath);
console.log(safeNormalized); // "\\\\?\\C:\\VeryLongPath\\..." (unchanged)

URL Path Resolution

Joins path segments with forward slashes specifically for URL construction.

/**
 * Joins path segments with forward slashes, suitable for URLs
 * Always uses forward slashes regardless of platform
 * @param segments - Path segments to join
 * @returns URL-safe path with forward slashes
 */
function urlResolve(...segments: Array<string>): string;

Usage Examples:

import { urlResolve } from "gatsby-core-utils";

// Build URLs for static assets
const assetUrl = urlResolve("/static", "images", "hero.jpg");
console.log(assetUrl); // "/static/images/hero.jpg"

// API endpoint construction
const apiPath = urlResolve("/api", "v1", "users", userId);
const fullUrl = `https://api.example.com${apiPath}`;

// Relative URL building
const relativePath = urlResolve(".", "assets", "css", "main.css");
console.log(relativePath); // "./assets/css/main.css"

Node.js Internal Module Detection

Checks if a filename corresponds to Node.js internal modules for filtering and special handling.

/**
 * Checks if a filename matches Node.js internal module paths
 * Includes core modules and internal/ directory modules
 * @param fileName - Filename to check (just the filename, not full path)
 * @returns True if the filename is a Node.js internal module
 */
function isNodeInternalModulePath(fileName: string): boolean;

Usage Examples:

import { isNodeInternalModulePath } from "gatsby-core-utils";

// Filter out Node.js internals from stack traces
const stackLines = error.stack.split('\n');
const userStackLines = stackLines.filter(line => {
  const filename = extractFilename(line);
  return !isNodeInternalModulePath(filename);
});

// Skip processing internal modules in build tools
if (isNodeInternalModulePath("fs.js")) {
  console.log("Skipping Node.js internal module");
}

// Examples of detected internal modules
console.log(isNodeInternalModulePath("http.js")); // true
console.log(isNodeInternalModulePath("internal/util.js")); // true  
console.log(isNodeInternalModulePath("my-module.js")); // false

Platform-Specific Behavior

Windows Path Handling

  • joinPath() doubles backslashes (\\) to ensure proper escaping
  • slash() converts backslashes to forward slashes except for extended-length paths
  • Extended-length paths (starting with \\\\?\\) are preserved unchanged

Unix/Linux/macOS

  • joinPath() behaves like standard path.join()
  • slash() has no effect since paths already use forward slashes
  • urlResolve() works identically across all platforms

Cross-Platform Consistency

import { joinPath, slash, urlResolve } from "gatsby-core-utils";

// These produce consistent results across platforms:
const buildPath = joinPath("build", "static", "js");
const urlPath = urlResolve("/", "static", "js");  
const normalizedPath = slash(someWindowsPath);

// Platform-aware file operations
const configFile = joinPath(process.cwd(), "gatsby-config.js");
const publicUrl = urlResolve(pathPrefix, "page-data", pagePath);

Error Handling

All path utility functions are designed to handle edge cases gracefully:

  • Empty string arguments are handled safely
  • Null/undefined inputs may cause errors - validate inputs before calling
  • Extended-length Windows paths are preserved to prevent path corruption
  • Path separators are normalized consistently

Integration with Gatsby Build System

These utilities are extensively used throughout Gatsby's build process:

  • Page generation: Building file paths for page components and data files
  • Asset handling: Creating URLs for static assets and public resources
  • Plugin system: Resolving plugin paths and configuration files
  • Development server: Mapping file system paths to serve URLs
  • Build output: Generating production file structures and asset references

docs

configuration-management.md

content-hashing.md

file-operations.md

index.md

path-utilities.md

process-coordination.md

system-information.md

tile.json