Skip to content

Commit 314267e

Browse files
committed
fix(loadNpmTasks): load tasks relative to package location
1 parent 8372e11 commit 314267e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/grunt/task.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,16 +372,22 @@ task.loadNpmTasks = function(name) {
372372
var root = path.resolve('node_modules');
373373
var pkgpath = path.join(root, name);
374374
var pkgfile = path.join(pkgpath, 'package.json');
375+
var pkgDistDir = '';
375376
// If package does not exist where grunt expects it to be,
376377
// try to find it using Node's package path resolution mechanism
377378
if (!grunt.file.exists(pkgpath)) {
378379
var nameParts = name.split('/');
379380
// In case name points to directory inside module,
380381
// get real name of the module with respect to scope (if any)
381382
var normailzedName = (name[0] === '@' ? nameParts.slice(0,2).join('/') : nameParts[0]);
383+
// Sometimes a module's task are not at the root of the package, the "name" could contain
384+
// the directory to find the tasks (e.g. grunt-foo/dist/tasks). Therefore, if the name
385+
// contains more than two part, then treat the last part as the directory to find the tasks.
386+
if (nameParts.length > 2) {
387+
pkgDistDir = nameParts.slice(2).join('/');
388+
}
382389
try {
383390
pkgfile = require.resolve(normailzedName + '/package.json');
384-
root = pkgfile.substr(0, pkgfile.length - normailzedName.length - '/package.json'.length);
385391
} catch (err) {
386392
grunt.log.error('Local Npm module "' + normailzedName + '" not found. Is it installed?');
387393
return;
@@ -409,7 +415,7 @@ task.loadNpmTasks = function(name) {
409415
}
410416

411417
// Process task plugins.
412-
var tasksdir = path.join(root, name, 'tasks');
418+
var tasksdir = path.join(path.dirname(pkgfile), pkgDistDir, 'tasks');
413419
if (grunt.file.exists(tasksdir)) {
414420
loadTasks(tasksdir);
415421
} else {

0 commit comments

Comments
 (0)