Serverless Framework plugin that bundles Lambda functions with Webpack for optimized JavaScript deployment packages
—
Core utility functions for webpack integration, process management, and serverless framework operations.
Utilities for detecting runtime environments and function configurations.
/**
* Check if a runtime string indicates a Node.js runtime
* @param runtime - Runtime identifier (e.g., 'nodejs18.x', 'python3.9')
* @returns True if runtime is Node.js-based
*/
function isNodeRuntime(runtime: string): boolean;
/**
* Get all Node.js functions from the serverless configuration
* @returns Array of function names that use Node.js runtime
*/
function getAllNodeFunctions(): string[];
/**
* Check if the serverless provider is Google Cloud Platform
* @param serverless - Serverless framework instance
* @returns True if provider is Google Cloud
*/
function isProviderGoogle(serverless: ServerlessInstance): boolean;Usage Examples:
const { isNodeRuntime, getAllNodeFunctions, isProviderGoogle } = require('serverless-webpack/lib/utils');
// Check if runtime is Node.js
if (isNodeRuntime('nodejs18.x')) {
console.log('Using Node.js runtime');
}
// Get all Node.js functions (context: 'this' bound to plugin instance)
const nodeFunctions = getAllNodeFunctions.call(this);
console.log('Node.js functions:', nodeFunctions);
// Check provider
if (isProviderGoogle(serverless)) {
console.log('Using Google Cloud provider');
}Utilities for spawning child processes and generating unique identifiers.
/**
* Execute a child process with unlimited stdout/stderr buffer
* @param command - Command to execute
* @param args - Array of command arguments
* @param options - Child process spawn options
* @returns Promise resolving to stdout and stderr strings
* @throws SpawnError if process exits with non-zero code
*/
function spawnProcess(command: string, args?: string[], options?: object): Promise<{stdout: string, stderr: string}>;
/**
* Generate a unique GUID string
* @returns UUID-like string for unique identification
*/
function guid(): string;Usage Examples:
const { spawnProcess, guid, SpawnError } = require('serverless-webpack/lib/utils');
// Execute npm install
try {
const { stdout, stderr } = await spawnProcess('npm', ['install'], { cwd: '/path/to/project' });
console.log('Install output:', stdout);
} catch (error) {
if (error instanceof SpawnError) {
console.error('Install failed:', error.stderr);
}
}
// Generate unique identifier
const uniqueId = guid();
console.log('Generated ID:', uniqueId); // e.g., "a1b2-c3d4-e5f6-g7h8-i9j0k1l2m3n4"Utilities for managing Node.js require cache and module resolution.
/**
* Remove a module and its dependencies from the require cache
* @param moduleName - Name of module to purge from cache
* @returns Promise that resolves when cache is cleared
*/
function purgeCache(moduleName: string): Promise<void>;
/**
* Search require cache for a module and process it with callback
* @param moduleName - Module name to search for
* @param processor - Function to call for each cache entry found
* @returns Promise that resolves when processing is complete
*/
function searchAndProcessCache(moduleName: string, processor: (mod: any) => void): Promise<void>;Usage Examples:
const { purgeCache, searchAndProcessCache } = require('serverless-webpack/lib/utils');
// Clear webpack config from cache for hot reloading
await purgeCache('./webpack.config.js');
// Process all lodash modules in cache
await searchAndProcessCache('lodash', (mod) => {
console.log('Found lodash module:', mod.id);
});Utilities for safe data parsing and string manipulation.
/**
* Safely parse JSON string without throwing errors
* @param str - JSON string to parse
* @returns Parsed object or null if parsing fails
*/
function safeJsonParse(str: string): any | null;
/**
* Split string by line breaks (handles both \n and \r\n)
* @param str - String to split into lines
* @returns Array of lines
*/
function splitLines(str: string): string[];Usage Examples:
const { safeJsonParse, splitLines } = require('serverless-webpack/lib/utils');
// Parse JSON safely
const packageJson = safeJsonParse(packageFileContent);
if (packageJson) {
console.log('Package name:', packageJson.name);
} else {
console.log('Invalid JSON format');
}
// Split multiline output
const lines = splitLines(commandOutput);
lines.forEach((line, index) => {
console.log(`Line ${index + 1}: ${line}`);
});Enhanced error class for process execution failures.
/**
* Error thrown by spawnProcess when child process fails
*/
class SpawnError extends Error {
stdout: string; // Process stdout output
stderr: string; // Process stderr output
constructor(message: string, stdout: string, stderr: string);
/**
* Convert error to string with stderr included
* @returns Formatted error message with stderr
*/
toString(): string;
}Usage Examples:
const { spawnProcess, SpawnError } = require('serverless-webpack/lib/utils');
try {
await spawnProcess('npm', ['test']);
} catch (error) {
if (error instanceof SpawnError) {
console.error('Command failed:', error.message);
console.error('Error output:', error.stderr);
console.log('Partial output:', error.stdout);
}
}// Import specific utilities
const { isNodeRuntime, spawnProcess } = require('serverless-webpack/lib/utils');
// Import all utilities
const utils = require('serverless-webpack/lib/utils');
// Access utilities
const isNode = utils.isNodeRuntime('nodejs18.x');
const result = await utils.spawnProcess('npm', ['--version']);These utilities are used internally by the serverless-webpack plugin and can also be accessed directly for custom webpack configurations or external tooling:
// In webpack.config.js - access through serverless-webpack lib
const slsw = require('serverless-webpack');
// Check if running in local development
if (slsw.lib.webpack.isLocal) {
// Use utilities for local-specific configuration
const utils = require('serverless-webpack/lib/utils');
const uniqueId = utils.guid();
}Install with Tessl CLI
npx tessl i tessl/npm-serverless-webpack