mirror of
https://github.com/zadam/trilium.git
synced 2025-12-22 07:14:24 +01:00
feat(script/jsx): support export default syntax
This commit is contained in:
parent
dcd73ff9f9
commit
284b66acd2
@ -95,7 +95,7 @@ describe("JSX building", () => {
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
const expected = trimIndentation`\
|
const expected = trimIndentation`\
|
||||||
const _jsxFileName = "";function MyComponent() {
|
"use strict";const _jsxFileName = "";function MyComponent() {
|
||||||
return api.preact.h('p', {__self: this, __source: {fileName: _jsxFileName, lineNumber: 2}}, "Hello world." );
|
return api.preact.h('p', {__self: this, __source: {fileName: _jsxFileName, lineNumber: 2}}, "Hello world." );
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
@ -112,7 +112,7 @@ describe("JSX building", () => {
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
const expected = trimIndentation`\
|
const expected = trimIndentation`\
|
||||||
const _jsxFileName = "";function MyComponent() {
|
"use strict";const _jsxFileName = "";function MyComponent() {
|
||||||
return api.preact.h(api.preact.Fragment, null
|
return api.preact.h(api.preact.Fragment, null
|
||||||
, api.preact.h('p', {__self: this, __source: {fileName: _jsxFileName, lineNumber: 3}}, "Hi")
|
, api.preact.h('p', {__self: this, __source: {fileName: _jsxFileName, lineNumber: 3}}, "Hi")
|
||||||
, api.preact.h('p', {__self: this, __source: {fileName: _jsxFileName, lineNumber: 4}}, "there")
|
, api.preact.h('p', {__self: this, __source: {fileName: _jsxFileName, lineNumber: 4}}, "there")
|
||||||
@ -121,4 +121,28 @@ describe("JSX building", () => {
|
|||||||
`;
|
`;
|
||||||
expect(buildJsx(script).code).toStrictEqual(expected);
|
expect(buildJsx(script).code).toStrictEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("rewrites export", () => {
|
||||||
|
const script = trimIndentation`\
|
||||||
|
const { defineWidget } = api.preact;
|
||||||
|
|
||||||
|
export default defineWidget({
|
||||||
|
parent: "right-pane",
|
||||||
|
render() {
|
||||||
|
return <></>;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
`;
|
||||||
|
const expected = trimIndentation`\
|
||||||
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});const { defineWidget } = api.preact;
|
||||||
|
|
||||||
|
module.exports = defineWidget({
|
||||||
|
parent: "right-pane",
|
||||||
|
render() {
|
||||||
|
return api.preact.h(api.preact.Fragment, null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
`;
|
||||||
|
expect(buildJsx(script).code).toStrictEqual(expected);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -220,11 +220,17 @@ return module.exports;
|
|||||||
|
|
||||||
export function buildJsx(contentRaw: string | Buffer) {
|
export function buildJsx(contentRaw: string | Buffer) {
|
||||||
const content = Buffer.isBuffer(contentRaw) ? contentRaw.toString("utf-8") : contentRaw;
|
const content = Buffer.isBuffer(contentRaw) ? contentRaw.toString("utf-8") : contentRaw;
|
||||||
return transform(content, {
|
const output = transform(content, {
|
||||||
transforms: ["jsx"],
|
transforms: ["jsx", "imports"],
|
||||||
jsxPragma: "api.preact.h",
|
jsxPragma: "api.preact.h",
|
||||||
jsxFragmentPragma: "api.preact.Fragment",
|
jsxFragmentPragma: "api.preact.Fragment",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
output.code = output.code.replaceAll(
|
||||||
|
/\bexports\s*\.\s*default\s*=\s*/g,
|
||||||
|
'module.exports = '
|
||||||
|
);
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sanitizeVariableName(str: string) {
|
function sanitizeVariableName(str: string) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user