Created
July 27, 2015 14:14
-
-
Save pthrasher/115cb52d5da254ad1ab5 to your computer and use it in GitHub Desktop.
Example of babel plugin to aid rails sprocket tie-in.
This file contains 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
var fs = require('fs'); | |
var path = require('path'); | |
var md5itCfg = require('../grunt/config/cl_md5it'); | |
module.exports = function(babel) { | |
var t = babel.types; | |
var md5sFile = md5itCfg.options.manifestFile; | |
var md5s, cache = {}; | |
try { | |
md5s = JSON.parse(fs.readFileSync(md5sFile, 'utf8')) | |
} catch (e) { | |
md5s = {}; | |
} | |
function getUrl(path) { | |
if (!cache[path]) { | |
if (!md5s[path]) { | |
cache[path] = path; | |
} else { | |
cache[path] = md5s[path][0].fullUrl; | |
} | |
} | |
return cache[path]; | |
} | |
return new babel.Transformer('get_asset_url', { | |
CallExpression: { | |
enter: function(node, parent) { | |
if (t.isIdentifier(node.callee, {name: 'get_asset_url'})) { | |
var url = getUrl(node.arguments[0].value); | |
return t.expressionStatement(t.literal(url)); | |
} | |
}, | |
}, | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment