Skip to content

Commit b1c2744

Browse files
committed
build-registry
1 parent d528168 commit b1c2744

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

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+
}

0 commit comments

Comments
 (0)