V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zxq0610
V2EX  ›  前端开发

推一下自己的 gulp 插件处女作: gulp-code-replace

  •  
  •   zxq0610 ·
    heekei · 2018-06-25 22:39:50 +08:00 · 987 次点击
    这是一个创建于 2139 天前的主题,其中的信息可能已经有所发展或是发生改变。

    github: https://github.com/heekei/gulp-code-replace

    用途

    将代码中的置标替换成对应的文件内容。

    示例

    假设现在有一个项目,项目结构如下:

    • tmps
      • body.html
      • header.js
    • index.html

    index.html 内容如下:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>gulp-code-replace</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        {{body.html}}
        <script>{{header.js}}</script>
    </body>
    </html>
    

    tmps/body.html 内容如下:

    <main>
        hello, you got it!
    </main>
    

    tmps/header.js 内容如下:

    (function () {
        console.log('gulp-code-replace')
    })();
    

    经过处理后:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>gulp-code-replace</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <main>
            hello, you got it!
        </main>
        <script>
            (function () {
                console.log('gulp-code-replace')
            })();
        </script>
    </body>
    </html>
    

    gulp 示例:

    var codeReplace = require('gulp-code-replace');
    var gulp = require('gulp');
    
    gulp.task('default', function () {
        gulp.src(['./**/*.*', '!tmps/**/*.*'])
            .pipe(codeReplace('./tmps')) //pass the template base path
            .pipe(gulp.dest('build'));
    });
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2280 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 03:31 · PVG 11:31 · LAX 20:31 · JFK 23:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.