Skip to content

Commit

Permalink
Extending withModules module to support dynamically added modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Nov 19, 2024
1 parent 7adef61 commit 24e8d0e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
17 changes: 16 additions & 1 deletion addon/addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class Resolver {
constructor(props) {
Object.assign(this, props);
if (!this._moduleRegistry) {
const explicitModules = this.constructor.explicitModules;
let explicitModules = this.constructor.explicitModules;
if (explicitModules) {
this._moduleRegistry = {
moduleNames() {
Expand All @@ -75,6 +75,9 @@ export default class Resolver {
get(name) {
return explicitModules[name];
},
addModules(modules) {
explicitModules = Object.assign({}, explicitModules, modules);
},
};
} else {
if (typeof globalThis.requirejs.entries === 'undefined') {
Expand Down Expand Up @@ -204,6 +207,9 @@ export default class Resolver {
}

resolve(fullName) {
if (fullName === 'resolver:current') {
return this;
}
let parsedName = this.parseName(fullName);
let resolveMethodName = parsedName.resolveMethodName;
let resolved;
Expand All @@ -219,6 +225,15 @@ export default class Resolver {
return resolved;
}

addModules(modules) {
if (!this._moduleRegistry.addModules) {
throw new Error(
`addModules is only supported when your Resolver has been configured to use static modules via Resolver.withModules()`
);
}
this._moduleRegistry.addModules(modules);
}

_normalize(fullName) {
// A) Convert underscores to dashes
// B) Convert camelCase to dash-case, except for components (their
Expand Down
1 change: 1 addition & 0 deletions addon/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class Resolver {
}
export default interface Resolver extends Required<ResolverContract> {
pluralizedTypes: Record<string, string>;
addModules(modules: Record<string, unknown>): void;
}


21 changes: 21 additions & 0 deletions test-app/tests/unit/resolvers/classic/with-modues-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,25 @@ module('ember-resolver withModules', function () {

assert.strictEqual((0, resolver.resolve('component:hello'))(), 'it works');
});

test('can resolve self', function (assert) {
let resolver = Resolver.create({ namespace: { modulePrefix: 'alpha' } });
assert.strictEqual(resolver, resolver.resolve('resolver:current'));
});

test('can addModules', function (assert) {
let resolver = Resolver.withModules({}).create({
namespace: { modulePrefix: 'alpha' },
});

resolver.addModules({
'alpha/components/hello': {
default: function () {
return 'it works';
},
},
});

assert.strictEqual((0, resolver.resolve('component:hello'))(), 'it works');
});
});

0 comments on commit 24e8d0e

Please sign in to comment.