From 11814114bd39c7a424611ac1000492508af4b878 Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Tue, 19 Nov 2024 10:24:24 -0500 Subject: [PATCH] Extending withModules module to support dynamically added modules --- addon/addon/index.js | 17 +++++++++++- addon/index.d.ts | 1 + .../resolvers/classic/with-modues-test.js | 27 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/addon/addon/index.js b/addon/addon/index.js index 5348e5c2..7baf833a 100644 --- a/addon/addon/index.js +++ b/addon/addon/index.js @@ -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() { @@ -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') { @@ -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; @@ -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 diff --git a/addon/index.d.ts b/addon/index.d.ts index 62789bc2..7c665b49 100644 --- a/addon/index.d.ts +++ b/addon/index.d.ts @@ -6,6 +6,7 @@ export default class Resolver { } export default interface Resolver extends Required { pluralizedTypes: Record; + addModules(modules: Record): void; } diff --git a/test-app/tests/unit/resolvers/classic/with-modues-test.js b/test-app/tests/unit/resolvers/classic/with-modues-test.js index 990a0244..8a91d920 100644 --- a/test-app/tests/unit/resolvers/classic/with-modues-test.js +++ b/test-app/tests/unit/resolvers/classic/with-modues-test.js @@ -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' + ); + }); });