Convert AMD & commonjs modules to commonjs, AMD, UMD or single .js
and run/test on nodejs, Web/AMD or Web/Script.
All documentation is moved to the wiki and http://urequire.org
Write modular, maintainable & reusable code:
-
Clearly stated dependencies & imports.
-
Employ standards and trusted tools.
-
Have a dynamic code loading mechanism.
-
The damnation of one huge .js file or concatenation must end!
Many woes on Module formats & incombatibilities:
-
Verbose syntax, boilerplate ceremony & intricacies (especially AMD)
-
execution environment (AMD only for Web, CommonJs only for nodejs)
-
capabilities, dependency/path resolutions, plugins, semantics etc are a mess
-
UMD is a semi-standard boilerplate, far from usable.
require('more').than(this);
-
Convert from any format to any other:
-
from AMD and CommonJS
-
to AMD, CommonJS, UMD, Combined for nodejs-Web/AMD-Web/Script
-
ES6/Harmonywhen standard/parsers mature
-
-
Forget the woes or Module formats incompatiblities
-
Eliminate boilerplate & write modular Javascript code once, run everywhere : Web/Script, Web/AMD, nodejs
-
A Universal Module Format with the power, goodies & standards from all.
-
Convert to a single
combined.js
, that runs everywhere & is super optimized
Simplest Module Authoring
define(['dep1','dep2'], function(dep1,dep2) {
// do stuff with dep1, dep2
return {my:'module'}
});
// or
var dep1 = require('dep1'),
dep2 = require('dep2');
// do stuff with dep1, dep2
module.exports = {my: 'module'}
// or both, in a relaxed, non-weird way
define(['dep1','dep2'], function(dep1,dep2) {
var dep3JSON = require('json!dep3AsJSON');
// do stuff with dep1, dep2, dep3JSON
return {my:'module'}
});
A Modules & Dependencies aware builder.
Exporting modules to window
/global
variables (like window._
, window.$
etc), demystified and with no boilerplate.
Want noConflict()
, baked in? Its a simple declaration away.
// file `uberscore.js` - export it to root (`window`) as `_B`
({ urequire: { rootExports: '_B', noConflict: true }});
module.exports = {...}
The same in a config is
dependencies: { exports: { root: { 'uberscore': '_B' }}}`
How about exporting to your bundle only?
// export/inject `_` in (all) bundle's modules
dependencies: { exports: { bundle: { 'lodash': '_' }}}
Want to replace deps with mocks or alternative versions ?
Inject, replace or even delete dependencies with a simple declaration or a callback:
// underscore is dead, long live _
dependencies: { replace: { lodash: 'underscore'}}
// with code
function(modyle){ modyle.replaceDeps('models/PersonModel', 'mock/models/PersonModelMock'); }
A versatile in-memory Resource Conversion
Manipulate Module code while building:
- inject, replace or delete code fragments or AST nodes or dependencies with one liners.
// delete matching code of code skeleton
function(m){ m.replaceCode('if (debug){}') }
// traverse matching nodes, replace or delete em
function(m){ m.replaceCode('console.log()', function(nodeAST){return nodeOrStringOrUndefined}) }
Perform any code manipulation - eg remove debug code, inject initializations etc
- Merge repeating statements: keep DRY, save space & speed when
combined
in a single.js
# unify / merge repeating statements
bundle: commonCode: 'var expect = chai.expect;'
- Initialize custom module code, for common tasks:
function(m) { m.beforeBody = 'var l = new _B.Logger("Logger" + m.dstFilename);' }
A ResourceConverter for our .coco
files (included along with coffeescript, LiveScript, iced-coffee-script)
[ '$coco', [ '**/*.co'], ((r)-> require('coco').compile r.converted), '.js']
This 'uberscore'
config (coffeescript) will:
-
read files from
source
, [write tobuild
](http://urequire.org/(masterdefaultsconfig.coffee#build.dstPath) -
convert each module in
path
to UMD (default) -
copy all other files there
-
Allow
runtimeInfo
(eg__isNode
,__isAMD
) selectively -
inject
lodash
dep in each module as_
-
export a global
window._B
with anoConflict()
-
inject
'var VERSION =...'
before body ofuberscore.js
-
minify each module with UglifyJs2's defaults
-
add a banner (after UMD template & minification)
-
clean directory at
dstPath
(before writing anything) -
watch for changes, convert only what's really changed
# Config as a `Gruntfile.coffee` task
# Can be a .coffee, .js, .json, .yml & more
uberscore:
path: 'source'
dstPath: 'build'
filez: ['**/*', (f)-> f isnt 'badfile']
copy: [/./]
runtimeInfo: ['!**/*', 'Logger.js']
dependencies: exports:
bundle: 'lodash': '_'
root: 'uberscore': '_B'
resources: [
['+inject:VERSION', ['uberscore.js'],
(module)-> module.beforeBody =
"var VERSION = '0.0.15';"]
]
template: banner: "// uBerscore v0.0.15"
optimize: 'uglify2'
clean: true
watch: true
Parent configs ? Lets derive!
The 'distribute'
config will:
-
derive (i.e deep inherit & modify) the above
-
filter some more filez
-
change template to 'combined'
-
output to a different filename
Lets derive some children
distribute:
derive: ['uberscore']
filez: ['!', /useRegExpsAsFileSpecs/]
template: 'combined'
dstPath: 'build/uberscore-combined.js'
optimize: uglify2: {more: uglify2: options}
Continue reading at http://urequire.org
The MIT License
Copyright (c) 2013 Agelos Pikoulas ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.