-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
script.js
177 lines (142 loc) · 5.58 KB
/
script.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
import { platform } from '../../core/platform.js';
import { script } from '../script.js';
import { ScriptType } from '../script/script-type.js';
import { ScriptTypes } from '../script/script-types.js';
import { registerScript } from '../script/script.js';
import { ResourceLoader } from './loader.js';
import { ResourceHandler } from './handler.js';
/**
* Resource handler for loading JavaScript files dynamically. Two types of JavaScript files can be
* loaded, PlayCanvas scripts which contain calls to {@link createScript}, or regular JavaScript
* files, such as third-party libraries.
*
* @category Script
*/
class ScriptHandler extends ResourceHandler {
/**
* Create a new ScriptHandler instance.
*
* @param {import('../app-base.js').AppBase} app - The running {@link AppBase}.
* @ignore
*/
constructor(app) {
super(app);
this.handlerType = 'script';
this._scripts = { };
this._cache = { };
}
clearCache() {
for (const key in this._cache) {
const element = this._cache[key];
const parent = element.parentNode;
if (parent)
parent.removeChild(element);
}
this._cache = {};
}
load(url, callback) {
// Scripts don't support bundling since we concatenate them. Below is for consistency.
if (typeof url === 'string') {
url = {
load: url,
original: url
};
}
const self = this;
script.app = this._app;
const onScriptLoad = (url.load, (err, url, extra) => {
if (!err) {
if (script.legacy) {
let Type = null;
// pop the type from the loading stack
if (ScriptTypes._types.length) {
Type = ScriptTypes._types.pop();
}
if (Type) {
// store indexed by URL
this._scripts[url] = Type;
} else {
Type = null;
}
// return the resource
callback(null, Type, extra);
} else {
const obj = { };
for (let i = 0; i < ScriptTypes._types.length; i++)
obj[ScriptTypes._types[i].name] = ScriptTypes._types[i];
ScriptTypes._types.length = 0;
callback(null, obj, extra);
// no cache for scripts
delete self._loader._cache[ResourceLoader.makeKey(url, 'script')];
}
} else {
callback(err);
}
});
// check if we're loading a module or a classic script
const [basePath, search] = url.load.split('?');
const isEsmScript = basePath.endsWith('.mjs');
if (isEsmScript) {
// The browser will hold its own cache of the script, so we need to bust it
let path = url.load;
if (path.startsWith(this._app.assets.prefix)) {
path = path.replace(this._app.assets.prefix, '');
}
const hash = this._app.assets.getByUrl(path).file.hash;
const searchParams = new URLSearchParams(search);
searchParams.set('hash', hash);
const urlWithHash = `${basePath}?${searchParams.toString()}`;
this._loadModule(urlWithHash, onScriptLoad);
} else {
this._loadScript(url.load, onScriptLoad);
}
}
open(url, data) {
return data;
}
patch(asset, assets) { }
_loadScript(url, callback) {
const head = document.head;
const element = document.createElement('script');
this._cache[url] = element;
// use async=false to force scripts to execute in order
element.async = false;
element.addEventListener('error', function (e) {
callback(`Script: ${e.target.src} failed to load`);
}, false);
let done = false;
element.onload = element.onreadystatechange = function () {
if (!done && (!this.readyState || (this.readyState === 'loaded' || this.readyState === 'complete'))) {
done = true; // prevent double event firing
callback(null, url, element);
}
};
// set the src attribute after the onload callback is set, to avoid an instant loading failing to fire the callback
element.src = url;
head.appendChild(element);
}
_loadModule(url, callback) {
// if we're in the browser, we need to use the full URL
const baseUrl = platform.browser ? window.location.origin : import.meta.url;
const importUrl = new URL(url, baseUrl);
// @ts-ignore
import(importUrl.toString()).then((module) => {
for (const key in module) {
const scriptClass = module[key];
const extendsScriptType = scriptClass.prototype instanceof ScriptType;
if (extendsScriptType) {
if (script.attributesDefinition) {
for (const key in script.attributesDefinition) {
scriptClass.attributes.add(key, script.attributesDefinition[key]);
}
}
registerScript(scriptClass, scriptClass.name.toLowerCase());
}
}
callback(null, url, null);
}).catch((err) => {
callback(err);
});
}
}
export { ScriptHandler };