Created
December 3, 2014 01:29
-
-
Save chromakode/4cbcc6da0418c0235b4a to your computer and use it in GitHub Desktop.
jsio require preprocessor
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 importExpr = /^(\s*)(import\s+[^=+*"'\r\n;\/]+|from\s+[^=+"'\r\n;\/]+)(;|\/|$)/gm; | |
function replaceImport(raw, p1, p2, p3) { | |
if (!/\/\//.test(p1)) { | |
return p1 + 'jsio(\'' + p2 + '\')' + p3; | |
} | |
return raw; | |
} | |
var requireExpr = /^(.*)require\(['"]([^=+*"'\r\n;]+)['"]\)(;|\/|$)/gm; | |
function replaceRequire(raw, p1, p2, p3) { | |
if (!/\/\//.test(p1)) { | |
return p1 + 'jsio(\'' + p2 + '\')' + p3; | |
} | |
return raw; | |
} | |
exports = function (path, moduleDef, opts) { | |
moduleDef.src = moduleDef.src.replace(importExpr, replaceImport); | |
moduleDef.src = moduleDef.src.replace(requireExpr, replaceRequire); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment