gulp-iife
This small gulp plugin wraps your code in IIFE to distribute on three platforms: AMD, CommonJS and Browser environment.
Installation
npm i gulp-iife-wrap
Status
Currently it is well supported scripts written in CoffeeScript. JavaScript is supported too but it is buggy.
Usage
Assume you have some module definition:
Module = : -> : -> : ->
You would like to distribute this module on browser, AMD, and CommonJS environment. To do this you need to wrap your code with IIFE.
gulp = require'gulp'iife = require'gulp-iife-wrap' # Define dependencies for your module. Here we will define two dependencies: lodash and jquery. dependencies = // in AMD/CommonJS: require'lodash' // in browser: window_ // argument name in factory: _ require: 'lodash'global: '_' // in AMD/CommonJS: require'jquery' // in browser: window$ // argument name in factory: $ require: 'jquery'global: '$'; # Here we specify how our module will be named in browser environment. # In other words here we specify property name which users will use to access module in browsers: window.Module global = 'Module'; gulptask 'build'-> gulpsrc'source/module.coffee' pipe iife globaldependencies pipe gulpdest'build'
After running gulp we will have this code:
# Browser and WebWorker root = if typeof self is 'object' and self?self is self self # Server else if typeof global is 'object' and global?global is global global # AMD if typeof define is 'function' and defineamd define 'lodash''jquery''exports' root.Module = factoryroot_$ # CommonJS else if typeof module is 'object' and module isnt null and moduleexports? and typeof moduleexports is 'object' module.exports = factoryrootrequire'lodash'require'jquery' # Browser and the rest else root.Module = factoryrootroot_root$ # No return value return Module = : -> : -> : ->