【问题标题】:Making Grunt automatically remove files that are deleted让 Grunt 自动删除被删除的文件
【发布时间】:2015-08-09 17:17:40
【问题描述】:

我使用 Grunt 自动转换我的玉文件。为此,我使用此脚本:

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

这很好用。但是,当我删除一个玉文件时,html 文件仍然存在。删除jade文件时,有没有办法让grunt删除对应的html文件?

【问题讨论】:

  • 这个插件应该可以解决你的烦恼npmjs.com/package/grunt-contrib-clean
  • @Avalanche 我试过了,但这只是删除了每个文件夹中的所有文件。文档很差,很难看出它是否可以用于我的目的。
  • 您可以明确地将“index.html”文件声明为 grunt-contrib-clean 的目标。
  • @PeteTNT 这并不能解决我的问题。我想 grunt 删除我删除的翡翠文件的 html 等效文件,将'index.html'文件作为目标如何做到这一点?
  • @ArashSaidi - 抱歉,我赶时间:请参阅我的答案以获得完整的解决方案。

标签: html gruntjs pug


【解决方案1】:

如果我理解正确,如果你删除foo.jade,你也想删除foo.html,对吗?这是使用grunt-contrib-cleangrunt-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

如果您需要动态修改配置,则必须禁用 spawn 选项以保持手表在相同的上下文中运行。

【讨论】:

    【解决方案2】:

    你需要 grunt-contrib-clean。但是这段代码清除了所有相同类型的文件并缓慢地发出咕噜声,并且每个任务都需要特定的配置。所以经常在 grunt 启动时使用 clean 单次:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      相关资源
      最近更新 更多