1.0.1 • Published 8 years ago

gulp-library-umd v1.0.1

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
8 years ago

gulp-library-umd

Gulp plugin for transforming node modules into various flavors of UMD (Universal Module Definition). Wraps files with a specified UMD template.

Supports gulp-sourcemaps! Supports gulp-data!

Templates

gulp-library-umd supports many templates out of the box, including all those provided by UMDjs and gulp-umd

origintemplateamdwebcjsnodenotes
UMDjsamdWeb.jsXXUses AMD or browser globals to create a module
UMDjsamdWebGlobal.jsXXUses AMD or browser globals to create a module. Creates a web global when AMD is used
UMDjscommonjsAdapter.jsXXDefines a module that works in CommonJS and AMD
UMDjscommonjsStrict.jsXXXUses CommonJS, AMD or browser globals to create a module
UMDjscommonjsStrictGlobal.jsXXXUses CommonJS, AMD or browser globals to create a module. Creates a web global when AMD is used
UMDjsjqueryPlugin.jsXXXXUses CommonJS, AMD or browser globals to create a jQuery plugin
UMDjsnodeAdapter.jsXXDefines a module that works in Node and AMD
UMDjsreturnExports.jsXXXUses Node, AMD or browser globals to create a module.
UMDjsreturnExportsGlobal.jsXXXUses Node, AMD or browser globals to create a module. Creates a web global when AMD is used
gulp-umdamd.jsX
gulp-umdamdCommonWeb.jsXXX
gulp-umdcommon.jsX
gulp-umdnode.jsX
gulp-umdweb.jsX
gulp-library-umdumd.jsXXXXSimple support for each of the major export platforms
gulp-library-umdumdGlobal.jsXXXXSimple support for each of the major export platforms. Creates a web global when AMD is used

Usage

gulpfile.js

var gulp = require('gulp');
var gulpSourceMaps = require('gulp-sourcemaps'); // optional
var gulpData = require('gulp-data'); // optional
var gulpLibraryUmd = require('gulp-library-umd')

gulp.task('build', function () {
  return gulp.src('source.js')
    // optional per-file settings
    .pipe(gulpData(function (file) {
      return {};
    }))
    // optional source map support
    .pipe(gulpSourceMaps.init())
    // ...
    .pipe(gulpLibraryUmd({
      templateName: 'umd',
      require: {
        libA: 'libA',
        libB: {
          name: 'lib-b-default',
          amd: 'lib-b-amd',
          cjs: 'lib-b-cjs',
          node: 'lib-b-node',
          web: 'libBWeb'
        }
      },
      exports: 'result',
      namespace: 'myModuleGlobal'
    }))
    .pipe(gulpSourceMaps.write())
    .pipe(gulp.dest('dist'))
});

input: source.js

var result = {
  A: libA,
  B: libB,
  B: libC
};

result: dist/source.umd.js

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    
    define(["libA","lib-b-amd"], factory);
  } else if (typeof module === 'object' && module.exports) {
    // Node. Does not work with strict CommonJS, but
    // only CommonJS-like environments that support module.exports,
    // like Node.
    
    module.exports = factory(require("libA"), require("lib-b-node"));
  } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
    // CommonJs
    
    (function (results) {
      
      for (var key in results) {
        if ({}.hasOwnProperty.call(results, key))
          exports[key] = results[key];
      }
      
    })(factory(require("libA"), require("lib-b-cjs")));
  } else {
   // Browser globals
   
   root.myModuleGlobal = factory(root.libA, root.libBWeb);
  }
}(this, function (libA,libB) {
  var result = {
    A: libA,
    B: libB,
    B: libC
  };
  
  return result;
}));

//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNvdXJjZS5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0VBQUE7RUFDQTtFQUNBO0VBQ0E7RUFDQTtFQUNBIiwiZmlsZSI6InNvdXJjZS51bWQuanMiLCJzb3VyY2VSb290IjoiL3NvdXJjZS8iLCJzb3VyY2VzQ29udGVudCI6WyJ2YXIgcmVzdWx0ID0ge1xyXG4gIEE6IGxpYkEsXHJcbiAgQjogbGliQixcclxuICBCOiBsaWJDXHJcbn07XHJcbiJdfQ==

Options

nametypedefaultdescription
templateCacheBooleantrueCache compiled templates
templateNameStringundefinedName of the template to use. Overrides templatePath
templatePathStringundefinedFull file path of the template to use. Overrides template
templateStringundefineddoT.js template source to be compiled and used
templateFunctionundefinedfunction which takes a single argument, context, and returns a string
modesArray['cjs', 'node', 'amd', 'web']List of modes to support. Used when providing distinct libraries for each mode
indentBooleantrueIndent content according to the specified template
renameBooleantrueRename files to include the template name as an extension suffix on the basename. eg, source.js -> source.amd.js
requireObject{}variable name:library name id mapping of each required library
exportsStringexportsName of the variable to export. In CommonJs, only own properties of this variable will be exported.
exportsArrayList of names to exports. In most modes, these will be exported as an object literal with the given properties
namespaceStringundefinedName to use when exporting as a global. Used in web and global modes. If no namespace is provied, one will be generated from the filename

Specifying libraries

No libraries

gulpLibraryUmd({ })

Identical libraries for each mode

gulpLibraryUmd({ 
  requires: { 
    libA: 'library-a', 
    libB: 'library-b' 
  } 
}) 

Different libraries for each mode

gulpLibraryUmd({ 
  requires: { 
    libA: {
      name: 'library-a', // used as default name when no alternative is specified for the mode
      web : 'libraryA',  // web globals should be valid identifiers 
      amd : null         // AMD does not require this library
    }
  } 
})

Installation

npm install --save-dev gulp-library-data