Created
September 25, 2018 21:30
-
-
Save laurenashpole/2426da02553fe5d66009a77cad40f6c1 to your computer and use it in GitHub Desktop.
A custom Swig tag for getting versioned asset paths.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let assets = require('../../public/assets.json'); | |
module.exports = function (swig) { | |
swig.setTag('asset', _parse, _compile, false, true); | |
}; | |
let _parse = function (str, line, parser) { | |
parser.on('*', function (token) { | |
let match = token.match.match(/^["'](.*?)["']$/); | |
let assetPath = match ? match[1] : null; | |
if (assetPath) { | |
let asset = assets[assetPath] || assetPath; | |
this.out.push(asset); | |
} | |
}); | |
return true; | |
}; | |
let _compile = function (compiler, args) { | |
if (!args || !args[0]) { | |
throw new Error('The asset tag expects a filename as a string'); | |
} | |
let assetPath = args[0].startsWith('/') ? args[0] : '/' + args[0]; | |
return `_output +="${assetPath}";`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment