Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/RequireContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,15 @@ 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 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;
}
}
}

Expand Down
41 changes: 40 additions & 1 deletion test/commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,43 @@ describe("commonjs", function() {
fs.should.be.equal(require("fs"));
});

});
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();
});

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();
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And tests for these modules:

define(function(require, exports, module) {
  function Test() {}
  module.exports = new Test();
});

define(function(require, exports, module) {
  module.exports = undefined;
});

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();
});




});
2 changes: 2 additions & 0 deletions test/fixtures/commonjs/wrappedEmpty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
define(function(require, exports, module){
});
3 changes: 3 additions & 0 deletions test/fixtures/commonjs/wrappedFalsy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
define(function(require, exports, module){
return false;
});
3 changes: 3 additions & 0 deletions test/fixtures/commonjs/wrappedModuleExportsUndefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
define(function(require, exports, module) {
module.exports = undefined;
});
3 changes: 3 additions & 0 deletions test/fixtures/commonjs/wrappedModuleExportsValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
define(function(require, exports, module) {
module.exports = {test: 1}
});
5 changes: 5 additions & 0 deletions test/fixtures/commonjs/wrappedReturn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
define(function(require, exports, module){
return {
a: 1
}
});
6 changes: 6 additions & 0 deletions test/fixtures/commonjs/wrappedReturnAndSetExports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
define(function(require, exports, module){
exports.a = 1;
return {
a: 2
}
});
1 change: 1 addition & 0 deletions test/fixtures/hot/counter-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 3