New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support monorepos #1677
support monorepos #1677
Conversation
allow usage of grunt plugins that are located in any location that is visible to Node.js and NPM, instead of `node_modules` directly inside package that have a dev dependency to these plugins.
In `loadNpmTasks()`` try to resolve package location. In case package not found, try to resolve it's location using `require.resolve()`
add integration test for monorepo case, where grunt plugin hoisted to the `node_modules` above package having dev dependency to it
@vladikoff any way this can get reviewed or merged? |
ping @shama 👋 |
@micellius so one use case this may break is if your // all tasks are located under the '/dist/tasks' folder.
grunt.loadNpmTasks('grunt-foo/dist'); Because Perhaps this use case is unique to me, but I don't think its that farfetched that users might have a Interested to hear your thoughts... |
I think we've intended to support a |
@shama this would be great. Please keep us posted. In our monorepo, we basically have to copy all the grunt* packages into each packages |
In case `loadNpmTasks()` is called with relative path instead of just module name, try to resolve module name and find relevant package.json, but use tasks from specified relative path.
@redonkulus , I pushed the change that takes your use case into account by trying to normalize the argument of task.loadNpmTasks = function(name) {
loadTasksMessage('"' + name + '" local Npm module');
var root = path.resolve('node_modules');
var pkgpath = path.join(root, name);
var pkgfile = path.join(pkgpath, 'package.json');
// If package does not exist where grunt expects it to be,
// try to find it using Node's package path resolution mechanism
if (!grunt.file.exists(pkgpath)) {
var nameParts = name.split('/');
// In case name points to directory inside module,
// get real name of the module with respect to scope (if any)
var normailzedName = (name[0] === '@' ? nameParts.slice(0,2).join('/') : nameParts[0]);
try {
pkgfile = require.resolve(normailzedName + '/package.json');
root = pkgfile.substr(0, pkgfile.length - normailzedName.length - '/package.json'.length);
} catch (err) {
grunt.log.error('Local Npm module "' + normailzedName + '" not found. Is it installed?');
return;
}
}
...
// Process task plugins.
var tasksdir = path.join(root, name, 'tasks');
if (grunt.file.exists(tasksdir)) {
loadTasks(tasksdir);
} else {
grunt.log.error('Local Npm module "' + name + '" not found. Is it installed?');
}
} Now you should be able to use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making the change. Just one comment on path separators.
// If package does not exist where grunt expects it to be, | ||
// try to find it using Node's package path resolution mechanism | ||
if (!grunt.file.exists(pkgpath)) { | ||
var nameParts = name.split('/'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to use path.sep
to support windows?
Looks like there are three more uses of /
below too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@redonculus , I tested my PR on windows and it works fine. There is a need to use path.sep
only if someone is using syntax like require(".\\my\\foo\\index.js")
. While this may work on windows, it fails on other platform. In opposite, using require("./my/foo/index.js")
works on all platforms (including windows). So, practically, I don't see any reason someone would use windows specific paths, if there is a way to write cross-platform code with less typing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok that’s fine then. If it works on windows then I’m good.
What's the status on this PR? Any reason it hasn't been merged asides from the path.sep comment mentioned above? |
@Lucaszw I have been busy traveling that last few weeks so I haven’t had time to test this yet but will try out the latest changes in this PR this week. @micellius please see my comment above about using |
@redonkulus Awesome! |
@micellius @Lucaszw I've tested this locally and it works well. Its up to @shama in how he wants to proceed with this PR. |
@vladikoff thanks for merging this in. When do you think this will be released to npm? |
@redonkulus I am thinking to tag this with v1.2.0 this week. How's that? |
@vladikoff that would be perfect, thanks! |
allow usage of grunt plugins that are located in any location that
is visible to Node.js and NPM, instead of
node_modules
directlyinside package that have a dev dependency to these plugins.
Fix #1676.