test(script/jsx): basic JSX processing

This commit is contained in:
Elian Doran 2025-12-20 21:35:52 +02:00
parent 22a83d9f82
commit 645557b505
No known key found for this signature in database
2 changed files with 23 additions and 7 deletions

View File

@ -1,9 +1,11 @@
import { trimIndentation } from "@triliumnext/commons";
import becca from "../becca/becca.js";
import { note, NoteBuilder } from "../test/becca_mocking.js";
import cls from "./cls.js";
import { executeBundle, getScriptBundle } from "./script.js";
import BBranch from "../becca/entities/bbranch.js";
import BNote from "../becca/entities/bnote.js";
import { note, NoteBuilder } from "../test/becca_mocking.js";
import cls from "./cls.js";
import { buildJsx, executeBundle, getScriptBundle } from "./script.js";
describe("Script", () => {
@ -84,3 +86,19 @@ describe("Script", () => {
});
});
});
describe("JSX building", () => {
it("processes basic JSX", () => {
const script = trimIndentation`\
function MyComponent() {
return <p>Hello world.</p>;
}
`;
const expected = trimIndentation`\
const _jsxFileName = "";function MyComponent() {
return api.preact.h('p', {__self: this, __source: {fileName: _jsxFileName, lineNumber: 2}}, "Hello world." );
}
`;
expect(buildJsx(script).code).toStrictEqual(expected);
});
});

View File

@ -198,8 +198,7 @@ export function getScriptBundle(note: BNote, root: boolean = true, scriptEnv: st
let scriptContent = note.getContent();
if (note.isJsx()) {
console.log("GOT JSX!!!");
scriptContent = buildJsx(note).code;
scriptContent = buildJsx(scriptContent).code;
}
bundle.script += `
@ -219,8 +218,7 @@ return module.exports;
return bundle;
}
function buildJsx(jsxNote: BNote) {
const contentRaw = jsxNote.getContent();
export function buildJsx(contentRaw: string | Buffer) {
const content = Buffer.isBuffer(contentRaw) ? contentRaw.toString("utf-8") : contentRaw;
return transform(content, {
transforms: ["jsx"],