mirror of
https://github.com/zadam/trilium.git
synced 2025-12-22 07:14:24 +01:00
feat(script/jsx): support import syntax for api
This commit is contained in:
parent
6c1b327f5f
commit
e0416097e1
@ -146,7 +146,7 @@ describe("JSX building", () => {
|
|||||||
expect(buildJsx(script).code).toStrictEqual(expected);
|
expect(buildJsx(script).code).toStrictEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("rewrite React API imports", () => {
|
it("rewrites React API imports", () => {
|
||||||
const script = trimIndentation`\
|
const script = trimIndentation`\
|
||||||
import { defineWidget, RightPanelWidget} from "trilium:preact";
|
import { defineWidget, RightPanelWidget} from "trilium:preact";
|
||||||
defineWidget({
|
defineWidget({
|
||||||
@ -163,6 +163,18 @@ describe("JSX building", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
`;
|
`;
|
||||||
|
expect(buildJsx(script).code).toStrictEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rewrites internal API imports", () => {
|
||||||
|
const script = trimIndentation`\
|
||||||
|
import { log } from "trilium:api";
|
||||||
|
log("Hi");
|
||||||
|
`;
|
||||||
|
const expected = trimIndentation`\
|
||||||
|
"use strict";const _triliumapi = api;
|
||||||
|
_triliumapi.log.call(void 0, "Hi");
|
||||||
|
`;
|
||||||
console.log(buildJsx(script).code);
|
console.log(buildJsx(script).code);
|
||||||
expect(buildJsx(script).code).toStrictEqual(expected);
|
expect(buildJsx(script).code).toStrictEqual(expected);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -240,6 +240,12 @@ export function buildJsx(contentRaw: string | Buffer) {
|
|||||||
'const $1 = api.preact;'
|
'const $1 = api.preact;'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Rewrite ESM-like imports to internal API, to `const { foo } = api.preact`
|
||||||
|
code = code.replaceAll(
|
||||||
|
/var\s+(\w+)\s*=\s*require\(['"]trilium:api['"]\);?/g,
|
||||||
|
'const $1 = api;'
|
||||||
|
);
|
||||||
|
|
||||||
output.code = code;
|
output.code = code;
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user