Skip to content

Commit

Permalink
feat: 为 ts-loader 添加 AppRouter 生成目标
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed May 5, 2024
1 parent 9e8e40f commit 37d4fbb
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 192 deletions.
14 changes: 6 additions & 8 deletions lib/compiler/ui-loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path";
import { getResourceLoaderName, toIdent } from "./utils.js";
import { getResourceLoaderName, toIdent } from "../utils.js";

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

const { name: fileName, base: fileBase } = path.parse(filePath);
const refs = [];
const globalLines = [];
const resourceLines = [];
const headerFiles = ["<ui.h>"];
Expand All @@ -65,7 +64,9 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
if (schema.refs.length > 0) {
lines.push(
"typedef struct {",
...refs.map((ref) => `${indentStr}ui_widget_t *${ref};`),
...Array.from(new Set(schema.refs)).map(
(ref) => `${indentStr}ui_widget_t *${ref};`
),
`} ${identPrefix}_refs_t;`,
""
);
Expand Down Expand Up @@ -95,7 +96,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
}
lines.push(
`static void ${identPrefix}_load_template(ui_widget_t *parent${
refs.length > 0 ? `, ${identPrefix}_refs_t *refs` : ""
schema.refs.length > 0 ? `, ${identPrefix}_refs_t *refs` : ""
})`,
"{",
...(count > 0 ? [`${indentStr}ui_widget_t *w[${count}];\n`] : []),
Expand Down Expand Up @@ -126,9 +127,6 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
}

function generateResourceFunc() {
if (resourceLines.length < 1) {
return [];
}
return [
`void ${getResourceLoaderName(
fileName,
Expand Down Expand Up @@ -220,7 +218,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
} else {
if (attrs.ref && typeof attrs.ref === "string") {
ident = toIdent(attrs.ref);
refs.push(ident);
currentSchema.refs.push(ident);
ident = `refs->${ident}`;
} else {
ident = `w[${count++}]`;
Expand Down
9 changes: 0 additions & 9 deletions lib/compiler/utils.js

This file was deleted.

6 changes: 3 additions & 3 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface CompilerContext extends CompilerOptions {
/** 输出文件 */
emitFile(name: string, content: string | Buffer): void;
/** 生成模块 */
generateModule(name: string, generator: () => Promise<string | Buffer>): Promise<void>;
generateModule(name: string, generator: () => string | Buffer | Promise<string | Buffer>): Promise<void>;
logger: import("winston").Logger;
}
export interface LoaderContext extends CompilerContext {
Expand Down Expand Up @@ -83,8 +83,8 @@ type LoaderOptions = Record<string, any>;
export interface UILoaderOptions extends LoaderOptions {
indent?: number;
}
type LoaderInput = string | Buffer | ResourceNode;
type Loader = (this: LoaderContext, content: LoaderInput) => LoaderInput | Promise<undefined>;
export type LoaderInput = string | Buffer | ResourceNode;
export type Loader = (this: LoaderContext, content: LoaderInput) => LoaderInput | Promise<LoaderInput>;
type LoaderRule = {
loader: string | Loader;
options: LoaderOptions;
Expand Down
104 changes: 0 additions & 104 deletions lib/utils.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/app-plugin/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class AppComponentsCompiler {
this.options.appDir,
path.join(this.options.rootContext, c.headerFilePath)
)}"`
),
).join('\n'),
initCode: [
componentList
.filter((c) => c.resourceLoaderName)
Expand Down
13 changes: 9 additions & 4 deletions src/app-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ int main(int argc, char *argv[])
{
lcui_app_init();
// Create a router and route to the root path "/", This means that
// your app will present the user interface in app/page.tsx
router_t *router = create_app_router();
// Get app router and route to the root path "/", This means that
// your app will present the user interface in app/page.ts
router_t *router = router_get_by_name("AppRouter");
router_location_t *location = router_location_create(NULL, "/");
router_push(router, location);
Expand Down Expand Up @@ -52,7 +52,8 @@ export default class AppPlugin {
}
fs.writeFileSync(
mainHeaderFile,
`#include <LCUI.h>
`#include <locale.h>
#include <LCUI.h>
#include <LCUI/main.h>
${router.includeCode}
${components.includeCode}
Expand All @@ -61,8 +62,12 @@ ${router.globalCode}
static void lcui_app_init(void)
{
setlocale(LC_CTYPE, "");
lcui_init();
${router.initCode}
${components.initCode}
ui_widget_set_attr(ui_root(), "router", "AppRouter");
ui_widget_append(ui_root(), ui_create_root_layout());
}
static int lcui_app_run(void)
Expand Down
Loading

0 comments on commit 37d4fbb

Please sign in to comment.