-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
is-land-autoinit.js
42 lines (39 loc) · 1.07 KB
/
is-land-autoinit.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
class IslandAutoinit {
static attr = {
autoInitType: "autoinit",
import: "import",
}
static types = {
"petite-vue": function(lib) {
lib.createApp().mount(this);
},
"vue": function(lib) {
lib.createApp().mount(this);
},
"svelte": function(lib) {
new lib.default({ target: this });
},
"svelte-ssr": function(lib) {
new lib.default({ target: this, hydrate: true });
},
"preact": function(lib) {
lib.default(this);
}
};
}
Island.onReady.set("import-autoinit", async function(Island) {
let mod;
// [dependency="my-component-code.js"]
let importScript = this.getAttribute(IslandAutoinit.attr.import);
if(importScript) {
// we could resolve import maps here manually but you’d still have to use the full URL in your script’s import anyway
mod = await import(importScript);
}
if(mod) {
// Use `import=""` for when import maps are available e.g. `import="petite-vue"`
let fn = IslandAutoinit.types[this.getAttribute(IslandAutoinit.attr.autoInitType) || importScript];
if(fn) {
await fn.call(this, mod);
}
}
})