Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<head>
<script type="module">window.location = '/tests'</script>
</head>
</html>
55 changes: 55 additions & 0 deletions src/build-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const DEFAULT_NAMESPACE = './';

/**
* For libraries to provide a registry to use in apps.
*/
export function buildRegistry(entries: Record<string, unknown>) {
/**
* 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<string, unknown> = {};

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('/./', '/');
}
38 changes: 38 additions & 0 deletions tests/build-registry-test.ts
Original file line number Diff line number Diff line change
@@ -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 });
});
});