Skip to main content

Using grunt-replace to do some token replacement

While learning about grunt, I found a cool feature called grunt-replace which is perfect for me to replace tokens I have set in my html file where I have a small piece of code to do some initialization. As stated below in the example, the tokens are in the form of @@Whatever.



 
 


Then, I setup using the following code which I replicated from https://www.npmjs.com/package/grunt-replace.

  replace: {
   distHtml: [{
    options: {
     patterns: [
      {
       match: "reply",
       replacement: "<%= cfg.reply %>"
      },
      {
       match: "helperUrl",
       replacement: "<%= cfg.helperUrl %>"
      }
     ]
    },
    files: {
     src: "index-base.html",
     dest: "index.html"
    }
   }]
  }

That's it. It not only did token replacement for me, but also renamed my file. By the way, it will also warn you if it couldn't find a match. Way cool!

Unable to match 1 pattern, remember for simple matches (String) we are using the prefix @@ for replacement lookup.

Comments