【问题标题】:Is it possible to have one node_module folder for multiple projects in a repository?是否可以在一个存储库中为多个项目创建一个 node_module 文件夹?
【发布时间】:2015-08-17 11:46:35
【问题描述】:

假设我们有一个名为 REPO 的存储库,其中包含几个 grunt 项目。假设我们想要这个结构:

ROOT
-- bower_components
-- node_modules
-- project_1
-- project_2
-- project_3 etc

现在的问题是我需要为每个项目使用不同的 gruntfile.js,这样我就可以拥有不同的分布式缩小 js 文件、不同的任务等。

我现在得到但一点用处都没有的解决方案是每个项目文件夹都有自己的 node_module 文件夹,其中包含所有需要的模块。

所以我已经得到了这个,我想像上面的架构一样更改它。

ROOT
-- bower_components
-- project_1
---- node_modules
-- project_2
---- node_modules
-- project_3
---- node_modules

现在每个 gruntfile 看起来像这样:

function config(name) {
  return require ('./tasks/' + name + '.js');
}

module.exports = function (grunt){

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    jshint: config('jshint'),
    concat: config('concat'),
    uglify: config('uglify'),
    cssmin: config('cssmin'),
    jsbeautifier: config('jsbeautifier'),
    watch: {
      options: {livereload: true},
      files: ['index.html', 'app/views/**/*.html', 'app/css/*.css', 'js/*.js'],
      tasks: ['jshint']
    },
    karma: {
      unit: {
        configFile: 'test/test.conf.js'
      }
    }

  });

  //load plugins
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-jsbeautifier');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-karma');

  //tasks
  grunt.registerTask('dist', ['jshint', 'concat', 'uglify', 'cssmin', 'jsbeautifier']);
  grunt.registerTask('default', ['watch']);

};

我可以为每个 NpmTask 添加一个路径,而不是在我的 REPO 中为每个项目安装模块吗?

如果您需要更多信息或者我的英语不好,请问我什么,我会更新我的问题。

谢谢

【问题讨论】:

    标签: javascript git gruntjs npm


    【解决方案1】:

    您可以使用load-grunt-parent-tasks 来执行此操作。

    我知道我没有使用这个插件,但我想我会帮助你的。

    【讨论】:

    • 可以用我上面的 gruntfile 中的例子来更新你的答案吗?
    • 我看到这个插件仍然在每个子项目中创建一个 node_module 文件夹。这可能会给我一个巨大的 .gitignore 文件,因为该仓库中的项目很快就会超过 1000 个。
    • 您可以将“**/node_modules/”添加到您的 gitignore。
    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多