fid-umd
Version:
Easily add UMD to your JavaScripts so they can be loaded with CommonJS, AMD (RequireJS), Rhino, node.js, module.js, in the browser and everywhere else.
1 lines • 8.77 kB
JavaScript
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;"undefined"!=typeof window?t=window:"undefined"!=typeof global?t=global:"undefined"!=typeof self&&(t=self),t.FidUmd=e()}}(function(){return function e(t,n,o){function i(s,d){if(!n[s]){if(!t[s]){var a="function"==typeof require&&require;if(!d&&a)return a(s,!0);if(r)return r(s,!0);var u=new Error("Cannot find module '"+s+"'");throw u.code="MODULE_NOT_FOUND",u}var c=n[s]={exports:{}};t[s][0].call(c.exports,function(e){var n=t[s][1][e];return i(n?n:e)},c,c.exports,e,t,n,o)}return n[s].exports}for(var r="function"==typeof require&&require,s=0;s<o.length;s++)i(o[s]);return i}({1:[function(e,t){"use strict";function n(){return this instanceof n?(this.debug=!1,this.depends=[],this.functionsNeeded={},this.global=!1,this.globalVariables={},this.jslint=!1,void(this.name="Unknown")):new n}n.prototype.configProperties={name:"string",debug:"boolean",jslint:"boolean",global:"boolean",depends:"array"},n.prototype.configModuleSystems=["amd","commonjs","modulejs","nodejs","root","yui"],n.prototype.configModuleSystemsAdditional=["commonjsmod"],n.prototype.dependsProperty=function(e){return this.depends.map(function(t){return t[e]})},n.prototype.expandDepends=function(e){function t(t){void 0!==e[t]&&(o[t]=e[t])}function n(e){var t;return t={name:e},i.configModuleSystems.forEach(function(n){t[n]=e}),i.configModuleSystemsAdditional.forEach(function(e){t[e]=""}),t}var o,i;return i=this,"string"==typeof e?n(e):(o=n(e.name?e.name:"Unknown"),this.configModuleSystems.forEach(t),this.configModuleSystemsAdditional.forEach(t),o)},n.prototype.exportConfig=function(){var e,t;return t=this,e={},Object.keys(this.configProperties).forEach(function(n){var o=t[n];if(o){switch(t.configProperties[n]){case"array":if(!Array.isArray(o)||!o.length)return;break;case"boolean":o=1;break;case"string":if("string"!=typeof o||!o.length)return}e[n]=o}}),e.depends&&(e.depends=e.depends.map(this.unexpandDepends.bind(this))),JSON.stringify(e)},n.prototype.loadConfig=function(e){var t;return t=this,Object.keys(this.configProperties).forEach(function(n){var o=e[n];switch(t.configProperties[n]){case"array":Array.isArray(o)&&o.length||(o=[]);break;case"boolean":o=!!o;break;case"string":"string"==typeof o&&o.length||(o="")}t[n]=o}),""===t.name&&(t.name="Unknown"),t.depends=t.depends.map(this.expandDepends.bind(this)),this},n.prototype.unexpandDepends=function(e){var t;return t={name:e.name},this.configModuleSystems.forEach(function(n){e[n]!==e.name&&(t[n]=e[n])}),this.configModuleSystemsAdditional.forEach(function(n){e[n]&&(t[n]=e[n])}),1===Object.keys(t).length?t.name:t},t.exports=n},{}],2:[function(e,t){"use strict";function n(e){return this instanceof n?(this.config=e,this.name="amd",this.depends=e.dependsProperty(this.name),e.functionsNeeded.isObject=!0,void(e.globalVariables.define=!0)):new n(e)}n.prototype.condition=function(){return"isObject(root.define) && root.define.amd"},n.prototype.loader=function(){var e;return e="root.define(name, [",this.depends.length&&(e+='"'+this.depends.join('", "')+'"'),e+="], factory);"},t.exports=n},{}],3:[function(e,t){"use strict";function n(e){return this instanceof n?(this.name="commonjs",this.config=e,this.config.globalVariables.exports=!0,this.dependsFile=e.dependsProperty(this.name),this.dependsModule=e.dependsProperty(this.name+"mod"),void(e.functionsNeeded.isObject=!0)):new n(e)}n.prototype.condition=function(){return"isObject(root.exports)"},n.prototype.loader=function(){var e,t;for(e="root.exports[name] = factory(",t=0;t<this.dependsFile.length;t+=1)t>0&&(e+=", "),e+="root.require("+JSON.stringify(this.dependsFile[t])+")",this.dependsModule[t]&&(e+="."+this.dependsModule[t]);return e+=");"},t.exports=n},{}],4:[function(e,t){"use strict";function n(e){return this instanceof n?(this.config=e,this.name="modulejs",this.depends=e.dependsProperty(this.name),e.functionsNeeded.isObject=!0,void(e.globalVariables.modulejs=!0)):new n(e)}n.prototype.condition=function(){return"isObject(root.modulejs)"},n.prototype.loader=function(){var e;return e="root.modulejs.define(name, ",this.depends.length&&(e+='["'+this.depends.join('", "')+'"], '),e+="factory);"},t.exports=n},{}],5:[function(e,t){"use strict";function n(e){return this instanceof n?(this.config=e,this.config.globalVariables.module=!0,this.name="nodejs",this.depends=e.dependsProperty(this.name),void(e.functionsNeeded.isObject=!0)):new n(e)}n.prototype.condition=function(){return"isObject(root.module) && isObject(root.module.exports)"},n.prototype.loader=function(){var e,t;for(e="root.module.exports = factory(",t=0;t<this.depends.length;t+=1)t>0&&(e+=", "),e+="root.require("+JSON.stringify(this.depends[t])+")";return e+=");"},t.exports=n},{}],6:[function(e,t){"use strict";function n(e){return this instanceof n?(this.config=e,this.name="root",void(this.depends=e.dependsProperty(this.name))):new n(e)}n.prototype.condition=function(){return""},n.prototype.loader=function(){var e;return e="root[name] = factory(",this.depends.length&&(e+="root."+this.depends.join(", root.")),e+=");"},t.exports=n},{}],7:[function(e,t){"use strict";function n(e){return this instanceof n?(this.config=e,this.name="yui",this.config.functionsNeeded.isObject=!0,this.config.globalVariables.YUI=!0,void(this.depends=e.dependsProperty(this.name))):new n(e)}n.prototype.condition=function(){var e;return e="isObject(root.YUI)"},n.prototype.loader=function(){var e;return e="root.YUI.add(name, function (Y) { Y[name] = factory(",this.depends.length&&(e+="Y."+this.depends.join(", Y.")),e+="); }",this.depends.length&&(e+=', "", { requires: ["'+this.depends.join('", "')+'"] }'),e+=");"},t.exports=n},{}],8:[function(e,t){"use strict";function n(){return this instanceof n?void 0:new n}var o,i;i=[e("./modules/nodejs"),e("./modules/commonjs"),e("./modules/amd"),e("./modules/modulejs"),e("./modules/yui"),e("./modules/root")],o=e("./config"),n.prototype.configureModuleSystems=function(e){return i.map(function(t){return new t(e)})},n.prototype.createPostamble=function(){return" // fid-umd post\n}));\n// fid-umd post-end\n"},n.prototype.createPreamble=function(e){var t,n,o,i,r;for(i=this.configureModuleSystems(e),r="// fid-umd "+e.exportConfig()+"\n",r+=this.jslintGlobalVariables(e),r+=e.global?"(function (name, root, factoryForGlobal) {\n":"(function (name, root, factory) {\n",e.jslint&&(r+=' "use strict";\n'),e.global&&(r+=this.createPreambleGlobalScope(e)),r+=this.writeNeededFunctions(e),e.debug&&(r+=' console.log("Attempting to define " + name);\n'),o=0;o<i.length;o+=1)r+=0===o?" ":" else ",t=i[o].condition(),t&&(r+="if ("+t+") "),r+="{\n",e.debug&&(r+=' console.log("'+i[o].name+' detected");\n'),r+=" ",r+=i[o].loader(),r+="\n",e.debug&&(r+=' console.log("'+i[o].name+' success");\n'),r+=" }";return n=e.dependsProperty("name").join(", "),r+="\n",r+="}("+JSON.stringify(e.name)+", this, function ("+n+") {\n",e.jslint&&(r+=' "use strict";\n'),r+=" // fid-umd end\n"},n.prototype.createPreambleGlobalScope=function(e){var t,n;return t=e.dependsProperty("name").join(", "),n=" function factory("+t+") { return factoryForGlobal.call(root",t&&(n+=", "+t),n+="); };\n"},n.prototype.detectConfig=function(e,t){var n,o,i;if(o=e.match(/\/\/ fid-umd (\{.*\})/),n=new t,o)try{i=JSON.parse(o[1]),n.loadConfig(i)}catch(r){throw new Error("Invalid JSON: "+o[1])}return n},n.prototype.jslintGlobalVariables=function(e){var t,n;t=[];for(n in e.globalVariables)e.globalVariables.hasOwnProperty(n)&&t.push(n);return e.jslint&&t.length?"/* global "+t.join(", ")+" */\n":""},n.prototype.splitCode=function(e,t,n){var o,i;return i=[e,""],(o=e.match(t))?(i[0]=e.substr(0,o.index),i[1]=e.substr(o.index+o[0].length),o=i[1].match(n),o&&(i[1]=i[1].substr(o.index+o[0].length)),i):i},n.prototype.update=function(e){var t,n;return e||(e=""),e=e.toString(),t=this.detectConfig(e,o),n=e,n=this.updatePreamble(n,t),n=this.updatePostamble(n)},n.prototype.updatePostamble=function(e){var t,n;return n=this.splitCode(e,/^[\t ]*\/\/ fid-umd post\n?/m,/[\t ]*\/\/ fid-umd post-end\n?/m),t=n[0],"\n"!==t.substr(-1)&&(t+="\n"),t+=this.createPostamble()+n[1]},n.prototype.updatePreamble=function(e,t){var n,o;return o=this.splitCode(e,/^[ \t]*\/\/ fid-umd \{.*\n?/m,/^[ \t]*\/\/ fid-umd end\n?/m),""===o[1]&&(o[1]=o[0],o[0]=""),n=o[0]+this.createPreamble(t)+o[1]},n.prototype.writeNeededFunctions=function(e){var t;return t="",e.functionsNeeded.isObject&&(t+=' function isObject(x) { return typeof x === "object"; }\n'),t},t.exports=n},{"./config":1,"./modules/amd":2,"./modules/commonjs":3,"./modules/modulejs":4,"./modules/nodejs":5,"./modules/root":6,"./modules/yui":7}]},{},[8])(8)});