release: WRNexusJS 0.2.39
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@wrnexus/compiler",
|
||||
"version": "0.2.38",
|
||||
"version": "0.2.39",
|
||||
"type": "module",
|
||||
"main": "src/index.ts",
|
||||
"exports": {
|
||||
|
||||
@@ -1232,6 +1232,11 @@ function generateComponent(ast: PageAst): string {
|
||||
};
|
||||
const ctx: CompCtx = { stateNames, resolveExpr };
|
||||
|
||||
const serverFunctions = ast.functions
|
||||
.map((body) => body.trim())
|
||||
.filter(Boolean)
|
||||
.join("\n\n");
|
||||
|
||||
const viewCode = ast.view.map((node) => renderComponentNode(node, ctx)).join("");
|
||||
const styles = ast.styles.map((body) => body.trim()).filter(Boolean);
|
||||
const styleTag =
|
||||
@@ -1262,7 +1267,7 @@ function generateComponent(ast: PageAst): string {
|
||||
);
|
||||
}
|
||||
for (const state of ast.states) {
|
||||
decls.push(` const ${nameRefs.get(state.name)} = (${resolveExpr(state.expr)});`);
|
||||
decls.push(` let ${nameRefs.get(state.name)} = (${resolveExpr(state.expr)});`);
|
||||
}
|
||||
|
||||
const returnExpr = needsScope
|
||||
@@ -1402,10 +1407,13 @@ function __wireRaw(v: any): string {
|
||||
}`);
|
||||
}
|
||||
|
||||
const serverFunctionSource = serverFunctions ? `${serverFunctions}\n` : "";
|
||||
|
||||
out.push(
|
||||
`export function render(props: Record<string, any> = {}): string {\n` +
|
||||
` const __p = props || {};\n` +
|
||||
(decls.length > 0 ? decls.join("\n") + "\n" : "") +
|
||||
serverFunctionSource +
|
||||
scopeLine +
|
||||
` return ${returnExpr};\n` +
|
||||
`}`,
|
||||
|
||||
@@ -783,3 +783,58 @@ page Home {
|
||||
|
||||
expect(output).toContain("__wrnexusPropAttr([");
|
||||
});
|
||||
test("component functions are available during server rendering", () => {
|
||||
const output = generate(
|
||||
parse(`
|
||||
component FAQAccordion {
|
||||
props {
|
||||
allowMultiple = false
|
||||
items = []
|
||||
}
|
||||
|
||||
state openIndexes = []
|
||||
|
||||
functions {
|
||||
function isOpen(index) {
|
||||
return openIndexes.includes(index)
|
||||
}
|
||||
|
||||
function toggleItem(index) {
|
||||
if (isOpen(index)) {
|
||||
openIndexes = openIndexes.filter(
|
||||
(itemIndex) =>
|
||||
itemIndex !== index
|
||||
)
|
||||
} else if (allowMultiple) {
|
||||
openIndexes = [
|
||||
...openIndexes,
|
||||
index
|
||||
]
|
||||
} else {
|
||||
openIndexes = [index]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
view {
|
||||
{#each items as item, index}
|
||||
<button
|
||||
class:hidden="!isOpen(index)"
|
||||
@click="toggleItem(index)"
|
||||
>
|
||||
{item.title}
|
||||
</button>
|
||||
{/each}
|
||||
}
|
||||
}
|
||||
`),
|
||||
);
|
||||
|
||||
expect(output).toContain("function isOpen(index)");
|
||||
|
||||
expect(output).toContain("function toggleItem(index)");
|
||||
|
||||
expect(output).toContain("let openIndexes =");
|
||||
|
||||
expect(output).toContain("isOpen(index)");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user