/
index.js
245 lines (207 loc) · 6.72 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*eslint dot-notation:0*/
import {Origin} from 'aurelia-metadata';
import {TemplateRegistryEntry, Loader} from 'aurelia-loader';
import {TextTemplateLoader} from './text-template-loader';
import {PLATFORM} from 'aurelia-pal';
function ensureOriginOnExports(executed, name) {
let target = executed;
let key;
let exportedValue;
if (target.__useDefault) {
target = target['default'];
}
Origin.set(target, new Origin(name, 'default'));
for (key in target) {
exportedValue = target[key];
if (typeof exportedValue === 'function') {
Origin.set(exportedValue, new Origin(name, key));
}
}
return executed;
}
/**
* Represents a template loader.
*/
interface TemplateLoader {
/**
* Loads a template.
* @param loader The loader that is requesting the template load.
* @param entry The TemplateRegistryEntry to load and populate with a template.
* @return A promise which resolves when the TemplateRegistryEntry is loaded with a template.
*/
loadTemplate(loader: Loader, entry: TemplateRegistryEntry): Promise<any>;
}
/**
* A default implementation of the Loader abstraction which works with SystemJS, RequireJS and Dojo Loader.
*/
export class DefaultLoader extends Loader {
/**
* The name of the underlying native loader plugin used to load text.
*/
textPluginName: string = 'text';
/**
* Creates an instance of the DefaultLoader.
*/
constructor() {
super();
this.moduleRegistry = Object.create(null);
this.useTemplateLoader(new TextTemplateLoader());
let that = this;
this.addPlugin('template-registry-entry', {
'fetch': function(address) {
let entry = that.getOrCreateTemplateRegistryEntry(address);
return entry.templateIsLoaded ? entry : that.templateLoader.loadTemplate(that, entry).then(x => entry);
}
});
}
/**
* Instructs the loader to use a specific TemplateLoader instance for loading templates
* @param templateLoader The instance of TemplateLoader to use for loading templates.
*/
useTemplateLoader(templateLoader: TemplateLoader): void {
this.templateLoader = templateLoader;
}
/**
* Loads a collection of modules.
* @param ids The set of module ids to load.
* @return A Promise for an array of loaded modules.
*/
loadAllModules(ids: string[]): Promise<any[]> {
let loads = [];
for (let i = 0, ii = ids.length; i < ii; ++i) {
loads.push(this.loadModule(ids[i]));
}
return Promise.all(loads);
}
/**
* Loads a template.
* @param url The url of the template to load.
* @return A Promise for a TemplateRegistryEntry containing the template.
*/
loadTemplate(url: string): Promise<TemplateRegistryEntry> {
return this._import(this.applyPluginToUrl(url, 'template-registry-entry'));
}
/**
* Loads a text-based resource.
* @param url The url of the text file to load.
* @return A Promise for text content.
*/
loadText(url: string): Promise<string> {
return this._import(this.applyPluginToUrl(url, this.textPluginName));
}
}
PLATFORM.Loader = DefaultLoader;
if (!PLATFORM.global.System || !PLATFORM.global.System.import) {
if (PLATFORM.global.requirejs && requirejs.s && requirejs.s.contexts && requirejs.s.contexts._ && requirejs.s.contexts._.defined) {
PLATFORM.eachModule = function(callback) {
let defined = requirejs.s.contexts._.defined;
for (let key in defined) {
try {
if (callback(key, defined[key])) return;
} catch (e) {}
}
};
} else {
PLATFORM.eachModule = function(callback) {};
}
DefaultLoader.prototype._import = function(moduleId) {
return new Promise((resolve, reject) => {
require([moduleId], resolve, reject);
});
};
DefaultLoader.prototype.loadModule = function(id) {
let existing = this.moduleRegistry[id];
if (existing !== undefined) {
return Promise.resolve(existing);
}
return new Promise((resolve, reject) => {
require([id], m => {
this.moduleRegistry[id] = m;
resolve(ensureOriginOnExports(m, id));
}, reject);
});
};
DefaultLoader.prototype.map = function(id, source) {};
DefaultLoader.prototype.normalize = function(moduleId, relativeTo) {
return Promise.resolve(moduleId);
};
DefaultLoader.prototype.normalizeSync = function(moduleId, relativeTo) {
return moduleId;
};
DefaultLoader.prototype.applyPluginToUrl = function(url, pluginName) {
return `${pluginName}!${url}`;
};
DefaultLoader.prototype.addPlugin = function(pluginName, implementation) {
let nonAnonDefine = define;
nonAnonDefine(pluginName, [], {
'load': function(name, req, onload) {
let result = implementation.fetch(name);
Promise.resolve(result).then(onload);
}
});
};
} else {
PLATFORM.eachModule = function(callback) {
let modules = System._loader.modules;
for (let key in modules) {
try {
if (callback(key, modules[key].module)) return;
} catch (e) {}
}
};
System.set('text', System.newModule({
'translate': function(load) {
return 'module.exports = "' + load.source
.replace(/(["\\])/g, '\\$1')
.replace(/[\f]/g, '\\f')
.replace(/[\b]/g, '\\b')
.replace(/[\n]/g, '\\n')
.replace(/[\t]/g, '\\t')
.replace(/[\r]/g, '\\r')
.replace(/[\u2028]/g, '\\u2028')
.replace(/[\u2029]/g, '\\u2029')
+ '";';
}
}));
DefaultLoader.prototype._import = function(moduleId) {
return System.import(moduleId);
};
DefaultLoader.prototype.loadModule = function(id) {
return System.normalize(id).then(newId => {
let existing = this.moduleRegistry[newId];
if (existing !== undefined) {
return Promise.resolve(existing);
}
return System.import(newId).then(m => {
this.moduleRegistry[newId] = m;
return ensureOriginOnExports(m, newId);
});
});
};
DefaultLoader.prototype.map = function(id, source) {
System.map[id] = source;
};
DefaultLoader.prototype.normalizeSync = function(moduleId, relativeTo) {
return System.normalizeSync(moduleId, relativeTo);
};
DefaultLoader.prototype.normalize = function(moduleId, relativeTo) {
return System.normalize(moduleId, relativeTo);
};
DefaultLoader.prototype.applyPluginToUrl = function(url, pluginName) {
return `${url}!${pluginName}`;
};
DefaultLoader.prototype.addPlugin = function(pluginName, implementation) {
System.set(pluginName, System.newModule({
'fetch': function(load, _fetch) {
let result = implementation.fetch(load.address);
return Promise.resolve(result).then(x => {
load.metadata.result = x;
return '';
});
},
'instantiate': function(load) {
return load.metadata.result;
}
}));
};
}