Search code examples
gruntjsgrunt-contrib-concat

Cleanly-maintained concat steps -- deleting temp files?


Using grunt-contrib-concat, I have the following entry:

application: {
    files: {
      'purgatory/js/appbase.js': [
          'src/js/**/*.js', '!src/js/**/tests/**', '!src/js/**/workers/**'
      ],
      'purgatory/js/appworkers.js': [
          'src/js/workers/shared.js', 'src/js/workers/*.js'
      ],
      'purgatory/js/application.js': [
          'purgatory/js/appbase.js', 'purgatory/js/appworkers.js'
      ]
    }
}

The explanation is this:

"purgatory" is what I call the staging area. In theory, temp files can live here and not necessarily make it to production. So, what happens in the "application" task is that I construct a temp file called "appbase" that contains most of my logic except my web workers and tests. I make the workers phase separate because the order is important. Then I assemble the two temp files into the final application file.

It works.

But my current process eventually just grabs ALL of purgatory/js, because until today, none of my files were actually temp, they were all final targets. I would like to continue doing this instead of granularizing the copy phase or running a clean on the temp files first.

I can't help feel that I'm missing an opportunity right within grunt-contrib-concat.

Something that goes,

"grab everything except workers and tests, but then grab a particular workers file, and then grab the rest of the workers files".

I feel like if I understood the destination map better, I could do it all in one shot instead of bothering with the two temp files. The end goal is to only ever send "application.js" to purgatory. meaning there are no other temp files to clean up or otherwise deal with. Any suggestions?


Solution

  • Well, the answer was staring me straight in the face: you just have to think of the list of paths as a progressively built filter. So, you can exclude all of "workers" earlier in the list, and then include the specific ordered files afterwards. All in the same entry:

    application: {
        files: {
          'purgatory/js/application.js': [
              'src/js/**/*.js', '!src/js/**/tests/**', '!src/js/**/workers/**', 'src/js/workers/shared.js', 'src/js/workers/*.js'
          ]
    }
    

    of course, having done that, you could then just use the src/dest properties if that's what you're more comfortable with.