目標文件: 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%}
** 簡書怎么代碼粘貼進去被過濾掉了 **