Skip to content

Instantly share code, notes, and snippets.

@pthrasher
Created July 27, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pthrasher/115cb52d5da254ad1ab5 to your computer and use it in GitHub Desktop.
Save pthrasher/115cb52d5da254ad1ab5 to your computer and use it in GitHub Desktop.
Example of babel plugin to aid rails sprocket tie-in.
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