Search code examples
javascriptgruntjspugdevelopment-environmentgrunt-contrib-jade

Sync delete .html files corresponding to .pug files between src/ and dist/


I use grunt-contrib-pug to compile my .pug files from src/ and distribute the corresponding .html files to dist/. Here is my pug-task config (written in .coffee):

compile:
    options: pretty: false
    files: [ {
        expand: true
        cwd: 'src/'
        src: [ '**/*.pug', '!includes/**' ]
        dest: 'dist/'
        ext: '.html'
    } ]

When I delete a .pug file from src/, is there any way to synchronize delete corresponding html files in dist/? I know you can use grunt-contrib-clean followed by compiling the pug files again, but this is not time efficient when working with a large codebase.


Solution

  • As referenced by I-LOVE-2-REVIVE, I looked further into Grunt's file API, and on that basis, this is the solution I came up with:

    grunt.event.on 'watch', (action, filepath, target) ->
      if action == 'deleted' && /pug/.test(filepath)
    
         file = 'dist' + filepath.slice(3, -3) + 'html'
         grunt.file.delete file
         # Log deleted files
         grunt.log.write '\n' + filepath + ' deleted > ' + file + ' deleted.\n'
    

    It works great!