让 Grunt 自动删除被删除的文件
Making Grunt automatically remove files that are deleted
我使用 Grunt 自动转换我的 jade 文件。为此,我使用这个脚本:
jade: {
compile: {
options: {
client: false,
pretty: true
},
files: [{
cwd: "_/components/jade",
src: "**/*.jade",
dest: "_/html",
expand: true,
ext: ".html"
}]
}
}
我也有这个看脚本运行:
watch: {
jade: {
files: ['_/components/jade/**/*.jade'],
tasks: ['jade']
}
}
这很好用。但是,当我删除一个 jade 文件时,html 文件仍然存在。有没有办法让 grunt 在删除 jade 文件时删除相应的 html 文件?
你需要 grunt-contrib-clean。但是这段代码清除了所有相同类型的文件并使 grunt 变慢并且需要为每个任务进行特定的配置。所以经常在 grunt 启动时只使用 clean single time:
module.exports = function (grunt){
grunt.initConfig({
pckg: grunt.file.readJSON('package.json'),
clean: { // Grun-contrib-clean tasks
jade: ["dist/*.html"],
all: ["dist"]
},
jade: {
dist: {
files: [{
expand: true,
cwd: 'src/templates',
src: ['**/*.jade'],
dest: 'dist',
filter: 'isFile',
ext: '.html'
}]
}
},
watch: {
jade: {
files: ['src/templates/**/*.jade'],
tasks: ['clean:jade','jade']
},
}
});
require('load-grunt-tasks')(grunt);
grunt.registerTask('default', ['clean:all', 'jade', 'watch']);
};
如果我没理解错的话,如果你删除了foo.jade
你还想删除foo.html
对吗?这是一个使用 grunt-contrib-clean
和 grunt-contrib-watch
的完整示例:
您首先查看所有扩展名为 .jade
且 grunt watch
的文件。当以某种方式修改监视文件时,将发出 watch
事件。如果事件是 deleted
,我们获取文件路径,将扩展名更改为 .html
,将其设置为 clean:jade
任务的 src
值和 运行任务。
module.exports = function(grunt) {
grunt.initConfig({
clean: {
jade: {
src: null
}
},
watch: {
jade: {
files: ['*.jade'],
options: {
spawn: false
}
},
}
});
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.event.on('watch', function(action, filepath) {
if (action === "deleted") {
var file = filepath.slice(0, -5) + ".html";
grunt.config.set('clean.jade.src', [file]);
grunt.task.run("clean:jade");
}
});
};
有关详细信息,请参阅 Using the watch event @ grunt-contrib-watch。注意 spawn
选项必须是 false
If you need to dynamically modify your config, the spawn option must be disabled to keep the watch running under the same context.
我使用 Grunt 自动转换我的 jade 文件。为此,我使用这个脚本:
jade: {
compile: {
options: {
client: false,
pretty: true
},
files: [{
cwd: "_/components/jade",
src: "**/*.jade",
dest: "_/html",
expand: true,
ext: ".html"
}]
}
}
我也有这个看脚本运行:
watch: {
jade: {
files: ['_/components/jade/**/*.jade'],
tasks: ['jade']
}
}
这很好用。但是,当我删除一个 jade 文件时,html 文件仍然存在。有没有办法让 grunt 在删除 jade 文件时删除相应的 html 文件?
你需要 grunt-contrib-clean。但是这段代码清除了所有相同类型的文件并使 grunt 变慢并且需要为每个任务进行特定的配置。所以经常在 grunt 启动时只使用 clean single time:
module.exports = function (grunt){
grunt.initConfig({
pckg: grunt.file.readJSON('package.json'),
clean: { // Grun-contrib-clean tasks
jade: ["dist/*.html"],
all: ["dist"]
},
jade: {
dist: {
files: [{
expand: true,
cwd: 'src/templates',
src: ['**/*.jade'],
dest: 'dist',
filter: 'isFile',
ext: '.html'
}]
}
},
watch: {
jade: {
files: ['src/templates/**/*.jade'],
tasks: ['clean:jade','jade']
},
}
});
require('load-grunt-tasks')(grunt);
grunt.registerTask('default', ['clean:all', 'jade', 'watch']);
};
如果我没理解错的话,如果你删除了foo.jade
你还想删除foo.html
对吗?这是一个使用 grunt-contrib-clean
和 grunt-contrib-watch
的完整示例:
您首先查看所有扩展名为 .jade
且 grunt watch
的文件。当以某种方式修改监视文件时,将发出 watch
事件。如果事件是 deleted
,我们获取文件路径,将扩展名更改为 .html
,将其设置为 clean:jade
任务的 src
值和 运行任务。
module.exports = function(grunt) {
grunt.initConfig({
clean: {
jade: {
src: null
}
},
watch: {
jade: {
files: ['*.jade'],
options: {
spawn: false
}
},
}
});
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.event.on('watch', function(action, filepath) {
if (action === "deleted") {
var file = filepath.slice(0, -5) + ".html";
grunt.config.set('clean.jade.src', [file]);
grunt.task.run("clean:jade");
}
});
};
有关详细信息,请参阅 Using the watch event @ grunt-contrib-watch。注意 spawn
选项必须是 false
If you need to dynamically modify your config, the spawn option must be disabled to keep the watch running under the same context.