or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-load-grunt-tasks

Load multiple grunt tasks using globbing patterns

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/load-grunt-tasks@5.1.x

To install, run

npx @tessl/cli install tessl/npm-load-grunt-tasks@5.1.0

index.mddocs/

Load Grunt Tasks

Load multiple grunt tasks using globbing patterns. This package eliminates the need to manually load each grunt task plugin individually by reading dependencies from package.json and automatically loading tasks that match specified patterns.

Package Information

  • Package Name: load-grunt-tasks
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install --save-dev load-grunt-tasks

Core Imports

const loadGruntTasks = require('load-grunt-tasks');

Basic Usage

// Gruntfile.js
module.exports = grunt => {
    // Load all grunt tasks matching the ['grunt-*', '@*/grunt-*'] patterns
    require('load-grunt-tasks')(grunt);

    grunt.initConfig({
        // Your task configurations
    });
    
    grunt.registerTask('default', ['your-tasks']);
};

Capabilities

Main Function

Loads multiple grunt tasks automatically using globbing patterns based on package.json dependencies.

/**
 * Load multiple grunt tasks using globbing patterns
 * @param {Object} grunt - The grunt instance to load tasks into
 * @param {Object} options - Configuration options for task loading behavior
 * @returns {undefined} - Function has side effects (loads tasks into grunt) but returns nothing
 */
function loadGruntTasks(grunt, options);

Usage Examples:

// Load all grunt tasks (default behavior)
require('load-grunt-tasks')(grunt);

// Load only grunt-contrib tasks
require('load-grunt-tasks')(grunt, {
    pattern: 'grunt-contrib-*'
});

// Load from specific dependency types only
require('load-grunt-tasks')(grunt, {
    scope: ['devDependencies']
});

// Load with custom package.json path
require('load-grunt-tasks')(grunt, {
    config: '../package.json'
});

// Enable require resolution for traversal
require('load-grunt-tasks')(grunt, {
    requireResolution: true
});

// All options combined
require('load-grunt-tasks')(grunt, {
    pattern: ['grunt-contrib-*', 'grunt-shell'],
    config: '../package.json',
    scope: ['devDependencies', 'dependencies'],
    requireResolution: true
});

Configuration Options

The options object supports the following properties:

/**
 * Configuration options for load-grunt-tasks
 */
interface Options {
    /** Glob patterns to match package names for task loading */
    pattern?: string | string[];
    /** Package.json dependency sections to search for grunt tasks */
    scope?: string | string[];
    /** Path to package.json file or package.json object to read dependencies from */
    config?: string | object;
    /** Whether to traverse up file hierarchy looking for dependencies using require() resolution */
    requireResolution?: boolean;
}

pattern

Type: string | string[]
Default: ['grunt-*', '@*/grunt-*']

Glob patterns to match package names for task loading. Supports negative patterns using ! prefix.

// Single pattern
require('load-grunt-tasks')(grunt, {
    pattern: 'grunt-contrib-*'
});

// Multiple patterns
require('load-grunt-tasks')(grunt, {
    pattern: ['grunt-contrib-*', 'grunt-shell']
});

// Exclude specific tasks
require('load-grunt-tasks')(grunt, {
    pattern: ['grunt-contrib-*', '!grunt-contrib-coffee']
});

scope

Type: string | string[]
Default: ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']

Package.json dependency sections to search for grunt tasks. Handles both npm-style dependency objects and array formats automatically.

Valid values: 'dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies', 'bundledDependencies'

// Load only from devDependencies
require('load-grunt-tasks')(grunt, {
    scope: 'devDependencies'
});

// Load from multiple scopes
require('load-grunt-tasks')(grunt, {
    scope: ['devDependencies', 'dependencies']
});

config

Type: string | object
Default: Path to nearest package.json (found using pkg-up)

Path to package.json file or package.json object to read dependencies from. When a string path is provided, the working directory is updated to the directory containing that package.json file (affects requireResolution behavior).

// Custom path to package.json
require('load-grunt-tasks')(grunt, {
    config: '../package.json'
});

// Pass package.json object directly
const packageJson = require('./package.json');
require('load-grunt-tasks')(grunt, {
    config: packageJson
});

requireResolution

Type: boolean
Default: false

Whether to traverse up the file hierarchy looking for dependencies using require() resolution, rather than the default grunt-like behavior of loading tasks only in the immediate node_modules directory. When enabled, uses grunt.loadTasks() with resolved paths instead of grunt.loadNpmTasks().

// Enable require resolution (uses grunt.loadTasks with path resolution)
require('load-grunt-tasks')(grunt, {
    requireResolution: true
});

// Default behavior (uses grunt.loadNpmTasks)
require('load-grunt-tasks')(grunt, {
    requireResolution: false
});

Error Handling

The function handles errors as follows:

  • Fatal Error: Throws a fatal error if no package.json is found when config is not provided
  • Non-fatal Errors: Logs error messages for packages that cannot be found when requireResolution: true is used
// This will throw a fatal error if no package.json is found
require('load-grunt-tasks')(grunt);

// This will log errors for missing packages but continue execution
require('load-grunt-tasks')(grunt, {
    requireResolution: true
});

Behavior Notes

  • Automatically excludes 'grunt' and 'grunt-cli' packages to prevent conflicts
  • Supports both scoped (@scope/grunt-*) and unscoped (grunt-*) npm packages
  • Uses grunt.loadNpmTasks() by default or grunt.loadTasks() with resolution when requireResolution: true
  • Processes dependencies synchronously
  • Integrates seamlessly with Grunt's native task loading system