这是开箱即用的。npm
寻找node_modules
在当前目录及其所有父目录中,然后在全局位置中查找。
所以你甚至可以这样做:
-Project
--subproject1
---node_modules
--subproject2
--subproject3
--node_modules
subproject1
将有权访问里面的所有 npmProject/subproject1/node_modules
and Project/node_modules
, while subproject2
and subproject3
只会找到里面的那些Project/node_modules
Update
有一个很少有文档记录的功能,称为 grunt 集合。它需要一些设置,但您不需要每个子项目中所有 grunt 插件的副本。
这是文件布局
-Project
--subproject1
---node_modules
----grunt-collection
-----package.json
--subproject2
...
--subproject3
...
--node_modules
---grunt
---grunt-contrib-concat
---grunt-contrib-jshint
---grunt-contrib-qunit
---grunt-contrib-watch
---grunt-html
---grunt-contrib-clean
---grunt-contrib-copy
---grunt-contrib-less
---grunt-contrib-uglify
---grunt-css
--package.json
项目/package.json
{
"description": "subproject",
"version": "0.0.0",
"name": "Lorem",
"title": "Lorem Ipsum",
"devDependencies": {
"grunt": "*",
"grunt-contrib-watch": "~0.2.0",
"grunt-contrib-jshint": "~0.1.1",
"grunt-contrib-less": "~0.5.0",
"grunt-contrib-uglify": "~0.1.1",
"grunt-contrib-copy": "~0.4.0",
"grunt-contrib-qunit": "~0.1.1",
"grunt-css": "~0.5.4",
"grunt-contrib-clean": "~0.4.0",
"grunt-html": "~0.3.3",
"grunt-contrib-concat": "~0.1.3"
}
}
项目/subproject1/package.json
{
"description": "subproject",
"version": "0.0.0",
"name": "Lorem",
"title": "Lorem Ipsum",
"devDependencies": {
}
}
项目/subproject1/Gruntfile.js摘录(您只需要 grunt-collect 任务)。
grunt.loadNpmTasks('grunt-collection');
// grunt.loadNpmTasks('grunt-contrib-jshint');
// grunt.loadNpmTasks('grunt-html');
// grunt.loadNpmTasks('grunt-css');
// grunt.loadNpmTasks('grunt-contrib-less');
// grunt.loadNpmTasks('grunt-contrib-copy');
项目/subproject1/node_modules/grunt-collection/package.json
{
"description": "subproject",
"version": "0.0.0",
"name": "Lorem",
"title": "Lorem Ipsum",
"dependencies": {
"grunt-contrib-watch": "~0.2.0",
"grunt-contrib-jshint": "~0.1.1",
"grunt-contrib-less": "~0.5.0",
"grunt-contrib-uglify": "~0.1.1",
"grunt-contrib-copy": "~0.4.0",
"grunt-contrib-qunit": "~0.1.1",
"grunt-css": "~0.5.4",
"grunt-contrib-clean": "~0.4.0",
"grunt-html": "~0.3.3",
"grunt-contrib-concat": "~0.1.3"
},
"keywords": ["gruntcollection"]
}
关键是在每个子项目中创建一个只有 package.json 的小模块,其中包含keyword
gruntcollection
并包括您的 Grunfile 使用的依赖项。
然后 Grunt 将使用相同的策略加载它们require
用途,这意味着它们可以在node_modules
您的父项目的。
注意:grunt 集合的工作方式是使用 package.json 的依赖标记,这意味着您不能使用 npm install 来安装它,但您应该能够存储它的源代码控制。