-
-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathcore.ts
More file actions
293 lines (247 loc) · 10.2 KB
/
Copy pathcore.ts
File metadata and controls
293 lines (247 loc) · 10.2 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import "systemjs";
declare type ReadyCallbackFn = () => void;
/**
* A map for a package name to it's URL path.
*/
declare type PackageMap = {
/** The name of the package that will be used during imports. */
package: string;
/** The alias to be used instead of the requested import name. */
alias: string;
/**
* True if this is a wild card package. That is, if a '*' character was
* found that will be used for substitution.
*/
wildcard: boolean;
};
/**
* Describes the export names from the vendor bundle.
*/
const vendorMaps: Record<string, string> = {
"ant-design-vue": "AntDesignVue",
"axios": "Axios",
"luxon": "Luxon",
"mitt": "Mitt",
"vue": "Vue",
"tslib": "TSLib"
};
/**
* Describes the alias maps for package paths.
*/
const packageMaps: Record<string, string> = {
"@Obsidian/Controls/*": "/Obsidian/Controls/*",
"@Obsidian/Core/*": "/Obsidian/Core/*",
"@Obsidian/Directives/*": "/Obsidian/Directives/*",
"@Obsidian/Enums/*": "/Obsidian/Enums/*.js",
"@Obsidian/Libs/*": "/Obsidian/Libs/*",
"@Obsidian/FieldTypes/*": "/Obsidian/FieldTypes/*",
"@Obsidian/PageState": "/Obsidian/PageState.js",
"@Obsidian/SystemGuids/*": "/Obsidian/SystemGuids/*",
"@Obsidian/Templates/*": "/Obsidian/Templates/*",
"@Obsidian/Utility/*": "/Obsidian/Utility.js",
"@Obsidian/Utility": "/Obsidian/Utility.js",
"@Obsidian/ValidationRules": "/Obsidian/ValidationRules.js"
};
/**
* The options that obsidian should be initialized with.
*/
interface IObsidianOptions {
fingerprint?: string;
}
/**
* Handles initialization of the Obsidian framework. This allows for blocks
* and pages to delay their own initialization until the Obsidian framework
* is ready. Currently SystemJS and other base requirements are bundled together,
* but that could change in the future to be asynchronous loading of components.
*/
class Obsidian {
/** Functions that will need to be called once Obsidian is ready. */
private readonly callbacks: Array<ReadyCallbackFn> = [];
/** True if Obsidian has fully initialized and is ready to render. */
private isReady: boolean = false;
/** The options Obsidian was initialized with. */
private options!: Required<IObsidianOptions>;
/** The package maps that have been parsed. */
private packageMaps: PackageMap[];
/** Creates a new instance of the Obsidian core framework. */
constructor() {
this.packageMaps = [];
for (const packageId in packageMaps) {
this.packageMaps.push({
package: packageId.replace("*", ""),
alias: packageMaps[packageId],
wildcard: packageId.indexOf("*") !== -1
});
}
}
/**
* Registers a callback to be called when the Obsidian framework is ready.
*
* @param callback The callback.
*/
public onReady(callback: ReadyCallbackFn): void {
if (this.isReady) {
callback();
}
else {
this.callbacks.push(callback);
}
}
/**
* Configures SystemJS to append the default extension.
*/
private configureDefaultExtension(): void {
const origResolve = System.constructor.prototype.resolve;
const defaultExtension = ".js";
const expectedExtensions = [defaultExtension, ".ts", ".css", ".json"];
System.constructor.prototype.resolve = function (moduleId: string, ...args: unknown[]): string {
const moduleWithoutQuery = moduleId.indexOf("?") !== -1 ? moduleId.substr(0, moduleId.indexOf("?")) : moduleId;
const isPackage = moduleId.indexOf("/") === -1 && moduleId.indexOf("\\") === -1;
const hasExtension = expectedExtensions.some(ext => moduleWithoutQuery.endsWith(ext));
return origResolve.call(
this,
(hasExtension || isPackage) ? moduleId : `${moduleId}${defaultExtension}`,
...args
);
};
}
/**
* Configures SystemJS to append the default extension.
*/
private configureThumbprint(): void {
const origResolve = System.constructor.prototype.resolve;
const defaultExtension = ".js";
const expectedExtensions = [defaultExtension, ".ts", ".css", ".json"];
const options = this.options;
System.constructor.prototype.resolve = function (moduleId: string, ...args: unknown[]): string {
let url = origResolve.call(this, moduleId, ...args) as string;
const hasExtension = expectedExtensions.some(ext => url.endsWith(ext));
if (hasExtension) {
if (url.indexOf("?") === -1) {
url += `?${options.fingerprint}`;
}
else {
url += `&${options.fingerprint}`;
}
}
return url;
};
}
/**
* Configures any modules that are bundled in this script file.
*/
private configureBundledMaps(): void {
// Instruct SystemJS to accept the module name if it is a mapped one.
const originalResolve = System.constructor.prototype.resolve;
System.constructor.prototype.resolve = function (id: string, parentUrl?: string): string {
if (vendorMaps[id] !== undefined) {
return id;
}
return originalResolve(id, parentUrl);
};
// Intercept a request to instantiate a new module and if it is one
// of our mapped modules then just return it from the bundle.
const originalInstantiate = System.constructor.prototype.instantiate;
System.constructor.prototype.instantiate = async function (url: string, parentUrl?: string): Promise<unknown> {
if (vendorMaps[url] !== undefined) {
const module = await System.import("/Obsidian/obsidian-vendor.js", parentUrl);
return [[], function (exportFn: System.ExportFn): System.Declare {
return {
execute(): void {
exportFn(module[vendorMaps[url]]);
}
};
}];
}
else {
return await originalInstantiate.call(this, url, parentUrl);
}
};
}
/**
* Configures any modules that are aliased to specific locations.
*/
private configurePackageMaps(): void {
const originalResolve = System.constructor.prototype.resolve;
const parsedPackageMaps = this.packageMaps;
// Instruct SystemJS to accept the package alias if it is a package.
System.constructor.prototype.resolve = function (id: string, parentUrl?: string): string {
const map = parsedPackageMaps.find(pkg => id.startsWith(pkg.package));
if (!map) {
return originalResolve(id, parentUrl);
}
if (map.wildcard) {
if (map.alias.indexOf("*.js") !== -1) {
const packageSuffix = id.substring(map.package.length).split("/")[0];
const newAlias = `${map.alias.replace("*", packageSuffix)}${id.substring(map.package.length + packageSuffix.length)}`;
return originalResolve(newAlias);
}
else if (map.alias.indexOf("*") !== -1) {
const packageSuffix = id.substring(map.package.length);
return originalResolve(map.alias.replace("*", packageSuffix));
}
else {
const packageSuffix = id.substring(map.package.length);
return originalResolve(`${map.alias}/${packageSuffix}`);
}
}
else {
return originalResolve(map.alias);
}
};
// Intercept a request to instantiate a new module and if it is one
// of our mapped modules then just return it from the bundle.
const originalInstantiate = System.constructor.prototype.instantiate;
System.constructor.prototype.instantiate = async function (url: string, parentUrl?: string): Promise<unknown> {
if (url.indexOf(".js/") !== -1) {
let newUrl = url.substring(0, url.indexOf(".js/") + 3);
const path = url.substring(url.indexOf(".js/") + 4, url.indexOf("?") !== -1 ? url.indexOf("?") : undefined);
if (url.indexOf("?") !== -1) {
newUrl = newUrl + url.substring(url.indexOf("?"));
}
let module = await System.import(newUrl, parentUrl);
return [[], function (exportFn: System.ExportFn): System.Declare {
return {
execute(): void {
const segments = path.split("/");
for (let i = 0; i < segments.length; i++) {
let segment = segments[i];
if (segment.endsWith(".js")) {
segment = segment.substring(0, segment.length - 3);
}
//segment = segment[0].toUpperCase() + segment.substring(1);
module = module[segment];
}
exportFn(module);
}
};
}];
}
else {
return await originalInstantiate.call(this, url, parentUrl);
}
};
}
/**
* Initialize the framework.
*/
public init(options?: IObsidianOptions): void {
this.options = {
fingerprint: options?.fingerprint ?? ""
};
this.configureDefaultExtension();
if (this.options.fingerprint !== "") {
this.configureThumbprint();
}
this.configureBundledMaps();
this.configurePackageMaps();
this.isReady = true;
// The concept of the callbacks is here due to backwards compatibility
// as well as allowing for later requiring dynamically loading other
// pre-requisites before initializing Obsidian.
for (const callback of this.callbacks) {
callback();
}
}
}
export default new Obsidian();