使用子项目集中项目中的node_modules

有没有可能configurationgrunt的方式,你有一个中心点所需的模块?

我有以下项目结构

-Project -- subproject -- subproject -- subproject 

我通过grunt与所有子项目build立项目,我也可以为自己build立每个子项目。 目前我有一个Gruntfile.js,package.json和文件夹node_modules(〜50mb),在每个子项目和根级别都有所有必需的模块。

那么是否有可能在一个级别上只有node_modules文件夹,例如在根级上,而子项目是否在根级上引用node_modules?

 -Project --subproject --subproject --subproject --node_modules 

有没有办法引用node_module文件夹通过package.json或其他?

编辑:

Gruntfile.js(子项目级别)

 /*global module:false */ /*jshint node:true */ module.exports = function(grunt) { "use strict"; // ================================================================================ // project configuration // ================================================================================ grunt.initConfig({ pkg : grunt.file.readJSON('package.json'), jshint: { globals : { d3:true, Modernizr:true, tempo:true }, options: grunt.file.readJSON('.jshintrc') }, csslint: { subproject: { src: 'css/**/*.css' } }, htmllint : { subproject: { src: 'html/**/*.html' } }, clean : [ 'output' ], less : { options: { paths: ['./'] }, src: { expand: true, cwd: 'css/', src: ['**/*.less'], dest: 'css/', ext: '.css' } }, copy: { subproject: { files: [ {src: ['img/**', 'js/**', 'folderX/**','!**/*.less'], dest: 'output/subproject/'} ] } } }); grunt.loadNpmTasks('grunt-contrib-clean'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-html'); grunt.loadNpmTasks('grunt-css'); grunt.loadNpmTasks('grunt-contrib-less'); grunt.loadNpmTasks('grunt-contrib-copy'); // ================================================================================ // default task // ================================================================================ grunt.registerTask('default', ['clean', 'less', 'csslint', 'htmllint', 'copy']); }; 

package.json(subproejct级别)

 { "description": "subproject", "title": "Lorem Ipsum", "devDependencies": { "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" } } 

BR,mybecks

这是开箱即用的。 npm在当前目录中查找node_modules ,并查找其全部父目录,然后查找全局位置。

所以你甚至可以这样做:

 -Project --subproject1 ---node_modules --subproject2 --subproject3 --node_modules 

subproject1将有权访问Project/subproject1/node_modulesProject/node_modules ,而subproject2subproject3只能findProject/node_modules那些

更新

有一个很lesslogging的function称为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": { } } 

Project / subproject1 / Gruntfile.js摘录(你只需要grunt-collection任务)。

 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 /咕噜收集/的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"] } 

关键是在每个子项目中创build一个只包含keyword gruntcollection的package.json的小模块,并包含Grunfile使用的依赖关系。

然后,Grunt会使用相同的策略来加载这些require ,这意味着它们可以在父项目的node_modules中find。

注意:grunt集合的工作方式是使用package.json的依赖标签,这意味着你不能用npm install来安装它,但你应该能够存储它的源代码控制。

我创build了一个npm模块load-grunt-parent-tasks来解决这个问题。 它的灵感来自Pascal Belloncle给出的答案,并使用一个gruntcollection hack。

所有你需要做的是需要模块,传递它的grunt和一个configuration对象,它会做的rest。

 module.exports = function(grunt) { require('load-grunt-parent-tasks')(grunt, { config: 'package.json', pattern: 'grunt-*', scope: 'dependencies', module: 'grunt-collection' }); }; 

您可以根据传递给pattern的通配符模式来过滤要加载的咕噜声。

你可以在Npm上看看这个模块: https ://www.npmjs.org/package/load-grunt-parent-tasks

另一种解决scheme,在https://github.com/gruntjs/grunt/issues/696中findmodule.exports = function (grunt) { grunt.file.expand('../node_modules/grunt-*/tasks').forEach(grunt.loadTasks); } module.exports = function (grunt) { grunt.file.expand('../node_modules/grunt-*/tasks').forEach(grunt.loadTasks); }

以下是一个简单的解决方法。 更改Gruntfile.js。 module.exports = function (grunt) { var cwd = process.cwd(); process.chdir(cwd+'/../'); require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks); process.chdir(cwd); }