diff --git a/index.js b/index.js index d701d1b..e4d4d1a 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ 'use strict'; + var shouldUseInstrumentedBuild = require('./utils').shouldUseInstrumentedBuild; module.exports = { diff --git a/lib/loader/loader.js b/lib/loader/loader.js index 2b93d37..79ef535 100644 --- a/lib/loader/loader.js +++ b/lib/loader/loader.js @@ -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++; @@ -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 { diff --git a/tests/all.js b/tests/all.js index c885e5f..b43a6d4 100644 --- a/tests/all.js +++ b/tests/all.js @@ -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 + }); +});