@sigil-dev/compiler 0.9.29 → 0.9.30
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/index.ts +6 -4
- package/package.json +1 -1
- package/src/babel/index.ts +763 -750
- package/src/babel/jsx/anchor-mount.ts +338 -333
- package/src/babel/jsx/children.ts +116 -116
- package/src/babel/jsx/element.ts +1040 -1034
- package/src/babel/jsx/fragment.ts +271 -271
- package/src/babel/jsx/keyed-list.ts +339 -339
- package/src/babel/jsx/utils.ts +356 -356
- package/src/babel/util/bind.ts +143 -143
- package/src/babel/util/css.ts +208 -208
- package/src/babel/util/dead-code.ts +94 -93
- package/src/babel/util/links.ts +101 -0
- package/src/babel/util/reactive-fns.ts +110 -110
- package/src/babel/util/runtime-names.ts +88 -0
- package/src/babel/util/state-imports.ts +122 -121
- package/src/babel/util/static-assertion.ts +51 -51
- package/src/babel/util/static-env.ts +84 -0
- package/src/vite/index.ts +61 -61
- package/test/bun-plugin.test.ts +45 -45
- package/test/context.test.ts +110 -102
- package/test/fixtures/stores/index.ts +2 -2
- package/test/hydration-analysis.test.ts +133 -133
- package/test/hydration.test.ts +315 -313
- package/test/jsx.test.ts +254 -254
- package/test/links.test.ts +57 -0
- package/test/reactive-helpers.test.ts +135 -135
- package/test/render-parity.test.ts +909 -907
- package/test/runtime-names.test.ts +60 -0
- package/test/scoped-css.test.ts +166 -166
- package/test/ssr-boolean-attrs.test.ts +56 -56
- package/test/ssr-raw-text.test.ts +68 -68
- package/test/ssr.test.ts +30 -0
- package/test/static-assertion.test.ts +123 -123
- package/test/static-env.test.ts +85 -0
- package/test/static-skip.test.ts +292 -292
- package/test/style-object.test.ts +36 -36
- package/test/tree-shaking.test.ts +122 -120
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
import { type NodePath, types as t } from "@babel/core";
|
|
2
|
-
import { checkStaticUse, FORCE_HYDRATE } from "../../analysis/hydration.ts";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* `<Footer static />` asserts that this use needs no JavaScript in the
|
|
6
|
-
* browser, and fails the build when it does not.
|
|
7
|
-
*
|
|
8
|
-
* An assertion and not a hint on purpose: a hint the compiler is free to
|
|
9
|
-
* ignore turns into a page that looks right and does nothing, and a compile
|
|
10
|
-
* error that names the line is far cheaper than that. The attribute is removed
|
|
11
|
-
* either way, so the component never receives a `static` prop.
|
|
12
|
-
*
|
|
13
|
-
* Runs before anything else in the file is rewritten, in every mode (the
|
|
14
|
-
* server build must reach the same verdict as the browser build).
|
|
15
|
-
*/
|
|
16
|
-
export function applyStaticAssertions(
|
|
17
|
-
program: NodePath<t.Program>,
|
|
18
|
-
filename: string,
|
|
19
|
-
source: string,
|
|
20
|
-
): void {
|
|
21
|
-
program.traverse({
|
|
22
|
-
JSXAttribute(attr) {
|
|
23
|
-
if (!t.isJSXIdentifier(attr.node.name, { name: "static" })) return;
|
|
24
|
-
const opening = attr.parentPath;
|
|
25
|
-
if (!opening?.isJSXOpeningElement()) return;
|
|
26
|
-
const element = opening.parentPath as NodePath<t.JSXElement>;
|
|
27
|
-
|
|
28
|
-
// `static={false}` is the override the other way: this use hydrates,
|
|
29
|
-
// whatever the analysis would have said
|
|
30
|
-
const v = attr.node.value;
|
|
31
|
-
const off =
|
|
32
|
-
t.isJSXExpressionContainer(v) &&
|
|
33
|
-
t.isBooleanLiteral(v.expression, { value: false });
|
|
34
|
-
if (!off) {
|
|
35
|
-
const problems = checkStaticUse(element, filename, source);
|
|
36
|
-
if (problems.length) {
|
|
37
|
-
const tag = t.isJSXIdentifier(opening.node.name)
|
|
38
|
-
? opening.node.name.name
|
|
39
|
-
: "component";
|
|
40
|
-
const lines = problems.map((p) => ` line ${p.line}: ${p.detail}`);
|
|
41
|
-
throw attr.buildCodeFrameError(
|
|
42
|
-
`<${tag} static> is not static:\n${lines.join("\n")}\n` +
|
|
43
|
-
"Remove `static`, or make what it depends on static.",
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
if (off) FORCE_HYDRATE.add(element.node);
|
|
48
|
-
attr.remove();
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
}
|
|
1
|
+
import { type NodePath, types as t } from "@babel/core";
|
|
2
|
+
import { checkStaticUse, FORCE_HYDRATE } from "../../analysis/hydration.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `<Footer static />` asserts that this use needs no JavaScript in the
|
|
6
|
+
* browser, and fails the build when it does not.
|
|
7
|
+
*
|
|
8
|
+
* An assertion and not a hint on purpose: a hint the compiler is free to
|
|
9
|
+
* ignore turns into a page that looks right and does nothing, and a compile
|
|
10
|
+
* error that names the line is far cheaper than that. The attribute is removed
|
|
11
|
+
* either way, so the component never receives a `static` prop.
|
|
12
|
+
*
|
|
13
|
+
* Runs before anything else in the file is rewritten, in every mode (the
|
|
14
|
+
* server build must reach the same verdict as the browser build).
|
|
15
|
+
*/
|
|
16
|
+
export function applyStaticAssertions(
|
|
17
|
+
program: NodePath<t.Program>,
|
|
18
|
+
filename: string,
|
|
19
|
+
source: string,
|
|
20
|
+
): void {
|
|
21
|
+
program.traverse({
|
|
22
|
+
JSXAttribute(attr) {
|
|
23
|
+
if (!t.isJSXIdentifier(attr.node.name, { name: "static" })) return;
|
|
24
|
+
const opening = attr.parentPath;
|
|
25
|
+
if (!opening?.isJSXOpeningElement()) return;
|
|
26
|
+
const element = opening.parentPath as NodePath<t.JSXElement>;
|
|
27
|
+
|
|
28
|
+
// `static={false}` is the override the other way: this use hydrates,
|
|
29
|
+
// whatever the analysis would have said
|
|
30
|
+
const v = attr.node.value;
|
|
31
|
+
const off =
|
|
32
|
+
t.isJSXExpressionContainer(v) &&
|
|
33
|
+
t.isBooleanLiteral(v.expression, { value: false });
|
|
34
|
+
if (!off) {
|
|
35
|
+
const problems = checkStaticUse(element, filename, source);
|
|
36
|
+
if (problems.length) {
|
|
37
|
+
const tag = t.isJSXIdentifier(opening.node.name)
|
|
38
|
+
? opening.node.name.name
|
|
39
|
+
: "component";
|
|
40
|
+
const lines = problems.map((p) => ` line ${p.line}: ${p.detail}`);
|
|
41
|
+
throw attr.buildCodeFrameError(
|
|
42
|
+
`<${tag} static> is not static:\n${lines.join("\n")}\n` +
|
|
43
|
+
"Remove `static`, or make what it depends on static.",
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (off) FORCE_HYDRATE.add(element.node);
|
|
48
|
+
attr.remove();
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { types as t } from "@babel/core";
|
|
2
|
+
import type { NodePath } from "@babel/traverse";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The values of `$env/static/*`, set by grimoire when it builds. Undefined
|
|
6
|
+
* outside an app, where the imports are left to the `$env` modules.
|
|
7
|
+
*/
|
|
8
|
+
let values:
|
|
9
|
+
| { public: Record<string, string>; private: Record<string, string> }
|
|
10
|
+
| undefined;
|
|
11
|
+
|
|
12
|
+
export function setStaticEnv(
|
|
13
|
+
pub: Record<string, string> | undefined,
|
|
14
|
+
priv: Record<string, string> = {},
|
|
15
|
+
): void {
|
|
16
|
+
values = pub ? { public: pub, private: priv } : undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Put `$env/static/*` values into the code as string literals.
|
|
21
|
+
*
|
|
22
|
+
* `$env/static` is read when the app is built, so a value cannot change
|
|
23
|
+
* afterwards, and written in place it lets the bundler drop the branches it
|
|
24
|
+
* decides: `if (PUBLIC_BETA === "on")` with BETA off leaves nothing of the
|
|
25
|
+
* feature in the browser bundle. `$env/static/private` is inlined only on
|
|
26
|
+
* the server; a browser import of it stays, and fails the build as before.
|
|
27
|
+
* Missing variables read as the module reads them: `env.MISSING` is
|
|
28
|
+
* undefined, and a named import of one is left for the module to refuse.
|
|
29
|
+
*/
|
|
30
|
+
export function inlineStaticEnv(
|
|
31
|
+
program: NodePath<t.Program>,
|
|
32
|
+
mode: string,
|
|
33
|
+
): void {
|
|
34
|
+
const env = values;
|
|
35
|
+
if (!env) return;
|
|
36
|
+
for (const decl of program.get("body")) {
|
|
37
|
+
if (!decl.isImportDeclaration()) continue;
|
|
38
|
+
const source = decl.node.source.value;
|
|
39
|
+
const table =
|
|
40
|
+
source === "$env/static/public"
|
|
41
|
+
? env.public
|
|
42
|
+
: source === "$env/static/private" && mode === "ssr"
|
|
43
|
+
? env.private
|
|
44
|
+
: undefined;
|
|
45
|
+
if (!table) continue;
|
|
46
|
+
for (const spec of decl.get("specifiers")) {
|
|
47
|
+
if (!spec.isImportSpecifier()) continue;
|
|
48
|
+
const imported = spec.node.imported;
|
|
49
|
+
const name = t.isIdentifier(imported) ? imported.name : imported.value;
|
|
50
|
+
const binding = spec.scope.getBinding(spec.node.local.name);
|
|
51
|
+
if (!binding) continue;
|
|
52
|
+
const value = (key: string) =>
|
|
53
|
+
key in table ? t.stringLiteral(table[key]!) : t.identifier("undefined");
|
|
54
|
+
if (name !== "env" && !(name in table)) continue;
|
|
55
|
+
let kept = false;
|
|
56
|
+
for (const ref of binding.referencePaths) {
|
|
57
|
+
if (name === "env") {
|
|
58
|
+
// env.PUBLIC_X / env["PUBLIC_X"]
|
|
59
|
+
const parent = ref.parentPath;
|
|
60
|
+
if (parent?.isMemberExpression({ object: ref.node })) {
|
|
61
|
+
const prop = parent.node.property;
|
|
62
|
+
const key =
|
|
63
|
+
!parent.node.computed && t.isIdentifier(prop)
|
|
64
|
+
? prop.name
|
|
65
|
+
: t.isStringLiteral(prop)
|
|
66
|
+
? prop.value
|
|
67
|
+
: undefined;
|
|
68
|
+
if (key !== undefined) {
|
|
69
|
+
parent.replaceWith(value(key));
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
kept = true;
|
|
74
|
+
} else if (ref.isIdentifier() && !ref.parentPath?.isExportSpecifier()) {
|
|
75
|
+
ref.replaceWith(value(name));
|
|
76
|
+
} else {
|
|
77
|
+
kept = true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (!kept) spec.remove();
|
|
81
|
+
}
|
|
82
|
+
if (decl.node.specifiers.length === 0) decl.remove();
|
|
83
|
+
}
|
|
84
|
+
}
|
package/src/vite/index.ts
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import { transformSync } from "@babel/core";
|
|
2
|
-
import type { Plugin } from "vite";
|
|
3
|
-
import sigilPlugin from "../babel/index.ts";
|
|
4
|
-
import { computeHash, scopeCSS } from "../babel/util/css.ts";
|
|
5
|
-
|
|
6
|
-
const STYLE_RE = /<style[^>]*>([\s\S]*?)<\/style>/gi;
|
|
7
|
-
|
|
8
|
-
export function sigil(options?: {
|
|
9
|
-
hydrate?: boolean;
|
|
10
|
-
styleNonce?: string;
|
|
11
|
-
}): Plugin {
|
|
12
|
-
// Track injected CSS per component to avoid duplicates
|
|
13
|
-
const injected = new Set<string>();
|
|
14
|
-
const styleNonce = options?.styleNonce ?? "";
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
name: "sigil",
|
|
18
|
-
transform(code, id) {
|
|
19
|
-
if (!id.match(/\.[jt]sx$/)) return;
|
|
20
|
-
|
|
21
|
-
// 1. Extract <style> from raw source BEFORE Babel
|
|
22
|
-
let scopedCSS = "";
|
|
23
|
-
const codeWithoutStyle = code.replace(STYLE_RE, (_, cssContent) => {
|
|
24
|
-
const hash = computeHash(id);
|
|
25
|
-
scopedCSS = scopeCSS(cssContent.trim(), hash);
|
|
26
|
-
return ""; // Remove <style> from source
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
// 2. Transform JSX with Babel, passing hash + mode
|
|
30
|
-
const hash = computeHash(id);
|
|
31
|
-
const mode = options?.hydrate ? ("hydrate" as const) : undefined;
|
|
32
|
-
const res = transformSync(codeWithoutStyle, {
|
|
33
|
-
plugins: [
|
|
34
|
-
"@babel/plugin-syntax-jsx",
|
|
35
|
-
[sigilPlugin, { hash: scopedCSS ? hash : undefined, mode }],
|
|
36
|
-
],
|
|
37
|
-
filename: id,
|
|
38
|
-
});
|
|
39
|
-
if (!res) return;
|
|
40
|
-
|
|
41
|
-
// 3. Inject scoped CSS into <head> (once per component)
|
|
42
|
-
let cssInjection = "";
|
|
43
|
-
if (scopedCSS && !injected.has(id)) {
|
|
44
|
-
injected.add(id);
|
|
45
|
-
const nonceAttr = styleNonce ? ` nonce="${styleNonce}"` : "";
|
|
46
|
-
cssInjection = `
|
|
47
|
-
if (typeof document !== 'undefined' && !document.getElementById('sigil-${hash}')) {
|
|
48
|
-
const __style = document.createElement('style');
|
|
49
|
-
__style.id = 'sigil-${hash}';${nonceAttr ? `\n __style.setAttribute('nonce', '${styleNonce}');` : ""}
|
|
50
|
-
__style.textContent = ${JSON.stringify(scopedCSS)};
|
|
51
|
-
document.head.appendChild(__style);
|
|
52
|
-
}`;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
code: cssInjection + (res.code ?? ""),
|
|
57
|
-
map: res.map as any,
|
|
58
|
-
};
|
|
59
|
-
},
|
|
60
|
-
};
|
|
61
|
-
}
|
|
1
|
+
import { transformSync } from "@babel/core";
|
|
2
|
+
import type { Plugin } from "vite";
|
|
3
|
+
import sigilPlugin from "../babel/index.ts";
|
|
4
|
+
import { computeHash, scopeCSS } from "../babel/util/css.ts";
|
|
5
|
+
|
|
6
|
+
const STYLE_RE = /<style[^>]*>([\s\S]*?)<\/style>/gi;
|
|
7
|
+
|
|
8
|
+
export function sigil(options?: {
|
|
9
|
+
hydrate?: boolean;
|
|
10
|
+
styleNonce?: string;
|
|
11
|
+
}): Plugin {
|
|
12
|
+
// Track injected CSS per component to avoid duplicates
|
|
13
|
+
const injected = new Set<string>();
|
|
14
|
+
const styleNonce = options?.styleNonce ?? "";
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
name: "sigil",
|
|
18
|
+
transform(code, id) {
|
|
19
|
+
if (!id.match(/\.[jt]sx$/)) return;
|
|
20
|
+
|
|
21
|
+
// 1. Extract <style> from raw source BEFORE Babel
|
|
22
|
+
let scopedCSS = "";
|
|
23
|
+
const codeWithoutStyle = code.replace(STYLE_RE, (_, cssContent) => {
|
|
24
|
+
const hash = computeHash(id);
|
|
25
|
+
scopedCSS = scopeCSS(cssContent.trim(), hash);
|
|
26
|
+
return ""; // Remove <style> from source
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// 2. Transform JSX with Babel, passing hash + mode
|
|
30
|
+
const hash = computeHash(id);
|
|
31
|
+
const mode = options?.hydrate ? ("hydrate" as const) : undefined;
|
|
32
|
+
const res = transformSync(codeWithoutStyle, {
|
|
33
|
+
plugins: [
|
|
34
|
+
"@babel/plugin-syntax-jsx",
|
|
35
|
+
[sigilPlugin, { hash: scopedCSS ? hash : undefined, mode }],
|
|
36
|
+
],
|
|
37
|
+
filename: id,
|
|
38
|
+
});
|
|
39
|
+
if (!res) return;
|
|
40
|
+
|
|
41
|
+
// 3. Inject scoped CSS into <head> (once per component)
|
|
42
|
+
let cssInjection = "";
|
|
43
|
+
if (scopedCSS && !injected.has(id)) {
|
|
44
|
+
injected.add(id);
|
|
45
|
+
const nonceAttr = styleNonce ? ` nonce="${styleNonce}"` : "";
|
|
46
|
+
cssInjection = `
|
|
47
|
+
if (typeof document !== 'undefined' && !document.getElementById('sigil-${hash}')) {
|
|
48
|
+
const __style = document.createElement('style');
|
|
49
|
+
__style.id = 'sigil-${hash}';${nonceAttr ? `\n __style.setAttribute('nonce', '${styleNonce}');` : ""}
|
|
50
|
+
__style.textContent = ${JSON.stringify(scopedCSS)};
|
|
51
|
+
document.head.appendChild(__style);
|
|
52
|
+
}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
code: cssInjection + (res.code ?? ""),
|
|
57
|
+
map: res.map as any,
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
package/test/bun-plugin.test.ts
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import { afterAll, beforeAll, expect, test } from "bun:test";
|
|
2
|
-
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { sigil } from "../src/bun-plugin.ts";
|
|
6
|
-
|
|
7
|
-
const dir = join(tmpdir(), `sigil-bun-plugin-${Date.now()}`);
|
|
8
|
-
|
|
9
|
-
beforeAll(async () => {
|
|
10
|
-
await mkdir(dir, { recursive: true });
|
|
11
|
-
await writeFile(
|
|
12
|
-
join(dir, "theme.ts"),
|
|
13
|
-
'export let theme = $store<"light" | "dark">("light");\nexport function toggle() { theme = theme === "light" ? "dark" : "light"; }',
|
|
14
|
-
);
|
|
15
|
-
await writeFile(
|
|
16
|
-
join(dir, "raw.ts"),
|
|
17
|
-
"export let rows = $state.raw([1, 2]);\nexport const first = () => rows[0];",
|
|
18
|
-
);
|
|
19
|
-
await writeFile(
|
|
20
|
-
join(dir, "label.ts"),
|
|
21
|
-
'import { theme } from "./theme";\nexport const label = () => "theme: " + theme;',
|
|
22
|
-
);
|
|
23
|
-
await writeFile(
|
|
24
|
-
join(dir, "entry.ts"),
|
|
25
|
-
'import { toggle } from "./theme";\nimport { first } from "./raw";\nimport { label } from "./label";\nconsole.log(toggle, first, label);',
|
|
26
|
-
);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
afterAll(() => rm(dir, { recursive: true, force: true }));
|
|
30
|
-
|
|
31
|
-
test("plain .ts modules with any macro or a store import are compiled", async () => {
|
|
32
|
-
const result = await Bun.build({
|
|
33
|
-
entrypoints: [join(dir, "entry.ts")],
|
|
34
|
-
plugins: [sigil({ mode: "dom" })],
|
|
35
|
-
external: ["@sigil-dev/runtime"],
|
|
36
|
-
throw: false,
|
|
37
|
-
});
|
|
38
|
-
expect(result.success).toBe(true);
|
|
39
|
-
const code = await result.outputs[0]!.text();
|
|
40
|
-
// $store<T>(…) with a type argument, and $state.raw(…)
|
|
41
|
-
expect(code).not.toMatch(/\$store|\$state/);
|
|
42
|
-
expect(code).toContain("createRawSignal");
|
|
43
|
-
// label.ts has no macro, but reads an imported store
|
|
1
|
+
import { afterAll, beforeAll, expect, test } from "bun:test";
|
|
2
|
+
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { sigil } from "../src/bun-plugin.ts";
|
|
6
|
+
|
|
7
|
+
const dir = join(tmpdir(), `sigil-bun-plugin-${Date.now()}`);
|
|
8
|
+
|
|
9
|
+
beforeAll(async () => {
|
|
10
|
+
await mkdir(dir, { recursive: true });
|
|
11
|
+
await writeFile(
|
|
12
|
+
join(dir, "theme.ts"),
|
|
13
|
+
'export let theme = $store<"light" | "dark">("light");\nexport function toggle() { theme = theme === "light" ? "dark" : "light"; }',
|
|
14
|
+
);
|
|
15
|
+
await writeFile(
|
|
16
|
+
join(dir, "raw.ts"),
|
|
17
|
+
"export let rows = $state.raw([1, 2]);\nexport const first = () => rows[0];",
|
|
18
|
+
);
|
|
19
|
+
await writeFile(
|
|
20
|
+
join(dir, "label.ts"),
|
|
21
|
+
'import { theme } from "./theme";\nexport const label = () => "theme: " + theme;',
|
|
22
|
+
);
|
|
23
|
+
await writeFile(
|
|
24
|
+
join(dir, "entry.ts"),
|
|
25
|
+
'import { toggle } from "./theme";\nimport { first } from "./raw";\nimport { label } from "./label";\nconsole.log(toggle, first, label);',
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterAll(() => rm(dir, { recursive: true, force: true }));
|
|
30
|
+
|
|
31
|
+
test("plain .ts modules with any macro or a store import are compiled", async () => {
|
|
32
|
+
const result = await Bun.build({
|
|
33
|
+
entrypoints: [join(dir, "entry.ts")],
|
|
34
|
+
plugins: [sigil({ mode: "dom" })],
|
|
35
|
+
external: ["@sigil-dev/runtime"],
|
|
36
|
+
throw: false,
|
|
37
|
+
});
|
|
38
|
+
expect(result.success).toBe(true);
|
|
39
|
+
const code = await result.outputs[0]!.text();
|
|
40
|
+
// $store<T>(…) with a type argument, and $state.raw(…)
|
|
41
|
+
expect(code).not.toMatch(/\$store|\$state/);
|
|
42
|
+
expect(code).toContain("createRawSignal");
|
|
43
|
+
// label.ts has no macro, but reads an imported store
|
|
44
44
|
// compiled statically: the import is known to be state
|
|
45
45
|
expect(code).toContain(`"theme: " + theme()`);
|
|
46
|
-
expect(code).not.toContain("readStore");
|
|
47
|
-
});
|
|
46
|
+
expect(code).not.toContain("readStore");
|
|
47
|
+
});
|
package/test/context.test.ts
CHANGED
|
@@ -1,102 +1,110 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Context follows the component tree: nested and sibling providers, rows a
|
|
3
|
-
* list builds later, server rendering, and async components after an await.
|
|
4
|
-
*/
|
|
5
|
-
import { describe, expect, test } from "bun:test";
|
|
6
|
-
import { transformSync } from "@babel/core";
|
|
7
|
-
import * as runtime from "../../runtime/index.ts";
|
|
8
|
-
import sigilPlugin from "../src/babel/index.ts";
|
|
9
|
-
|
|
10
|
-
function compile(
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
test("
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
expect(runtime.
|
|
101
|
-
});
|
|
102
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Context follows the component tree: nested and sibling providers, rows a
|
|
3
|
+
* list builds later, server rendering, and async components after an await.
|
|
4
|
+
*/
|
|
5
|
+
import { describe, expect, test } from "bun:test";
|
|
6
|
+
import { transformSync } from "@babel/core";
|
|
7
|
+
import * as runtime from "../../runtime/index.ts";
|
|
8
|
+
import sigilPlugin from "../src/babel/index.ts";
|
|
9
|
+
|
|
10
|
+
function compile(
|
|
11
|
+
source: string,
|
|
12
|
+
mode: "ssr" | "hydrate" | "dom",
|
|
13
|
+
names: string[],
|
|
14
|
+
) {
|
|
15
|
+
const code = transformSync(source, {
|
|
16
|
+
parserOpts: { plugins: ["typescript", "jsx"] },
|
|
17
|
+
plugins: [[sigilPlugin, { mode }]],
|
|
18
|
+
filename: "t.tsx",
|
|
19
|
+
})!.code!;
|
|
20
|
+
const body = code
|
|
21
|
+
.replace(/import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/runtime["'];?/g, "")
|
|
22
|
+
.replace(/^export\s+/gm, "");
|
|
23
|
+
return new Function(
|
|
24
|
+
"document",
|
|
25
|
+
...Object.keys(runtime),
|
|
26
|
+
`${body};\nreturn { ${names.join(", ")} };`,
|
|
27
|
+
)(globalThis.document, ...Object.values(runtime));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const noMarkers = (html: string) => html.replace(/<!--[^>]*-->/g, "");
|
|
31
|
+
|
|
32
|
+
function clientRender(source: string) {
|
|
33
|
+
const { App } = compile(source, "hydrate", ["App"]);
|
|
34
|
+
const host = document.createElement("div");
|
|
35
|
+
runtime.pushHydrationNodes([]);
|
|
36
|
+
runtime.insert(host, runtime.__component(App, {}));
|
|
37
|
+
runtime.popHydrationNodes();
|
|
38
|
+
return host;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const providers = `
|
|
42
|
+
const K = createContext();
|
|
43
|
+
function Provide(p) { setContext(K, p.v); return <section>{p.children}</section>; }
|
|
44
|
+
function Read() { return <i>{getContext(K) ?? "none"}</i>; }
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
describe("context follows the component tree", () => {
|
|
48
|
+
const source = `${providers}
|
|
49
|
+
export const App = () => (
|
|
50
|
+
<div>
|
|
51
|
+
<Provide v="a"><Read /></Provide>
|
|
52
|
+
<Provide v="b"><Read /><Provide v="c"><Read /></Provide><Read /></Provide>
|
|
53
|
+
<Read />
|
|
54
|
+
</div>
|
|
55
|
+
);`;
|
|
56
|
+
const html =
|
|
57
|
+
"<div><section><i>a</i></section><section><i>b</i><section><i>c</i></section><i>b</i></section><i>none</i></div>";
|
|
58
|
+
|
|
59
|
+
test("client: siblings don't overwrite each other, the nearest wins", () => {
|
|
60
|
+
expect(noMarkers(clientRender(source).innerHTML)).toBe(html);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("server: children render inside their provider", () => {
|
|
64
|
+
const { App } = compile(source, "ssr", ["App"]);
|
|
65
|
+
expect(noMarkers(String(runtime.__component(App, {})))).toBe(html);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("rows a list adds later still see the provider", async () => {
|
|
69
|
+
const host = clientRender(`${providers}
|
|
70
|
+
let items = $state(["x"]);
|
|
71
|
+
globalThis.__addRow = () => items.push("y");
|
|
72
|
+
function Row(p) { return <li>{getContext(K)}:{p.n}</li>; }
|
|
73
|
+
export const App = () => (
|
|
74
|
+
<Provide v="ctx"><ul>{items.map((n) => <Row key={n} n={n} />)}</ul></Provide>
|
|
75
|
+
);`);
|
|
76
|
+
(globalThis as unknown as { __addRow: () => void }).__addRow();
|
|
77
|
+
await runtime.tick();
|
|
78
|
+
expect(noMarkers(host.innerHTML)).toBe(
|
|
79
|
+
"<section><ul><li>ctx:x</li><li>ctx:y</li></ul></section>",
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("async components keep their owner across awaits", async () => {
|
|
84
|
+
const { Outer } = compile(
|
|
85
|
+
`const K = createContext();
|
|
86
|
+
export async function Inner() {
|
|
87
|
+
await Promise.resolve();
|
|
88
|
+
await new Promise((r) => setTimeout(r, 1));
|
|
89
|
+
return getContext(K);
|
|
90
|
+
}
|
|
91
|
+
export function Outer() { setContext(K, "kept"); return Inner(); }`,
|
|
92
|
+
"dom",
|
|
93
|
+
["Outer"],
|
|
94
|
+
);
|
|
95
|
+
expect(await runtime.__component(Outer as () => Promise<string>, {})).toBe(
|
|
96
|
+
"kept",
|
|
97
|
+
);
|
|
98
|
+
await runtime.tick();
|
|
99
|
+
// and nothing leaks once the component is done
|
|
100
|
+
expect(runtime.getOwner()).toBe(null);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("outside any component, the root store is used", () => {
|
|
104
|
+
const K = runtime.createContext<string>();
|
|
105
|
+
runtime.setContext(K, "root");
|
|
106
|
+
expect(runtime.__component((_: object) => runtime.getContext(K), {})).toBe(
|
|
107
|
+
"root",
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { cart as basket } from "./cart";
|
|
2
|
+
export * from "./counter";
|