-
-
Save yamadapc/9976164 to your computer and use it in GitHub Desktop.
browserify dynamic require
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
'use strict'; | |
module.exports = 'a.js'; |
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
'use strict'; | |
module.exports = 'b.js'; |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<script src="browser.js"></script> | |
</body> | |
</html> |
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
// build with `browserify -o browser.js main.js` | |
require('./a'); // if this could be automated, or especified in another manner it'd be great. | |
require('./b'); // (this is the brick wall, as of today) | |
var modules = ['a', 'b']; | |
var mds = {}; | |
modules.forEach(function(moduleName) { | |
mds[moduleName] = require('./' + moduleName); | |
}); | |
console.log(mds.a, mds.b); // => "a.js b.js" |
The problem with your guest is that you're actually doing a require of the modules on the first static requires. Thing is, the program may never use some or all of the required modules in real life scenario. That dummy function prevents that and only makes the code actually do the require of the module when needed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// dont call the function, this is just to trick browserify to actually include the files without calling em'
function ⊙_⊙ () {
require('./a'); // if this could be automated, or especified in another manner it'd be great.
require('./b'); // (this is the brick wall, as of today)
}