JSPM

  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q47025F

Package Exports

  • re-define

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (re-define) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

re-define

Let's re-define something ... without any configuration ... just do the magic for me.

Easy way to convert AMD and CommonJS projects to one bundle wrapped in UMD.

Why

  • to get decent encapsulation, registering a bundle not a part
  • to make project compatibile across node and web without effort

Features

  • highly customizable: templates, transforms (like with browserify), discoverable folders
  • now also resolving modules dependencies
  • looking for dependencies in node_modules and bower_components
  • resolving whole dependencies trees as well as single file
  • handlebars for templating with custom helpers
  • resolving AMD !text plugin within cjs and amd code
  • using vinyl, integration with gulp should be easy
  • (WiP) detailed report, to get whole picture, display modules, dependencies and externals

Nice things

  • when re-define meet external dep, automatically checks descriptor files, such as bower.json and package.json, there is also a fallback to node_modulesas well asbower_componentswhen descriptor is missing or there is nomain` defined
  • when including an UMD file as a dep and your define module is anonymous, re-define will add a name for you also check internal depenencies

Limitation

  • does not resolve circular dependencies

Getting Started

Install the module: npm install -g re-define

###Usage

Usage: re-define [options]

Options:
  '-t, --transform [libs]'      , 'Custom transforms stream invoked for each module'
  '-m, --main [filepath]'       , 'Main file'
  '-b, --base [dir]'            , 'CWD'
  '-d, --discoverable [dirs]'   , 'Check in folders when encouter an external dep'
  '-e, --external [json]'       , 'Exact dep locations, when discoverable is not enough'
  '-g, --globals [module#as]'   , 'Global reference to external libs'
  '-w, --wrapper [type]'        , 'Wrapper type report, default: umd'
  '-n, --name [module]'         , 'Module name'
  '-r, --returns [module]'      , 'What module returns'
  '-e, --exclude-deps [deps]'   , 'Exclude deps'

Example usage

check example folder and appropriate build files

Debbuging

To run re-define in debug mode is fairly easy, just run re-define with appropriate category like below

//all
DEBUG=re-define:* 

//specific:
re-define:transform:* 
re-define:converter
re-define:bin

###Config

  { cwd          : '.'
  , name         : 'module_name'
  , wrapper      : 'umd'
  , returns      : ''
  , excludeDeps  : ['\.css$', 'require', 'modules', 'exports']
  , globals      : [] //external module_name#global_ref
  , external     : {} //{"jquery": "location"} or {"..": "{path: "...", cwd: "..."}
  , discoverable : ['node_modules', 'bower_component']
  , descriptors  : ['package.json', 'bower.json']
  , plugins      : [{extension: '.html', pattern : '^(text\/?)*!'}]
  , escape       : function (val) { return val.replace(/\.\/|\!|\.|\/|\\|-/g, '_') }
  , scopeVar     : '_scope_'
  , internalRequire : 'require'
  }

Example wrapper:

(function (parent, factory){
  if (typeof define === 'function' && define.amd) {
    define('{{{config.name}}}', [{{{chain external map '\'|\'' reduce}}}], factory)
  } else if (typeof exports === 'object') {
  module.exports = factory({{{chain external map 'require(\'|\')' reduce}}})
  } else {
    {{#each external}}var {{{escape this}}} = {{{global this}}}
    {{/each}}
    {{#if config.returns}}parent['{{{config.name}}}'] = {{/if}}factory({{{chain external escape}}})
  }
  }(this, function ({{{chain external escape}}}) {
  {{{chain external escape map 'this.| = |'}}}

  {{#each bundles}}
  {{#if this.wrap}} 
  (function() {
    var {{{../../config.scopeVar}}} = {}
    {{{this.code}}}
    function {{{../../config.internalRequire}}} (name) { return this[name] || {{{../../config.scopeVar}}}[name] }
  }).call(this);
  {{else}} {{{this.code}}} {{/if}} {{/each}}

  {{#if config.returns }} return this.{{{config.returns}}} {{/if}}
}))

####Custom transform

var through = require('through2')

module.exports = function(config) {

  //config instance

  return through.obj(function(file, enc, next) {
    /* 
    //inside of file you can find following properties
    {
      "cwd": "lib",
      "base": "lib",
      "path": "./lib/main.js",
      "ext": ".js",
      "type": "require",
      "ast" : ...
      "contents" : ...
      "deps": [
        {
          "path": "jquery",
          "require": "jquery"
        }
        ...
      ]
    */

    //your own spells

    this.push(file)
    next()
  })
}