Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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