-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathstrict-resolver.ts
More file actions
145 lines (127 loc) · 4.01 KB
/
strict-resolver.ts
File metadata and controls
145 lines (127 loc) · 4.01 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import type { Factory, Resolver } from '@ember/owner';
export class StrictResolver implements Resolver {
// Ember's router uses this flag to decide whether to auto-generate
// `${name}_loading` and `${name}_error` substates for routes defined in
// `Router.map(...)`. Since we always resolve against an ES module registry,
// we unconditionally opt in.
moduleBasedResolver = true;
#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 (let [singular, plural] of Object.entries(plurals)) {
this.#plurals.set(singular, plural);
}
}
}
addModules(modules: Record<string, unknown>) {
for (let [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 {
let [type, name] = fullName.split(':') as [string, string];
name = this.#normalizeName(type, name);
for (let strategy of [
this.#resolveSelf,
this.#mainLookup,
this.#defaultLookup,
this.#nestedColocationLookup,
]) {
let 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}` {
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') {
let module = this.#modules.get(type);
if (module) {
return { hit: module };
}
}
return undefined;
}
#defaultLookup(type: string, name: string): Result {
let dir = this.#plural(type);
let target = `${dir}/${name}`;
let module = this.#modules.get(target);
if (module) {
return { hit: module };
}
return undefined;
}
// Supports the nested colocation pattern where `component:my-widget`
// resolves to `./components/my-widget/index.{js,ts,gjs,gts}`. The index
// file is typically the component class, and it's commonly paired with a
// sibling `index.hbs` inside the same folder.
#nestedColocationLookup(type: string, name: string): Result {
if (type !== 'component') return undefined;
let dir = this.#plural(type);
let target = `${dir}/${name}/index`;
let module = this.#modules.get(target);
if (module) {
return { hit: module };
}
return undefined;
}
}
const fileExtension = /\.\w{1,4}$/;
const leadingDotSlash = /^\.\//;
const camelCaseBoundary = /([a-z\d])([A-Z])/g;
const spacesAndUnderscores = /[ _]/g;
function dasherize(str: string): string {
return str.replace(camelCaseBoundary, '$1_$2').toLowerCase().replace(spacesAndUnderscores, '-');
}
type Result =
| {
hit: any;
}
| undefined;