大多数Grunt任务都找不到

时间:2017-08-18 19:32:44

标签: javascript gruntjs grunt-contrib-watch

这是我与grunt的第一天,我正在尝试使用这些教程

https://24ways.org/2013/grunt-is-not-weird-and-hard/

https://css-tricks.com/autoprefixer/

我的Gruntfile.js是这样的:

module.exports = function(grunt) {
// 1. All configuration goes here 
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
  scripts: {
    files: ['scripts/app.js'],
    tasks: ['uglify'],
    options: {
      spawn: false,
    }//For some reason I had a come here. Don't know if it matters
  },
  css: {
    files: ['content/app.scss'],
    tasks: ['sass'],
    options: {
      spawn: false,
    }
  },
  styles: {
    files: ['content/app.css'],
    tasks: ['autoprefixer']
  }
},
uglify: {
  build: {
    src: "scripts/app.js",
    dest: "scripts/app-final.js"
  }
},
sass: {
  dist: {
    options: {
      style: 'compressed'
    },
    files: {
      'content/app.css': 'content/app.scss'
    }
  }
},
autoprefixer: {
  dist: {
    files: {
      'content/app-prefixed.css': 'content/app.css'
    }
  }
},
imagemin: {
    dynamic: {
        files: [{
            expand: true,
            cwd: 'assets/img/',
            src: ['**/*.{png,jpg,gif}'],
            dest: 'assets/img/'
        }]
    }
}
});


 // 3. Where we tell Grunt we plan to use this plug-in.
 grunt.loadNpmTasks(
'grunt-contrib-uglify',
'grunt-contrib-sass',
'grunt-autoprefixer',
'grunt-contrib-watch',
'grunt-contrib-imagemin'
);

 // 4. Where we tell Grunt what to do when we type "grunt" into the terminal.
grunt.registerTask(
'default', [
  'watch',
  'uglify',
  'sass',
  'autoprefixer',
  'imagemin'
]);
};

但是当我尝试grunt watch watch时,我得到了这个:

 # grunt watch
 Warning: Task "watch" not found. Use --force to continue.
 Aborted due to warnings.

要看到奇怪的事件grunt uglify

# grunt uglify
Running "uglify:build" (uglify) task
>> Destination scripts/app-final.js not written because src files were empty.
>> No files created.
Done, without errors.

正在运行grunt --help给我一个有趣的事情

Available tasks
    uglify  Minify files with UglifyJS. *
    default  Alias for "watch", "uglify", "sass", "autoprefixer", "imagemin" tasks.

我真的找不到uglify和其他功能之间的区别。 VS Code没有给我任何错误。我安装了所有已用的任务。我安装了节点。

重新启动VS Code无济于事。我认为这不重要,但为了以防万一,我正在使用Linux。

重新安装依赖项无济于事

1 个答案:

答案 0 :(得分:1)

您执行了以下操作:

 grunt.loadNpmTasks(
'grunt-contrib-uglify',
'grunt-contrib-sass',
'grunt-autoprefixer',
'grunt-contrib-watch',
'grunt-contrib-imagemin'
);

替换为:

grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');

由于某种原因,Grunt在grunt.loadNpmTasks中没有多个参数。您可以在文档中看到loadNpmTasks - function的正确用法:https://gruntjs.com/sample-gruntfile