我正在使用grunt-contrib-htmlmin作为我的HTML文件。
我为node_modules目录添加了一个异常,所以我的Gruntfile.js如下所示:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
htmlmin: {
prod: {
options: {
removeComments: true,
collapseWhitespace: true
},
files: {
src: ['**/*.html', '!node_modules/'],
dest: 'prod/'
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-htmlmin');当我运行grunt build时,我在控制台中得到的结果是:
运行"htmlmin:prod“(htmlmin)任务 缩小1个文件(1个失败)完成,没有错误。
我有4个HTML文件,没有一个是小型化的,我甚至看不到错误。我在这里错过了什么?
发布于 2016-06-18 16:54:11
我无法复制您的具体错误,但我确实看到了您的grunt配置中存在的问题。
首先,在使用文件对象格式时,可以指定一个src-dest映射,目标路径作为键,源路径(或源路径数组)作为值。例如:
files: {
'prod/minified.html': 'src/unminified.html'
}对于当前的配置,src被解释为目标文件路径。
为了让您以您想要的方式使用src和dest属性,您必须使用文件阵列格式。
files: [{
src: ['**/*.html', '!node_modules/'],
dest: 'prod/'
}]不幸的是,这还不能使我们达到预期的产出。我们需要解决以下几个问题:
prod/被解释为单一目标路径,而不是预期的前缀。我们需要通过在文件对象上设置动态构建文件对象来告诉expand: true。
接下来,我们需要在文件配置中设置flatten: true,告诉grunt在计算目标文件路径时删除源文件路径的路径。
第三,模式!node_modules/将只排除完全匹配,字面意思是“node_node/”。要豁免node_modules文件夹下的所有文件,我们必须使用球星匹配模式!node_modules/**。
我们的结果如下:
files: [{
expand: true,
flatten: true,
src: ['**/*.html', '!node_modules/**'],
dest: 'prod/'
}]https://stackoverflow.com/questions/37885512
复制相似问题