javascriptjsondomgulpgulp-replace

What's the most efficient way to read, rebuild, and replace a block of content in a file using Gulp?


I'm creating a system that can read through any file (php, jsp, html, etc), locate block tags, and do a replacement based on the information in the block tag.

Code I would write into my file:

<!-- build:<name> -->
    {
        "testObject": {
             "name": "jonathan",
             "number": 3,
             "male": true
         }
    }
<!-- endBuild -->

Desired replacement:

<h1>Jonathan</h1>
<p>is a male and is positioned at #3.</p>

As you may notice, I've used components of gulp-html-replace. I've researched gulp-data and know how to use gulp.src and gulp.dest to build out files. Just missing the read, build, and replace the object step. Ideally this will work on multiple object instances throughout the document. Thanks.


Solution

  • Because Gulp uses a pipe system and all the files become streams, you can write your own pipe/plug-in to process files in very specific ways. Check out Gulp Writing Plugins, section Modifying file content.

    Notable Mention

    gulp-replace might actually do the trick