@skgo/sveltekit-adapter 0.3.4 → 0.3.6

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/skgo-adapter/env.js +0 -124
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skgo/sveltekit-adapter",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "SvelteKit adapter for skgo: builds a frontend that a Go binary renders and answers",
5
5
  "keywords": [
6
6
  "adapter",
@@ -61,7 +61,6 @@ async function fromApp(specifier) {
61
61
 
62
62
  const { rolldown } = await fromApp('vite/rolldown');
63
63
  const { transformSync } = await fromApp('vite/rolldown/experimental');
64
- const { parseAst } = await fromApp('vite/rolldown/parseAst');
65
64
  // `fetchModule` operates on the app's own `DevEnvironment`, so it has to be
66
65
  // the app's copy for the same reason rolldown is: a second module realm's
67
66
  // vite does not recognise this one's environments.
@@ -481,7 +480,6 @@ function engineTransform(code, id, { lowered, helpers } = {}) {
481
480
  '=> Promise.reject(new Error("skgo: no dynamic import in the SSR engine"))'
482
481
  );
483
482
  }
484
- out = wrapFieldInitialisers(out, id);
485
483
  if (!UNPARSEABLE.test(out)) {
486
484
  return out === code ? null : { code: out, map: null };
487
485
  }
@@ -497,128 +495,6 @@ function engineTransform(code, id, { lowered, helpers } = {}) {
497
495
  return { code: lower, map: null };
498
496
  }
499
497
 
500
- /**
501
- * Wraps every class field initialiser that closes over `this` in a plain
502
- * function called with the instance, so the initialiser itself only reads
503
- * `this` and the closure is made inside an ordinary call.
504
- *
505
- * goja runs field initialisers with the argument count of whichever function
506
- * is executing `new`, and an initialiser that closes over `this` derives its
507
- * stack base from that count. Constructed inside a function with parameters —
508
- * every component is one — such a class panics the render or throws "Value is
509
- * not an Object". Svelte emits exactly that for a class-field `$derived`:
510
- * `#graph = derived(() => this.doc.toGraph())`. The engine fix is
511
- * dop251/goja#737; once skgo requires a goja carrying it, this goes.
512
- *
513
- * `(function () { return VALUE; }).call(this)` evaluates VALUE at the same
514
- * moment with the same `this` and `new.target`, and an initialiser can hold no
515
- * `arguments`, `await` or `yield` for the function to capture instead. An
516
- * anonymous function keeps the name the field would have given it by being
517
- * defined as a property of that name. `super` cannot move into a plain
518
- * function, so an initialiser that reaches it is left alone.
519
- *
520
- * @param {string} code
521
- * @param {string} id
522
- */
523
- function wrapFieldInitialisers(code, id) {
524
- const evals = code.includes('eval(');
525
- if (!evals && !(code.includes('=>') && /\bthis\b/.test(code))) return code;
526
-
527
- /** @type {Array<{ at: number, text: string, closes: boolean, order: number }>} */
528
- const inserts = [];
529
- visit(parseAst(code, undefined, id.replace(/\0/g, '_')), (node) => {
530
- if (node.type !== 'PropertyDefinition' || !node.value) return;
531
- const found = captures(node.value);
532
- if (!found.this || found.super) return;
533
- const { start, end } = node.value;
534
- const order = inserts.length;
535
- if (!node.computed && anonymousDefinition(node.value)) {
536
- const name = JSON.stringify(node.key.type === 'PrivateIdentifier' ? '#' + node.key.name : String(node.key.name ?? node.key.value));
537
- inserts.push({ at: start, text: `(function () { return { ${name}: `, closes: false, order });
538
- inserts.push({ at: end, text: ` }[${name}]; }).call(this)`, closes: true, order });
539
- } else {
540
- inserts.push({ at: start, text: '(function () { return (', closes: false, order });
541
- inserts.push({ at: end, text: '); }).call(this)', closes: true, order });
542
- }
543
- });
544
- if (!inserts.length) return code;
545
-
546
- // Fields nest (a class inside an initialiser), so one offset can take several
547
- // inserts: whatever closes goes before whatever opens, an inner field closes
548
- // before its outer one, and an outer one opens before its inner one. Visit
549
- // order is outer first.
550
- inserts.sort((a, b) => a.at - b.at || Number(b.closes) - Number(a.closes) || (a.closes ? b.order - a.order : a.order - b.order));
551
- let out = '';
552
- let from = 0;
553
- for (const { at, text } of inserts) {
554
- out += code.slice(from, at) + text;
555
- from = at;
556
- }
557
- return out + code.slice(from);
558
- }
559
-
560
- /**
561
- * Whether an initialiser closes over `this` — inside an arrow, or through a
562
- * direct `eval` — and whether it reaches `super`. Neither crosses an ordinary
563
- * function, which has its own of both.
564
- *
565
- * @param {any} value
566
- * @returns {{ this: boolean, super: boolean }}
567
- */
568
- function captures(value) {
569
- const found = { this: false, super: false };
570
- (function walk(node, arrow) {
571
- switch (node.type) {
572
- case 'ThisExpression':
573
- if (arrow) found.this = true;
574
- return;
575
- case 'Super':
576
- found.super = true;
577
- return;
578
- case 'FunctionExpression':
579
- case 'FunctionDeclaration':
580
- return;
581
- case 'CallExpression':
582
- if (node.callee.type === 'Identifier' && node.callee.name === 'eval') found.this = true;
583
- break;
584
- case 'ArrowFunctionExpression':
585
- arrow = true;
586
- break;
587
- }
588
- for (const child of children(node)) walk(child, arrow);
589
- })(value, false);
590
- return found;
591
- }
592
-
593
- /** @param {any} node */
594
- function anonymousDefinition(node) {
595
- return (
596
- node.type === 'ArrowFunctionExpression' ||
597
- ((node.type === 'FunctionExpression' || node.type === 'ClassExpression') && !node.id)
598
- );
599
- }
600
-
601
- /**
602
- * @param {any} node
603
- * @param {(node: any) => void} fn
604
- */
605
- function visit(node, fn) {
606
- fn(node);
607
- for (const child of children(node)) visit(child, fn);
608
- }
609
-
610
- /** @param {any} node */
611
- function* children(node) {
612
- for (const key in node) {
613
- const value = node[key];
614
- if (Array.isArray(value)) {
615
- for (const item of value) if (item && typeof item.type === 'string') yield item;
616
- } else if (value && typeof value.type === 'string') {
617
- yield value;
618
- }
619
- }
620
- }
621
-
622
498
  /**
623
499
  * Where oxc's lowering helpers live, resolved the way dev has to resolve them:
624
500
  * from vite rather than from the app. Under pnpm `@oxc-project/runtime` is a