咕噜 htmlmin 错误

Grunt htmlmin errors

我正在使用 grunt-contrib-htmlmin 插件。这就是我的 Gruntfile.js 的样子:

module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    htmlmin: {
      dist: {
        options: {
          removeComments: true,
          collapseWhitespace: true
        },
        files: {
          '/views/build/404.html': '/view/src/404.html'
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-htmlmin');

  grunt.registerTask('default', ['htmlmin']);
};

没什么特别的,真的。我的 404.html 也很简单,但即使在更简单的 HTML 文件上,grunt 也不起作用。例如,假设采用以下 HTML 文件:

<html>
    <head>
    </head>
    <body>
    </body>
</html>

它甚至都不起作用。我在控制台中得到的结果是:

Running "htmlmin:dist" (htmlmin) task
Minified 0 files (1 failed)

Done, without errors.

我在哪里可以看到失败的原因?

我尝试了很多东西,例如删除 options,我还尝试更改 file 结构,结果是:

files: {
    expand: true,
    cwd: 'views/src/',
    src: '404.html',
    build: 'views/build/404.html'
}

但这也不起作用,我现在得到的错误是:

Running "htmlmin:dist" (htmlmin) task
Warning: pattern.indexOf is not a function Use --force to continue.

Aborted due to warnings.

我做错了什么?

我不知道错误的真正原因。但是通过添加方括号修改源代码,如下所示,对我有用。

files: [{
    expand: true,
    cwd: 'views/src/',
    src: '404.html',
    build: 'views/build/404.html'
}]
  1. 查看日志 运行 命令: g运行t task:target --verbose

    在你的情况下它将是:g运行t htmlmin:dist --verbose

  2. 错误似乎在您的文件路径中:'/views/build/404.html'。 '/' 表示插件将从 unix 系统的根文件夹开始搜索。使用相对路径:

    'views/build/404.html': 'view/src/404.html'

    './views/build/404.html': './view/src/404.html'

P.S。对于所有其他人,请注意在 'files' 中,您应该首先指定 DESTINATION 路径,然后才指定 SRC 路径。

 htmlmin: {
      dist: {
        options: {
          /* options here*/
        },
        files: {
          'path/to/destination/folder/404.html': 'path/to/src/folder/404.html'
        }
      }
    }