Created
October 31, 2016 15:14
-
-
Save boopathi/9578380f842a56d6194f8ac3c57789cb to your computer and use it in GitHub Desktop.
Converts System.import to a synchronous require call. For NodeJS
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
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