Express+gulp construction Project (v) Swig template

Source: Internet
Author: User

The file here is responsible for configuring the Swig template engine.

Index.js

varJsonhash = require ('./json_file '));varStatictag = require ("./tag-static"); Exports.init=function(Swig) {swig.setextension (' Static ',function(input) {//swig.setextension Add extensions for custom labels. ' Static ' is the name of the custom label        //input is the value taken from the template tag        varHashlist ={}, Output=input; varIsjs = (/(\.js) $/). Test (input); varIscss = (/(\) ( SCSS|CSS)) $/). Test (input); if(ISJS) {hashlist=Jsonhash.jshash (); } Else if(iscss) {hashlist=Jsonhash.csshash (); Input= Input.replace (/(\.SCSS) $/, '. css ');//}Else{hashlist=Jsonhash.imghash (); }        //get the list according to the file type        if(Hashlist[input]) {Output= '/' + Static_url +Hashlist[input]; } Else if(node_env = = = ' Dev ' && iscss | |Isjs) {Output= '/' + Static_url +input; } Else{Output= '/' + Static_url +input; }        //determine the file name and path        returnoutput;    }); Swig.settag (' Static ', Statictag.parse, Statictag.compile, Statictag.ends, statictag.blocklevel); //Parse Custom parse function, compile custom compilation function, ends whether to end tag, blocklevel if False, it will not be compiled outside the block when it inherits a parent template};

Json_file.js

varJsonfile = require (' Jsonfile '));varCsshashfile, Jshashfile, Imghashfile;varcsscompressed, jscompressed, imgcompressed;varIsdev = Node_env = = = ' Dev ';Try{csshashfile= Jsonfile.readfilesync ('./hash/css_hash.json ')); //Read the hash list, if you can read to the JSON file, indicating that the Gulp build task has been performed, generate the corresponding list file under the hash foldercsscompressed =true;} Catch(e) {csshashfile= {};}Try{jshashfile= Jsonfile.readfilesync ('./hash/js_hash.json ')); Jscompressed=true;} Catch(e) {jshashfile= {};}Try{imghashfile= Jsonfile.readfilesync ('./hash/img_hash.json ')); Imgcompressed=true;} Catch(e) {imghashfile= {};} Exports.csshash=function () {    varFile = Isdev? (Csscompressed jsonfile.readfilesync ('./hash/css_hash.json ')): csshashfile): Csshashfile; varJSON = {};  for(varKeyinchfile) {        varL =Key, R=File[key]; json['/' + l] = '/' + static_files_output + '/' + R;//Take out the list and return to the object    }    returnjson;}; Exports.jshash=function () {    varFile = Isdev? (Jscompressed jsonfile.readfilesync ('./hash/js_hash.json ')): jshashfile): Jshashfile; varJSON = {};  for(varKeyinchfile) {        varL =Key, R=File[key]; json['/' + l] = '/' + static_files_output + '/' +R; }    returnjson;}; Exports.imghash=function () {    varFile = Isdev? (Imgcompressed jsonfile.readfilesync ('./hash/img_hash.json ')): imghashfile): Imghashfile; varJSON = {};  for(varKeyinchfile) {        varL =Key, R=File[key]; json['/' + l] = '/' + static_files_output + '/' +R; }    returnjson;};

Tag-static.js

Exports.parse =function(str, line, parser, types, options) {varmatched =false; Parser.on (‘*‘,function(token) {if(matched) {Throw NewError (' unexpected token ' + Token.match + '. ')); } Matched=true; return true;    }); return true;}; Exports.compile=function(compiler, args, content, parents, options, Blockname) {return' _output + = _ext.static (' + args[0] + '); ';}; Exports.ends=false; Exports.blocklevel=false;

What is the effect of the Swig template through this series of configurations?

Originally in the Gulp task through the gulp-rev-all-fixed processing, the file name of all static files are added to the hash value, and if the file changes, the hash file name will change.

Just like this, the six bits before the extension are the names of the hashes that change as the file changes.

Our custom static swig tag is then used to deal with file name problems when we write HTML, and the files introduced are written with custom tags.

<link rel= "stylesheet" href= "{% static '/css/base.scss '%}"/><link rel= "stylesheet" href= "{% static '/widget/ Global/fonts/font-awesome/font-awesome.min.css '%} ' >

When swig compile HTML to see the custom label will go to find the hash folder under the list file, the list file is an object, is the source file name corresponding to the changed hash file name, and then according to the rules replaced, the introduction of the file will not be the wrong name and error. The URL of the background image in the CSS style will also be replaced. The reason for this setting is to prevent the browser from caching static files.

Express+gulp construction Project (v) Swig template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.