jbrantly / yabble Goto Github PK
View Code? Open in Web Editor NEWYet Another (CommonJS) Browser Loader
Home Page: http://blog.jbrantly.com
License: MIT License
Yet Another (CommonJS) Browser Loader
Home Page: http://blog.jbrantly.com
License: MIT License
A general purpose browser-side CommonJS module loader Yabble is a general purpose browser-side CommonJS module loader. Other similar module loaders are available, but Yabble strives to be a flexible loader in a small package. To see live unit tests, visit: http://jbrantly.github.com/yabble/test/index.html To see an overview of the goals, visit: http://blog.jbrantly.com/2010/04/introducing-yabble.html Features: * Compliant with Modules 1.1.1 * Supports async XHR/eval() - Allows the use of non-transport-wrapped module code for quick development * Supports script tags with a Transport format - Easier debugging, potential for file concatenation - Supports Transport/D and Transport/C * Supports require.ensure() for lazy-loading modules * Passes unit tests on YUI's A-grade browsers * Includes a tool for automatically wrapping modules in a Transport format * Small size (<600 lines, ~15kb raw, ~5kb minified, and ~2kb when minified/gzipped) Roadmap: * Stability - Even more cross-browser testing - More unit tests for Async/A * Features - Async/A error handling once its decided (maybe before) - Tested support for loading from another domain (it may work now..) * Documentation * Tooling - Currently under-developed. Not recommended to use. - Use loader code in tooling (no copy/paste code) - Explicit support for concatenated Transport files tooling (loader should already support) - Expand supported engines for tooling (node.js) - Better consideration for platform (line endings, encoding, etc) Usage: require.setModuleRoot(path) Use this to configure the root path to find modules. Think of this as a single entry in require.paths. require.useScriptTags() The default behavior is to use XHR+eval() and expects unwrapped modules. Call this method to configure Yabble to use script tags and expect wrapped modules. require.run(programId) This kicks off an application using the specified module ID. require.ensure(dependencies, callback) See Async/A for more details. Can be used to retrieve modules and run code without using require.run(). See unit tests for example usage. Changelog: v0.3.0 (2010-04-18) - Restructured to use Packages/1.0 v0.2.0 (2010-04-14) - Now using QUnit for unit tests - Added support for Modules 1.1.1 plus unit tests - Finished support for Transport/D plus unit tests - Added support for Transport/C plus unit tests - Added unit tests for Async/A and fixed bugs - Added unit tests for resolveModuleId and determineShallowDependencies v0.1.0 (2010-04-12) - Initial commit
Example:
require.def({
"underscorex": {
injects: [ 'require', 'exports', 'module' ],
factory: function (require, exports, module) {
var _ = exports = module.exports = require('underscore');
console.log(_)
// libary defined here
}
},
"underscore": {
injects: [ 'require', 'exports', 'module' ],
factory: function (require, exports, module) {
var _ = ...
// Must be commented out, because Yabble does not define module.exports
if (typeof module !== 'undefined'/* && module.exports*/) {
module.exports = _;
_._ = _;
} else {
root._ = _;
}
// libary defined here
}
}
})
console.log() prints {} instead of the underscore library.
Some libraries (example: underscore) are using checks whether module.exports is defined, but when I define a module with Transport/D and injects. module and exports are defined but module.exports (=== exports) is not.
require.def({ name: { injects: [ 'require', 'exports', 'module' ], factory: function (require,exports,module) { ... } } })
I use 64-bit Arch Linux and Chromium 5.0.342.9 (43360).
Modules/1.0 – all failed
Modules/1.1.1 – all failed
Modules/1.1.1 Wrapped – 9 passed, 3 failed
Async/A – all failed
determineShallowDependencies() – all failed
The error that I see most often is “Died on test #1: NETWORK_ERR: XMLHttpRequest Exception 101.”, but there’s also stuff like “FAIL module.uri is absolute and ends with program.js”.
It's just a warning so no biggie, but apparently FF has deprecated the second parameter to eval
:
[17:06:41.705] Support for eval(code, scopeObject) has been removed. Use |with (scopeObject) eval(code);| instead. @ http://testharness.fluencychallenge.com/js/node_modules/yabble/lib/yabble.js:565
I'm not sure what the solution is.
In test.html
<script type="text/javascript" src="yabble/lib/yabble.js"></script>
<script type="text/javascript">
require.useScriptTags()
require.ensure(['module'], function (require) {
console.log(require('module')) // *)
})
</script>
module.js is in the same path:
require.def({
m: function (require, exports, module) {
exports.name = 'm'
exports.other = require('ok/other')
},
module: function (require, exports, module) {
exports.name = 'module'
exports.m = require('m')
exports.other = require('ok/other')
}
}, ['ok/other'])
ok/other.js:
require.def({other:function(require, exports, module) {
exports.name = 'other'
}})
Both files finish loading with script tags, but I get this error in this line (*):
Uncaught Error loading module
It seems that yabble hasn't registered the module although the scripts have successfully loaded. Same result, if I explicitly set the module root.
Using the path ./ok/other
will result in this error:
Uncaught TypeError: Object [object DOMWindow] has no method '_resolveModuleId'
Passing the absolute URI is failing as well.
Putting other.js in root is working.
Using params on to the main() call can be very usefull (flexibility, reusability, ...) : require.run("app/main", [params], cb).
For instance by calling several instance of an application using different parameters ...
//index.html
[...]
url = http://localhost/app/foo/bar;
require.setModuleRoot(url);
require.run("app", [url]);
[...]
//app.js
[...]
function main(url) {
//some work using url
}
[...]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.