当前位置: 动力学知识库 > 问答 > 编程问答 >

html - Making Grunt automatically remove files that are deleted

问题描述:

I use Grunt to automate converting my jade files. For that I use this script:

 jade: {

compile: {

options: {

client: false,

pretty: true

},

files: [{

cwd: "_/components/jade",

src: "**/*.jade",

dest: "_/html",

expand: true,

ext: ".html"

}]

}

}

I also have this watch script running:

 watch: {

jade: {

files: ['_/components/jade/**/*.jade'],

tasks: ['jade']

}

}

This works fine. However, when I delete a jade file, the html file remains. Is there a way to make grunt delete the corresponding html files when I delete a jade file?

网友答案:

If I understood you correctly, if you delete foo.jade you also want to delete foo.html correct? Here's an complete example using grunt-contrib-clean and grunt-contrib-watch:

You start by watching all the files with .jade extension with grunt watch. When a watched file is modified in some way, a watch event is emitted. If the event is deleted, we take the file path, change the extension to .html, set it as the src value of the clean:jade task and run the task.

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");
        }
    });
};

For more information, see Using the watch event @ grunt-contrib-watch. Note that spawn option must be false

If you need to dynamically modify your config, the spawn option must be disabled to keep the watch running under the same context.

网友答案:

You need grunt-contrib-clean. But this code clear all files of same type and make grunt slowly and require a specific config for every task. So often just use clean single time when grunt start:

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

};
分享给朋友:
您可能感兴趣的文章:
随机阅读: