UNPKG

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