diff --git a/README.md b/README.md index ba9b683..6b596da 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,20 @@ require('foo/bar/baz') // => 'hi'; require('foo') === require('foo/bar/baz'); ``` + + +### `define.default('old/path', 'new-name')` + +`define.default` is short-hand for `export { default } from 'foo';` + +```js +define('foo', [], () => 'hi'); +define.default('foo', 'foo/bar/baz'); + +require('foo/bar/baz') // => 'hi'; +require('foo') === require('foo/bar/baz'); +``` + ### `require('require')` When within a module, one can require `require`. This provides a `require` scoped to the current module. Enabling dynamic, relatively path requires. diff --git a/lib/loader/loader.js b/lib/loader/loader.js index deb9973..c8df336 100644 --- a/lib/loader/loader.js +++ b/lib/loader/loader.js @@ -265,8 +265,9 @@ var loader, define, requireModule, require, requirejs; // we don't support all of AMD // define.amd = {}; - function Alias(id) { + function Alias(id, prop) { this.id = id; + this.prop = prop; } define.alias = function(id, target) { @@ -277,6 +278,16 @@ var loader, define, requireModule, require, requirejs; return new Alias(id); }; + define.default = function(id, target) { + define(id, ['exports', target], function(exports, targetExports) { + Object.defineProperty(exports, 'default', { + get: function() { + return targetExports.default; + } + }); + }); + }; + function missingModule(id, referrer) { throw new Error('Could not find module `' + id + '` imported from `' + referrer + '`'); } diff --git a/tests/all.js b/tests/all.js index 725ab3b..d4422c4 100644 --- a/tests/all.js +++ b/tests/all.js @@ -1260,6 +1260,43 @@ test('alias entries share same module instance', function() { }); }); +test('define.default with default and other exports', function() { + var count = 0; + define.default('to', 'from'); + define('from', ['exports'], function(exports) { + count++; + + exports.default = 'YUP'; + exports.other = 'NOPE'; + }); + + equal(count, 0); + var result = require('to'); + equal(count, 1); + equal(result.default, 'YUP'); + deepEqual(require('from'), { + default: 'YUP', + other: 'NOPE' + }); + + equal(count, 1, 'second require should use existing instance'); + + var stats = statsForMonitor('loaderjs', tree); + + deepEqual(stats, { + define: 2, + exports: 2, + findDeps: 2, + findModule: 3, + modules: 2, + pendingQueueLength: 2, + reify: 2, + require: 2, + resolve: 1, + resolveRelative: 0 + }); +}); + test('alias with 2 arguments entries share same module instance', function() { var count = 0; define.alias('foo/index', 'bar');