Skip to content

Commit 37d4fbb

Browse files
committed
feat: 为 ts-loader 添加 AppRouter 生成目标
1 parent 9e8e40f commit 37d4fbb

File tree

11 files changed

+151
-192
lines changed

11 files changed

+151
-192
lines changed

lib/compiler/ui-loader.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path";
2-
import { getResourceLoaderName, toIdent } from "./utils.js";
2+
import { getResourceLoaderName, toIdent } from "../utils.js";
33

44
function toSnakeCase(str) {
55
return str.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
@@ -40,7 +40,6 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
4040
let schemas = {};
4141

4242
const { name: fileName, base: fileBase } = path.parse(filePath);
43-
const refs = [];
4443
const globalLines = [];
4544
const resourceLines = [];
4645
const headerFiles = ["<ui.h>"];
@@ -65,7 +64,9 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
6564
if (schema.refs.length > 0) {
6665
lines.push(
6766
"typedef struct {",
68-
...refs.map((ref) => `${indentStr}ui_widget_t *${ref};`),
67+
...Array.from(new Set(schema.refs)).map(
68+
(ref) => `${indentStr}ui_widget_t *${ref};`
69+
),
6970
`} ${identPrefix}_refs_t;`,
7071
""
7172
);
@@ -95,7 +96,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
9596
}
9697
lines.push(
9798
`static void ${identPrefix}_load_template(ui_widget_t *parent${
98-
refs.length > 0 ? `, ${identPrefix}_refs_t *refs` : ""
99+
schema.refs.length > 0 ? `, ${identPrefix}_refs_t *refs` : ""
99100
})`,
100101
"{",
101102
...(count > 0 ? [`${indentStr}ui_widget_t *w[${count}];\n`] : []),
@@ -126,9 +127,6 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
126127
}
127128

128129
function generateResourceFunc() {
129-
if (resourceLines.length < 1) {
130-
return [];
131-
}
132130
return [
133131
`void ${getResourceLoaderName(
134132
fileName,
@@ -220,7 +218,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
220218
} else {
221219
if (attrs.ref && typeof attrs.ref === "string") {
222220
ident = toIdent(attrs.ref);
223-
refs.push(ident);
221+
currentSchema.refs.push(ident);
224222
ident = `refs->${ident}`;
225223
} else {
226224
ident = `w[${count++}]`;

lib/compiler/utils.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/types.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export interface CompilerContext extends CompilerOptions {
5151
/** 输出文件 */
5252
emitFile(name: string, content: string | Buffer): void;
5353
/** 生成模块 */
54-
generateModule(name: string, generator: () => Promise<string | Buffer>): Promise<void>;
54+
generateModule(name: string, generator: () => string | Buffer | Promise<string | Buffer>): Promise<void>;
5555
logger: import("winston").Logger;
5656
}
5757
export interface LoaderContext extends CompilerContext {
@@ -83,8 +83,8 @@ type LoaderOptions = Record<string, any>;
8383
export interface UILoaderOptions extends LoaderOptions {
8484
indent?: number;
8585
}
86-
type LoaderInput = string | Buffer | ResourceNode;
87-
type Loader = (this: LoaderContext, content: LoaderInput) => LoaderInput | Promise<undefined>;
86+
export type LoaderInput = string | Buffer | ResourceNode;
87+
export type Loader = (this: LoaderContext, content: LoaderInput) => LoaderInput | Promise<LoaderInput>;
8888
type LoaderRule = {
8989
loader: string | Loader;
9090
options: LoaderOptions;

lib/utils.js

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/app-plugin/components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class AppComponentsCompiler {
4242
this.options.appDir,
4343
path.join(this.options.rootContext, c.headerFilePath)
4444
)}"`
45-
),
45+
).join('\n'),
4646
initCode: [
4747
componentList
4848
.filter((c) => c.resourceLoaderName)

src/app-plugin/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ int main(int argc, char *argv[])
1010
{
1111
lcui_app_init();
1212
13-
// Create a router and route to the root path "/", This means that
14-
// your app will present the user interface in app/page.tsx
15-
router_t *router = create_app_router();
13+
// Get app router and route to the root path "/", This means that
14+
// your app will present the user interface in app/page.ts
15+
router_t *router = router_get_by_name("AppRouter");
1616
router_location_t *location = router_location_create(NULL, "/");
1717
router_push(router, location);
1818
@@ -52,7 +52,8 @@ export default class AppPlugin {
5252
}
5353
fs.writeFileSync(
5454
mainHeaderFile,
55-
`#include <LCUI.h>
55+
`#include <locale.h>
56+
#include <LCUI.h>
5657
#include <LCUI/main.h>
5758
${router.includeCode}
5859
${components.includeCode}
@@ -61,8 +62,12 @@ ${router.globalCode}
6162
6263
static void lcui_app_init(void)
6364
{
65+
setlocale(LC_CTYPE, "");
6466
lcui_init();
67+
${router.initCode}
6568
${components.initCode}
69+
ui_widget_set_attr(ui_root(), "router", "AppRouter");
70+
ui_widget_append(ui_root(), ui_create_root_layout());
6671
}
6772
6873
static int lcui_app_run(void)

0 commit comments

Comments
 (0)