diff --git a/README.md b/README.md index 99703d3..cbbe220 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ In your app.js or app.ts, or wherever you configure your application - import EmberResolver from 'ember-resolver'; + import EmberApp from 'ember-strict-application-resolver'; - class TestApp extends EmberApp { + class YouApp extends EmberApp { - modulePrefix = config.modulePrefix; - podModulePrefix = config.podModulePrefix; - Resolver = EmberResolver.withModules({ @@ -47,6 +47,46 @@ The type of `modules` is: }; ``` +### `buildRegistry` + +Libraries may declare `ember-strict-application-resolver` as a `dependencies` entry, and then import from `./build-registry` - to provide helpers for packages all the library's services and other things that need to be in the registry (such as from the library's dependencies as well) + +For example: +```js +// in src/registry.ts (or js) +import { buildRegistry } from 'ember-strict-application-resolver'; +import TheService from 'from-dependency/services/the-service'; + +export default buildRegistry({ + ...import.meta.glob('./services/*', { eager: true }), + './services/the-service': { default: TheService }, +}); +``` + +Then consumers of your library would either splat this into their `modules` object: +```js +import libraryRegistry from 'your-library/registry'; +// ... + +modules = { + // if the app is using ember-strict-application-resolver + ...libraryRegistry(), + // or if using ember-resolver + ...libraryRegistry('name-of-app'), +} +``` + +Or consuming libraries would propagate the configuration for their consumers: +```js +import { buildRegistry } from 'ember-strict-application-resolver'; +import libraryRegistry from 'your-library/registry'; + +export default buildRegistry({ + ...import.meta.glob('./services/*', { eager: true }), + // No argument should be passed here + ...libraryRegistry(), +}); +``` ## Contributing diff --git a/index.html b/index.html new file mode 100644 index 0000000..e50840c --- /dev/null +++ b/index.html @@ -0,0 +1,5 @@ + + + + + diff --git a/src/build-registry.ts b/src/build-registry.ts new file mode 100644 index 0000000..efb1eb4 --- /dev/null +++ b/src/build-registry.ts @@ -0,0 +1,55 @@ +const DEFAULT_NAMESPACE = './'; + +/** + * For libraries to provide a registry to use in apps. + */ +export function buildRegistry(entries: Record) { + /** + * Injest a sub-registry from a library + * + * ```js + * import EmberApp from 'ember-strict-application-resolver'; + * + * import { libraryRegistry } from 'some-library/registry'; + * + * class TestApp extends EmberApp { + * modules = { + * './router': { default: Router }, + * ...libraryRegistry(), + * }; + * } + * ``` + * + * Or if using `ember-resolver` + * ```js + * import Application from '@ember/application'; + * import Resolver from 'ember-resolver'; + * import config from '#config'; + * + * import { registry } from './registry.ts'; + + * export default class App extends Application { + * modulePrefix = config.modulePrefix; + * Resolver = Resolver.withModules({ + * ...libraryRegistry('my-app-name'), + * }); + } + * ``` + */ + return function createRegistry(namespace = DEFAULT_NAMESPACE) { + const result: Record = {}; + + for (const [key, value] of Object.entries(entries)) { + const entry = + namespace === DEFAULT_NAMESPACE ? key : join(namespace, key); + + result[entry] = value; + } + + return result; + }; +} + +function join(namespace: string, key: string) { + return `${namespace}/${key}`.replace('/./', '/'); +} diff --git a/tests/build-registry-test.ts b/tests/build-registry-test.ts new file mode 100644 index 0000000..741ec59 --- /dev/null +++ b/tests/build-registry-test.ts @@ -0,0 +1,38 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +import { buildRegistry } from '#src/build-registry.ts'; + +module('buildRegistry', function (hooks) { + setupTest(hooks); + + test('returns a function', function (assert) { + const result = buildRegistry({}); + + assert.strictEqual(typeof result, 'function'); + }); + + test('has entries', function (assert) { + const result = buildRegistry({ + './foo': 2, + }); + + assert.deepEqual(result(), { './foo': 2 }); + }); + + test('entries can be prefixed', function (assert) { + const result = buildRegistry({ + './foo': 2, + }); + + assert.deepEqual(result('test-app'), { 'test-app/foo': 2 }); + }); + + test('entries can be prefixed with scope', function (assert) { + const result = buildRegistry({ + './foo': 2, + }); + + assert.deepEqual(result('@scope/test-app'), { '@scope/test-app/foo': 2 }); + }); +});