UNPKG

15.7 kBJavaScriptView Raw
1var __extends = (this && this.__extends) || (function () {
2 var extendStatics = Object.setPrototypeOf ||
3 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
4 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
5 return function (d, b) {
6 extendStatics(d, b);
7 function __() { this.constructor = d; }
8 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
9 };
10})();
11var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12 return new (P || (P = Promise))(function (resolve, reject) {
13 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
14 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
15 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
16 step((generator = generator.apply(thisArg, _arguments || [])).next());
17 });
18};
19var __generator = (this && this.__generator) || function (thisArg, body) {
20 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
21 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
22 function verb(n) { return function (v) { return step([n, v]); }; }
23 function step(op) {
24 if (f) throw new TypeError("Generator is already executing.");
25 while (_) try {
26 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
27 if (y = 0, t) op = [op[0] & 2, t.value];
28 switch (op[0]) {
29 case 0: case 1: t = op; break;
30 case 4: _.label++; return { value: op[1], done: false };
31 case 5: _.label++; y = op[1]; op = [0]; continue;
32 case 7: op = _.ops.pop(); _.trys.pop(); continue;
33 default:
34 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
35 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
36 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
37 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
38 if (t[2]) _.ops.pop();
39 _.trys.pop(); continue;
40 }
41 op = body.call(thisArg, _);
42 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
43 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
44 }
45};
46import { Origin } from 'aurelia-metadata';
47import { Loader } from 'aurelia-loader';
48import { DOM, PLATFORM } from 'aurelia-pal';
49/**
50* An implementation of the TemplateLoader interface implemented with text-based loading.
51*/
52var TextTemplateLoader = /** @class */ (function () {
53 function TextTemplateLoader() {
54 }
55 /**
56 * Loads a template.
57 * @param loader The loader that is requesting the template load.
58 * @param entry The TemplateRegistryEntry to load and populate with a template.
59 * @return A promise which resolves when the TemplateRegistryEntry is loaded with a template.
60 */
61 TextTemplateLoader.prototype.loadTemplate = function (loader, entry) {
62 return __awaiter(this, void 0, void 0, function () {
63 var text;
64 return __generator(this, function (_a) {
65 switch (_a.label) {
66 case 0: return [4 /*yield*/, loader.loadText(entry.address)];
67 case 1:
68 text = _a.sent();
69 entry.template = DOM.createTemplateFromMarkup(text);
70 return [2 /*return*/];
71 }
72 });
73 });
74 };
75 return TextTemplateLoader;
76}());
77export { TextTemplateLoader };
78export function ensureOriginOnExports(moduleExports, moduleId) {
79 var target = moduleExports;
80 var key;
81 var exportedValue;
82 if (target.__useDefault) {
83 target = target.default;
84 }
85 Origin.set(target, new Origin(moduleId, 'default'));
86 if (typeof target === 'object') {
87 for (key in target) {
88 exportedValue = target[key];
89 if (typeof exportedValue === 'function') {
90 Origin.set(exportedValue, new Origin(moduleId, key));
91 }
92 }
93 }
94 return moduleExports;
95}
96/**
97* A default implementation of the Loader abstraction which works with webpack (extended common-js style).
98*/
99var WebpackLoader = /** @class */ (function (_super) {
100 __extends(WebpackLoader, _super);
101 function WebpackLoader() {
102 var _this = _super.call(this) || this;
103 _this.moduleRegistry = Object.create(null);
104 _this.loaderPlugins = Object.create(null);
105 _this.modulesBeingLoaded = new Map();
106 _this.useTemplateLoader(new TextTemplateLoader());
107 _this.addPlugin('template-registry-entry', {
108 fetch: function (moduleId) { return __awaiter(_this, void 0, void 0, function () {
109 var HmrContext, entry;
110 var _this = this;
111 return __generator(this, function (_a) {
112 switch (_a.label) {
113 case 0:
114 // HMR:
115 if (module.hot) {
116 if (!this.hmrContext) {
117 HmrContext = require('aurelia-hot-module-reload').HmrContext;
118 this.hmrContext = new HmrContext(this);
119 }
120 module.hot.accept(moduleId, function () { return __awaiter(_this, void 0, void 0, function () {
121 return __generator(this, function (_a) {
122 switch (_a.label) {
123 case 0: return [4 /*yield*/, this.hmrContext.handleViewChange(moduleId)];
124 case 1:
125 _a.sent();
126 return [2 /*return*/];
127 }
128 });
129 }); });
130 }
131 entry = this.getOrCreateTemplateRegistryEntry(moduleId);
132 if (!!entry.templateIsLoaded) return [3 /*break*/, 2];
133 return [4 /*yield*/, this.templateLoader.loadTemplate(this, entry)];
134 case 1:
135 _a.sent();
136 _a.label = 2;
137 case 2: return [2 /*return*/, entry];
138 }
139 });
140 }); }
141 });
142 PLATFORM.eachModule = function (callback) {
143 var registry = __webpack_require__.c;
144 var cachedModuleIds = Object.getOwnPropertyNames(registry);
145 cachedModuleIds
146 // Note: we use .some here like a .forEach that can be "break"ed out of.
147 // It will stop iterating only when a truthy value is returned.
148 // Even though the docs say "true" explicitly, loader-default also goes by truthy
149 // and this is to keep it consistent with that.
150 .some(function (moduleId) {
151 var moduleExports = registry[moduleId].exports;
152 if (typeof moduleExports === 'object') {
153 return callback(moduleId, moduleExports);
154 }
155 return false;
156 });
157 };
158 return _this;
159 }
160 WebpackLoader.prototype._import = function (address, defaultHMR) {
161 if (defaultHMR === void 0) { defaultHMR = true; }
162 return __awaiter(this, void 0, void 0, function () {
163 var addressParts, moduleId, loaderPlugin, plugin_1, asyncModuleId, callback;
164 var _this = this;
165 return __generator(this, function (_a) {
166 switch (_a.label) {
167 case 0:
168 addressParts = address.split('!');
169 moduleId = addressParts.splice(addressParts.length - 1, 1)[0];
170 loaderPlugin = addressParts.length === 1 ? addressParts[0] : null;
171 if (!loaderPlugin) return [3 /*break*/, 2];
172 plugin_1 = this.loaderPlugins[loaderPlugin];
173 if (!plugin_1) {
174 throw new Error("Plugin " + loaderPlugin + " is not registered in the loader.");
175 }
176 if (module.hot && plugin_1.hot) {
177 module.hot.accept(moduleId, function () { return plugin_1.hot(moduleId); });
178 }
179 return [4 /*yield*/, plugin_1.fetch(moduleId)];
180 case 1: return [2 /*return*/, _a.sent()];
181 case 2:
182 if (__webpack_require__.m[moduleId]) {
183 if (defaultHMR && module.hot && this.hmrContext) {
184 module.hot.accept(moduleId, function () { return _this.hmrContext.handleModuleChange(moduleId, module.hot); });
185 }
186 return [2 /*return*/, __webpack_require__(moduleId)];
187 }
188 asyncModuleId = "async!" + moduleId;
189 if (!__webpack_require__.m[asyncModuleId]) return [3 /*break*/, 4];
190 if (defaultHMR && module.hot && this.hmrContext) {
191 module.hot.accept(moduleId, function () { return _this.hmrContext.handleModuleChange(moduleId, module.hot); });
192 module.hot.accept(asyncModuleId, function () { return _this.hmrContext.handleModuleChange(moduleId, module.hot); });
193 }
194 callback = __webpack_require__(asyncModuleId);
195 return [4 /*yield*/, new Promise(callback)];
196 case 3: return [2 /*return*/, _a.sent()];
197 case 4: throw new Error("Unable to find module with ID: " + moduleId);
198 }
199 });
200 });
201 };
202 /**
203 * Maps a module id to a source.
204 * @param id The module id.
205 * @param source The source to map the module to.
206 */
207 WebpackLoader.prototype.map = function (id, source) { };
208 /**
209 * Normalizes a module id.
210 * @param moduleId The module id to normalize.
211 * @param relativeTo What the module id should be normalized relative to.
212 * @return The normalized module id.
213 */
214 WebpackLoader.prototype.normalizeSync = function (moduleId, relativeTo) {
215 return moduleId;
216 };
217 /**
218 * Normalizes a module id.
219 * @param moduleId The module id to normalize.
220 * @param relativeTo What the module id should be normalized relative to.
221 * @return The normalized module id.
222 */
223 WebpackLoader.prototype.normalize = function (moduleId, relativeTo) {
224 return Promise.resolve(moduleId);
225 };
226 /**
227 * Instructs the loader to use a specific TemplateLoader instance for loading templates
228 * @param templateLoader The instance of TemplateLoader to use for loading templates.
229 */
230 WebpackLoader.prototype.useTemplateLoader = function (templateLoader) {
231 this.templateLoader = templateLoader;
232 };
233 /**
234 * Loads a collection of modules.
235 * @param ids The set of module ids to load.
236 * @return A Promise for an array of loaded modules.
237 */
238 WebpackLoader.prototype.loadAllModules = function (ids) {
239 var _this = this;
240 return Promise.all(ids.map(function (id) { return _this.loadModule(id); }));
241 };
242 /**
243 * Loads a module.
244 * @param moduleId The module ID to load.
245 * @return A Promise for the loaded module.
246 */
247 WebpackLoader.prototype.loadModule = function (moduleId, defaultHMR) {
248 if (defaultHMR === void 0) { defaultHMR = true; }
249 return __awaiter(this, void 0, void 0, function () {
250 var existing, beingLoaded, moduleExports;
251 return __generator(this, function (_a) {
252 switch (_a.label) {
253 case 0:
254 existing = this.moduleRegistry[moduleId];
255 if (existing) {
256 return [2 /*return*/, existing];
257 }
258 beingLoaded = this.modulesBeingLoaded.get(moduleId);
259 if (beingLoaded) {
260 return [2 /*return*/, beingLoaded];
261 }
262 beingLoaded = this._import(moduleId, defaultHMR);
263 this.modulesBeingLoaded.set(moduleId, beingLoaded);
264 return [4 /*yield*/, beingLoaded];
265 case 1:
266 moduleExports = _a.sent();
267 this.moduleRegistry[moduleId] = ensureOriginOnExports(moduleExports, moduleId);
268 this.modulesBeingLoaded.delete(moduleId);
269 return [2 /*return*/, moduleExports];
270 }
271 });
272 });
273 };
274 /**
275 * Loads a template.
276 * @param url The url of the template to load.
277 * @return A Promise for a TemplateRegistryEntry containing the template.
278 */
279 WebpackLoader.prototype.loadTemplate = function (url) {
280 return this.loadModule(this.applyPluginToUrl(url, 'template-registry-entry'), false);
281 };
282 /**
283 * Loads a text-based resource.
284 * @param url The url of the text file to load.
285 * @return A Promise for text content.
286 */
287 WebpackLoader.prototype.loadText = function (url) {
288 return __awaiter(this, void 0, void 0, function () {
289 var result, defaultExport;
290 return __generator(this, function (_a) {
291 switch (_a.label) {
292 case 0: return [4 /*yield*/, this.loadModule(url, false)];
293 case 1:
294 result = _a.sent();
295 defaultExport = result && result.__esModule ? result.default : result;
296 if (defaultExport instanceof Array && defaultExport[0] instanceof Array && defaultExport.hasOwnProperty('toString')) {
297 // we're dealing with a file loaded using the css-loader:
298 return [2 /*return*/, defaultExport.toString()];
299 }
300 return [2 /*return*/, result];
301 }
302 });
303 });
304 };
305 /**
306 * Alters a module id so that it includes a plugin loader.
307 * @param url The url of the module to load.
308 * @param pluginName The plugin to apply to the module id.
309 * @return The plugin-based module id.
310 */
311 WebpackLoader.prototype.applyPluginToUrl = function (url, pluginName) {
312 return pluginName + "!" + url;
313 };
314 /**
315 * Registers a plugin with the loader.
316 * @param pluginName The name of the plugin.
317 * @param implementation The plugin implementation.
318 */
319 WebpackLoader.prototype.addPlugin = function (pluginName, implementation) {
320 this.loaderPlugins[pluginName] = implementation;
321 };
322 return WebpackLoader;
323}(Loader));
324export { WebpackLoader };
325PLATFORM.Loader = WebpackLoader;