Skip to content

Commit

Permalink
Merge pull request #986 from ember-cli/add-modules-api
Browse files Browse the repository at this point in the history
Extending withModules module to support dynamically added modules
  • Loading branch information
ef4 authored Nov 19, 2024
2 parents 7adef61 + 1181411 commit 803071b
Show file tree
Hide file tree
Showing 3 changed files with 44 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 { create: () => 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;
}


27 changes: 27 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,31 @@ 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').create());
});

test('can addModules', function (assert) {
let startingModules = {};
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');
assert.deepEqual(
[],
Object.keys(startingModules),
'did not mutate starting modules'
);
});
});

0 comments on commit 803071b

Please sign in to comment.