urequire

Convert AMD & commonjs modules to UMD, AMD, commonjs or combined.js (rjs & almond) & run/test on nodejs, Web/AMD or Web/Script. Manipulate & inject module code & dependencies while building & more

uRequire v0.7.0-beta10

Docs / wiki / http://uRequire.org mainly are Work In Progress (in transition from v0.6.x) - but everything should works except you'll need npm install urequire-cli -g & npm install urequire locally.

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

Support uRequire

  • @goto('http://github.com/anodynos/urequire').then -> @star() with your love :-)

Some Introduction

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:

require('more').than(this);

Simplest Module Authoring

var dep1 = require('./rel/path/dep1'),
    dep2 = require('abs/path/dep2');
// do stuff with dep1, dep2 
module.exports = {my: 'module'}
 
// or 
define(['./rel/path/dep1','abs/path/dep2'],
function(dep1dep2) {
  // do stuff with dep1, dep2 
  return {my:'module'}
});
 
// or both, in a relaxed, non-weird way 
define(['some/dirName','dep2'],
  function(dirNameIndexdep2) {
    var dep3JSON = require('json!dep3AsJSON');
    // do stuff with dirNameIndex, dep2, dep3JSON 
    return {my:'module'}
  });
 
// mix well known features from both 
define(['my/dep1'], function(dep1) {
  exports.prop = dep1 + require('./your/dep3');
});

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:

// 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

# unify / merge repeating statements 
bundle: commonCode: 'var expect = chai.expect;'
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:

# Config as a `Gruntfile.coffee` task 
# Can be a .coffee, .js, .json, .yml & more 
uberscore:
  path: 'source'
  dstPath: 'build'
  filez: ['**/*''!'(f)-> f is '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:

See more examples

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

License

The MIT License

Copyright (c) 2013-2014 Agelos Pikoulas (agelos.pikoulas@gmail.com)

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.