【问题标题】:Splitting Grunt-File into several parts with grunt-load-config: Task not found使用 grunt-load-config 将 Grunt-File 拆分为多个部分:找不到任务
【发布时间】:2015-03-30 01:10:30
【问题描述】:

我正在尝试使用 load-grunt-config 将我相当大的 Grunt 文件拆分为几个较小的部分。我在项目的根目录中有一个 Gruntfile.js,在一个名为 grunt 的目录中有子任务。

这是我的 Gruntfile.js:

module.exports = function(grunt) {
    require('load-grunt-config')(grunt, {
      loadGruntTasks: {
        pattern: '*',
        config: require('./bower.json'),
        scope: 'devDependencies'
      }
    });
};

例如,这是我的 grunt/copy.js 文件:

module.exports = function (grunt) {
  return {
      main: {
        files: [{
          cwd: 'init/php/templates',
          src: '<%= init.php.templates %>',
          dest: 'src/php/templates',
          expand: true
        }],
        options: {
          process: function (content, srcpath) {
            return grunt.template.process(content);
          }
        }
      }
  }
};

无论我尝试什么(包括将 copy.js 文件重命名为 grunt-copy.js),Grunt 总是抱怨它无法找到我的任务。我收到此错误消息:

Warning: Task "copy:main" not found. Use --force to continue.

我在这里咬花岗岩。一定有问题,但我真的不知道是什么,所以任何提示都表示赞赏。

【问题讨论】:

  • 您是否尝试过让基本版本正常工作?也许只有一项没有额外配置的任务:require('load-grunt-config')(grunt);?也许简化你复制任务配置只是为了看看问题是你的配置还是 load-grunt-config 插件。
  • 愚蠢的我,效果很好。原来问题出在 loadGruntTasks 部分。我使用它来将我的安装任务指向正确的 bower.json,因为现在它需要 grunt 文件夹中的一个 bower.json 和根目录中的一个。使用配置我解决了这个问题,但显然这会导致任务崩溃。现在我需要找到一种只使用一个 bower.json 的方法;)
  • 很高兴你至少找到了一部分!

标签: javascript node.js configuration gruntjs task


【解决方案1】:

对于那些有兴趣的人。这是我解决 Bower.json 问题的方法。我现在正在使用 grunt-bower-task 包,非常好。

这是我的 Gruntfile.js

module.exports = function(grunt) {
    require('load-grunt-config')(grunt);
};

grunt/bower.js 中的 install.js

module.exports = {
  install: {
    options: {
      targetDir: "./assets/dev/",
      layout: "byType",
      install: true,
      verbose: false,
      cleanBowerDir: false,
      cleanTargetDir: true,
      bowerOptions: {}
    }
  }
}

还有我的 bower.json

{
  "name": "myProject",
  "version": "1.0.0",
  "dependencies": {
    "bootstrap": "latest",
    "fontawesome": "latest"
  },
  "exportsOverride": {
    "bootstrap": {
      "vendor/js": "**/*.min.js",
      "vendor/css": "**/bootstrap.min.css"
    },
    "jquery": {
      "vendor/js": "**/jQuery.min.js"   
    },
    "fontawesome": {
      "vendor/css": "**/*.min.css",
      "vendor/css/fonts": "**/fontawesome-webfont.*"    
    }
  }
}

这会自动将所有文件分类到我的 dev/assets/ 文件夹中的 vendor/css 和 vendor/js 文件夹中,并且只从我指定的 bower.packages 中获取这些文件。这确实有助于保持整个设置干净整洁!

【讨论】:

    【解决方案2】:

    我认为您需要删除 Gruntfile.js 中的 grunt.initConfig()。 并使用require('load-grunt-config')(grunt, {data: {pkg: grunt.file.readJSON(./package.json), .....}}); 将您想要提供给每个任务的数据传递给

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多