Skip to content

Commit 55ec03b

Browse files
Merge pull request #8 from NullVoxPopuli/add-registry-building-utility
Add build-registry for libraries to support ember-resolver and make constructing registries easier
2 parents d528168 + 2bc7e0b commit 55ec03b

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ In your app.js or app.ts, or wherever you configure your application
1717
- import EmberResolver from 'ember-resolver';
1818
+ import EmberApp from 'ember-strict-application-resolver';
1919

20-
class TestApp extends EmberApp {
20+
class YouApp extends EmberApp {
2121
- modulePrefix = config.modulePrefix;
2222
- podModulePrefix = config.podModulePrefix;
2323
- Resolver = EmberResolver.withModules({
@@ -47,6 +47,46 @@ The type of `modules` is:
4747
};
4848
```
4949

50+
### `buildRegistry`
51+
52+
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)
53+
54+
For example:
55+
```js
56+
// in src/registry.ts (or js)
57+
import { buildRegistry } from 'ember-strict-application-resolver';
58+
import TheService from 'from-dependency/services/the-service';
59+
60+
export default buildRegistry({
61+
...import.meta.glob('./services/*', { eager: true }),
62+
'./services/the-service': { default: TheService },
63+
});
64+
```
65+
66+
Then consumers of your library would either splat this into their `modules` object:
67+
```js
68+
import libraryRegistry from 'your-library/registry';
69+
// ...
70+
71+
modules = {
72+
// if the app is using ember-strict-application-resolver
73+
...libraryRegistry(),
74+
// or if using ember-resolver
75+
...libraryRegistry('name-of-app'),
76+
}
77+
```
78+
79+
Or consuming libraries would propagate the configuration for their consumers:
80+
```js
81+
import { buildRegistry } from 'ember-strict-application-resolver';
82+
import libraryRegistry from 'your-library/registry';
83+
84+
export default buildRegistry({
85+
...import.meta.glob('./services/*', { eager: true }),
86+
// No argument should be passed here
87+
...libraryRegistry(),
88+
});
89+
```
5090

5191
## Contributing
5292

index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<head>
3+
<script type="module">window.location = '/tests'</script>
4+
</head>
5+
</html>

src/build-registry.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const DEFAULT_NAMESPACE = './';
2+
3+
/**
4+
* For libraries to provide a registry to use in apps.
5+
*/
6+
export function buildRegistry(entries: Record<string, unknown>) {
7+
/**
8+
* Injest a sub-registry from a library
9+
*
10+
* ```js
11+
* import EmberApp from 'ember-strict-application-resolver';
12+
*
13+
* import { libraryRegistry } from 'some-library/registry';
14+
*
15+
* class TestApp extends EmberApp {
16+
* modules = {
17+
* './router': { default: Router },
18+
* ...libraryRegistry(),
19+
* };
20+
* }
21+
* ```
22+
*
23+
* Or if using `ember-resolver`
24+
* ```js
25+
* import Application from '@ember/application';
26+
* import Resolver from 'ember-resolver';
27+
* import config from '#config';
28+
*
29+
* import { registry } from './registry.ts';
30+
31+
* export default class App extends Application {
32+
* modulePrefix = config.modulePrefix;
33+
* Resolver = Resolver.withModules({
34+
* ...libraryRegistry('my-app-name'),
35+
* });
36+
}
37+
* ```
38+
*/
39+
return function createRegistry(namespace = DEFAULT_NAMESPACE) {
40+
const result: Record<string, unknown> = {};
41+
42+
for (const [key, value] of Object.entries(entries)) {
43+
const entry =
44+
namespace === DEFAULT_NAMESPACE ? key : join(namespace, key);
45+
46+
result[entry] = value;
47+
}
48+
49+
return result;
50+
};
51+
}
52+
53+
function join(namespace: string, key: string) {
54+
return `${namespace}/${key}`.replace('/./', '/');
55+
}

tests/build-registry-test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { module, test } from 'qunit';
2+
import { setupTest } from 'ember-qunit';
3+
4+
import { buildRegistry } from '#src/build-registry.ts';
5+
6+
module('buildRegistry', function (hooks) {
7+
setupTest(hooks);
8+
9+
test('returns a function', function (assert) {
10+
const result = buildRegistry({});
11+
12+
assert.strictEqual(typeof result, 'function');
13+
});
14+
15+
test('has entries', function (assert) {
16+
const result = buildRegistry({
17+
'./foo': 2,
18+
});
19+
20+
assert.deepEqual(result(), { './foo': 2 });
21+
});
22+
23+
test('entries can be prefixed', function (assert) {
24+
const result = buildRegistry({
25+
'./foo': 2,
26+
});
27+
28+
assert.deepEqual(result('test-app'), { 'test-app/foo': 2 });
29+
});
30+
31+
test('entries can be prefixed with scope', function (assert) {
32+
const result = buildRegistry({
33+
'./foo': 2,
34+
});
35+
36+
assert.deepEqual(result('@scope/test-app'), { '@scope/test-app/foo': 2 });
37+
});
38+
});

0 commit comments

Comments
 (0)