1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | 1 1 1 1 1 4 4 4 6 5 5 4 1 5 4 3 5 1 2 2 2 2 1 1 1 2 1 10 1 22 22 22 22 22 22 22 22 22 22 9 6 6 3 6 6 9 22 18 13 11 1 21 21 21 | 'use strict'; var through = require('through'); var path = require('path'); var gutil = require('gulp-util'); var _ = require('lodash'); function makeAMD(moduleContents, opts) { // define(['dependency'], function(Dependency) { return moduleObject; }); var includes = []; var defines = []; _.each(opts.require, function(include, define) { if (include !== null) { includes.push(JSON.stringify(include)); defines.push(define); } }); return 'define([' + includes.join(',') + '], ' + 'function(' + defines.join(',') + ') { return ' + moduleContents + '; });'; } function makeCommonJS(moduleContents, opts) { // var Dependency = require('dependency');module.exports = moduleObject; var requires = _.map(opts.require, function(key, value) { if (key !== null) { return 'var ' + value + ' = require(' + JSON.stringify(key) + ');'; } }); return requires.join('') + 'module.exports = ' + moduleContents + ';'; } function makeHybrid(moduleContents, opts) { // (function(definition) { if (typeof exports === 'object') { module.exports = definition(require('library')); } // else if (typeof define === 'function' && define.amd) { define(['library'], definition); } else { definition(Library); } // })(function(Library) { return moduleObject; }); var includes = []; var requires = []; var defines = []; _.each(opts.require, function(include, define) { includes.push(JSON.stringify(include)); requires.push('require(' + JSON.stringify(include) + ')'); defines.push(define); }); return '(function(definition) { ' + 'if (typeof exports === \'object\') { module.exports = definition(' + requires.join(',') + '); } ' + 'else if (typeof define === \'function\' && define.amd) { define([' + includes.join(',') + '], definition); } ' + 'else { definition(' + defines.join(',') + '); } ' + '})(function(' + defines.join(',') + ') { return ' + moduleContents + '; });'; } function makePlain(moduleContents, opts) { // moduleObject; return moduleContents + ';'; } module.exports = function(type, options) { return through(function(file) { Iif (file.isNull()) { return this.queue(file); } // pass along Iif (file.isStream()) { return this.emit('error', new gutil.PluginError('gulp-define-module', 'Streaming not supported')); } var opts = _.defaults({}, options, file.defineModuleOptions); opts.context = _([file.defineModuleOptions, options]) .filter(_.identity).pluck('context') .filter(_.identity).value(); opts.require = _.merge.apply(null, _([file.defineModuleOptions, options, { require: {} }]) .filter(_.identity).pluck('require') .filter(_.identity).value()); var contents = file.contents.toString(); var name = path.basename(file.path, path.extname(file.path)); var context = { name: name, file: file, contents: contents }; if (opts.wrapper) { opts.context.forEach(function(extensions) { Iif (!extensions) { return; } if (typeof extensions === 'function') { extensions = extensions(context); } _.merge(context, _(extensions).map(function(value, key) { return [key, _.template(value)(context)]; }).object().value()); }); contents = _.template(opts.wrapper)(context); } if (type === 'amd') { contents = makeAMD(contents, opts); } else if (type === 'commonjs' || type === 'node') { contents = makeCommonJS(contents, opts); } else if (type === 'hybrid') { contents = makeHybrid(contents, opts); } else if (type === 'plain') { contents = makePlain(contents, opts); } else { throw new Error('Unsupported module type for gulp-define-module: ' + type); } file.path = gutil.replaceExtension(file.path, '.js'); file.contents = new Buffer(contents); this.queue(file); }); }; |