-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
requirejs.js
97 lines (77 loc) · 2.61 KB
/
requirejs.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
/* global require, requirejs */
import {
deserializeSpecifier
} from '@glimmer/di';
export default class RequireJSRegistry {
constructor(config, modulePrefix, require=self.requirejs) {
this._config = config;
this._modulePrefix = modulePrefix;
this._require = require;
}
_baseSegments(s) {
let collectionDefinition = this._config.collections[s.collection];
let group = collectionDefinition && collectionDefinition.group;
let segments = [ s.rootName, this._modulePrefix ];
if (group) {
segments.push(group);
}
// Special case to handle definitiveCollection for templates
// eventually want to find a better way to address.
// Dgeb wants to find a better way to handle these
// in config without needing definitiveCollection.
let ignoreCollection = s.type === 'template' &&
s.collection === 'routes' &&
s.namespace === 'components';
if (s.collection !== 'main' && !ignoreCollection) {
segments.push(s.collection);
}
if (s.namespace) {
segments.push(s.namespace);
}
if (s.name !== 'main' || s.collection !== 'main') {
segments.push(s.name);
}
return segments;
}
_detectModule(specifier, lookupDefault, lookupNamed) {
let segments = this._baseSegments(specifier);
let basePath = `${segments.join('/')}`;
let typedPath = `${basePath}/${specifier.type}`;
let lookupResult = lookupDefault(typedPath);
if (!lookupResult) {
if (this._checkDefaultType(specifier)) {
lookupResult = lookupDefault(basePath);
} else {
lookupResult = lookupNamed(basePath);
}
}
return lookupResult;
}
_checkDefaultType(specifier) {
let {defaultType} = this._config.collections[specifier.collection];
return defaultType && defaultType === specifier.type;
}
has(specifierString) {
let specifier = deserializeSpecifier(specifierString);
/* return a boolean */
return this._detectModule(specifier, path => {
return (path in this._require.entries);
}, path => {
if (path in this._require.entries) {
let result = this._require(path);
return (specifier.type in result);
}
});
}
get(specifierString) {
let specifier = deserializeSpecifier(specifierString);
let useDefaultType = this._checkDefaultType(specifier);
/* return an export */
let moduleExport = this._detectModule(specifier, path => {
return (path in this._require.entries) && this._require(path).default;
}, path => {
return (path in this._require.entries) && this._require(path)[specifier.type];
});
return moduleExport;
}
}