找不到使用grunt的任务
我正在尝试在grunt中使用多个任务,其中一个正在部分工作,另一个则是not found task
。
这个问题是基于这个 ,我使用相同的gruntfile.js
和相同的结构,这是这样的:
├── Gruntfile.js └── grunt ├── config │ ├── conf_sass.js │ └── conf_home.js └── register ├── reg_sass.js └── reg_home.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
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部分,即使没有任何错误。
主进程,返回一个错误
警告:找不到任务“主页”
它出什么问题了? 我已经安装了所有的模块,因为在转移到这个新的结构之前它们都在工作。
有几个问题:
– 文件的名称必须与您的任务名称相同( 例如 : ngAnnotate.js
– > task: ngAnnotate
)
– 你的任务不会被grunt插件分组
– configuration任务和注册任务的名称是共享的,所以你不能设置一个home
configuration任务和一个home
注册任务,因为当你用grunt home
调用它,grunt无法知道你正在引用的任务。
您必须遵守configuration任务的这个规则: 在一个文件中使用相同插件的组configuration任务,不要在configuration任务中混合使用grunt-plugins,注册任务在这个任务中。
与您当前的configuration的问题,要调用cssmin任务,您必须运行grunt sass:cssmin
并没有任何意义。 这就是为什么通过grunt插件对configuration任务进行分组的原因,你可以用grunt cssmin
或者其子任务来调用你的cssmin任务grunt cssmin:<subtask_name>
这里是一个例子:
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'); };
在这里我们已经注册了sassconfiguration任务,运行它,我们使用grunt sass
。 它将运行网站和pipe理子任务中的所有子任务。 现在,如果我们只想运行网站任务,我们运行: grunt sass:website
那么在你的情况下,你的结构应该是这样的:
├── Gruntfile.js └── grunt ├── config │ ├── sass.js │ ├── concat.js │ ├── cssmin.js │ ├── uglify.js │ └── ngAnnotate.js └── register ├── sassAndCssmin.js └── home.js
你的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'); };
你的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'); };
你的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'); };
你的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'); };
其他configuration任务的过程相同。
这里是一个注册任务的例子:
你的sassAndCssmin.js
文件:
module.exports = function(grunt) { grunt.registerTask('sassAndCssmin', [ 'sass', 'cssmin' ]); };
你的home.js
文件:
module.exports = function(grunt) { grunt.registerTask('home', [ 'concat', 'ngAnnotate', 'uglify' ]); };