Skip to content

Commit

Permalink
Support named exports
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Jul 8, 2017
1 parent fdb4ad8 commit 4f399a9
Showing 2 changed files with 44 additions and 29 deletions.
62 changes: 33 additions & 29 deletions mu-trees/addon/module-registries/requirejs.js
Original file line number Diff line number Diff line change
@@ -43,51 +43,55 @@ export default class RequireJSRegistry {
return segments;
}

_detectModule(specifierString, lookupMethod) {
let specifier = deserializeSpecifier(specifierString);

_detectModule(specifier, lookupDefault, lookupNamed) {
let segments = this._baseSegments(specifier);
let basePath = `${segments.join('/')}`;
let typedPath = `${basePath}/${specifier.type}`;

let lookupResult = lookupMethod(typedPath);
let lookupResult = lookupDefault(typedPath);

if (
!lookupResult &&
this._config.collections[specifier.collection].defaultType === specifier.type
) {
lookupResult = lookupMethod(basePath);
if (!lookupResult) {
if (this._checkDefaultType(specifier)) {
lookupResult = lookupDefault(basePath);
} else {
lookupResult = lookupNamed(basePath);
}
}

return lookupResult;
}

_checkDefaultType(specifier) {
let {defaultType} = this._config.collections[specifier.collection];
return defaultType && defaultType === specifier.type;
}

has(specifierString) {
return this._detectModule(specifierString, path => {
/*
* Worth noting this does not confirm there is a default export,
* as would be expected with this simple implementation of the module
* registry.
*
* To preserve sanity, the `get` method throws when a `default`
* export is not found.
*/
return path in this._require.entries;
let specifier = deserializeSpecifier(specifierString);

/* return a boolean */
return this._detectModule(specifier, path => {
return (path in this._require.entries);
}, path => {
if (path in this._require.entries) {
let result = this._require(path);
return (specifier.type in result);
}
});
}

get(specifierString) {
let module = this._detectModule(specifierString, path => {
return (path in this._require.entries) && this._require(path);
});
let specifier = deserializeSpecifier(specifierString);

if (!module) {
return module;
}
let useDefaultType = this._checkDefaultType(specifier);

if (!module.default) {
throw new Error('RequireJSRegistry expects all resolved modules to have a default export.');
}
return module.default;
/* return an export */
let moduleExport = this._detectModule(specifier, path => {
return (path in this._require.entries) && this._require(path).default;
}, path => {
return (path in this._require.entries) && this._require(path)[specifier.type];
});

return moduleExport;
}
}
11 changes: 11 additions & 0 deletions mu-trees/tests/unit/module-registries/requirejs-test.js
Original file line number Diff line number Diff line change
@@ -118,3 +118,14 @@ test('un-typed module name with default export when resolved type is not the def
`did not resolve the module`
);
});

test('un-typed module name with named export of resolved type', function(assert) {
let expectedModule = {};
this.addModule(`my-app/src/ui/routes/index`, {template: expectedModule});

let actualModule = this.registry.get(`template:/my-app/routes/index`);
assert.equal(
actualModule, expectedModule,
`did not resolve the module`
);
});

0 comments on commit 4f399a9

Please sign in to comment.