Skip to content

Commit

Permalink
Create official loader module API.
Browse files Browse the repository at this point in the history
Provides an internal `loader` module with the following named exports:

* `getIds` - Returns an array containing the `id`'s of all registered
  modules.
* `has` - Returns `true` or `false` if a given module exists.
* `define` - Defines a new module.
* `require` - Requires a module (optionally relative to the current module).
* `resolve` - Returns the expanded module name given a relative path.
  • Loading branch information
rwjblue committed Apr 18, 2017
1 parent d247367 commit cbc5da9
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

var shouldUseInstrumentedBuild = require('./utils').shouldUseInstrumentedBuild;

module.exports = {
Expand Down
32 changes: 32 additions & 0 deletions lib/loader/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,36 @@ var loader, define, requireModule, require, requirejs;

var defaultDeps = ['require', 'exports', 'module'];

class LoaderModule {
constructor(source) {
this.source = source;
}

default(dep) {
return this.require(dep);
}

define(name, deps, callback) {
return define(name, deps, callback);
}

require(dep) {
return require(resolve(dep, this.source));
}

has(dep) {
return has(resolve(dep, this.source));
}

resolve(dep) {
return resolve(dep, this.source);
}

getIds() {
return Object.keys(registry);
}
}

function Module(name, deps, callback, alias) {
heimdall.increment(modules);
this.id = uuid++;
Expand Down Expand Up @@ -196,6 +226,8 @@ var loader, define, requireModule, require, requirejs;
entry.exports = this.module.exports;
} else if (dep === 'require') {
entry.exports = this.makeRequire();
} else if (dep === 'loader') {
entry.exports = new LoaderModule(this.name);
} else if (dep === 'module') {
entry.exports = this.module;
} else {
Expand Down
155 changes: 155 additions & 0 deletions tests/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -1647,3 +1647,158 @@ test('redefining a module when "finalized" should no-op', function(assert) {
require('foo');
assert.notOk(second, 'second module definition never used');
});

test('loader module API: require', function() {
define('foo/baz/index', function () {
return 'I AM baz';
});

define('foo/index', ['loader'], function (loader) {
return loader.require('./baz');
});

equal(require('foo'), 'I AM baz');

var stats = statsForMonitor('loaderjs', tree);

deepEqual(stats, {
findDeps: 2,
define: 2,
exports: 2,
findModule: 2,
modules: 2,
reify: 2,
require: 2,
resolve: 1,
resolveRelative: 1,
pendingQueueLength: 2
});
});

test('loader module API: has', function() {
define('foo/baz/index', function () {
return 'I AM baz';
});

define('foo/index', ['loader'], function (loader) {
if (loader.has('./baz')) {
return loader.require('./baz');
}
});

equal(require('foo'), 'I AM baz');

var stats = statsForMonitor('loaderjs', tree);

deepEqual(stats, {
findDeps: 2,
define: 2,
exports: 2,
findModule: 2,
modules: 2,
reify: 2,
require: 2,
resolve: 2,
resolveRelative: 2,
pendingQueueLength: 2
});
});

test('loader module API: resolve', function() {
expect(2);

define('foo/baz/index', function () {
return 'I AM baz';
});

define('foo/index', ['loader'], function (loader) {
equal(loader.resolve('./baz'), 'foo/baz');
});

require('foo');

var stats = statsForMonitor('loaderjs', tree);

deepEqual(stats, {
findDeps: 1,
define: 2,
exports: 1,
findModule: 1,
modules: 2,
reify: 1,
require: 1,
resolve: 1,
resolveRelative: 1,
pendingQueueLength: 1
});
});

test('loader module API: define', function() {
expect(3);

define('foo/index', ['loader'], function (loader) {
loader.define('foo/derp', function() {
ok(true, 'does not need have to have deps');

return 'derp!';
});

loader.define('foo/baz', ['foo/derp'], function(derp) {
equal(derp, 'derp!', 'can have deps');

return 'I AM baz';
});

return loader.require('./baz');
});

require('foo');

var stats = statsForMonitor('loaderjs', tree);

deepEqual(stats, {
findDeps: 3,
define: 3,
exports: 3,
findModule: 3,
modules: 3,
reify: 3,
require: 2,
resolve: 2,
resolveRelative: 1,
pendingQueueLength: 3
});
});

test('loader module API: getIds', function() {
expect(2);

define('foo/derp', function() {
return 'derp!';
});

define('foo/baz', ['foo/derp'], function() {
return 'I AM baz';
});

define('foo/index', ['loader'], function (loader) {
deepEqual(loader.getIds(), ['foo/derp', 'foo/baz', 'foo/index']);
});

require('foo');

var stats = statsForMonitor('loaderjs', tree);

deepEqual(stats, {
findDeps: 1,
define: 3,
exports: 1,
findModule: 1,
modules: 3,
reify: 1,
require: 1,
resolve: 0,
resolveRelative: 0,
pendingQueueLength: 1
});
});

0 comments on commit cbc5da9

Please sign in to comment.