rjs-subcontext
v0.1.2
Published
Context inheritance for require.js: load shared dependencies from parent contexts
Downloads
3
Maintainers
Readme
require.js subcontext
Function to create require.js contexts that share modules with parent contexts (inherit from other contexts), but still have their own modules. It's like a prototype chain for require.js contexts.
Usage
const createSubContext = require('rjs-subcontext')
createSubContext('subcontext_name')
require({ context: 'subcontext_name' }
, [ 'dependency' ]
, function (dep) { /* do something */ })
Now, the "dependency" will be loaded from the "subcontext_name" context if it is defined there, or from the global context if it is defined there. If the requested module can not be found on any of the parent contexts, it's loaded through the child context instead.
#AnotherExample
A simple, contrived app with a custom view layer based on jQuery and Handlebars:
// top-level libraries context
require({ context: 'Libraries' })
createSubContext('Helpers', 'Libraries')
createSubContext('Views', 'Helpers')
With module definitions:
- Libraries context:
jquery
handlebars
- Helpers context:
handlebars
handlebars-helpers
, depends onhandlebars
andjquery
- Views context:
view
, depends onjquery
login-view
, depends onview
andhandlebars
Now, require()
does the followng:
require({ context: 'Views' }, [ 'view' ])
loads "view" from the Views context. "view" depends on jQuery, which is loaded from the Libraries context.require({ context: 'Views' }, [ 'handlebars-helpers' ])
loads "handlebars-helpers" from the Helpers context. It depends on "handlebars", which is also loaded from the Helpers context, and on "jquery" which is loaded from the Libraries context.require({ context: 'Libraries' }, [ 'handlebars' ])
loads "handlebars" from the Libraries context.
Building
rjs-subcontext
uses Babel:
npm run babel