html - 无法找到错误 "Warning: Task "uglify“未找到”的解决方案

标签 html css node.js npm gruntjs

这是我的 gruntfile.js:

module.exports = function(grunt) {

  require('load-grunt-tasks')(grunt);

  grunt.initConfig({

    uglify: {
      start: {
        files: {
          'js/script.min.js': ['js/script.js'],
        }
      }
    },

    imagemin: {
      build: {
        options: {
          optimizationLevel: 3
        },
        files: [{
          expand: true,
          src: ['img/sprite_svg/*.svg'],
        }]
      }
    },

    svgstore: {
      options: {
        includeTitleElement: false,
        svg: {
          style: 'display:none',
        },
        cleanup: [
          'fill',
        ],
      },
      default : {
        files: {
          'img/sprite.svg': ['img/sprite_svg/*.svg'],
        },
      },
    },

    watch: {
      livereload: {
        options: { livereload: true },
        files: ['build/**/*'],
      },
      scripts: {
        files: ['js/script.js'],
        tasks: ['js'],
        options: {
          spawn: false
        },
      },
      images: {
        files: [
          'img/sprite_svg/*.svg'
        ],
        tasks: ['img'],
        options: {
          spawn: false
        },
      },
      html: {
        files: ['./index.html'],
        // tasks: ['html'],
        options: {
          spawn: false
        },
      },
    },

    browserSync: {
      dev: {
        bsFiles: {
          src : [
            'img/sprite.svg',
            './index.html',
          ]
        },
        options: {
          watchTask: true,
          server: {
            baseDir: "./",
          },
          startPath: "index.html",
          ghostMode: {
            clicks: true,
            forms: true,
            scroll: false
          }
        }
      }
    }

  });

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-imagemin');

  grunt.registerTask('default', [
    'js',
    'img',
    'browserSync',
    'watch'
  ]);

  grunt.registerTask('js', [
    'uglify'
  ]);

  grunt.registerTask('img', [
    'imagemin',
    'svgstore'
  ]);

};

我已经安装了 uglify 和 imagemin npm 包,但我仍然收到错误: 警告:未找到任务“uglify”

这一直有效,直到我添加了 svgstore,我认为这可能是一个语法错误。然而,这是我第一次使用它,我根本不知道是什么导致了这个问题。任何帮助将不胜感激。

最佳答案

你的 package.json 文件里有什么?

“load-grunt-tasks”从 package.json 中的“dependencies”和“devDependencies”读取,因此您不必调用“grunt.loadNpmTasks”。

此外,您应该安装“grunt-contrib-uglify”而不是“uglify”。

关于html - 无法找到错误 "Warning: Task "uglify“未找到”的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45822660/

相关文章:

javascript - Jquery 确定有效 DPI 来缩放 Canvas 中的图像

html - CSS font-size 属性从百分比到像素,如何?

html - 位置后的 Bootstrap 字形图标

html - 如何设置样式但排除类及其子类?

css - 目视向下滑动固定元素,元素在 flex-end 对齐

node.js - 将 Node 与 Nano 和 couchdb 一起使用时出现 ECONNREFUSED

node.js - 当服务器启动使用keystonejs时,如何触发函数?

html - 链接颜色在 Chrome 中发生变化,但在 Firefox 和 IE 中没有

css - SASS 匹配类名中的数字

node.js - Mongoose schematype.castForQuery 错误。用户身份验证失败