From 653988b70bc12375de801f284485e134bb361c97 Mon Sep 17 00:00:00 2001 From: Bit Planets Date: Fri, 5 Dec 2014 10:22:11 +0000 Subject: [PATCH 1/4] Fix #6 Now you can also return value from define function like can be done with require.js --- lib/RequireContext.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/RequireContext.js b/lib/RequireContext.js index 24ed035..b523223 100644 --- a/lib/RequireContext.js +++ b/lib/RequireContext.js @@ -251,10 +251,16 @@ RequireContext.prototype.theDefine = function theDefine(dependencies, fn, arg3) parent.exports = fn; } else { fn = dependencies; - if(typeof fn == "function") - fn(reqFn, parent.exports, parent); - else + if(typeof fn == "function"){ + var fnReturnExports = fn(reqFn, parent.exports, parent); + // If nothing has been added to the parent.exports then replace with the + // return value of the function. + if(Object.keys(parent.exports).length === 0){ + parent.exports = fnReturnExports; + } + }else{ parent.exports = fn; + } } } From 819fe70716b106b3fb29f83bfd499294d5033937 Mon Sep 17 00:00:00 2001 From: Bit Planets Date: Fri, 5 Dec 2014 14:36:14 +0000 Subject: [PATCH 2/4] Added tests issue #6 --- test/commonjs.js | 14 +++++++++++++- test/fixtures/commonjs/wrappedReturn.js | 5 +++++ .../commonjs/wrappedReturnAndSetExports.js | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/commonjs/wrappedReturn.js create mode 100644 test/fixtures/commonjs/wrappedReturnAndSetExports.js diff --git a/test/commonjs.js b/test/commonjs.js index df85629..eb1294d 100644 --- a/test/commonjs.js +++ b/test/commonjs.js @@ -77,4 +77,16 @@ describe("commonjs", function() { fs.should.be.equal(require("fs")); }); -}); \ No newline at end of file + it("should define a module by returning a value from a define wrapper", function(done) { + var data = req('./fixtures/commonjs/wrappedReturn'); + data.a.should.be.equal(1); + done(); + }); + + it("should give priority to 'module.exports' or 'exports' instead of the value returned", function(done) { + var data = req('./fixtures/commonjs/wrappedReturnAndSetExports'); + data.a.should.be.equal(1); + done(); + }); + +}); diff --git a/test/fixtures/commonjs/wrappedReturn.js b/test/fixtures/commonjs/wrappedReturn.js new file mode 100644 index 0000000..13b4e63 --- /dev/null +++ b/test/fixtures/commonjs/wrappedReturn.js @@ -0,0 +1,5 @@ +define(function(require, exports, module){ + return { + a: 1 + } +}); diff --git a/test/fixtures/commonjs/wrappedReturnAndSetExports.js b/test/fixtures/commonjs/wrappedReturnAndSetExports.js new file mode 100644 index 0000000..ee07617 --- /dev/null +++ b/test/fixtures/commonjs/wrappedReturnAndSetExports.js @@ -0,0 +1,6 @@ +define(function(require, exports, module){ + exports.a = 1; + return { + a: 2 + } +}); From aa75ab71ede7d923093eeaaa7eb4473dd2fe2c34 Mon Sep 17 00:00:00 2001 From: Bit Planets Date: Fri, 5 Dec 2014 16:46:53 +0000 Subject: [PATCH 3/4] Return an object if return is undefined; Also accepts falsy (false, 0) values to be returned; And their relative tests; --- lib/RequireContext.js | 3 ++- test/commonjs.js | 13 +++++++++++++ test/fixtures/commonjs/wrappedEmpty.js | 2 ++ test/fixtures/commonjs/wrappedFalsy.js | 3 +++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/commonjs/wrappedEmpty.js create mode 100644 test/fixtures/commonjs/wrappedFalsy.js diff --git a/lib/RequireContext.js b/lib/RequireContext.js index b523223..d79b8e5 100644 --- a/lib/RequireContext.js +++ b/lib/RequireContext.js @@ -256,7 +256,8 @@ RequireContext.prototype.theDefine = function theDefine(dependencies, fn, arg3) // If nothing has been added to the parent.exports then replace with the // return value of the function. if(Object.keys(parent.exports).length === 0){ - parent.exports = fnReturnExports; + // You can return 0, false and other falsy values. + parent.exports = fnReturnExports === void 0 ? {} : fnReturnExports; } }else{ parent.exports = fn; diff --git a/test/commonjs.js b/test/commonjs.js index eb1294d..58cee1d 100644 --- a/test/commonjs.js +++ b/test/commonjs.js @@ -89,4 +89,17 @@ describe("commonjs", function() { done(); }); + it("should return an empty object if nothing is returned or set on 'module.exports' or 'exports'", function(done) { + var data = req('./fixtures/commonjs/wrappedEmpty'); + data.should.be.a("object"); + done(); + }); + + it("should be able to return a falsy value", function(done) { + var data = req('./fixtures/commonjs/wrappedFalsy'); + data.should.be.equal(false); + done(); + }); + + }); diff --git a/test/fixtures/commonjs/wrappedEmpty.js b/test/fixtures/commonjs/wrappedEmpty.js new file mode 100644 index 0000000..11a3cc7 --- /dev/null +++ b/test/fixtures/commonjs/wrappedEmpty.js @@ -0,0 +1,2 @@ +define(function(require, exports, module){ +}); diff --git a/test/fixtures/commonjs/wrappedFalsy.js b/test/fixtures/commonjs/wrappedFalsy.js new file mode 100644 index 0000000..a4b6f1b --- /dev/null +++ b/test/fixtures/commonjs/wrappedFalsy.js @@ -0,0 +1,3 @@ +define(function(require, exports, module){ + return false; +}); From d4512978c46f19b512e53cd0e1f621bf84ae6150 Mon Sep 17 00:00:00 2001 From: Bit Planets Date: Mon, 15 Dec 2014 12:01:25 +0000 Subject: [PATCH 4/4] Tests wrapped modules with module.exports. --- lib/RequireContext.js | 12 +++++------- test/commonjs.js | 14 ++++++++++++++ .../commonjs/wrappedModuleExportsUndefined.js | 3 +++ .../fixtures/commonjs/wrappedModuleExportsValue.js | 3 +++ test/fixtures/hot/counter-value.js | 1 + 5 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/commonjs/wrappedModuleExportsUndefined.js create mode 100644 test/fixtures/commonjs/wrappedModuleExportsValue.js create mode 100644 test/fixtures/hot/counter-value.js diff --git a/lib/RequireContext.js b/lib/RequireContext.js index d79b8e5..428569e 100644 --- a/lib/RequireContext.js +++ b/lib/RequireContext.js @@ -252,13 +252,11 @@ RequireContext.prototype.theDefine = function theDefine(dependencies, fn, arg3) } else { fn = dependencies; if(typeof fn == "function"){ - var fnReturnExports = fn(reqFn, parent.exports, parent); - // If nothing has been added to the parent.exports then replace with the - // return value of the function. - if(Object.keys(parent.exports).length === 0){ - // You can return 0, false and other falsy values. - parent.exports = fnReturnExports === void 0 ? {} : fnReturnExports; - } + var orignalExports = parent.exports; + var fnReturnExports = fn(reqFn, parent.exports, parent); + if(orignalExports === parent.exports && Object.keys(orignalExports).length === 0 && fnReturnExports !== undefined) { + parent.exports = fnReturnExports; + } }else{ parent.exports = fn; } diff --git a/test/commonjs.js b/test/commonjs.js index 58cee1d..287dd5e 100644 --- a/test/commonjs.js +++ b/test/commonjs.js @@ -101,5 +101,19 @@ describe("commonjs", function() { done(); }); + it("should return an empty object when returns undefined", function(done) { + var data = req('./fixtures/commonjs/wrappedModuleExportsUndefined'); + should.not.exist(data); + done(); + }); + + it("should define a module with exports when wrapped", function(done) { + var data = req('./fixtures/commonjs/wrappedModuleExportsValue'); + data.test.should.be.equal(1); + done(); + }); + + + }); diff --git a/test/fixtures/commonjs/wrappedModuleExportsUndefined.js b/test/fixtures/commonjs/wrappedModuleExportsUndefined.js new file mode 100644 index 0000000..50e52a1 --- /dev/null +++ b/test/fixtures/commonjs/wrappedModuleExportsUndefined.js @@ -0,0 +1,3 @@ +define(function(require, exports, module) { + module.exports = undefined; +}); diff --git a/test/fixtures/commonjs/wrappedModuleExportsValue.js b/test/fixtures/commonjs/wrappedModuleExportsValue.js new file mode 100644 index 0000000..1336dc5 --- /dev/null +++ b/test/fixtures/commonjs/wrappedModuleExportsValue.js @@ -0,0 +1,3 @@ +define(function(require, exports, module) { + module.exports = {test: 1} +}); diff --git a/test/fixtures/hot/counter-value.js b/test/fixtures/hot/counter-value.js new file mode 100644 index 0000000..944faf1 --- /dev/null +++ b/test/fixtures/hot/counter-value.js @@ -0,0 +1 @@ +module.exports = 3 \ No newline at end of file