outaTiME / grunt-replace

Replace text patterns with applause.
MIT License
411 stars 47 forks source link

Removing unused prefixed matches #61

Closed panstav closed 9 years ago

panstav commented 10 years ago

Title might be confusing, I'll try to clearify..

For exaple: I'm using this awesome task to insert Google Analytics into the build that goes remote. Production / Dev routine..

What I do is:

var addGoogleAnalytics = {
  match: 'GOOGLEANALYTICS',
  replacement: '<script>lalala</script>'
 };

remotePatterns.push(addGoogleAnalytics);
localPatterns.push({ 
  match: addGoogleAnalytics.match, 
  replacement: '<!-- Google Analytics goes here -->' 
});

...grunt.initConfig...
remote: {
  files: {
    'index.html': 'app.html'
  },
  options: {
    patterns: remotePatterns
  }
}
...grunt.initConfig...

At first I thought the default preservePrefix: false was to remove the unused prefixes off the source code after a replace (for example, on the localBuild task, when that match doesn't get replaced with the script), but obviously that's was a horrible assumption to make. So I ended up using this pattern for every replace task I write..

I was wondering if you had any thoughts of a solution for this, a pool of matchers & removeUnreplaced, perhaps?

What do you think?

outaTiME commented 9 years ago

Hi pal,

Glad to hear you about using grunt-replace / applause ^^

In other words you need to clean unreplaced matches ? you can create a new regexp for that or not ? or i misunderstanding ?

Remember ... you can preserve patterns execution using preserveOrder parameter (https://github.com/outaTiME/grunt-replace#preserveorder)

outaTiME commented 9 years ago

Im got something like that in mind:

remote: {
  files: {
    'index.html': 'app.html'
  },
  options: {
    patterns: [
      {
        match: 'GOOGLEANALYTICS',
        replacement: '<script>lalala</script>'
      }, 
      {
        // remove unused
        match: /@@\w+/g, 
        replacement: ''
      }
    ], 
    preserveOrder: true
  }
}

what do you think ??

panstav commented 9 years ago

Thank you for giving the time for such an easy one. =]

outaTiME commented 9 years ago

Wooow, do you try it ?!? It works?!?