1 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2 | return new (P || (P = Promise))(function (resolve, reject) {
|
3 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
4 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
5 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
6 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
7 | });
|
8 | };
|
9 | import { Origin } from 'aurelia-metadata';
|
10 | import { Loader } from 'aurelia-loader';
|
11 | import { DOM, PLATFORM } from 'aurelia-pal';
|
12 |
|
13 |
|
14 |
|
15 | export class TextTemplateLoader {
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | loadTemplate(loader, entry) {
|
23 | return __awaiter(this, void 0, void 0, function* () {
|
24 | const text = yield loader.loadText(entry.address);
|
25 | entry.template = DOM.createTemplateFromMarkup(text);
|
26 | });
|
27 | }
|
28 | }
|
29 | export function ensureOriginOnExports(moduleExports, moduleId) {
|
30 | let target = moduleExports;
|
31 | let key;
|
32 | let exportedValue;
|
33 | if (target.__useDefault) {
|
34 | target = target.default;
|
35 | }
|
36 | Origin.set(target, new Origin(moduleId, 'default'));
|
37 | if (typeof target === 'object') {
|
38 | for (key in target) {
|
39 | exportedValue = target[key];
|
40 | if (typeof exportedValue === 'function') {
|
41 | Origin.set(exportedValue, new Origin(moduleId, key));
|
42 | }
|
43 | }
|
44 | }
|
45 | return moduleExports;
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 | export class WebpackLoader extends Loader {
|
51 | constructor() {
|
52 | super();
|
53 | this.moduleRegistry = Object.create(null);
|
54 | this.loaderPlugins = Object.create(null);
|
55 | this.modulesBeingLoaded = new Map();
|
56 | this.useTemplateLoader(new TextTemplateLoader());
|
57 | this.addPlugin('template-registry-entry', {
|
58 | fetch: (moduleId) => __awaiter(this, void 0, void 0, function* () {
|
59 |
|
60 | if (module.hot) {
|
61 | if (!this.hmrContext) {
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | const { HmrContext } = require('aurelia-hot-module-reload');
|
67 | this.hmrContext = new HmrContext(this);
|
68 | }
|
69 | module.hot.accept(moduleId, () => __awaiter(this, void 0, void 0, function* () {
|
70 | yield this.hmrContext.handleViewChange(moduleId);
|
71 | }));
|
72 | }
|
73 | const entry = this.getOrCreateTemplateRegistryEntry(moduleId);
|
74 | if (!entry.templateIsLoaded) {
|
75 | yield this.templateLoader.loadTemplate(this, entry);
|
76 | }
|
77 | return entry;
|
78 | })
|
79 | });
|
80 | PLATFORM.eachModule = callback => {
|
81 | const registry = __webpack_require__.c;
|
82 | const cachedModuleIds = Object.getOwnPropertyNames(registry);
|
83 | cachedModuleIds
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | .some(moduleId => {
|
89 | const moduleExports = registry[moduleId].exports;
|
90 | if (typeof moduleExports === 'object') {
|
91 | return callback(moduleId, moduleExports);
|
92 | }
|
93 | return false;
|
94 | });
|
95 | };
|
96 | }
|
97 | _import(address, defaultHMR = true) {
|
98 | return __awaiter(this, void 0, void 0, function* () {
|
99 | const addressParts = address.split('!');
|
100 | const moduleId = addressParts.splice(addressParts.length - 1, 1)[0];
|
101 | const loaderPlugin = addressParts.length === 1 ? addressParts[0] : null;
|
102 | if (loaderPlugin) {
|
103 | const plugin = this.loaderPlugins[loaderPlugin];
|
104 | if (!plugin) {
|
105 | throw new Error(`Plugin ${loaderPlugin} is not registered in the loader.`);
|
106 | }
|
107 | if (module.hot && plugin.hot) {
|
108 | module.hot.accept(moduleId, () => plugin.hot(moduleId));
|
109 | }
|
110 | return yield plugin.fetch(moduleId);
|
111 | }
|
112 | if (__webpack_require__.m[moduleId]) {
|
113 | if (defaultHMR && module.hot && this.hmrContext) {
|
114 | module.hot.accept(moduleId, () => this.hmrContext.handleModuleChange(moduleId, module.hot));
|
115 | }
|
116 | return __webpack_require__(moduleId);
|
117 | }
|
118 | const asyncModuleId = `async!${moduleId}`;
|
119 | if (__webpack_require__.m[asyncModuleId]) {
|
120 | if (defaultHMR && module.hot && this.hmrContext) {
|
121 | module.hot.accept(moduleId, () => this.hmrContext.handleModuleChange(moduleId, module.hot));
|
122 | module.hot.accept(asyncModuleId, () => this.hmrContext.handleModuleChange(moduleId, module.hot));
|
123 | }
|
124 | const callback = __webpack_require__(asyncModuleId);
|
125 | return yield new Promise(callback);
|
126 | }
|
127 | throw new Error(`Unable to find module with ID: ${moduleId}`);
|
128 | });
|
129 | }
|
130 | |
131 |
|
132 |
|
133 |
|
134 |
|
135 | map(id, source) { }
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | normalizeSync(moduleId, relativeTo) {
|
143 | return moduleId;
|
144 | }
|
145 | |
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | normalize(moduleId, relativeTo) {
|
152 | return Promise.resolve(moduleId);
|
153 | }
|
154 | |
155 |
|
156 |
|
157 |
|
158 | useTemplateLoader(templateLoader) {
|
159 | this.templateLoader = templateLoader;
|
160 | }
|
161 | |
162 |
|
163 |
|
164 |
|
165 |
|
166 | loadAllModules(ids) {
|
167 | return Promise.all(ids.map(id => this.loadModule(id)));
|
168 | }
|
169 | |
170 |
|
171 |
|
172 |
|
173 |
|
174 | loadModule(moduleId, defaultHMR = true) {
|
175 | return __awaiter(this, void 0, void 0, function* () {
|
176 | let existing = this.moduleRegistry[moduleId];
|
177 | if (existing) {
|
178 | return existing;
|
179 | }
|
180 | let beingLoaded = this.modulesBeingLoaded.get(moduleId);
|
181 | if (beingLoaded) {
|
182 | return beingLoaded;
|
183 | }
|
184 | beingLoaded = this._import(moduleId, defaultHMR);
|
185 | this.modulesBeingLoaded.set(moduleId, beingLoaded);
|
186 | const moduleExports = yield beingLoaded;
|
187 | this.moduleRegistry[moduleId] = ensureOriginOnExports(moduleExports, moduleId);
|
188 | this.modulesBeingLoaded.delete(moduleId);
|
189 | return moduleExports;
|
190 | });
|
191 | }
|
192 | |
193 |
|
194 |
|
195 |
|
196 |
|
197 | loadTemplate(url) {
|
198 | return this.loadModule(this.applyPluginToUrl(url, 'template-registry-entry'), false);
|
199 | }
|
200 | |
201 |
|
202 |
|
203 |
|
204 |
|
205 | loadText(url) {
|
206 | return __awaiter(this, void 0, void 0, function* () {
|
207 | const result = yield this.loadModule(url, false);
|
208 |
|
209 | const defaultExport = result && result.__esModule ? result.default : result;
|
210 | if (defaultExport instanceof Array && defaultExport[0] instanceof Array && defaultExport.hasOwnProperty('toString')) {
|
211 |
|
212 | return defaultExport.toString();
|
213 | }
|
214 | return result;
|
215 | });
|
216 | }
|
217 | |
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | applyPluginToUrl(url, pluginName) {
|
224 | return `${pluginName}!${url}`;
|
225 | }
|
226 | |
227 |
|
228 |
|
229 |
|
230 |
|
231 | addPlugin(pluginName, implementation) {
|
232 | this.loaderPlugins[pluginName] = implementation;
|
233 | }
|
234 | }
|
235 | PLATFORM.Loader = WebpackLoader;
|