-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstrict-resolver.ts
More file actions
128 lines (112 loc) · 3.32 KB
/
strict-resolver.ts
File metadata and controls
128 lines (112 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type { Factory, Resolver } from '@ember/owner';
export class StrictResolver implements Resolver {
#modules = new Map<string, unknown>();
#plurals = new Map<string, string>();
original: any;
constructor(
modules: Record<string, unknown>,
plurals: Record<string, string> | undefined = undefined,
) {
this.addModules(modules);
this.#plurals.set('config', 'config');
if (plurals) {
for (const [singular, plural] of Object.entries(plurals)) {
this.#plurals.set(singular, plural);
}
}
}
addModules(modules: Record<string, unknown>) {
for (const [moduleName, module] of Object.entries(modules)) {
this.#modules.set(this.#normalizeModule(moduleName), module);
}
}
#normalizeModule(moduleName: string) {
return moduleName.replace(fileExtension, '').replace(leadingDotSlash, '');
}
#plural(s: string) {
return this.#plurals.get(s) ?? s + 's';
}
resolve(fullName: string): Factory<object> | object | undefined {
// eslint-disable-next-line prefer-const
let [type, name] = fullName.split(':') as [string, string];
name = this.#normalizeName(type, name);
for (const strategy of [
this.#resolveSelf,
this.#mainLookup,
this.#defaultLookup,
]) {
const result = strategy.call(this, type, name);
if (result) {
return this.#extractDefaultExport(result.hit);
}
}
return undefined;
}
#extractDefaultExport(module: any): Factory<object> | object | undefined {
if (module && module['default']) {
module = module['default'];
}
return module as Factory<object> | object | undefined;
}
normalize(fullName: `${string}:${string}`): `${string}:${string}` {
// eslint-disable-next-line prefer-const
let [type, name] = fullName.split(':') as [string, string];
name = this.#normalizeName(type, name);
return `${type}:${name}`;
}
#normalizeName(type: string, name: string): string {
if (
type === 'component' ||
type === 'helper' ||
type === 'modifier' ||
(type === 'template' && name.indexOf('components/') === 0)
) {
return name.replace(/_/g, '-');
} else {
return dasherize(name.replace(/\./g, '/'));
}
}
#resolveSelf(type: string, name: string): Result {
if (type === 'resolver' && name === 'current') {
return {
hit: {
create: () => this,
},
};
}
return undefined;
}
#mainLookup(type: string, name: string): Result {
if (name === 'main') {
const module = this.#modules.get(type);
if (module) {
return { hit: module };
}
}
return undefined;
}
#defaultLookup(type: string, name: string): Result {
const dir = this.#plural(type);
const target = `${dir}/${name}`;
const module = this.#modules.get(target);
if (module) {
return { hit: module };
}
return undefined;
}
}
const fileExtension = /\.\w{1,4}$/;
const leadingDotSlash = /^\.\//;
const STRING_DECAMELIZE_REGEXP = /([a-z\d])([A-Z])/g;
function decamelize(str: string): string {
return str.replace(STRING_DECAMELIZE_REGEXP, '$1_$2').toLowerCase();
}
const STRING_DASHERIZE_REGEXP = /[ _]/g;
function dasherize(key: string): string {
return decamelize(key).replace(STRING_DASHERIZE_REGEXP, '-');
}
type Result =
| {
hit: any;
}
| undefined;