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

主宰稳场 提交于 2020-01-04 00:42:12

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/36334980/whats-the-most-efficient-way-to-read-rebuild-and-replace-a-block-of-content-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!