-
Notifications
You must be signed in to change notification settings - Fork 27
Adopting modules
- Module parameters: exports, require, bind
If you are using jquery as in-package module or any other module without exports. LMD can easily convert it to LMD format.
You may add "exports"
and/or "require"
to your module descriptor to notify LMD that this module should be converted to LMD format.
The design of non-lmd2lmd patching is close to require.js shim
Example
You have one 3-party module and you have to include it to the LMD-package.
function pewpew () {
}
function ololo() {
}
var someVariable = "string";
It easy, just add "exports"
to your module descriptor, add "require" to start module deps:
"third_party_module_b": {
"path": "vendors/other_module.js",
"exports": {
"pewpew": "pewpew",
"ololo": "ololo",
"someVariable": "someVariable"
} // return ALL THE SUFF!!!
}
Or return just one exports, and some deps
"third_party_module_b": {
"path": "vendors/other_module.js",
"exports": "pewpew || ololo", // or var name "pewpew"
// Modules may have some deps
"require": {
"third_party_module_b": "third_party_module_b-dep",
"someGlobal": "Function"
}
}
LMD will make a LMD module from you code (while building LMD-package)
(function (require) { // << added
var third_party_module_b = require("third_party_module_b-dep"), // << added
someGlobal = require("Function"); // << added
function pewpew () {
}
function ololo() {
}
var someVariable = "string";
return pewpew || ololo; // << added
}) // << added
"exports"
should be valid JavaScript code or object of valid JavaScript
You may use more complex exports as "exports": "require('$').noConflict(true)"
if you are exporting jQuery.
Note Try not to use complex expressions!
There is a modules that requires custom this
, for example Bacon.js. And if you using jQuery as jQuery.noConflict(true)
Bacon will "patch" global jQuery not yourjQuery made by jQuery.noConflict(true)
... But there is a way to thange context of module!
Config
{
"modules": {
"jQuery": {
"path": "jquery.min.js",
"exports": "jQuery.noConflict(true)"
},
"bacon": {
"path": "bacon.js",
"bind": {
"jQuery": "jQuery"
},
"exports": "this.Bacon"
}
}
}
LMD builder will add extra wrapper to that module to change context of the function
function (require, module, exports) { // added by wrapper
return (function () { // added by wrapper
// 3-party module code
return this.Bacon; // added by wrapper
}).call({ // added by wrapper
"jQuery": require("jQuery") // added by wrapper
}); // added by wrapper
} // added by wrapper
Sometimes you need to use your favorite lib with plugins. And "true" modular way when each plugin is module is not convinient - each plugin is dependent on main library and you have to keep order manually.
Eg. Doing this is not cool...
var $ = require('$');
require('$.plugin1');
require('$.plugin2');
require('$.plugin3');
Instead of doing that stuf, define module as multi-path module.
{
"modules": {
"jquery": {
"path": ["vendors/fake_jquery.js", "vendors/fake_jquery-plugin3.js", "vendors/fake_jquery.*.js"],
"exports": "jQuery"
},
"backbone": ["vendors/backbone.js", "vendors/backbone.*.js"]
}
}
And have fun!
var $ = require('$');
You can use widcards and globbing to match multiply plugins.
IMPORTANT If you want to keep modules in exact order - list them manually. Globbed files may appear in random order depend on Node.js version and you OS File Sistem.