改寫 html-webpack-plugin 支持jinja模板

目標文件: html-webpack-plugin/index.js

/**

* Injects the assets into the given html string

*/

HtmlWebpackPlugin.prototype.injectAssetsIntoHtml=function(html,assets,assetTags) {

varhtmlRegExp =/(]*>)/i;

varheadRegExp =/(<\/head>)/i;

varbodyRegExp =/(<\/body>)/i;

varcssRegExp =/()/i;

varscriptRegExp =/()/i;

varbody = assetTags.body.map(this.createHtmlTag);

varhead = assetTags.head.map(this.createHtmlTag);

varself =this;

if(self.options.inject ==="tpltag"){

if(cssRegExp.test(html)){

headRegExp = cssRegExp;

}

if(scriptRegExp.test(html)){

bodyRegExp = scriptRegExp;

}

}

if(body.length) {

if(bodyRegExp.test(html)) {

// Append assets to body element

html = html.replace(bodyRegExp,function(match) {

if(self.options.inject ==="tpltag") match =''

returnbody.join('') + match;

});

}else{

// Append scripts to the end of the file if no element exists:

html += body.join('');

}

}

if(head.length) {

// Create a head tag if none exists

if(!headRegExp.test(html)) {

if(!htmlRegExp.test(html)) {

html =''+ html;

}else{

html = html.replace(htmlRegExp,function(match) {

returnmatch +'';

});

}

}

// Append assets to head element

html = html.replace(headRegExp,function(match) {

if(self.options.inject ==="tpltag") match =''

returnhead.join('') + match;

});

}

// Inject manifest into the opening html tag

if(assets.manifest) {

html = html.replace(/(]*)(>)/i,function(match,start,end) {

// Append the manifest only if no manifest was specified

if(/\smanifest\s*=/.test(match)) {

returnmatch;

}

returnstart +' manifest="'+ assets.manifest+'"'+ end;

});

}

returnhtml;

};


使用 webpack配置

varconf = {

filename: '',//編譯后生成的jinja存放路徑,相對于path

template:'',//html模板路徑

inject:'tpltag',//js插入的位置,true/'head'/'body'/false

hash:false,

minify:false,

chunks: []

};

return newHtmlWebpackPlugin(conf);


模板實例

{%extends"base.jinja"%}

{%blocktitle%}音樂上傳-婚禮時光{%endblock%}

{%blockkeywords%}婚禮,婚禮布置,電子請柬,主題婚禮,婚禮圖片,婚禮靈感{%endblock%}

{%blockdescription%}婚禮時光是一站式婚禮創(chuàng)意平臺,可以尋找婚禮靈感,主題婚禮案例,免費定制電子請柬,預(yù)約婚禮服務(wù),分享幸福時光{%endblock%}

{%blocksub_css%}


{%endblock%}

{%blockmain%}

{%endblock%}

{%blockfooter%}

{%endblock%}

{%blockscript%}


{%endblock%}

編譯完后的代碼

{%extends"v4/base.jinja"%}

{%blocktitle%}音樂上傳-婚禮時光{%endblock%}

{%blockkeywords%}婚禮,婚禮布置,電子請柬,主題婚禮,婚禮圖片,婚禮靈感{%endblock%}

{%blocksub_css%}

{%endblock%}

{%blockmain%}

{%endblock%}

{%blockfooter%}

{%endblock%}

{%blockscript%}

{%endblock%}

** 簡書怎么代碼粘貼進去被過濾掉了 **

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

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

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,699評論 19 139
  • 安裝 & 引用 結(jié)果 命令行 index.html Case1 添加 template 命令行 打包后 總結(jié) C...
    從此以后dapeng閱讀 904評論 0 0
  • 作用 直接為項目生成一個或多個HTML文件(HTML文件個數(shù)由插件實例的個數(shù)決定),并將webpack打包后輸出的...
    前端xiyoki閱讀 20,310評論 2 10
  • 喜歡馬。喜歡它溫柔的眼神,喜歡它側(cè)過臉來與人貼臉的愛嬌,喜歡它粗糙的舌頭舔過手心的癢,喜歡躍馬馳過平川,立馬山顛,...
    銘玥詠全閱讀 523評論 0 0
  • 最近,看到許多次詩和遠方,有人說,生活,除了眼前的茍且,還有詩和遠方。讓后我像是打雞血一樣,嚷著要去遠方。后來...
    嗯一期一會閱讀 813評論 0 1

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