orvyn-lang 0.2.0 → 0.3.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/GUIDE.md +149 -0
- package/LANGUAGE.md +744 -27
- package/README.md +211 -250
- package/brand/orvyn-sunset-animated.svg +15 -0
- package/package.json +3 -1
- package/src/cli/orvyn.js +186 -10
- package/src/core/signal.js +104 -6
- package/src/devtools/runtime.js +25 -1
- package/src/lang/a11y.js +102 -0
- package/src/lang/auth.js +239 -0
- package/src/lang/bundle-native.js +10 -6
- package/src/lang/codegen.js +428 -57
- package/src/lang/device.js +41 -0
- package/src/lang/doctor.js +60 -0
- package/src/lang/env.js +27 -0
- package/src/lang/errors.js +513 -0
- package/src/lang/form.js +69 -8
- package/src/lang/index.js +2 -1
- package/src/lang/lexer.js +2 -0
- package/src/lang/list.js +44 -0
- package/src/lang/native/android/MainActivity.kt +43 -4
- package/src/lang/native/android/OrvynDevice.kt +163 -0
- package/src/lang/native/android/OrvynEngine.kt +154 -7
- package/src/lang/native/android/OrvynRenderer.kt +518 -44
- package/src/lang/native/android.js +74 -11
- package/src/lang/native/build.js +130 -60
- package/src/lang/native/config.js +108 -0
- package/src/lang/native/ios/OrvynApp.swift +6 -0
- package/src/lang/native/ios/OrvynEngine.swift +266 -4
- package/src/lang/native/ios/OrvynViews.swift +429 -34
- package/src/lang/native/ios.js +75 -2
- package/src/lang/native-runtime.js +406 -56
- package/src/lang/page.js +52 -0
- package/src/lang/parser.js +243 -15
- package/src/lang/prerender.js +300 -0
- package/src/lang/read-import.js +11 -0
- package/src/lang/router.js +266 -0
- package/src/lang/runtime-shared.js +792 -10
- package/src/lang/runtime.js +362 -52
- package/src/lang/select.js +13 -0
- package/src/lang/server-code.js +233 -0
- package/src/lang/server-export.js +78 -0
- package/src/lang/server-libs.js +53 -0
- package/src/lang/server.js +409 -0
- package/src/lang/test-runner.js +134 -0
- package/src/lang/types.js +312 -0
- package/src/lang/vite-plugin.js +82 -3
- package/src/lang/web-overlay.js +578 -0
- package/src/lang/widgets.js +94 -0
- package/src/lang/words.js +38 -0
- package/src/runtime/mount.js +116 -27
- package/src/runtime/patch.js +90 -11
- package/src/runtime/style.js +11 -1
- package/src/styling/fluid.js +91 -4
- package/src/styling/spec.js +306 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { OrvynSyntaxError } from "./lexer.js";
|
|
2
|
+
import { parse } from "./parser.js";
|
|
3
|
+
const ANY = { k: "any" };
|
|
4
|
+
const BASIC = ["text", "number", "yesno"];
|
|
5
|
+
const describe = (t) => t.k === "list" ? `list of ${describe(t.of)}` : t.k === "record" ? t.decl.name : t.k === "null" ? "empty" : t.k;
|
|
6
|
+
function closest(name, names) {
|
|
7
|
+
const distance = (a, b) => {
|
|
8
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
9
|
+
for (let i = 1; i <= a.length; i++) {
|
|
10
|
+
let previous = row[0];
|
|
11
|
+
row[0] = i;
|
|
12
|
+
for (let j = 1; j <= b.length; j++) {
|
|
13
|
+
const kept = row[j];
|
|
14
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, previous + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
15
|
+
previous = kept;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return row[b.length];
|
|
19
|
+
};
|
|
20
|
+
const best = names.map((n) => [distance(name.toLowerCase(), n.toLowerCase()), n]).sort((a, b) => a[0] - b[0])[0];
|
|
21
|
+
return best && best[0] <= 2 ? ` Did you mean "${best[1]}"?` : ` It has: ${names.join(", ")}`;
|
|
22
|
+
}
|
|
23
|
+
function checkTypes(program, resolve) {
|
|
24
|
+
const types = /* @__PURE__ */ new Map();
|
|
25
|
+
const unknownImports = /* @__PURE__ */ new Set();
|
|
26
|
+
const typeImports = /* @__PURE__ */ new Set();
|
|
27
|
+
const globals = /* @__PURE__ */ new Map();
|
|
28
|
+
const signatures = /* @__PURE__ */ new Map();
|
|
29
|
+
const fail = (message, at) => {
|
|
30
|
+
throw new OrvynSyntaxError(message, at.line, at.col);
|
|
31
|
+
};
|
|
32
|
+
const toType = (ref, at) => {
|
|
33
|
+
if (!ref) return ANY;
|
|
34
|
+
if (ref.list) return { k: "list", of: toType(ref.list, at) };
|
|
35
|
+
const name = ref.name;
|
|
36
|
+
if (BASIC.includes(name)) return { k: name };
|
|
37
|
+
const found = types.get(name);
|
|
38
|
+
if (found) return { k: "record", decl: found };
|
|
39
|
+
if (unknownImports.has(name)) return ANY;
|
|
40
|
+
return fail(`There is no type "${name}".${closest(name, [...BASIC, ...types.keys()])}`, at);
|
|
41
|
+
};
|
|
42
|
+
const signature = (decl) => signatures.set(decl.name, { params: decl.params, types: decl.params.map((p) => decl.paramTypes?.[p] ? toType(decl.paramTypes[p], decl) : null) });
|
|
43
|
+
const read = (use, from) => {
|
|
44
|
+
if (!from || /\.(m?js|ts)$/.test(use.path) || !/^[./]/.test(use.path)) return null;
|
|
45
|
+
const found = from(use.path);
|
|
46
|
+
if (found === null) return null;
|
|
47
|
+
const { source, resolve: next } = typeof found === "string" ? { source: found, resolve: from } : found;
|
|
48
|
+
try {
|
|
49
|
+
return { program: parse(source), resolve: next };
|
|
50
|
+
} catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const seen = /* @__PURE__ */ new Set();
|
|
55
|
+
const gather = (other) => {
|
|
56
|
+
const key = JSON.stringify(other.program);
|
|
57
|
+
if (seen.has(key)) return;
|
|
58
|
+
seen.add(key);
|
|
59
|
+
for (const each of other.program.body) {
|
|
60
|
+
if (each.kind === "Type" && !types.has(each.name)) types.set(each.name, each);
|
|
61
|
+
if (each.kind === "Use") {
|
|
62
|
+
const deeper = read(each, other.resolve);
|
|
63
|
+
if (deeper) gather(deeper);
|
|
64
|
+
else for (const name of each.names) if (/^[A-Z]/.test(name)) unknownImports.add(name);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const imported = [];
|
|
69
|
+
for (const use of program.body) {
|
|
70
|
+
if (use.kind !== "Use") continue;
|
|
71
|
+
const other = read(use, resolve);
|
|
72
|
+
if (other) gather(other);
|
|
73
|
+
for (const name of use.names) {
|
|
74
|
+
const decl = other?.program.body.find((d) => "name" in d && d.name === name);
|
|
75
|
+
if (!decl) unknownImports.add(name);
|
|
76
|
+
else if (decl.kind === "Type") typeImports.add(name);
|
|
77
|
+
else if (decl.kind === "State" || decl.kind === "Const" || decl.kind === "Fn" || decl.kind === "Screen") imported.push(decl);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
for (const decl of program.body) {
|
|
81
|
+
if (decl.kind !== "Type") continue;
|
|
82
|
+
if (BASIC.includes(decl.name)) fail(`"${decl.name}" is already a type`, decl);
|
|
83
|
+
if (types.has(decl.name) && program.body.filter((d) => d.kind === "Type" && d.name === decl.name).length > 1) fail(`Type "${decl.name}" is written twice`, decl);
|
|
84
|
+
types.set(decl.name, decl);
|
|
85
|
+
}
|
|
86
|
+
for (const decl of types.values()) for (const field of decl.fields) toType(field.type, field);
|
|
87
|
+
for (const decl of [...imported, ...program.body]) {
|
|
88
|
+
if (decl.kind === "State" || decl.kind === "Const") globals.set(decl.name, toType(decl.type, decl));
|
|
89
|
+
if (decl.kind === "Fn") signature(decl);
|
|
90
|
+
if (decl.kind === "Screen" && decl.type) signature(decl);
|
|
91
|
+
}
|
|
92
|
+
let scopes = [globals];
|
|
93
|
+
const lookup = (name) => {
|
|
94
|
+
for (let i = scopes.length - 1; i >= 0; i--) if (scopes[i].has(name)) return scopes[i].get(name);
|
|
95
|
+
return ANY;
|
|
96
|
+
};
|
|
97
|
+
const within = (names, work) => {
|
|
98
|
+
scopes.push(new Map(names));
|
|
99
|
+
try {
|
|
100
|
+
work();
|
|
101
|
+
} finally {
|
|
102
|
+
scopes.pop();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const fieldOf = (record, name, at) => {
|
|
106
|
+
const field = record.decl.fields.find((f) => f.name === name);
|
|
107
|
+
if (!field) fail(`${record.decl.name} has no "${name}".${closest(name, record.decl.fields.map((f) => f.name))}`, at);
|
|
108
|
+
return toType(field.type, field);
|
|
109
|
+
};
|
|
110
|
+
const fits = (wanted, value, what, nullable = true) => {
|
|
111
|
+
if (wanted.k === "any") return void infer(value);
|
|
112
|
+
if (value.kind === "Null") {
|
|
113
|
+
if (!nullable) fail(`${what} can't be empty`, value);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (wanted.k === "record" && value.kind === "Object") {
|
|
117
|
+
for (const entry of value.entries) fits(fieldOf(wanted, entry.key, value), entry.value, `"${entry.key}"`);
|
|
118
|
+
const missing = wanted.decl.fields.filter((f) => !f.optional && !value.entries.some((e) => e.key === f.name));
|
|
119
|
+
if (missing.length) fail(`This ${wanted.decl.name} is missing: ${missing.map((f) => f.name).join(", ")}`, value);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (wanted.k === "list" && value.kind === "List") return value.items.forEach((item) => fits(wanted.of, item, "Each item", false));
|
|
123
|
+
const got = infer(value);
|
|
124
|
+
const same = (a, b) => a.k === "any" || b.k === "any" || b.k === "null" || (a.k === "list" && b.k === "list" ? same(a.of, b.of) : a.k === "record" && b.k === "record" ? a.decl.name === b.decl.name : a.k === b.k);
|
|
125
|
+
if (!same(wanted, got)) fail(`${what} should be ${describe(wanted)}, but this is ${describe(got)}`, value);
|
|
126
|
+
};
|
|
127
|
+
const call = (expr) => {
|
|
128
|
+
const callee = expr.callee;
|
|
129
|
+
if (callee.kind === "Member") {
|
|
130
|
+
const target = infer(callee.object);
|
|
131
|
+
const word = callee.property;
|
|
132
|
+
if (target.k !== "list") {
|
|
133
|
+
expr.args.forEach((a) => infer(a));
|
|
134
|
+
return ANY;
|
|
135
|
+
}
|
|
136
|
+
const item = target.of;
|
|
137
|
+
const named = (arg) => {
|
|
138
|
+
if (item.k === "record" && arg?.kind === "String" && arg.parts.length === 1 && typeof arg.parts[0] === "string") return fieldOf(item, arg.parts[0], arg);
|
|
139
|
+
if (arg) infer(arg);
|
|
140
|
+
return ANY;
|
|
141
|
+
};
|
|
142
|
+
if (["where", "count", "first"].includes(word)) {
|
|
143
|
+
const wanted = expr.args[0];
|
|
144
|
+
if (item.k === "record" && wanted?.kind === "Object") {
|
|
145
|
+
for (const entry of wanted.entries) fits(fieldOf(item, entry.key, wanted), entry.value, `"${entry.key}"`);
|
|
146
|
+
} else expr.args.forEach((a) => infer(a));
|
|
147
|
+
return word === "count" ? { k: "number" } : word === "first" ? item : target;
|
|
148
|
+
}
|
|
149
|
+
if (word === "sortBy") {
|
|
150
|
+
named(expr.args[0]);
|
|
151
|
+
return target;
|
|
152
|
+
}
|
|
153
|
+
if (word === "search") {
|
|
154
|
+
if (expr.args[0]) infer(expr.args[0]);
|
|
155
|
+
named(expr.args[1]);
|
|
156
|
+
return target;
|
|
157
|
+
}
|
|
158
|
+
if (word === "pick") return { k: "list", of: named(expr.args[0]) };
|
|
159
|
+
if (word === "total") {
|
|
160
|
+
expr.args.forEach(named);
|
|
161
|
+
return { k: "number" };
|
|
162
|
+
}
|
|
163
|
+
if (word === "add") {
|
|
164
|
+
if (expr.args[0]) fits(item, expr.args[0], "A new item", false);
|
|
165
|
+
return ANY;
|
|
166
|
+
}
|
|
167
|
+
expr.args.forEach((a) => infer(a, item));
|
|
168
|
+
return word === "filter" ? target : word === "find" ? item : ANY;
|
|
169
|
+
}
|
|
170
|
+
const known = callee.kind === "Ident" ? signatures.get(callee.name) : void 0;
|
|
171
|
+
expr.args.forEach((arg, i) => known?.types[i] ? fits(known.types[i], arg, `"${known.params[i]}"`) : infer(arg));
|
|
172
|
+
return ANY;
|
|
173
|
+
};
|
|
174
|
+
const infer = (expr, item = ANY) => {
|
|
175
|
+
switch (expr.kind) {
|
|
176
|
+
case "Number":
|
|
177
|
+
return { k: "number" };
|
|
178
|
+
case "Bool":
|
|
179
|
+
return { k: "yesno" };
|
|
180
|
+
case "Null":
|
|
181
|
+
return { k: "null" };
|
|
182
|
+
case "String":
|
|
183
|
+
for (const part of expr.parts) if (typeof part !== "string") infer(part);
|
|
184
|
+
return { k: "text" };
|
|
185
|
+
case "Ident":
|
|
186
|
+
return lookup(expr.name);
|
|
187
|
+
case "List": {
|
|
188
|
+
const all = expr.items.map((i) => infer(i));
|
|
189
|
+
return { k: "list", of: all.length && all.every((t) => t.k === all[0].k) ? all[0] : ANY };
|
|
190
|
+
}
|
|
191
|
+
case "Object":
|
|
192
|
+
expr.entries.forEach((e) => infer(e.value));
|
|
193
|
+
return ANY;
|
|
194
|
+
case "Member": {
|
|
195
|
+
const object = infer(expr.object);
|
|
196
|
+
if (object.k === "record") return fieldOf(object, expr.property, expr);
|
|
197
|
+
return expr.property === "length" && (object.k === "list" || object.k === "text") ? { k: "number" } : ANY;
|
|
198
|
+
}
|
|
199
|
+
case "Index": {
|
|
200
|
+
const object = infer(expr.object);
|
|
201
|
+
infer(expr.index);
|
|
202
|
+
return object.k === "list" ? object.of : ANY;
|
|
203
|
+
}
|
|
204
|
+
case "Call":
|
|
205
|
+
return call(expr);
|
|
206
|
+
case "Unary":
|
|
207
|
+
infer(expr.operand);
|
|
208
|
+
return expr.op === "!" ? { k: "yesno" } : { k: "number" };
|
|
209
|
+
case "Binary": {
|
|
210
|
+
const [left, right] = [infer(expr.left), infer(expr.right)];
|
|
211
|
+
if (["==", "!=", "<", ">", "<=", ">="].includes(expr.op)) return { k: "yesno" };
|
|
212
|
+
if (["-", "*", "/", "%"].includes(expr.op)) return { k: "number" };
|
|
213
|
+
if (expr.op === "+") return left.k === "text" || right.k === "text" ? { k: "text" } : left.k === "number" && right.k === "number" ? left : ANY;
|
|
214
|
+
return expr.op === "??" ? right : ANY;
|
|
215
|
+
}
|
|
216
|
+
case "Ternary":
|
|
217
|
+
infer(expr.cond);
|
|
218
|
+
infer(expr.else, item);
|
|
219
|
+
return infer(expr.then, item);
|
|
220
|
+
case "Wait":
|
|
221
|
+
return infer(expr.expr);
|
|
222
|
+
case "Lambda":
|
|
223
|
+
within(expr.params.map((p, i) => [p, i === 0 ? item : ANY]), () => infer(expr.body));
|
|
224
|
+
return ANY;
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
const statements = (body) => within([], () => {
|
|
228
|
+
for (const stmt of body) {
|
|
229
|
+
if (stmt.kind === "Let") {
|
|
230
|
+
const declared = toType(stmt.type, stmt);
|
|
231
|
+
let found = declared;
|
|
232
|
+
if (stmt.type) fits(declared, stmt.init, `"${stmt.name}"`);
|
|
233
|
+
else found = infer(stmt.init);
|
|
234
|
+
scopes.at(-1).set(stmt.name, found);
|
|
235
|
+
} else if (stmt.kind === "Assign") {
|
|
236
|
+
const wanted = infer(stmt.target);
|
|
237
|
+
if (stmt.op === "=") fits(wanted, stmt.value, stmt.target.kind === "Ident" ? `"${stmt.target.name}"` : "This field");
|
|
238
|
+
else infer(stmt.value);
|
|
239
|
+
} else if (stmt.kind === "ExprStmt") infer(stmt.expr);
|
|
240
|
+
else if (stmt.kind === "Return") {
|
|
241
|
+
if (stmt.value) infer(stmt.value);
|
|
242
|
+
} else if (stmt.kind === "If") {
|
|
243
|
+
infer(stmt.cond);
|
|
244
|
+
statements(stmt.then);
|
|
245
|
+
if (stmt.else) statements(stmt.else);
|
|
246
|
+
} else if (stmt.kind === "For") {
|
|
247
|
+
const list = infer(stmt.iter);
|
|
248
|
+
within([[stmt.name, list.k === "list" ? list.of : ANY]], () => statements(stmt.body));
|
|
249
|
+
} else if (stmt.kind === "Try") {
|
|
250
|
+
statements(stmt.body);
|
|
251
|
+
within(stmt.catchName ? [[stmt.catchName, { k: "text" }]] : [], () => statements(stmt.catchBody));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
const view = (node) => {
|
|
256
|
+
if (node.kind === "ViewIf") {
|
|
257
|
+
infer(node.cond);
|
|
258
|
+
node.then.forEach(view);
|
|
259
|
+
node.else?.forEach(view);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (node.kind === "ViewFor") {
|
|
263
|
+
const list = infer(node.iter);
|
|
264
|
+
return within([[node.name, list.k === "list" ? list.of : ANY]], () => {
|
|
265
|
+
if (node.key) infer(node.key);
|
|
266
|
+
node.body.forEach(view);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
if (node.arg) infer(node.arg);
|
|
270
|
+
const component = signatures.get(node.name);
|
|
271
|
+
for (const prop of node.props) {
|
|
272
|
+
if (prop.value.kind === "Handler") {
|
|
273
|
+
const body = prop.value.body;
|
|
274
|
+
within([["value", ANY]], () => statements(body));
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
const at = component?.params.indexOf(prop.key) ?? -1;
|
|
278
|
+
const wanted = at >= 0 ? component.types[at] : null;
|
|
279
|
+
if (wanted) fits(wanted, prop.value.expr, `"${prop.key}"`);
|
|
280
|
+
else infer(prop.value.expr);
|
|
281
|
+
}
|
|
282
|
+
node.children.forEach(view);
|
|
283
|
+
};
|
|
284
|
+
for (const decl of program.body) {
|
|
285
|
+
scopes = [globals];
|
|
286
|
+
if ((decl.kind === "State" || decl.kind === "Const") && decl.type) fits(globals.get(decl.name), decl.init, `"${decl.name}"`);
|
|
287
|
+
else if (decl.kind === "Start" || decl.kind === "Test") statements(decl.body);
|
|
288
|
+
else if (decl.kind === "Fn") within(decl.params.map((p) => [p, toType(decl.paramTypes?.[p], decl)]), () => statements(decl.body));
|
|
289
|
+
else if (decl.kind === "Screen") {
|
|
290
|
+
within(decl.params.map((p) => [p, toType(decl.paramTypes?.[p], decl)]), () => {
|
|
291
|
+
for (const local of decl.locals ?? []) {
|
|
292
|
+
const declared = toType(local.type, local);
|
|
293
|
+
if (local.type) fits(declared, local.init, `"${local.name}"`);
|
|
294
|
+
scopes.at(-1).set(local.name, local.type ? declared : infer(local.init));
|
|
295
|
+
}
|
|
296
|
+
decl.body.forEach(view);
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
...program,
|
|
302
|
+
body: program.body.flatMap((decl) => {
|
|
303
|
+
if (decl.kind === "Type") return [];
|
|
304
|
+
if (decl.kind !== "Use") return [decl];
|
|
305
|
+
const names = decl.names.filter((n) => !typeImports.has(n));
|
|
306
|
+
return names.length ? [{ ...decl, names }] : [];
|
|
307
|
+
})
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
export {
|
|
311
|
+
checkTypes
|
|
312
|
+
};
|
package/src/lang/vite-plugin.js
CHANGED
|
@@ -1,14 +1,79 @@
|
|
|
1
|
+
import { basename, relative } from "node:path";
|
|
1
2
|
import { fileURLToPath } from "node:url";
|
|
2
3
|
import { compile, OrvynSyntaxError } from "./index.js";
|
|
4
|
+
import { loadEnv } from "./env.js";
|
|
5
|
+
import { readImportFrom } from "./read-import.js";
|
|
3
6
|
const orvynRoot = fileURLToPath(new URL("../../", import.meta.url));
|
|
4
7
|
const ext = import.meta.url.endsWith(".ts") ? ".ts" : ".js";
|
|
5
8
|
const webRuntime = fileURLToPath(new URL(`./runtime${ext}`, import.meta.url));
|
|
6
|
-
function
|
|
9
|
+
function moduleNamespace(id, rootDir) {
|
|
10
|
+
const cleanId = id.split("?")[0];
|
|
11
|
+
const rel = relative(rootDir, cleanId).replace(/\\/g, "/");
|
|
12
|
+
if (!rel || rel.startsWith("..")) {
|
|
13
|
+
return basename(cleanId, ".orv") || "app";
|
|
14
|
+
}
|
|
15
|
+
return rel.replace(/\.orv$/, "");
|
|
16
|
+
}
|
|
17
|
+
const HOT = `
|
|
18
|
+
let __live = null
|
|
19
|
+
const __mount = mountApp
|
|
20
|
+
mountApp = function (target, screen, params) {
|
|
21
|
+
const stop = __mount(target, screen, params)
|
|
22
|
+
__live = { target, stop }
|
|
23
|
+
return stop
|
|
24
|
+
}
|
|
25
|
+
const __initial = {}
|
|
26
|
+
const __plain = value => { try { return JSON.stringify(value, (k, v) => { if (typeof v === "function") throw 0; return v }) } catch { return undefined } }
|
|
27
|
+
const __states = {}
|
|
28
|
+
if (import.meta.hot) {
|
|
29
|
+
import.meta.hot.dispose(data => {
|
|
30
|
+
data.live = __live
|
|
31
|
+
data.locals = __o.snapshotLocals()
|
|
32
|
+
data.scroll = typeof scrollY === "number" ? [scrollX, scrollY] : null
|
|
33
|
+
data.states = Object.fromEntries(Object.entries(__states).map(([name, s]) => [name, { value: s.get(), initial: __initial[name] }]))
|
|
34
|
+
if (typeof onError === "function") __o.onError(onError)()
|
|
35
|
+
})
|
|
36
|
+
import.meta.hot.accept(next => {
|
|
37
|
+
const { live, states, locals, scroll } = import.meta.hot.data
|
|
38
|
+
// a file without the running app passes the change up to the file that has it
|
|
39
|
+
if (!live) return import.meta.hot.invalidate()
|
|
40
|
+
if (!next) return
|
|
41
|
+
live.stop()
|
|
42
|
+
next.__restore(states)
|
|
43
|
+
__o.carryLocals(locals)
|
|
44
|
+
next.mountApp(live.target)
|
|
45
|
+
__o.carryLocals([])
|
|
46
|
+
if (scroll) scrollTo(...scroll)
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
`;
|
|
50
|
+
const restore = (names) => `
|
|
51
|
+
for (const [__name, __state] of [${names.map((n) => `[${JSON.stringify(n)}, ${n}]`).join(", ")}]) {
|
|
52
|
+
__states[__name] = __state
|
|
53
|
+
__initial[__name] = __plain(__state.get())
|
|
54
|
+
}
|
|
55
|
+
export function __restore(old) {
|
|
56
|
+
for (const [name, kept] of Object.entries(old ?? {})) {
|
|
57
|
+
if (__states[name] && kept.initial !== undefined && kept.initial === __initial[name] && __plain(kept.value) !== undefined) {
|
|
58
|
+
__states[name].set(kept.value)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
`;
|
|
63
|
+
function orvynLang(options = {}) {
|
|
64
|
+
let rootDir = options.root || process.cwd();
|
|
65
|
+
let isDev = Boolean(options.dev);
|
|
66
|
+
let env = options.env;
|
|
67
|
+
let hot = false;
|
|
7
68
|
return {
|
|
8
69
|
name: "orvyn-lang",
|
|
9
70
|
// let Vite serve Orvyn's runtime files when Orvyn is installed
|
|
10
71
|
// from another folder (npm install ../orvyn)
|
|
11
|
-
config(config) {
|
|
72
|
+
async config(config, run) {
|
|
73
|
+
if (options.dev === void 0 && run?.command) isDev = run.command === "serve";
|
|
74
|
+
hot = run?.command === "serve";
|
|
75
|
+
env = options.env ?? await loadEnv(config.root || rootDir, isDev ? "dev" : "prod");
|
|
76
|
+
if (!options.root && config.root) rootDir = config.root;
|
|
12
77
|
const allow = config.server?.fs?.allow;
|
|
13
78
|
if (allow) return { server: { fs: { allow: [...allow, orvynRoot] } } };
|
|
14
79
|
return { server: { fs: { allow: [process.cwd(), orvynRoot] } } };
|
|
@@ -21,7 +86,21 @@ function orvynLang() {
|
|
|
21
86
|
transform(source, id) {
|
|
22
87
|
if (!id.endsWith(".orv")) return null;
|
|
23
88
|
try {
|
|
24
|
-
|
|
89
|
+
let code = compile(source, {
|
|
90
|
+
moduleId: moduleNamespace(id, rootDir),
|
|
91
|
+
env,
|
|
92
|
+
resolveImport: readImportFrom(id.split("?")[0]),
|
|
93
|
+
tests: options.tests,
|
|
94
|
+
...isDev ? { sourceText: source } : {}
|
|
95
|
+
});
|
|
96
|
+
if (hot) {
|
|
97
|
+
const states = [...code.matchAll(/^export const (\S+) = (?:__signal|__o\.savedSignal)\(/gm)].map((m) => m[1]);
|
|
98
|
+
code = code + HOT + restore(states);
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
code,
|
|
102
|
+
map: null
|
|
103
|
+
};
|
|
25
104
|
} catch (error) {
|
|
26
105
|
if (error instanceof OrvynSyntaxError) {
|
|
27
106
|
const err = new Error(error.message);
|