Load multiple grunt tasks using globbing patterns
npx @tessl/cli install tessl/npm-load-grunt-tasks@5.1.0Load 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.
npm install --save-dev load-grunt-tasksconst loadGruntTasks = require('load-grunt-tasks');// 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']);
};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
});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;
}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']
});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']
});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
});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
});The function handles errors as follows:
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
});@scope/grunt-*) and unscoped (grunt-*) npm packagesgrunt.loadNpmTasks() by default or grunt.loadTasks() with resolution when requireResolution: true