Bridge Hydra.js module dependencies to RequireJS so third-party scripts load asynchronously and are injected into Hydra module factories.
Hydra resolves named dependencies when you register modules. This package registers an async mapping on the sl_ prefix: when a module asks for something like sl_jQuery, Hydra delegates to RequireJS, which loads the script (with optional shim exports), then passes the resolved value into your module callback.
The implementation is a small Hydra extension that calls Hydra.addAsyncMapping('sl_', …) and, for each dependency, configures RequireJS (paths + shim) and resolves a Hydra Promise when the AMD module is ready.
- Hydra.js (global
Hydra) - RequireJS (global
requirejs/require)
Load both before this file.
npm install hydrajs-requirejs-dependency-injectorThe package entry point is src/hydrajs-requirejs-dependency-injector.js. Serve it in the browser after Hydra and RequireJS (see below).
Use paths that match your app layout:
<script src="/path/to/require.js"></script>
<script src="/path/to/hydra.js"></script>
<script src="/path/to/node_modules/hydrajs-requirejs-dependency-injector/src/hydrajs-requirejs-dependency-injector.js"></script>Call Hydra.addMapping with the sl_ namespace and an object describing each dependency. Keys are the short names used after the sl_ prefix in module definitions.
Hydra.addMapping('sl_', {
jQuery: {
exports: 'jQuery',
url: 'https://code.jquery.com/jquery-1.11.0.js'
}
});| Field | Meaning |
|---|---|
url |
Script URL (the .js suffix is normalized when passed to RequireJS paths). |
exports |
Global name or AMD module id that RequireJS should expose after load (shim exports). |
Prefix the mapping key with sl_ in the dependency array:
Hydra.module.register('my-module', ['sl_jQuery'], function ($) {
return {
init: function () {
$('#my_layer').css('display', 'none');
}
};
});The factory receives the value resolved by RequireJS (for example the jQuery instance).
- Hydra.js site & examples (includes broader modular architecture)
- Changelog
- Issues
npm install
npm testTests run via Karma (see config/karma.conf.js).
MIT — see LICENSE.
