I want to create a JavaScript library for browser using Browserify / Browserify Shim and make it available within the browser from a global variable.
I would want that the exports of the entry JS file would be attached on this variable. For example, in my library, I would have this:
exports.doSomething = function() {
(...)
};
And, within my browser, I would like to do this:
<html>
<head>
<script type="text/javascript" src="mylib.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#test').click(function() {
mylib.doSomething();
});
});
</script>
</head>
(...)
</html>
How can I configure Browserify / Browserify Shim to achieve this?
Thanks very much for your help! Thierry
You cannot configure browserify to achieve this, because this is not how browserify works. But you can use browserify's global
variable to publish your function.
Add the following line to your library:
global.doSomething = function() { ... }
Then change your html code like this:
$(document).ready(function() {
$('#test').click(function() {
doSomething();
});
});
NOTE: You don't have to include mylib.js
separatly. Just include the bundle.
Be careful with your function/module names. The first code line will append the function to the window
-object, so you could "pollute" the global space or more important: You could override an other global module.