Skip to content

Commit

Permalink
Merge pull request #979 from ember-cli/explicit-modules
Browse files Browse the repository at this point in the history
Optional explicit modules
  • Loading branch information
ef4 authored Sep 11, 2024
2 parents a51d352 + 2eca39f commit fb11f7c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
30 changes: 25 additions & 5 deletions addon/addon/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { dasherize, classify, underscore } from './string';
import classFactory from './utils/class-factory';

if (typeof globalThis.requirejs.entries === 'undefined') {
globalThis.requirejs.entries = globalThis.requirejs._eak_seen;
}

export class ModuleRegistry {
constructor(entries) {
this._entries = entries || globalThis.requirejs.entries;
Expand Down Expand Up @@ -58,10 +54,34 @@ export default class Resolver {
this.nestedColocationComponentModuleName,
];

static withModules(explicitModules) {
return class extends this {
static explicitModules = explicitModules;
};
}

constructor(props) {
Object.assign(this, props);
if (!this._moduleRegistry) {
this._moduleRegistry = new ModuleRegistry();
const explicitModules = this.constructor.explicitModules;
if (explicitModules) {
this._moduleRegistry = {
moduleNames() {
return Object.keys(explicitModules);
},
has(name) {
return Boolean(explicitModules[name]);
},
get(name) {
return explicitModules[name];
},
};
} else {
if (typeof globalThis.requirejs.entries === 'undefined') {
globalThis.requirejs.entries = globalThis.requirejs._eak_seen;
}
this._moduleRegistry = new ModuleRegistry();
}
}

this.pluralizedTypes = this.pluralizedTypes || Object.create(null);
Expand Down
1 change: 1 addition & 0 deletions addon/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Resolver as ResolverContract } from "@ember/owner";

export default class Resolver {
static create(props: Record<string, unknown>): InstanceType<this>;
static withModules(modules: Record<string, unknown>): this;
}
export default interface Resolver extends Required<ResolverContract> {
pluralizedTypes: Record<string, string>;
Expand Down
18 changes: 18 additions & 0 deletions test-app/tests/unit/resolvers/classic/with-modues-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable no-console */

import { module, test } from 'qunit';
import Resolver from 'ember-resolver';

module('ember-resolver withModules', function () {
test('explicit withModules', function (assert) {
let resolver = Resolver.withModules({
'alpha/components/hello': {
default: function () {
return 'it works';
},
},
}).create({ namespace: { modulePrefix: 'alpha' } });

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

0 comments on commit fb11f7c

Please sign in to comment.