Skip to content

Instantly share code, notes, and snippets.

@boopathi
Created October 31, 2016 15: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 boopathi/9578380f842a56d6194f8ac3c57789cb to your computer and use it in GitHub Desktop.
Save boopathi/9578380f842a56d6194f8ac3c57789cb to your computer and use it in GitHub Desktop.
Converts System.import to a synchronous require call. For NodeJS
const template = require("babel-template");
module.exports = function SystemImportPlugin() {
return {
visitor: {
CallExpression(path) {
const callee = path.get("callee");
if (!callee.isMemberExpression()) return;
const object = callee.get("object");
const property = callee.get("property");
if (!object.isIdentifier() || !property.isIdentifier()) return;
if (object.node.name !== "System" || property.node.name !== "import") return;
const build = template(`
({
then(cb) {
cb(require(SOURCE));
}
})
`);
const replacement = build({
SOURCE: path.node.arguments[0]
}).expression;
path.replaceWith(replacement);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment