Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including urequire with all npm packages installed. Try it out:

var urequire = require("urequire")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

urequire v0.6.20

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.6.19

Build Status Up to date Status

The JavaScript Universal Module & Resource Converter

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 :-)

  • Flattr donate button PayPayl donate button

Some Introduction

Why use modules like AMD or Common/JS ?

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!

Are U still concatenating .js files ?

But javascript developers hate modules!

Many woes on Module formats & incombatibilities:

U need a bridge to enjoy the richness of modules.

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

Why use uRequire ?

If U required it or defined it, uRequire will find it.

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(dep1, dep2) {
  // do stuff with dep1, dep2
  return {my:'module'}
});

// or both, in a relaxed, non-weird way
define(['some/dirName','dep2'],
  function(dirNameIndex, dep2) {
    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']

A spartan Module builder & config

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

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

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free