使用grunt找不到任务 [英] Task not found using grunt
问题描述
我正在尝试在grunt中使用多个任务,其中一个正在工作,另一个正在获取的错误未找到任务
。
这个问题是基于在这一个上,我使用相同的 gruntfile.js
和相同的结构,这是:
├──Gruntfile.js
└──grunt
├──config
│├──conf_sass.js
│└──conf_home.js
└──注册
├──reg_sass.js
└──reg_home.js
使用这些文件:
conf_sass.js
module.exports = function(grunt){
grunt.config.set('sass',{
sass:{
选项:{
style:'expanded',
指南针:true
},
files:{
'src / css / app.css':'src / sass / app.scss'
}
},
cssmin:{
选项:{
shorthandCompacting:false,
roundingPrecision:-1
},
target:{
files:{
files:{
'dist / css / app.min.css':'src / css / app.css'
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-cssmin');
};
reg_sass.js
module.exports = function(grunt){
grunt.registerTask('compSass',['sass']);
};
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
module.exports = function(grunt){
grunt.registerTask('compHome',['home']);
};
问题是:
sass进程,运行一切都没有错误,但不执行css minify部分,即使没有任何错误。
主进程返回一个错误
警告:未找到任何'home'
它错了吗?我已经安装了所有的模块,因为它们在移动到这个新结构之前都在工作。
有几个问题: >
- 文件的名称必须与您的任务名称相同( Eg : ngAnnotate.js
- >任务: ngAnnotate
)
- 您的任务不会被grunt插件分组
- 配置任务和注册任务的名称被共享,您可以设置 home
配置任务和 home
注册任务,因为当您使用调用它时, grunt home
,grunt无法知道您要引用的任务。
您必须遵守此配置任务的规则:在一个文件中使用相同插件的组配置任务,不要在配置任务中混合使用grunt插件,注册任务是为这项工作。
当前配置的问题,要调用 cssmin 任务,您必须运行 grunt sass:cssmin
,这没有意义。这就是为什么通过使用grunt插件对配置任务进行分组的原因,您可以使用 grunt cssmin
调用 cssmin 任务,或者使用 grunt cssmin:< subtask_name>
下面是一个例子:
module.exports = function(grunt){
grunt.config.set('sass',{
website:{
选项:{
style:'expanded',
compass:true
},
files:{
'src / css / app.css':'src / sass / app.scss'
}
},
admin:{
options:{
style:'expanded',
compass:true
$,
文件:{
'src / css / app.css':'src / sass / app.scss'
}
},
} );
grunt.loadNpmTasks('grunt-contrib-sass');
};
这里我们已经注册了 sass 配置任务来运行它,使用 grunt sass
。它会在网站和管理子任务中运行所有子任务。
现在,如果我们只想运行网站任务,我们运行: grunt sass:website
然后在你的情况下,你的结构应该是这样的:
$ b
├──Gruntfile。 js
└──grunt
├──config
│├──sass.js
│├──concat.js
│├──cssmin.js
│├──uglify.js
│└──ngAnnotate.js
└──注册
├──sassAndCssmin.js
└──home.js
您的 sass.js
file:
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');
};
您的 cssmin.js
file:
module.exports = function(grunt){
grunt.config.set('cssmin ',{
cssmin:{
options:{
shorthandCompression:false,
roundingPrecision:-1
},
target:{
文件:{
'dist / css / app.min.css':'src / css / app.css'
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-cssmin');
};
您的 concat.js
file:
module.exports = function(grunt){
grunt.config.set('concat ',{
home:{
dist:{
src:'src /.../ home / *。js',
dest:'src /.../ concat_home.js'
}
},
});
grunt.loadNpmTasks('grunt-contrib-concat');
};
您的 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');
};
其他配置任务的处理过程相同。
这里是一个注册任务的例子:
您的 sassAndCssmin.js
文件:
module.exports = function(grunt){
grunt.registerTask('sassAndCssmin',[
'sass',
'cssmin'
]);
};
您的 home.js
file:
module.exports = function(grunt){
grunt.registerTask('home', [
'concat',
'ngAnnotate',
'uglify'
]);
};
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
.
This question is based on this one, where I'm using the same gruntfile.js
and the same structure, which is this:
├── Gruntfile.js
└── grunt
├── config
│ ├── conf_sass.js
│ └── conf_home.js
└── register
├── reg_sass.js
└── reg_home.js
With this respective files:
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
module.exports = function(grunt) {
grunt.registerTask('compSass', ['sass']);
};
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
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.
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 (E.g : ngAnnotate.js
-> task : ngAnnotate
)
- your task are not grouped by grunt plugins
- 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.
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.
The problem with your current configuration, to call cssmin task, you have to run grunt sass:cssmin
and it doesn't make sense. 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>
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
. 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
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 :
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 :
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 :
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 :
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:
module.exports = function(grunt) {
grunt.registerTask('sassAndCssmin', [
'sass',
'cssmin'
]);
};
your home.js
file :
module.exports = function(grunt) {
grunt.registerTask('home', [
'concat',
'ngAnnotate',
'uglify'
]);
};
这篇关于使用grunt找不到任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!