简体   繁体   English

使用grunt找不到任务

[英]Task not found using grunt

I'm trying to use multiple tasks in grunt, one of them is working partialy, and the other is getting erros of not found task . 我正在尝试在grunt中使用多个任务,其中一个是partialy工作,另一个是not found task

This question is based on this one , where I'm using the same gruntfile.js and the same structure, which is this: 这个问题是基于这个 ,我使用相同的gruntfile.js和相同的结构,这是:

├── Gruntfile.js
└── grunt
    ├── config
    │   ├── conf_sass.js
    │   └── conf_home.js
    └── register
        ├── reg_sass.js
        └── reg_home.js

With this respective files: 使用这个相应的文件:

conf_sass.js conf_sass.js

module.exports = function(grunt) {
    grunt.config.set('sass', {

        sass: {
            options: {
                style:'expanded',
                compass: true
            },
            files: {
                'src/css/app.css' : 'src/sass/app.scss'
            }
        },
        cssmin: {
            options: {
                shorthandCompacting: false,
                roundingPrecision: -1
            },
            target: {
                files: {
                    'dist/css/app.min.css': 'src/css/app.css'
                }
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
};

reg_sass.js reg_sass.js

module.exports = function(grunt) {
    grunt.registerTask('compSass', ['sass']);
};

conf_home.js conf_home.js

module.exports = function(grunt) {
    grunt.config.set('home', {

        concat: {
            dist : {
                src: 'src/.../home/*.js',
                dest: 'src/.../concat_home.js'
            }
        },
        ngAnnotate: {
            options: {
                add: true
            },
            dist: {
                files: {
                    'src/.../concat_home.js': 'src/.../concat_home.js'
                }
            }
        },
        uglify: {
            dist: {
                src:  'src/.../concat_home.js',
                dest: 'app/.../home.min.js'
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-ng-annotate');
};

reg_home.js reg_home.js

module.exports = function(grunt) {
    grunt.registerTask('compHome', ['home']);
};

The problem is: 问题是:

The sass process, runs everything without erros, but doesn't execute the css minify part, even with no errors at all. sass进程在没有错误的情况下运行所有​​内容,但是不执行css minify部分,即使没有任何错误。

The home process, return an error 回家过程中,返回错误

Warning: task 'home' not found 警告:未找到任务“主页”

What's wrong with it? 它出什么问题了? I have all the modules installed because they were all working before moving to this new structure. 我安装了所有模块,因为在迁移到这个新结构之前它们都在工作。

There are several problems : 有几个问题:
- the name of your file must be the same name of your task ( Eg : ngAnnotate.js -> task : ngAnnotate ) - 文件名必须与任务名相同( 例如ngAnnotate.js - > task: ngAnnotate
- your task are not grouped by grunt plugins - 您的任务不是由grunt插件分组的
- name of configuration task and register tasks are shared then you can't set a home configuration task and a home register task because when you call it with grunt home , grunt isn't able to know the task that you are refering to. - 共享配置任务和注册任务的名称,然后您无法设置home配置任务和home注册任务,因为当您使用grunt home调用它时,grunt无法知道您正在引用的任务。

You have to respect this rules for configuration task : group configuration tasks that use a same plugins in one file, don't mix grunt-plugins in configuration tasks, Register tasks are there for this job. 您必须遵守配置任务的此规则: 在一个文件中使用相同插件的组配置任务,不要在配置任务中混用grunt-plugins,注册任务适用于此作业。

The problem with your current configuration, to call cssmin task, you have to run grunt sass:cssmin and it doesn't make sense. 你当前的配置问题,要调用cssmin任务,你必须运行grunt sass:cssmin ,这没有意义。 That's why by grouping configuration tasks by grunt plugins, you call your cssmin task with grunt cssmin or a subtask in it with grunt cssmin:<subtask_name> 这就是为什么通过grunt插件对配置任务进行分组,用grunt cssmin调用你的cssmin任务或用grunt cssmin调用你的子任务grunt cssmin:<subtask_name>

Here is an example : 这是一个例子:

module.exports = function(grunt) {
    grunt.config.set('sass', { 
        website: {
            options: {
                style:'expanded',
                compass: true
            },
            files: {
                'src/css/app.css' : 'src/sass/app.scss'
            }
        },
        admin: {
            options: {
                style:'expanded',
                compass: true
            },
            files: {
                'src/css/app.css' : 'src/sass/app.scss'
            }
        },
    });
    grunt.loadNpmTasks('grunt-contrib-sass');
};

Here we have registered the sass configuration task, to run it , we use grunt sass . 这里我们注册了sass配置任务,运行它,我们使用grunt sass It will run all subtask in it website and admin sub tasks. 它将在其网站管理子任务中运行所有子任务。 Now, if we want to run only website task, we run : grunt sass:website 现在,如果我们只想运行网站任务,我们运行: grunt sass:website

Then in your case, here is what your structure should look like : 那么在你的情况下,这是你的结构应该是什么样子:

├── Gruntfile.js
└── grunt
    ├── config
    │   ├── sass.js
    │   ├── concat.js
    │   ├── cssmin.js
    │   ├── uglify.js
    │   └── ngAnnotate.js
    └── register
        ├── sassAndCssmin.js
        └── home.js

your sass.js file : 你的sass.js文件:

module.exports = function(grunt) {
    grunt.config.set('sass', {
        sass: {
            options: {
                style:'expanded',
                compass: true
            },
            files: {
                'src/css/app.css' : 'src/sass/app.scss'
            }
        },
    });
    grunt.loadNpmTasks('grunt-contrib-sass');
};

your cssmin.js file : 你的cssmin.js文件:

module.exports = function(grunt) {
    grunt.config.set('cssmin', {
        cssmin: {
            options: {
                shorthandCompacting: false,
                roundingPrecision: -1
            },
            target: {
                files: {
                    'dist/css/app.min.css': 'src/css/app.css'
                }
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-cssmin');
};

your concat.js file : 你的concat.js文件:

module.exports = function(grunt) {
    grunt.config.set('concat', {
        home: {
            dist : {
                src: 'src/.../home/*.js',
                dest: 'src/.../concat_home.js'
            }
        },
    });
    grunt.loadNpmTasks('grunt-contrib-concat');
};

your ngAnnotate.js file : 你的ngAnnotate.js文件:

module.exports = function(grunt) {
    grunt.config.set('ngAnnotate', {
        home: {
            options: {
                add: true
            },
            dist: {
                files: {
                    'src/.../concat_home.js': 'src/.../concat_home.js'
                }
            }
        },

    });
    grunt.loadNpmTasks('grunt-ng-annotate');
};

Same process for other configuration tasks. 其他配置任务的进程相同。

And here is an example with the register tasks : 以下是注册任务的示例:

your sassAndCssmin.js file: 你的sassAndCssmin.js文件:

module.exports = function(grunt) {
    grunt.registerTask('sassAndCssmin', [
        'sass',
        'cssmin'
    ]);
};

your home.js file : 你的home.js文件:

module.exports = function(grunt) {
    grunt.registerTask('home', [
        'concat',
        'ngAnnotate',
        'uglify'
    ]);
};

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

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