简体   繁体   English

使用grunt-load-config将Grunt-File分成几部分:找不到任务

[英]Splitting Grunt-File into several parts with grunt-load-config: Task not found

I am trying to split my fairly large Grunt File into several smaller parts using load-grunt-config. 我正在尝试使用load-grunt-config将相当大的Grunt文件拆分为几个较小的部分。 I have a Gruntfile.js in the root of my project and the child tasks in a directory called grunt. 我在项目的根目录中有一个Gruntfile.js,子任务在一个名为grunt的目录中。

This is my Gruntfile.js: 这是我的Gruntfile.js:

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

And this for example is my grunt/copy.js file: 例如,这是我的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);
          }
        }
      }
  }
};

However no matter what I try (including renaming the copy.js file to grunt-copy.js), Grunt always complains about how it is not able to find my tasks. 但是,无论我尝试什么(包括将copy.js文件重命名为grunt-copy.js),Grunt总是抱怨它无法找到我的任务。 I am getting this error message: 我收到此错误消息:

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

I am biting on Granite here. 我在这里咬花岗岩。 Something must be wrong, however I don't really know what, so any hint is appreciated. 一定有问题,但是我真的不知道是什么,所以任何提示都值得赞赏。

For those who are interested. 对于那些有兴趣的人。 Here is how I solved the Bower.json issue. 这是我解决Bower.json问题的方法。 I am now using the package grunt-bower-task, which is really nice. 我现在正在使用grunt-bower-task软件包,这真的很好。

Here is my Gruntfile.js 这是我的Gruntfile.js

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

The install.js at grunt/bower.js install.js位于grunt / bower.js

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

And my bower.json 还有我的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.*"    
    }
  }
}

This automatically sorts all files into vendor/css and vendor/js folders in my dev/assets/ folder and only takes those files from the bower.packages, that I have specified. 这会自动将所有文件分类到我的dev / assets /文件夹中的vendor / css和vendor / js文件夹中,并且仅从我指定的bower.packages中获取这些文件。 This really helps to keep the whole setup nice and clean! 这确实有助于使整个设置保持整洁美观!

I think you need to remove the grunt.initConfig() in the Gruntfile.js. 我认为您需要删除Gruntfile.js中的grunt.initConfig()。 and delivery the data which you want to supply to each task with require('load-grunt-config')(grunt, {data: {pkg: grunt.file.readJSON(./package.json), .....}}); 并使用require('load-grunt-config')(grunt, {data: {pkg: grunt.file.readJSON(./package.json), .....}});传递要提供给每个任务的require('load-grunt-config')(grunt, {data: {pkg: grunt.file.readJSON(./package.json), .....}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM