Comparing version 0.8.3 to 0.8.4
{ | ||
"name": "udefine", | ||
"version": "0.8.3", | ||
"version": "0.8.4", | ||
"homepage": "https://github.com/freezedev/udefine", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -13,3 +13,3 @@ (function() { | ||
hasModule = (typeof module !== "undefined" && module !== null) && (module.exports != null); | ||
hasModule = !!((typeof module !== "undefined" && module !== null) && module.exports); | ||
@@ -52,3 +52,3 @@ exportObject = {}; | ||
udefine = function(name, deps, factory) { | ||
var dep, depArr, exportable, injectName, injectObject, injectRoot, result, _ref; | ||
var dep, depArr, ignoreName, injectName, injectObject, injectRoot, result, _ref; | ||
@@ -58,2 +58,5 @@ if (name == null) { | ||
} | ||
if (name !== name.toLowerCase()) { | ||
console.warn('A module should be all lowercase'); | ||
} | ||
if (typeof deps === 'function' || isObject(deps)) { | ||
@@ -78,5 +81,2 @@ _ref = [name, [], deps], name = _ref[0], deps = _ref[1], factory = _ref[2]; | ||
result = resolveModule(factory, depArr); | ||
if (hasModule) { | ||
module.exports = result; | ||
} | ||
if (!Object.hasOwnProperty.call(udefine.modules[platform], name)) { | ||
@@ -86,18 +86,29 @@ udefine.modules[platform][name] = result; | ||
} | ||
if (Object.hasOwnProperty.call(udefine.inject.modules, name)) { | ||
injectObject = udefine.inject.modules[name]; | ||
injectRoot = injectObject.root, injectName = injectObject.name, exportable = injectObject.exportable; | ||
udefine.inject(injectRoot, injectName)(result); | ||
if (hasModule) { | ||
if (exportable || exportable === 'all') { | ||
module.exports = result; | ||
if (!Object.hasOwnProperty.call(udefine.inject.modules, name)) { | ||
if (udefine.autoInject) { | ||
if (udefine.env.globals) { | ||
udefine.inject.add(name, { | ||
root: root, | ||
name: name | ||
}); | ||
} | ||
if (exportable === 'partial') { | ||
exports[injectName] = result; | ||
} | ||
/* | ||
if udefine.env.commonjs | ||
udefine.inject.add name, | ||
root: module.exports | ||
name: name | ||
ignoreName: true | ||
*/ | ||
} | ||
} | ||
if (Object.hasOwnProperty.call(udefine.inject.modules, name)) { | ||
injectObject = udefine.inject.modules[name]; | ||
injectRoot = injectObject.root, injectName = injectObject.name, ignoreName = injectObject.ignoreName; | ||
udefine.inject(injectRoot, injectName, ignoreName)(result); | ||
} | ||
return result; | ||
}; | ||
udefine.inject = function(obj, name) { | ||
udefine.autoInject = true; | ||
udefine.inject = function(obj, name, ignoreName) { | ||
return function(res) { | ||
@@ -107,3 +118,7 @@ if (!((obj != null) && (name != null))) { | ||
} | ||
return obj[name] = res; | ||
if (ignoreName) { | ||
return obj = res; | ||
} else { | ||
return obj[name] = res; | ||
} | ||
}; | ||
@@ -110,0 +125,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
/*! udefine - v0.8.2 - 23-09-2013 */ | ||
(function(){"use strict";var e,n,o;(function(){var e;return null!=(e=Array.isArray)?e:Array.isArray=function(e){return e.push===Array.prototype.push&&null!=e.length}})(),n="undefined"!=typeof module&&null!==module&&null!=module.exports,e={},o=function(e){return"object"==typeof e&&!Array.isArray(e)},function(e){var t,r,u,l,i;return u=["commonjs","globals"],r=n?"commonjs":"globals",l=function(e,n){return"function"==typeof e?e.apply(this,n):e},t=function(e,o){var t,r;return n&&"string"==typeof i.modules[o][e]?(t=require("path"),r=function(){return i.paths[o].base?i.paths[o].base:""}(),require(t.join(process.cwd(),r,i.modules[o][e]))):i.modules[o][e]},i=function(e,u,s){var d,f,a,c,m,p,h,y;if(null==e)throw Error("A udefine module needs to have a name");return("function"==typeof u||o(u))&&(y=[e,[],u],e=y[0],u=y[1],s=y[2]),"undefined"!=typeof define&&null!==define?(define.amd||define.umd)&&(h=define.apply(this,arguments)):(f=function(){var e,n,o;for(o=[],e=0,n=u.length;n>e;e++)d=u[e],o.push(t(d,r));return o}(),h=l(s,f),n&&(module.exports=h),Object.hasOwnProperty.call(i.modules[r],e)||(i.modules[r][e]=h)),Object.hasOwnProperty.call(i.inject.modules,e)&&(m=i.inject.modules[e],p=m.root,c=m.name,a=m.exportable,i.inject(p,c)(h),n&&((a||"all"===a)&&(module.exports=h),"partial"===a&&(exports[c]=h))),h},i.inject=function(e,n){return function(o){return null!=e&&null!=n?e[n]=o:void 0}},i.inject.modules={},i.inject.add=function(n,o){return null!=n?(null==o&&(o={}),null==o.root&&(o.root=e),null==o.name&&(o.name=n),i.inject.modules[n]=o,this):void 0},i.inject.remove=function(e){return delete i.inject.modules[e],this},i.inject.clear=function(){return i.inject.modules={},this},i.modules={globals:{},commonjs:{},add:function(e,n){var o,t,u,l,s;if("object"==typeof e)for(o in e)u=e[o],this.add(o,u);else if(n)if(Array.isArray(n))for(l=0,s=n.length;s>l;l++)t=n[l],i.modules[t][e]=void 0;else i.modules.set(e,n);else i.modules[r][e]=void 0;return this},remove:function(e){var n,o,t;for(o=0,t=u.length;t>o;o++)n=u[o],Object.hasOwnProperty.call(i.modules[n],e)&&delete i.modules[n][e];return this},get:function(e){return i.modules[r][e]},set:function(e,n){var o,t;if("object"==typeof n)for(o in n)t=n[o],i.modules[o][e]=t;else i.modules[r][e]=n;return this},clear:function(){var e,n,o;for(n=0,o=u.length;o>n;n++)e=u[n],i.modules[e]={};return this}},i.env||(i.env={amd:function(){return"undefined"!=typeof define&&null!==define&&(define.amd||define.umd)}(),commonjs:n,browser:!n,globals:!n&&!i.amd}),i.paths={commonjs:{base:void 0}},i.defaultConfig=function(){return i.modules.commonjs.root=e,null!=e.define?define("root",function(){return e}):void 0},i.defaultConfig(),i.configure=function(n){return n.apply(i,[e])},n?module.exports=i:e.udefine=i}(n?{}:this)}).call(this); | ||
/*! udefine - v0.8.3 - 25-09-2013 */ | ||
(function(){"use strict";var e,n,o;(function(){var e;return null!=(e=Array.isArray)?e:Array.isArray=function(e){return e.push===Array.prototype.push&&null!=e.length}})(),n=!("undefined"==typeof module||null===module||!module.exports),e={},o=function(e){return"object"==typeof e&&!Array.isArray(e)},function(e){var t,r,u,l,i;return u=["commonjs","globals"],r=n?"commonjs":"globals",l=function(e,n){return"function"==typeof e?e.apply(this,n):e},t=function(e,o){var t,r;return n&&"string"==typeof i.modules[o][e]?(t=require("path"),r=function(){return i.paths[o].base?i.paths[o].base:""}(),require(t.join(process.cwd(),r,i.modules[o][e]))):i.modules[o][e]},i=function(n,u,s){var d,a,c,f,m,h,p,j;if(null==n)throw Error("A udefine module needs to have a name");return n!==n.toLowerCase()&&console.warn("A module should be all lowercase"),("function"==typeof u||o(u))&&(j=[n,[],u],n=j[0],u=j[1],s=j[2]),"undefined"!=typeof define&&null!==define?(define.amd||define.umd)&&(p=define.apply(this,arguments)):(a=function(){var e,n,o;for(o=[],e=0,n=u.length;n>e;e++)d=u[e],o.push(t(d,r));return o}(),p=l(s,a),Object.hasOwnProperty.call(i.modules[r],n)||(i.modules[r][n]=p)),Object.hasOwnProperty.call(i.inject.modules,n)||i.autoInject&&i.env.globals&&i.inject.add(n,{root:e,name:n}),Object.hasOwnProperty.call(i.inject.modules,n)&&(m=i.inject.modules[n],h=m.root,f=m.name,c=m.ignoreName,i.inject(h,f,c)(p)),p},i.autoInject=!0,i.inject=function(e,n,o){return function(t){return null!=e&&null!=n?o?e=t:e[n]=t:void 0}},i.inject.modules={},i.inject.add=function(n,o){return null!=n?(null==o&&(o={}),null==o.root&&(o.root=e),null==o.name&&(o.name=n),i.inject.modules[n]=o,this):void 0},i.inject.remove=function(e){return delete i.inject.modules[e],this},i.inject.clear=function(){return i.inject.modules={},this},i.modules={globals:{},commonjs:{},add:function(e,n){var o,t,u,l,s;if("object"==typeof e)for(o in e)u=e[o],this.add(o,u);else if(n)if(Array.isArray(n))for(l=0,s=n.length;s>l;l++)t=n[l],i.modules[t][e]=void 0;else i.modules.set(e,n);else i.modules[r][e]=void 0;return this},remove:function(e){var n,o,t;for(o=0,t=u.length;t>o;o++)n=u[o],Object.hasOwnProperty.call(i.modules[n],e)&&delete i.modules[n][e];return this},get:function(e){return i.modules[r][e]},set:function(e,n){var o,t;if("object"==typeof n)for(o in n)t=n[o],i.modules[o][e]=t;else i.modules[r][e]=n;return this},clear:function(){var e,n,o;for(n=0,o=u.length;o>n;n++)e=u[n],i.modules[e]={};return this}},i.env||(i.env={amd:function(){return"undefined"!=typeof define&&null!==define&&(define.amd||define.umd)}(),commonjs:n,browser:!n,globals:!n&&!i.amd}),i.paths={commonjs:{base:void 0}},i.defaultConfig=function(){return i.modules.commonjs.root=e,null!=e.define?define("root",function(){return e}):void 0},i.defaultConfig(),i.configure=function(n){return n.apply(i,[e])},n?module.exports=i:e.udefine=i}(n?{}:this)}).call(this); |
{ | ||
"name": "udefine", | ||
"description": "Define an UMD module like an AMD module and use it (almost) anywhere", | ||
"version": "0.8.3", | ||
"version": "0.8.4", | ||
"main": "dist/udefine.js", | ||
@@ -6,0 +6,0 @@ "devDependencies": { |
@@ -9,9 +9,12 @@ udefine | ||
**Why would you need it?** | ||
- You are writing that needs to available in Node.js, AMD and/or as global objects | ||
- You need some kind of basic dependency resolution for your modules | ||
**Features** | ||
- Provides a handy function wrapper for UMD modules | ||
- Same fuction signature as AMD modules | ||
- No need for boilerplate definitions | ||
- Less than 0.6 kB minified and gzipped (might increase a bit, but I'll keep it under 1kb) | ||
- Provides a handy function wrapper for UMD modules | ||
- Same fuction signature as AMD modules | ||
- No need for boilerplate definitions | ||
- Less than 0.6 kB minified and gzipped (might increase a bit, but I'll keep it under 1kb) | ||
@@ -39,8 +42,8 @@ **Usage** | ||
```javascript | ||
(function(root) { | ||
root.udefine.inject['myothermodule'] = { | ||
udefine.configure(function(root) { | ||
udefine.inject['myothermodule'] = { | ||
root: root, | ||
name: 'myOtherModule' | ||
}; | ||
})(this); | ||
}; | ||
}); | ||
@@ -58,3 +61,36 @@ udefine('myothermodule', function() { | ||
**CommonJS pitfalls** | ||
If using `module.exports` for exporting your modules on CommonJS, it does not | ||
export correctly on udefine 0.8.x. | ||
(Exporting with `exports` using the injection API works though.) | ||
To counteract this behavior, you would need a bit of boilerplate code: | ||
```javascript | ||
(function() { | ||
udefine.configure(function(root) { | ||
if (udefine.env.globals) { | ||
udefine.inject['mycoolmodule'] = { | ||
root: root, | ||
name: 'myCoolModule' | ||
}; | ||
} | ||
}); | ||
var modResult = udefine('mycoolmodule', function() { | ||
return { | ||
a: function() { return 5; }, | ||
b: 2 | ||
}; | ||
}); | ||
if (udefine.env.commonjs) { | ||
module.exports = modResult; | ||
} | ||
}).call(this); | ||
// root.myOtherModule now is an object with the properties a and b. | ||
// (In a non-AMD or non-CommonJS environment.) | ||
``` | ||
udefine is not and does not replace a module loader. It is primarily intended for | ||
@@ -61,0 +97,0 @@ developers who want their library to target AMD modules, CommonJS modules and/or |
@@ -33,2 +33,6 @@ (function() { | ||
}); | ||
it('udefine has a property .autoInject', function() { | ||
expect(udefine).to.have.property('autoInject'); | ||
return expect(udefine.autoInject).to.be.a('boolean'); | ||
}); | ||
it('udefine has module property', function() { | ||
@@ -35,0 +39,0 @@ expect(udefine).to.have.property('modules'); |
(function() { | ||
var expect, udefineFunction; | ||
var expect, udefineFunction, udefineGlobal; | ||
udefineFunction = require('../../dist/udefine'); | ||
require('../../dist/global'); | ||
udefineGlobal = require('../../dist/global'); | ||
@@ -14,2 +14,6 @@ expect = require('chai').expect; | ||
}); | ||
it('return value of required global udefine is empty oject', function() { | ||
expect(udefineGlobal).to.be.a('object'); | ||
return expect(udefineGlobal).to.be.empty; | ||
}); | ||
it('udefine can be called without global prefix', function() { | ||
@@ -16,0 +20,0 @@ return expect(udefine).to.be.a('function'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
63657
42
734
100