@zikojs/notebook-runtime-svelte 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/package.json +5 -2
- package/src/index.js +80 -80
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 ZAKARIA ELALAOUI
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ZAKARIA ELALAOUI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zikojs/notebook-runtime-svelte",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -14,8 +14,11 @@
|
|
|
14
14
|
"author": "zakaria elalaoui",
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"type": "module",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/zikojs/notebook.git"
|
|
20
|
+
},
|
|
17
21
|
"dependencies": {
|
|
18
|
-
"@babel/standalone": "latest",
|
|
19
22
|
"@codemirror/theme-one-dark": "^6.1.3",
|
|
20
23
|
"codemirror-lang-svelte": "^2.1.0",
|
|
21
24
|
"svelte": "^5.57.1",
|
package/src/index.js
CHANGED
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
import { compile } from "svelte/compiler";
|
|
2
|
-
import {
|
|
3
|
-
mount,
|
|
4
|
-
unmount
|
|
5
|
-
} from "https://esm.sh/svelte";
|
|
6
|
-
|
|
7
|
-
import { svelte } from "codemirror-lang-svelte";
|
|
8
|
-
// import { oneDark } from "@codemirror/theme-one-dark";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import { tags } from 'ziko/dom'
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export const SvelteIdentifier = 'svelte'
|
|
15
|
-
export const sveltePlugin = ({
|
|
16
|
-
runes = true,
|
|
17
|
-
} = {}) => ({
|
|
18
|
-
name: SvelteIdentifier,
|
|
19
|
-
language: svelte(),
|
|
20
|
-
|
|
21
|
-
async evaluate(code, context) {
|
|
22
|
-
const { TARGET } = context;
|
|
23
|
-
if (!TARGET) return;
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
// 1. Compile Svelte code
|
|
27
|
-
const result = compile(code, {
|
|
28
|
-
filename: "Component.svelte",
|
|
29
|
-
runes,
|
|
30
|
-
css: "injected",
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
let jsCode = result.js.code;
|
|
34
|
-
|
|
35
|
-
// 2. Rewrite bare svelte imports in the compiled code to full esm.sh URLs
|
|
36
|
-
jsCode = jsCode.replace(
|
|
37
|
-
/from\s+['"](svelte[^'"]*)['"]/g,
|
|
38
|
-
(match, source) => `from "https://esm.sh/${source}"`
|
|
39
|
-
);
|
|
40
|
-
jsCode = jsCode.replace(
|
|
41
|
-
/import\s+['"](svelte[^'"]*)['"]/g,
|
|
42
|
-
(match, source) => `import "https://esm.sh/${source}"`
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
// 3. Wrap in a Blob URL and import natively
|
|
46
|
-
const blob = new Blob([jsCode], { type: "application/javascript" });
|
|
47
|
-
const moduleUrl = URL.createObjectURL(blob);
|
|
48
|
-
|
|
49
|
-
const mod = await import(/* @vite-ignore */moduleUrl);
|
|
50
|
-
const Component = mod.default;
|
|
51
|
-
URL.revokeObjectURL(moduleUrl);
|
|
52
|
-
|
|
53
|
-
if (!Component) {
|
|
54
|
-
throw new Error("Svelte component failed to compile or export a default component.");
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// 4. Clean up and mount
|
|
58
|
-
if (TARGET.__svelteInstance) {
|
|
59
|
-
unmount(TARGET.__svelteInstance);
|
|
60
|
-
TARGET.__svelteInstance = null;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
TARGET.innerHTML = "";
|
|
64
|
-
const container = tags.div().element;
|
|
65
|
-
TARGET.appendChild(container);
|
|
66
|
-
|
|
67
|
-
TARGET.__svelteInstance = mount(Component, {
|
|
68
|
-
target: container,
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
return TARGET.__svelteInstance;
|
|
72
|
-
} catch (err) {
|
|
73
|
-
tags.pre(`Svelte Error: ${err.message}`).style({
|
|
74
|
-
color : 'red',
|
|
75
|
-
whiteSpace : 'pre-wrap'
|
|
76
|
-
}).mount(TARGET)
|
|
77
|
-
console.error(err);
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
|
|
1
|
+
import { compile } from "svelte/compiler";
|
|
2
|
+
import {
|
|
3
|
+
mount,
|
|
4
|
+
unmount
|
|
5
|
+
} from "https://esm.sh/svelte";
|
|
6
|
+
|
|
7
|
+
import { svelte } from "codemirror-lang-svelte";
|
|
8
|
+
// import { oneDark } from "@codemirror/theme-one-dark";
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
import { tags } from 'ziko/dom'
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export const SvelteIdentifier = 'svelte'
|
|
15
|
+
export const sveltePlugin = ({
|
|
16
|
+
runes = true,
|
|
17
|
+
} = {}) => ({
|
|
18
|
+
name: SvelteIdentifier,
|
|
19
|
+
language: svelte(),
|
|
20
|
+
|
|
21
|
+
async evaluate(code, context) {
|
|
22
|
+
const { TARGET } = context;
|
|
23
|
+
if (!TARGET) return;
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
// 1. Compile Svelte code
|
|
27
|
+
const result = compile(code, {
|
|
28
|
+
filename: "Component.svelte",
|
|
29
|
+
runes,
|
|
30
|
+
css: "injected",
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
let jsCode = result.js.code;
|
|
34
|
+
|
|
35
|
+
// 2. Rewrite bare svelte imports in the compiled code to full esm.sh URLs
|
|
36
|
+
jsCode = jsCode.replace(
|
|
37
|
+
/from\s+['"](svelte[^'"]*)['"]/g,
|
|
38
|
+
(match, source) => `from "https://esm.sh/${source}"`
|
|
39
|
+
);
|
|
40
|
+
jsCode = jsCode.replace(
|
|
41
|
+
/import\s+['"](svelte[^'"]*)['"]/g,
|
|
42
|
+
(match, source) => `import "https://esm.sh/${source}"`
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
// 3. Wrap in a Blob URL and import natively
|
|
46
|
+
const blob = new Blob([jsCode], { type: "application/javascript" });
|
|
47
|
+
const moduleUrl = URL.createObjectURL(blob);
|
|
48
|
+
|
|
49
|
+
const mod = await import(/* @vite-ignore */moduleUrl);
|
|
50
|
+
const Component = mod.default;
|
|
51
|
+
URL.revokeObjectURL(moduleUrl);
|
|
52
|
+
|
|
53
|
+
if (!Component) {
|
|
54
|
+
throw new Error("Svelte component failed to compile or export a default component.");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 4. Clean up and mount
|
|
58
|
+
if (TARGET.__svelteInstance) {
|
|
59
|
+
unmount(TARGET.__svelteInstance);
|
|
60
|
+
TARGET.__svelteInstance = null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
TARGET.innerHTML = "";
|
|
64
|
+
const container = tags.div().element;
|
|
65
|
+
TARGET.appendChild(container);
|
|
66
|
+
|
|
67
|
+
TARGET.__svelteInstance = mount(Component, {
|
|
68
|
+
target: container,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return TARGET.__svelteInstance;
|
|
72
|
+
} catch (err) {
|
|
73
|
+
tags.pre(`Svelte Error: ${err.message}`).style({
|
|
74
|
+
color : 'red',
|
|
75
|
+
whiteSpace : 'pre-wrap'
|
|
76
|
+
}).mount(TARGET)
|
|
77
|
+
console.error(err);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
|
|
81
81
|
});
|