第六周打卡

模板引擎

Long long ago, 動態(tài)頁面的兩種渲染方式:

  1. DOM API操作繁瑣
  2. innerHTML可讀性和可維護性差

于是出現(xiàn)了模板引擎,例如JavaScript Micro-Templating
實現(xiàn)代碼如下:

// 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解構(gòu)清晰,所見即所得。

模板引擎性能好需要:

  1. 體積小
  2. 支持線下預編譯,成為靜態(tài)JS文件

現(xiàn)在更好的方案
MVVM: DOM Template

React.js: JSX

  1. all in js(邏輯+內(nèi)容+樣式(可選))
  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

該插件的三個參數(shù):
use: 指需要什么樣的loader去編譯文件,栗子:原文件是.css,選擇css-loader
fallback: 編譯后用什么loader來提取css文件
publicfile: 用來覆蓋項目路徑,生成該css文件的文件路徑 其實有output就可以了

Reference

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 1 Webpack 1.1 概念簡介 1.1.1 WebPack是什么 1、一個打包工具 2、一個模塊加載工具 3...
    Kevin_Junbaozi閱讀 7,021評論 0 16
  • 版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 webpack介紹和使用 一、webpack介紹 1、由來 ...
    it筱竹閱讀 11,457評論 0 21
  • GitChat技術(shù)雜談 前言 本文較長,為了節(jié)省你的閱讀時間,在文前列寫作思路如下: 什么是 webpack,它要...
    蕭玄辭閱讀 12,887評論 7 110
  • 又到了每天寫東西的環(huán)節(jié)了,其實寫之前的構(gòu)思遠對我而言十分痛苦,寫了幾天以后發(fā)現(xiàn)原來我想說的東西并沒有那么多...
    不是什么偉大的人閱讀 399評論 0 0
  • 昨天晚上群里看到老師說發(fā)了通知書,需要家長簽字帶回,今早我翻遍了兒子的書包也沒看見,等他吃完早飯從爺爺家過來的時...
    博碩媽閱讀 216評論 0 0

友情鏈接更多精彩內(nèi)容