1

I'm using grunt-contrib-htmlmin plugin. This is how my Gruntfile.js looks like:

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']);
};

Nothing special, really. My 404.html is also pretty simple, but even on simpler HTML files, grunt doesn't work. For example, let's say take following HTML file:

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

It doesn't even work on that. What I get as a result in console is:

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

Done, without errors.

Where can I see why it failed?

I tried really many things like removing options and I also tried changing file structure where I ended up with this:

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

But that doesn't work either and error that I'm getting now is this:

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

Aborted due to warnings.

What am I doing wrong?

4

2 回答 2

4

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

files: [{
    expand: true,
    cwd: 'views/src/',
    src: '404.html',
    build: 'views/build/404.html'
}]
于 2015-11-18T09:00:00.980 回答
0
  1. 查看日志运行命令: grunt task:target --verbose

    在您的情况下,它将是:grunt 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'

PS 对于所有其他人,请注意,在“文件”中,您应该首先指定 DESTINATION 路径,然后才指定 SRC 路径。

 htmlmin: {
      dist: {
        options: {
          /* options here*/
        },
        files: {
          'path/to/destination/folder/404.html': 'path/to/src/folder/404.html'
        }
      }
    }
于 2016-11-28T15:24:01.990 回答