-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
index.js
153 lines (126 loc) · 4.46 KB
/
index.js
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
146
147
148
149
150
151
152
153
import { DEBUG } from '@glimmer/env';
import GlimmerResolver from '@glimmer/resolver/resolver';
import RequireJSRegistry from '../../module-registries/requirejs';
import GlobalsResolver from '@ember/application/globals-resolver';
import { dasherize } from '@ember/string';
function slasherize(dotted) {
return dotted.replace(/\./g, '/');
}
const TEMPLATE_TO_PARTIAL = /^template:(.*\/)?_([\w-]+)/;
function isAbsoluteSpecifier(specifier) {
return specifier.indexOf(':/') !== -1;
}
function cleanupEmberSpecifier(specifier, source, _namespace) {
let [type, name] = specifier.split(':');
if (!name) {
return [specifier, null];
}
if (type === 'component' && name) {
specifier = `${type}:${name}`;
} else if (type === 'service') {
/* Services may be camelCased */
specifier = `service:${dasherize(name)}`;
} else if (type === 'route') {
/* Routes may have.dot.paths */
specifier = `route:${slasherize(name)}`;
} else if (type === 'controller') {
/* Controllers may have.dot.paths */
specifier = `controller:${slasherize(name)}`;
} else if (type === 'template') {
if (name && name.indexOf('components/') === 0) {
let sliced = name.slice(11);
specifier = `template:${sliced}`;
} else {
/*
* Ember partials are looked up as templates. Here we replace the template
* resolution with a partial resolute when appropriate. Try to keep this
* code as "pay-go" as possible.
*/
let match = TEMPLATE_TO_PARTIAL.exec(specifier);
if (match) {
let namespace = match[1] || '';
let name = match[2];
specifier = `partial:${namespace}${name}`;
} else {
if (source) {
throw new Error(`Cannot look up a route template ${specifier} with a source`);
}
/*
* Templates for routes must be looked up with a source. They may
* have dots.in.paths
*/
specifier = `template`;
source = `route:/${_namespace}/routes/${slasherize(name)}`;
}
}
}
return [specifier, source];
}
const normalize = !DEBUG ? null : function(specifier) {
// This method is called by `Registry#validateInjections` in dev mode.
// https://github.com/ember-cli/ember-resolver/issues/299
const [type, name] = specifier.split(':', 2);
if (name && (type === 'service' || type === 'controller')) {
return `${type}:${dasherize(name)}`;
}
return specifier;
};
/*
* Wrap the @glimmer/resolver in Ember's resolver API. Although
* this code extends from the DefaultResolver, it should never
* call `_super` or call into that code.
*/
const Resolver = GlobalsResolver.extend({
init() {
this._super(...arguments);
this._configRootName = this.config.app.rootName || 'app';
if (!this.glimmerModuleRegistry) {
this.glimmerModuleRegistry = new RequireJSRegistry(this.config, 'src');
}
this._glimmerResolver = new GlimmerResolver(this.config, this.glimmerModuleRegistry);
},
normalize,
expandLocalLookup(specifier, source, namespace) {
if (isAbsoluteSpecifier(specifier)) {
return specifier; // specifier is absolute
}
if (source || namespace) {
let rootName = namespace || this._configRootName;
let [type] = specifier.split(':');
/*
* Ember components require their lookupString to be massaged. Make this
* as "pay-go" as possible.
*/
if (namespace) {
// This is only required because:
// https://github.com/glimmerjs/glimmer-di/issues/45
source = `${type}:/${rootName}/`;
} else if (source) {
// make absolute
let parts = source.split(':src/ui/');
source = `${parts[0]}:/${rootName}/${parts[1]}`;
source = source.split('/template.hbs')[0];
}
let [_specifier, _source] = cleanupEmberSpecifier(specifier, source, rootName);
let absoluteSpecifier = this._glimmerResolver.identify(_specifier, _source);
if (absoluteSpecifier) {
return absoluteSpecifier;
}
absoluteSpecifier = this._glimmerResolver.identify(_specifier);
if (absoluteSpecifier) {
return specifier;
}
}
return specifier;
},
resolve(specifier) {
let source = null;
if (!isAbsoluteSpecifier(specifier)) {
let [_specifier, _source] = cleanupEmberSpecifier(specifier, source, this._configRootName);
specifier = _specifier;
source = _source;
}
return this._glimmerResolver.resolve(specifier, source);
}
});
export default Resolver;