大多数 Grunt 任务都找不到

Most of Grunt tasks are not found

这是我使用 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.

重新安装依赖项也没有帮助

您执行了以下操作:

 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 函数的正确用法:https://gruntjs.com/sample-gruntfile