@zikojs/notebook 0.6.0 → 0.7.0
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/package.json +1 -3
- package/src/transform.js +12 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zikojs/notebook",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -45,10 +45,8 @@
|
|
|
45
45
|
"@codemirror/view": "^6.43.11",
|
|
46
46
|
"@zikojs/lucide": "^0.2.2",
|
|
47
47
|
"acorn": "^8.18.0",
|
|
48
|
-
"canvas-confetti": "^1.9.4",
|
|
49
48
|
"codemirror": "^6.0.2",
|
|
50
49
|
"lucide": "^1.44.0",
|
|
51
|
-
"marked": "^18.0.12",
|
|
52
50
|
"rehype-stringify": "^10.0.1",
|
|
53
51
|
"remark": "^15.0.1",
|
|
54
52
|
"remark-rehype": "^11.1.2",
|
package/src/transform.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import van from "vanjs-core";
|
|
2
1
|
import * as acorn from "acorn";
|
|
3
2
|
|
|
4
3
|
export const rewriteImportNode = (node, code, importMapConfig) => {
|
|
@@ -21,19 +20,19 @@ export const rewriteImportNode = (node, code, importMapConfig) => {
|
|
|
21
20
|
else if (spec.type === "ImportSpecifier") namedImports.push({ imported: spec.imported.name, local: spec.local.name });
|
|
22
21
|
});
|
|
23
22
|
|
|
24
|
-
if (namespaceId) return `
|
|
23
|
+
if (namespaceId) return `__scope__.${namespaceId} = await import("${source}");`;
|
|
25
24
|
|
|
26
25
|
const tmp = `__mod_${node.start}`;
|
|
27
26
|
let lines = [`const ${tmp} = await import("${source}");`];
|
|
28
27
|
|
|
29
28
|
if (defaultImportName && namedImports.length === 0) {
|
|
30
|
-
lines.push(`
|
|
29
|
+
lines.push(`__scope__.${defaultImportName} = ${tmp}.default ?? ${tmp};`);
|
|
31
30
|
return lines.join("\n");
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
if (defaultImportName) lines.push(`
|
|
33
|
+
if (defaultImportName) lines.push(`__scope__.${defaultImportName} = ${tmp}.default ?? ${tmp};`);
|
|
35
34
|
namedImports.forEach(({ imported, local }) => {
|
|
36
|
-
lines.push(`
|
|
35
|
+
lines.push(`__scope__.${local} = ${tmp}.${imported} !== undefined ? ${tmp}.${imported} : (${tmp}.default ?? ${tmp});`);
|
|
37
36
|
});
|
|
38
37
|
|
|
39
38
|
return lines.join("\n");
|
|
@@ -53,7 +52,7 @@ export const transformImportsAndScope = (code, importMapConfig) => {
|
|
|
53
52
|
if (decl.id.type === "Identifier") {
|
|
54
53
|
const varName = decl.id.name;
|
|
55
54
|
const initCode = decl.init ? code.slice(decl.init.start, decl.init.end) : "undefined";
|
|
56
|
-
replacementCode += `
|
|
55
|
+
replacementCode += `__scope__.${varName} = ${initCode};\n`;
|
|
57
56
|
} else if (decl.id.type === "ObjectPattern") {
|
|
58
57
|
const initCode = decl.init ? code.slice(decl.init.start, decl.init.end) : "undefined";
|
|
59
58
|
const tempVar = `__destruct_obj_${decl.start}`;
|
|
@@ -62,7 +61,7 @@ export const transformImportsAndScope = (code, importMapConfig) => {
|
|
|
62
61
|
if (prop.value && prop.value.type === "Identifier") {
|
|
63
62
|
const localName = prop.value.name;
|
|
64
63
|
const keyName = prop.key.type === "Identifier" ? prop.key.name : prop.key.value;
|
|
65
|
-
replacementCode += `
|
|
64
|
+
replacementCode += `__scope__.${localName} = ${tempVar}.${keyName}; `;
|
|
66
65
|
}
|
|
67
66
|
});
|
|
68
67
|
replacementCode += `})();\n`;
|
|
@@ -73,7 +72,7 @@ export const transformImportsAndScope = (code, importMapConfig) => {
|
|
|
73
72
|
decl.id.elements.forEach((elem, index) => {
|
|
74
73
|
if (elem && elem.type === "Identifier") {
|
|
75
74
|
const localName = elem.name;
|
|
76
|
-
replacementCode += `
|
|
75
|
+
replacementCode += `__scope__.${localName} = ${tempVar}[${index}]; `;
|
|
77
76
|
}
|
|
78
77
|
});
|
|
79
78
|
replacementCode += `})();\n`;
|
|
@@ -83,11 +82,11 @@ export const transformImportsAndScope = (code, importMapConfig) => {
|
|
|
83
82
|
} else if (node.type === "FunctionDeclaration" && node.id) {
|
|
84
83
|
const funcName = node.id.name;
|
|
85
84
|
const funcBody = code.slice(node.start, node.end);
|
|
86
|
-
modifications.push({ start: node.start, end: node.end, replacement: `
|
|
85
|
+
modifications.push({ start: node.start, end: node.end, replacement: `__scope__.${funcName} = ${funcBody.replace(/^function\s+\w+/, "function")}` });
|
|
87
86
|
} else if (node.type === "ClassDeclaration" && node.id) {
|
|
88
87
|
const className = node.id.name;
|
|
89
88
|
const classBody = code.slice(node.start, node.end);
|
|
90
|
-
modifications.push({ start: node.start, end: node.end, replacement: `
|
|
89
|
+
modifications.push({ start: node.start, end: node.end, replacement: `__scope__.${className} = ${classBody.replace(/^class\s+\w+/, "class")}` });
|
|
91
90
|
}
|
|
92
91
|
});
|
|
93
92
|
|
|
@@ -105,16 +104,13 @@ export const evaluateCodeAsync = async (code, TARGET, importMapConfig, scope) =>
|
|
|
105
104
|
const validScope = scope || Object.create(null);
|
|
106
105
|
const compiledCode = transformImportsAndScope(code, importMapConfig);
|
|
107
106
|
|
|
108
|
-
const runCell = new Function("TARGET", "
|
|
107
|
+
const runCell = new Function("TARGET", "__scope__", `
|
|
109
108
|
return (async () => {
|
|
110
|
-
with (
|
|
109
|
+
with (__scope__) {
|
|
111
110
|
${compiledCode}
|
|
112
111
|
}
|
|
113
112
|
})();
|
|
114
113
|
`);
|
|
115
114
|
|
|
116
|
-
const result = await runCell(TARGET,
|
|
117
|
-
// if (result !== undefined && result !== null) {
|
|
118
|
-
// van.add(TARGET, result);
|
|
119
|
-
// }
|
|
115
|
+
const result = await runCell(TARGET, validScope);
|
|
120
116
|
};
|