Created
April 9, 2014 08:49
-
-
Save Raynos/10243323 to your computer and use it in GitHub Desktop.
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
/* usage: | |
var requireMain = require('require-main') | |
// instead of `require.main === module` | |
if (requireMain(require, module)) { | |
doStuff(process.argv) | |
} | |
*/ | |
var MAGIC_BROWSERIFY_LINES = 8; | |
function nodeMain(require, module) { | |
return require.main === module; | |
} | |
function browserMain() { | |
var error; | |
try { | |
throw new Error('browserMain') | |
} catch (e) { | |
error = e; | |
} | |
var lines = error.stack && error.stack.split('\n') | |
return lines.length < MAGIC_BROWSERIFY_LINES; | |
} | |
if ("main" in require) { | |
module.exports = nodeMain; | |
} else { | |
module.exports = browserMain; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment