美文网首页
第六周打卡

第六周打卡

作者: seeddyan | 来源:发表于2018-07-28 22:26 被阅读0次

    模板引擎

    Long long ago, 动态页面的两种渲染方式:

    1. DOM API操作繁琐
    2. innerHTML可读性和可维护性差

    于是出现了模板引擎,例如JavaScript Micro-Templating
    实现代码如下:

    // Simple JavaScript Templating`
    // John Resig - [https://johnresig.com/](https://johnresig.com/) 
    (function(){
        var cache = {};
    
        this.tmpl = function tmpl(str, data){
            // Figure out if we're getting a template, or if we need to
            // load the template - and be sure to cache the result.
            var fn = !/\W/.test(str) ?
                cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) :
    
                // Generate a reusable function that will serve as a template
                // generator (and which will be cached).
                new Function("obj",
                    "var p=[],print=function(){p.push.apply(p,arguments);};" +
    
                    // Introduce the data as local variables using with(){}
                    "with(obj){p.push('" +
    
                    // Convert the template into pure JavaScript
                    str
                        .replace(/[\r\t\n]/g, " ")
                        .split("<%").join("\t")
                        .replace(/((^|%>)[^\t]*)'/g, "$1\r")
                        .replace(/\t=(.*?)%>/g, "',$1,'")
                        .split("\t").join("');")
                        .split("%>").join("p.push('")
                        .split("\r").join("\\'")
                + "');}return p.join('');");
                
            // Provide some basic currying to the user
            return data ? fn( data ) : fn;
        };
    })();
    

    使用时输写形如下面的代码:

    <script type="text/html" id="item_tmpl">
      <div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>">
        <div class="grid_1 alpha right">
          <img class="righted" src="<%=profile_image_url%>"/>
        </div>
        <div class="grid_6 omega contents">
          <p><b><a href="/<%=from_user%>"><%=from_user%></a>:</b> <%=text%></p>
        </div>
      </div>
    </script>
    

    做到了DOM解构清晰,所见即所得。

    模板引擎性能好需要:

    1. 体积小
    2. 支持线下预编译,成为静态JS文件

    现在更好的方案
    MVVM: DOM Template

    React.js: JSX

    1. all in js(逻辑+内容+样式(可选))
    2. 利于组件化

    Web Components

    webpack

    Concepts

    四个核心概念:
    入口(entry)
    输出(output)
    loader
    插件(plugins)

    依赖图(dependency graph)

    按需加载模块

    npm_lifecycle_event

    npm 提供一个npm_lifecycle_event变量,返回当前正在运行的脚本名称,比如dev、build、start等等。所以,可以利用这个变量,在同一个脚本文件里面,为不同的npm scripts命令编写代码。

    extract-text-webpack-plugin

    该插件的三个参数:
    use: 指需要什么样的loader去编译文件,栗子:原文件是.css,选择css-loader
    fallback: 编译后用什么loader来提取css文件
    publicfile: 用来覆盖项目路径,生成该css文件的文件路径 其实有output就可以了

    Reference

    handlebars
    Differences Between Handlebars.js and Mustache
    高性能JavaScript模板引擎原理解析
    doT

    相关文章

      网友评论

          本文标题:第六周打卡

          本文链接:https://www.haomeiwen.com/subject/rrabmftx.html