一个界面照着豆包抠的 AI 助手。主力是安卓端,完全离线自主:不依赖电脑、 不需要另外装 Termux。 安卓端 - 原生 Jetpack Compose,7 个页面,配色/圆角/间距对着真机截图量出来的 - 完整动效:抽屉滑入、页面切换、气泡淡入上移、面板底部升起、typing 三点、按钮回弹 - 内置 Termux:bash/apt/pkg 388 个命令、真 pty 终端; pty 不可用的 ROM 上自动降级成命令控制台 - APK 内嵌 CPython 3.13(Chaquopy),AI 内核跑在手机上 - 设备能力:定位 / 陀螺仪 / 加速度计 / 光线 / 电量 / 剪贴板 - 语音:朗读 + 语音输入,火山豆包音色 / OpenAI 兼容 / 设备自带三级引擎 AI 能力 - 10 家 OpenAI 兼容提供商:方舟 / OpenAI / DeepSeek / Kimi / 智谱 / 通义 / 硅基流动 / OpenRouter / 本地 Ollama / 自定义 - 思考强度按各家支持情况发不同字段(thinking / reasoning_effort / 不发), 认不出来的一律不发,避免给不支持的厂商发字段直接 400 - MCP 三种传输(http / stdio / inproc)、工具热加载、文件上传、权限模式 跨端 - 安卓走原生 Compose,Web / Windows 走 Lynx - Node 与 Python 两份内核接口逐字段一致,关键路径都有对拍脚本 自检脚本(都不需要真 Key) - verify-phone.mjs 设备端 24 项验收 - verify-speech.mjs 语音链路 31 项,两份内核各跑一遍 - parity-thinking.mjs 思考强度映射对拍 - probe-settings-wipe.mjs 设置防清空攻击测试 - mock-llm.mjs 假的 OpenAI + 火山语音服务 许可 MIT;第三方组件(Termux Apache-2.0、Chaquopy、Compose、LynxJS、Electron) 的授权见 LICENSE。
1337 lines
60 KiB
Markdown
1337 lines
60 KiB
Markdown
# LynxJS 在浏览器中运行移动端界面 —— 可执行搭建手册
|
||
|
||
> 目标:用 ReactLynx + Rspeedy 写一份移动端界面代码,构建出 Web 产物,在浏览器里通过 `<lynx-view>` 渲染出来。
|
||
> 调研时间:2026 年(版本号取自 npm registry 的 `dist-tags.latest`)。所有关键结论均附来源 URL。
|
||
|
||
---
|
||
|
||
## 0. 先看结论:最短可行路径(TL;DR)
|
||
|
||
```bash
|
||
# 1) 脚手架(注意:是 create rspeedy,不是 create lynx)
|
||
npm create rspeedy@latest my-lynx-app # 选择 React + TypeScript + Rspeedy
|
||
cd my-lynx-app
|
||
npm install
|
||
|
||
# 2) 让 Rspeedy 同时产出 web 产物:编辑 lynx.config.ts 加 environments
|
||
# environments: { lynx: {}, web: {} }
|
||
|
||
# 3) 因为产物是 .web.bundle,需要自己写一个 index.html 去挂 <lynx-view>
|
||
# 最省事的做法:把 index.html 放进项目根的 public/ 目录(见 §3.4)
|
||
|
||
# 4) 开发
|
||
npm run dev # Rspeedy dev server,默认 http://localhost:3000
|
||
|
||
# 5) 打开浏览器
|
||
# http://localhost:3000/index.html
|
||
```
|
||
|
||
**一句话原理**:Rspeedy 用 `environments` 区分平台,产物文件名模板是 `[name].[platform].bundle`,所以 `web` 环境产出 `dist/main.web.bundle`;浏览器侧用 `@lynx-js/web-core` 注册一个 `<lynx-view>` 自定义元素,把 `url` 指向这个 `.web.bundle`,Lynx 就在浏览器里跑起来了(自定义元素映射到真实 DOM,用浏览器的 CSS/布局引擎渲染)。
|
||
|
||
**必须知道的三个反直觉点**:
|
||
1. `npm create lynx@latest` 是**错的**(见 §1.3)。
|
||
2. Rspeedy **不会**给你生成 HTML 页面,`<lynx-view>` 那层 HTML 要你自己写(见 §4)。
|
||
3. 入口是 `root.render(<App />)` 而不是 `createRoot(...).render(...)`。
|
||
|
||
---
|
||
|
||
## 1. 当前版本号(2026 年,npm registry `dist-tags.latest`)
|
||
|
||
### 1.1 核心包版本表
|
||
|
||
| 包名 | latest 版本 | 说明 | 来源 |
|
||
| --- | --- | --- | --- |
|
||
| `@lynx-js/react` | **0.126.2** | ReactLynx 框架本体(基于 Preact) | https://registry.npmjs.org/@lynx-js%2Freact/latest |
|
||
| `@lynx-js/rspeedy` | **0.18.0** | 构建工具(Rspack 驱动) | https://registry.npmjs.org/@lynx-js%2Frspeedy/latest |
|
||
| `@lynx-js/react-rsbuild-plugin` | **0.20.3** | ReactLynx 的 Rsbuild 插件(`pluginReactLynx`) | https://registry.npmjs.org/@lynx-js%2Freact-rsbuild-plugin/latest |
|
||
| `@lynx-js/web-core` | **0.26.2** | **Web 端运行时,提供 `<lynx-view>`** | https://registry.npmjs.org/@lynx-js%2Fweb-core/latest |
|
||
| `@lynx-js/web-elements` | **0.12.12** | Web 自定义元素实现(web-core 的依赖) | https://registry.npmjs.org/@lynx-js%2Fweb-elements/latest |
|
||
| `@lynx-js/lynx-ui` | **3.139.0** | 官方 headless 组件库 | https://registry.npmjs.org/@lynx-js%2Flynx-ui/latest |
|
||
| `@lynx-js/types` | **4.3.0** | Lynx API / 元件类型定义 | https://registry.npmjs.org/@lynx-js%2Ftypes/latest |
|
||
| `@lynx-js/qrcode-rsbuild-plugin` | **0.7.3** | 终端二维码插件(`pluginQRCode`) | https://registry.npmjs.org/@lynx-js%2Fqrcode-rsbuild-plugin/latest |
|
||
| `@lynx-js/create-lynx` | **0.2.1** | **真正的脚手架本体** | https://registry.npmjs.org/@lynx-js%2Fcreate-lynx/latest |
|
||
| `create-rspeedy` | **0.18.0** | 脚手架入口(现已转发到 `@lynx-js/create-lynx`) | https://registry.npmjs.org/create-rspeedy/latest |
|
||
| `@lynx-js/web-platform-rsbuild-plugin` | **0.1.2** | Web 侧构建性能优化插件(可选) | https://registry.npmjs.org/@lynx-js%2Fweb-platform-rsbuild-plugin/latest |
|
||
|
||
### 1.2 关键兼容性约束(从 registry 元数据里读出来的)
|
||
|
||
- `@lynx-js/rspeedy@0.18.0` 的 `engines.node` = `^20.19.0 || >=22.12.0`。**Node 20.19 以下直接不支持**。
|
||
- `@lynx-js/react-rsbuild-plugin@0.20.3` 的 peerDependencies:
|
||
- `@lynx-js/react`: `^0.124.0 || ^0.125.0 || ^0.126.0`
|
||
- `@lynx-js/rspeedy`: `^0.17.0 || ^0.18.0`
|
||
- `@rsbuild/core`: `^2.0.0`
|
||
- 也就是说 `@lynx-js/react@0.126.2` + `@lynx-js/rspeedy@0.18.0` + 插件 `0.20.3` 是当前官方自测的组合。
|
||
- `@lynx-js/rspeedy@0.18.0` 依赖 `@rsbuild/core@2.2.9`(即 Rspeedy 0.18 建立在 Rsbuild 2.x / Rspack 2.x 之上)。
|
||
- `@lynx-js/web-core@0.26.2` 依赖 `@lynx-js/web-elements@0.12.12`、`@lynx-js/web-worker-rpc@0.26.2`;`browserslist.production` = `Chrome >= 92, Safari >= 16.4`。
|
||
- `@lynx-js/lynx-ui@3.139.0` 的 peerDependencies:`@lynx-js/react >=0.100.0`、`@lynx-js/types *`、`@types/react ^18`。
|
||
|
||
### 1.3 ⚠️ `create lynx` 是坑
|
||
|
||
- npm 上确实存在 `create-lynx@1.0.0`(https://www.npmjs.com/package/create-lynx),但它**不是字节的包**:作者 `loisz`,2025-01-17 发布,`unpackedSize` 仅 **225 字节**,只有一个 `index.js`,`license: ISC`,没有任何 Lynx 官方仓库关联。**不要用**。
|
||
- 官方脚手架是 **`@lynx-js/create-lynx`**(Apache-2.0,仓库 `github.com/lynx-family/lynx-stack` 的 `packages/rspeedy/create-lynx`)。
|
||
- `create-rspeedy@0.18.0` 的 package.json 描述写得很直白:`"description": "Deprecated: use @lynx-js/create-lynx instead"`,且 `"dependencies": { "@lynx-js/create-lynx": "0.2.1" }` —— 所以 `npm create rspeedy@latest` 只是官方留的兼容入口。
|
||
- 官方快速开始页明确推荐:**推荐通过 `create-rspeedy` 来快速创建一个 Lynx 项目**,命令是 `npm create rspeedy@latest`。
|
||
来源:https://lynxjs.org/zh/guide/start/quick-start.md
|
||
|
||
**两者都可用**:
|
||
|
||
```bash
|
||
npm create rspeedy@latest my-lynx-app # 官方文档写法(推荐,等价转发)
|
||
# 或
|
||
npm create @lynx-js/lynx@latest my-lynx-app # 直接调用真正的脚手架
|
||
```
|
||
|
||
---## 2. 脚手架生成的目录结构与关键文件内容
|
||
|
||
## 2. 脚手架生成的目录结构与关键文件内容
|
||
|
||
### 2.1 交互式提问
|
||
|
||
`npm create rspeedy@latest` 会依次问:项目名 → 是否用 TypeScript → 选模板。选 **React + TypeScript + Rspeedy** 会生成 `template-rspeedy-react-ts`,这也是下面所有内容的依据。
|
||
|
||
来源:https://github.com/lynx-family/lynx-stack/tree/main/packages/rspeedy/create-lynx/template-rspeedy-react-ts
|
||
|
||
### 2.2 目录结构(以 `template-rspeedy-react-ts` 为准)
|
||
|
||
```text
|
||
my-lynx-app/
|
||
├── package.json
|
||
├── lynx.config.ts # Rspeedy 配置(注意:不是 rsbuild.config.ts)
|
||
├── tsconfig.json # 根 tsconfig,只做 references
|
||
├── tsconfig.node.json # 给 lynx.config.ts 用
|
||
├── rstest.config.ts
|
||
└── src/
|
||
├── tsconfig.json # 源码的 tsconfig(jsx / jsxImportSource 在这里)
|
||
├── lynx-env.d.ts # Rspeedy 类型声明(等价于文档里的 rspeedy-env.d.ts)
|
||
├── index.tsx # 入口
|
||
├── App.tsx # 示例组件
|
||
├── App.css
|
||
├── assets/
|
||
│ ├── arrow.png
|
||
│ ├── lynx-logo.png
|
||
│ └── react-logo.png
|
||
├── lib/flappy.ts
|
||
├── useFlappy.ts
|
||
└── __tests__/index.test.tsx
|
||
```
|
||
|
||
> 注意:Rspeedy 模板里配置文件叫 **`lynx.config.ts`**;而那个 `template-rsbuild-react-ts`(不用 Rspeedy 的变体)里叫 `rsbuild.config.ts` 并且 `dev` 脚本是 `rsbuild dev`。**我们要的是 Rspeedy 版,务必确认文件名是 `lynx.config.ts`、scripts 里是 `rspeedy dev`。**
|
||
|
||
### 2.3 `package.json`
|
||
|
||
脚手架模板里的依赖是 monorepo 的 `workspace:*`,会由 create-lynx 在生成时替换为真实版本号。写成可直接安装的形式如下:
|
||
|
||
```json
|
||
{
|
||
"name": "my-lynx-app",
|
||
"version": "0.0.0",
|
||
"type": "module",
|
||
"scripts": {
|
||
"build": "rspeedy build",
|
||
"dev": "rspeedy dev",
|
||
"preview": "rspeedy preview",
|
||
"test": "rstest run"
|
||
},
|
||
"dependencies": {
|
||
"@lynx-js/react": "^0.126.2"
|
||
},
|
||
"devDependencies": {
|
||
"@lynx-js/preact-devtools": "^5.0.1-20260827072047-5f77e12",
|
||
"@lynx-js/qrcode-rsbuild-plugin": "^0.7.3",
|
||
"@lynx-js/react-rsbuild-plugin": "^0.20.3",
|
||
"@lynx-js/rspeedy": "^0.18.0",
|
||
"@lynx-js/types": "^4.3.0",
|
||
"@rsbuild/plugin-type-check": "^1.6.0",
|
||
"@rstest/core": "^0.12.1",
|
||
"@testing-library/dom": "^10.4.1",
|
||
"@testing-library/jest-dom": "^7.0.0",
|
||
"@types/react": "^19.2.18",
|
||
"jsdom": "^27.4.0",
|
||
"typescript": "^6.0.3"
|
||
},
|
||
"engines": {
|
||
"node": "^20.19.0 || >=22.12.0"
|
||
}
|
||
}
|
||
```
|
||
|
||
模板原始内容(`workspace:*` 形式):
|
||
|
||
```json
|
||
{
|
||
"name": "lynx-rspeedy-react-ts",
|
||
"version": "0.0.0",
|
||
"type": "module",
|
||
"scripts": {
|
||
"build": "rspeedy build",
|
||
"dev": "rspeedy dev",
|
||
"preview": "rspeedy preview",
|
||
"test": "rstest run"
|
||
},
|
||
"dependencies": { "@lynx-js/react": "workspace:*" },
|
||
"devDependencies": {
|
||
"@lynx-js/preact-devtools": "workspace:*",
|
||
"@lynx-js/qrcode-rsbuild-plugin": "workspace:*",
|
||
"@lynx-js/react-rsbuild-plugin": "workspace:*",
|
||
"@lynx-js/types": "workspace:*",
|
||
"@rsbuild/plugin-type-check": "workspace:*",
|
||
"@rstest/core": "workspace:*",
|
||
"@testing-library/dom": "workspace:*",
|
||
"@testing-library/jest-dom": "workspace:*",
|
||
"@types/react": "workspace:*",
|
||
"jsdom": "workspace:*",
|
||
"typescript": "workspace:*"
|
||
},
|
||
"engines": { "node": "^20.19.0 || >=22.12.0" }
|
||
}
|
||
```
|
||
|
||
注意 **`"type": "module"`** —— 配置文件是 ESM。
|
||
|
||
### 2.4 `lynx.config.ts`(模板原样)
|
||
|
||
```ts
|
||
import { pluginQRCode } from '@lynx-js/qrcode-rsbuild-plugin';
|
||
import { pluginReactLynx } from '@lynx-js/react-rsbuild-plugin';
|
||
import { defineConfig } from '@lynx-js/rspeedy';
|
||
|
||
export default defineConfig({
|
||
source: {
|
||
entry: {
|
||
main: './src/index.tsx',
|
||
},
|
||
},
|
||
plugins: [
|
||
pluginQRCode({
|
||
schema(url) {
|
||
// We use `?fullscreen=true` to open the page in LynxExplorer in fullscreen mode
|
||
return `${url}?fullscreen=true`;
|
||
},
|
||
}),
|
||
pluginReactLynx(),
|
||
],
|
||
});
|
||
```
|
||
|
||
**改造为同时产出 web 产物**(这是本手册的核心改动,见 §3.3):
|
||
|
||
```ts
|
||
import { pluginQRCode } from '@lynx-js/qrcode-rsbuild-plugin';
|
||
import { pluginReactLynx } from '@lynx-js/react-rsbuild-plugin';
|
||
import { defineConfig } from '@lynx-js/rspeedy';
|
||
|
||
export default defineConfig({
|
||
source: {
|
||
entry: {
|
||
main: './src/index.tsx',
|
||
},
|
||
},
|
||
environments: {
|
||
lynx: {},
|
||
web: {},
|
||
},
|
||
plugins: [
|
||
pluginQRCode({
|
||
schema(url) {
|
||
return `${url}?fullscreen=true`;
|
||
},
|
||
}),
|
||
pluginReactLynx(),
|
||
],
|
||
});
|
||
```
|
||
|
||
> `pluginQRCode` 只对原生调试有意义(终端二维码给 LynxExplorer 扫)。纯做 Web 时留着无害,也可以删掉。
|
||
|
||
### 2.5 `src/index.tsx`(模板原样,注意没有 createRoot)
|
||
|
||
```tsx
|
||
import '@lynx-js/preact-devtools';
|
||
import '@lynx-js/react/debug';
|
||
import { root } from '@lynx-js/react';
|
||
|
||
import { App } from './App.js';
|
||
|
||
root.render(<App />);
|
||
|
||
if (import.meta.webpackHot) {
|
||
import.meta.webpackHot.accept();
|
||
}
|
||
```
|
||
|
||
### 2.6 `src/App.tsx`(模板原样)
|
||
|
||
```tsx
|
||
import { useCallback, useEffect, useState } from '@lynx-js/react';
|
||
|
||
import './App.css';
|
||
import arrow from './assets/arrow.png';
|
||
import lynxLogo from './assets/lynx-logo.png';
|
||
import reactLynxLogo from './assets/react-logo.png';
|
||
|
||
export function App() {
|
||
const [alterLogo, setAlterLogo] = useState(false);
|
||
|
||
useEffect(() => {
|
||
console.info('Hello, ReactLynx');
|
||
}, []);
|
||
|
||
const onTap = useCallback(() => {
|
||
"background-only";
|
||
setAlterLogo(!alterLogo);
|
||
}, [alterLogo]);
|
||
|
||
return (
|
||
<page>
|
||
<view className="Background" />
|
||
<view className="App">
|
||
<view className="Banner">
|
||
<view className="Logo" bindtap={onTap}>
|
||
{alterLogo
|
||
? <image src={reactLynxLogo} className="Logo--react" />
|
||
: <image src={lynxLogo} className="Logo--lynx" />}
|
||
</view>
|
||
<text className="Title">React</text>
|
||
<text className="Subtitle">on Lynx</text>
|
||
</view>
|
||
<view className="Content">
|
||
<image src={arrow} className="Arrow" />
|
||
<text className="Description">Tap the logo and have fun!</text>
|
||
<text className="Hint">
|
||
Edit<text style={{ fontStyle: "italic", color: "rgba(255, 255, 255, 0.85)" }}>{" src/App.tsx "}</text>
|
||
to see updates!
|
||
</text>
|
||
</view>
|
||
<view style={{ flex: 1 }}></view>
|
||
</view>
|
||
</page>
|
||
);
|
||
}
|
||
```
|
||
|
||
要读出来的三件事:**`<page>` 是根节点**、文字全部包在 `<text>` 里、点击用 `bindtap` 而不是 `onClick`。
|
||
|
||
### 2.7 `tsconfig.json`(根,模板原样)
|
||
|
||
```json
|
||
{
|
||
"compilerOptions": {
|
||
"strict": true,
|
||
"isolatedModules": true,
|
||
"verbatimModuleSyntax": true,
|
||
|
||
"esModuleInterop": true,
|
||
"skipLibCheck": true
|
||
},
|
||
"references": [{ "path": "./tsconfig.node.json" }, { "path": "./src" }],
|
||
"files": []
|
||
}
|
||
```
|
||
|
||
### 2.8 `src/tsconfig.json`(**关键:jsxImportSource**)
|
||
|
||
```json
|
||
{
|
||
"extends": "../tsconfig.json",
|
||
"compilerOptions": {
|
||
"jsx": "react-jsx",
|
||
"jsxImportSource": "@lynx-js/react",
|
||
"lib": ["ES2023", "DOM", "DOM.Iterable"],
|
||
"module": "ESNext",
|
||
"moduleResolution": "Bundler",
|
||
"noEmit": true,
|
||
"allowJs": true,
|
||
"types": ["@lynx-js/rspeedy/client"],
|
||
"composite": true,
|
||
"verbatimModuleSyntax": true
|
||
},
|
||
"include": [".*/*.ts", "./*.tsx"]
|
||
}
|
||
```
|
||
|
||
> `"jsx": "react-jsx"` + `"jsxImportSource": "@lynx-js/react"` **两个都要有**。只写 `jsx: react-jsx` 会让 TS 去解析 `react/jsx-runtime`,类型全错。
|
||
> 来源:https://lynxjs.org/zh/llms.txt(第 8 节「类型系统」)
|
||
>
|
||
> ⚠️ 上面 `include` 的 glob 是从模板 base64 内容转录的,可能有转义失真。实际以脚手架生成的文件为准;它写错也不影响 Rspeedy 构建(Rspeedy **默认不跑类型检查**,需要的话要另外装 `@rsbuild/plugin-type-check`)。
|
||
> 来源:https://lynxjs.org/zh/rspeedy/typescript.md
|
||
>
|
||
> ⚠️ `@lynx-js/types`:脚手架模板里 pin 的是 `4.1.0`,而 npm 上 latest 已是 **4.3.0**。用 `^4.3.0` 或让脚手架自己决定都可以。
|
||
|
||
### 2.9 `tsconfig.node.json`
|
||
|
||
```json
|
||
{
|
||
"compilerOptions": {
|
||
"allowJS": true,
|
||
"composite": true,
|
||
"module": "ESNext",
|
||
"moduleResolution": "Bundler",
|
||
"noEmit": true,
|
||
"types": [],
|
||
"lib": ["ES2023"]
|
||
},
|
||
"include": ["./lynx.config.ts"]
|
||
}
|
||
```
|
||
|
||
### 2.10 `src/lynx-env.d.ts`
|
||
|
||
```ts
|
||
/// <reference types="@lynx-js/rspeedy/client" />
|
||
```
|
||
|
||
这一行负责给 `*.png`、`*.module.css` 等资源/样式导入提供类型。
|
||
来源:https://lynxjs.org/zh/rspeedy/typescript.md 、https://lynxjs.org/zh/rspeedy/assets.md
|
||
|
||
> 文档里这个文件叫 `src/rspeedy-env.d.ts`,模板里叫 `src/lynx-env.d.ts`,**文件名无所谓,内容那一行 reference 才是关键**。
|
||
|
||
---
|
||
|
||
## 3. 在浏览器/Web 中渲染 Lynx 页面
|
||
|
||
### 3.1 架构说明:Web 端是怎么跑的
|
||
|
||
- `@lynx-js/web-core` 在 Web 上实现了 Lynx 引擎的 native binding API:它用**浏览器的 CSS 引擎、布局引擎、渲染引擎**提供渲染管线,用浏览器的 JS 引擎跑 JS。
|
||
- UI 元件映射为一组 **Custom Elements**(`@lynx-js/web-elements` 包提供,如 `XView`/`XText`/`XImage`/`ScrollView`/`XList`…)。
|
||
- 与 Android/iOS 产物的差别在于:原生平台的产物做了 AOT 字节码优化(`encode`),因为那边没有 JIT;**Web 端的产物不做这个优化,所以是另一种格式** —— 这就是为什么需要 `.web.bundle`。
|
||
来源:https://github.com/lynx-family/lynx-stack/blob/main/packages/web-platform/readme.md
|
||
|
||
### 3.2 `<lynx-view>` 的用法(官方 API)
|
||
|
||
```ts
|
||
import '@lynx-js/web-core/client';
|
||
```
|
||
|
||
就这一行,**不需要再手动 import CSS,也不需要 import `@lynx-js/web-elements`**(`@lynx-js/web-core@0.20.0` 架构升级后已简化)。
|
||
|
||
`<lynx-view>` 属性:
|
||
|
||
| 属性 | 必填 | 说明 |
|
||
| --- | --- | --- |
|
||
| `url` | **是** | Rspeedy 产物的 URL(其它 chunk 的 url 会被编译期注入产物自动启动) |
|
||
| `globalProps` | 否 | 初始化时的 GlobalProps |
|
||
| `initData` | 否 | 初始化时的 InitData |
|
||
| `browser-config` | 否 | 实例级动态配置 `systemInfo`,如 `pixelRatio` / `pixelWidth` / `pixelHeight` |
|
||
| `transform-vw` | 否 | 开启 vw 转换,`1vw` → `calc(1 * var(--vw-unit))` |
|
||
| `transform-vh` | 否 | 同上,vh |
|
||
|
||
**宽高是硬性要求**:`lynx-view` 内部排版会被强制移出外部排版流,所有 `lynx-view` 都被强制应用 CSS Containment。默认**必须**给它设置宽高(可以来自 `flex-grow`,可以是百分比,但**不能靠内容撑开**)。确实需要内容决定宽度/高度时,用 `height="auto"` / `width="auto"` 启动自动宽高监听。
|
||
|
||
**已在 0.20.0 移除的属性**(别再照抄旧博客):`thread-strategy`、`customTemplateLoader`、`overrideLynxTagToHTMLTagMap`、`inject-head-links`。
|
||
|
||
来源:https://lynxjs.org/zh/api/lynx-native-api/lynx-view/lynx-view.md
|
||
|
||
### 3.3 Rspeedy 如何构建出 web 可用的 bundle
|
||
|
||
在 `lynx.config.ts` 里加 `environments`:
|
||
|
||
```ts
|
||
export default defineConfig({
|
||
environments: {
|
||
web: {},
|
||
lynx: {},
|
||
},
|
||
});
|
||
```
|
||
|
||
官方文档明确给了这个写法:
|
||
```ts
|
||
import { defineConfig } from '@lynx-js/rspeedy';
|
||
import { pluginReactLynx } from '@lynx-js/react-rsbuild-plugin';
|
||
|
||
export default defineConfig({
|
||
plugins: [pluginReactLynx()],
|
||
environments: {
|
||
web: {},
|
||
lynx: {},
|
||
},
|
||
});
|
||
```
|
||
来源:https://lynxjs.org/zh/guide/start/integrate-with-existing-apps.md (「将 Lynx 集成到 Web 平台」章节)
|
||
|
||
`environments` 的语义(API 文档):
|
||
- 用于设置 output environment。
|
||
- **未设置时,Rspeedy 向 Rsbuild 传 `{ lynx: {} }`** —— 这就是为什么默认只产出 `.lynx.bundle`。
|
||
- "Normally you don't need this if you are not using Lynx for Web."
|
||
来源:https://lynxjs.org/api/rspeedy/rspeedy.config.environments.md
|
||
|
||
**产物文件名**:`output.filename.bundle` 默认值是 **`'[name].[platform].bundle'`**,其中 `[platform]` = "the environment name of the bundle"。所以:
|
||
|
||
| 环境 | 入口名 | 产物 |
|
||
| --- | --- | --- |
|
||
| `lynx` | `main` | `dist/main.lynx.bundle` |
|
||
| `web` | `main` | **`dist/main.web.bundle`** |
|
||
|
||
来源:https://lynxjs.org/api/rspeedy/rspeedy.filename.bundle.md
|
||
|
||
官方也明确写了:
|
||
> 你将会看到项目中多出了 `dist/main.web.bundle` 文件,它就是最终的 web 构建产物。
|
||
|
||
### 3.4 你需要的 HTML 页面(必须自己写)
|
||
|
||
**Rspeedy 不生成 HTML。** 官方给的 Web 接入方式是「创建一个新的 web 工程(Rsbuild/React)→ `npm install @lynx-js/web-core` → 在 `App.tsx` 里用 `<lynx-view>`」,并且要么用 `server.publicDir` 指向 Lynx 项目的 `dist`,要么把 `.web.bundle` 当作静态资源。
|
||
|
||
来源:https://lynxjs.org/zh/guide/start/integrate-with-existing-apps.md
|
||
|
||
**最省事方案(单文件、零额外工程):把 `index.html` 放到项目根的 `public/` 目录。**
|
||
|
||
依据:Rspeedy 的 `public` 目录有两个行为 —— 开发服务器下这些资源被托管在 `/` 根路径;生产构建时被复制到输出目录。并且**不要用 import 引用 public 里的文件,用 URL 绝对路径引用**。
|
||
来源:https://lynxjs.org/zh/rspeedy/assets.md
|
||
|
||
新建 `public/index.html`:
|
||
|
||
```html
|
||
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>Lynx on Web</title>
|
||
<style>
|
||
html,
|
||
body {
|
||
margin: 0;
|
||
height: 100%;
|
||
}
|
||
/* 移动端界面:手机宽度居中显示 */
|
||
lynx-view {
|
||
display: block;
|
||
width: 100%;
|
||
max-width: 420px;
|
||
height: 100vh;
|
||
margin: 0 auto;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<lynx-view url="/main.web.bundle"></lynx-view>
|
||
<script type="module">
|
||
import '@lynx-js/web-core/client';
|
||
// 可选:等元素升级完成后再设置动态属性
|
||
const view = document.querySelector('lynx-view');
|
||
view.addEventListener('error', (e) => {
|
||
console.error('[lynx-view error]', e.detail);
|
||
});
|
||
</script>
|
||
</body>
|
||
</html>
|
||
```
|
||
|
||
> 这里用的是 **bare specifier** `'@lynx-js/web-core/client'`。浏览器本身不认识这种写法 —— 它能工作是因为 Rspeedy/Rspack 会处理 `public/index.html` 里的 module script 吗?**不会**。所以稳妥起见请用下面两种方式之一:
|
||
|
||
**方式 A(推荐,最稳):用 CDN 的预构建产物** —— `@lynx-js/web-core` 的 `exports` 里提供了已打包好的入口:
|
||
|
||
```html
|
||
<!-- 预构建的 client JS + CSS,直接由 CDN 提供 -->
|
||
<link rel="stylesheet" href="https://unpkg.com/@lynx-js/web-core@0.26.2/client.prod.css" />
|
||
<script type="module" src="https://unpkg.com/@lynx-js/web-core@0.26.2/client.prod.js"></script>
|
||
|
||
<lynx-view url="/main.web.bundle" style="display:block;width:100%;height:100vh"></lynx-view>
|
||
```
|
||
|
||
依据:`@lynx-js/web-core@0.26.2` 的 package.json `exports` 中含
|
||
`"./client.prod.js": { "default": "./dist/client_prod/static/js/client.js" }` 和
|
||
`"./client.prod.css": { "default": "./dist/client_prod/static/css/client.css" }`。
|
||
来源:https://registry.npmjs.org/@lynx-js%2Fweb-core/latest
|
||
|
||
**方式 B(工程化):独立 Rsbuild/React Web 工程**,按官方文档走:
|
||
|
||
```bash
|
||
# 在上述 Lynx 项目同级
|
||
npm create rsbuild@latest # 选 React
|
||
cd <web-project-name>
|
||
npm install
|
||
npm install @lynx-js/web-core
|
||
```
|
||
|
||
`src/App.tsx`:
|
||
```tsx
|
||
import './App.css';
|
||
import '@lynx-js/web-core/client';
|
||
import type { LynxViewElement as LynxView } from '@lynx-js/web-core/client';
|
||
|
||
const App = () => {
|
||
return (
|
||
<lynx-view
|
||
style={{ height: '100vh', width: '100vw' }}
|
||
url="/main.web.bundle"
|
||
></lynx-view>
|
||
);
|
||
};
|
||
|
||
export default App;
|
||
```
|
||
|
||
`rsbuild.config.ts`(把 Lynx 项目的 dist 作为 publicDir):
|
||
```ts
|
||
import { defineConfig } from '@rsbuild/core';
|
||
import { pluginReact } from '@rsbuild/plugin-react';
|
||
import path from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
const __filename = fileURLToPath(import.meta.url);
|
||
const __dirname = path.dirname(__filename);
|
||
|
||
export default defineConfig({
|
||
plugins: [pluginReact()],
|
||
server: {
|
||
publicDir: [
|
||
{
|
||
name: path.join(
|
||
__dirname,
|
||
'../',
|
||
// 这里请替换为你实际的 Lynx 项目名称
|
||
'lynx-project',
|
||
'dist',
|
||
),
|
||
},
|
||
],
|
||
},
|
||
});
|
||
```
|
||
然后 `npm run dev` → 访问 `http://localhost:3000`。
|
||
来源:https://lynxjs.org/zh/guide/start/integrate-with-existing-apps.md
|
||
|
||
### 3.5 single-threaded 还是 multi-threaded?需要 `.web.bundle` 吗?
|
||
|
||
**结论:**
|
||
|
||
1. **需要 `.web.bundle`。** `output.filename.bundle` 默认 `[name].[platform].bundle`,`platform` 取环境名,所以 `environments.web` 产出 `main.web.bundle`。`<lynx-view url="...">` 指向的就是它。**.lynx.bundle 不能给 web-core 用**(原生产物经过 `encode` AOT 优化、是二进制字节码,Web 端产物是另一种格式)。
|
||
来源:https://lynxjs.org/api/rspeedy/rspeedy.filename.bundle.md 、https://github.com/lynx-family/lynx-stack/blob/main/packages/web-platform/readme.md
|
||
|
||
2. **不再是 single/multi-threaded 二选一的问题** —— `thread-strategy` 这个属性在 `@lynx-js/web-core@0.20.0` 被**移除**了,同时移除了 `customTemplateLoader`、`overrideLynxTagToHTMLTagMap`、`inject-head-links`。旧版(≤0.19)才有 `thread-strategy`(值是 `multi-thread` / `single-thread` / `all-on-ui`)。
|
||
来源:https://lynxjs.org/zh/api/lynx-native-api/lynx-view/lynx-view.md (「从旧版本迁移」章节)
|
||
|
||
3. **你也不需要为 web-core 单独指定 bundle 类型或加 `.web` 后缀配置** —— 只要 `environments.web` 存在,Rspeedy 就按 web 目标编译,产物自然带 `.web`。
|
||
|
||
4. 早期那个 `Uncaught SecurityError: Failed to construct 'Worker': Script at 'xxx' cannot be accessed from origin 'xxx'` 报错(跨域 Worker)来自多线程架构。文档仍保留了这条 FAQ,解决方式是引入 `remote-web-worker`(且必须在 `@lynx-js/web-core` **之前**引入)。在同源部署 + 当前版本下一般不会遇到,但要知道有这么个东西。
|
||
来源:同上 FAQ 章节
|
||
|
||
---
|
||
|
||
## 4. Rspeedy dev server:端口、命令、是否产出 HTML
|
||
|
||
| 问题 | 答案 | 依据 |
|
||
| --- | --- | --- |
|
||
| 启动命令 | `rspeedy dev`,脚手架里是 `npm run dev` | https://lynxjs.org/zh/rspeedy/cli.md |
|
||
| 默认端口 | **3000**(Rsbuild 默认值) | https://lynxjs.org/api/rspeedy/rspeedy.server.port.md |
|
||
| 端口被占用 | 默认**自动递增**端口号,直到找到可用端口 | 同上 + `server.strictPort` |
|
||
| 自定义端口 | `server: { port: 3470 }` | 同上 |
|
||
| 是否产出可访问的 HTML | **否**。Rspeedy 产物只有 bundle / js / 静态资源,**没有 HTML**。`<lynx-view>` 所在的 HTML 要自己写 | https://lynxjs.org/zh/rspeedy/output.md 、 §3.4 |
|
||
| dev 产物写盘 | `dev.writeToDisk` 默认为 **`true`**,但**通过浏览器访问的 bundle 仍从内存提供**(走 webpack-dev-middleware) | https://lynxjs.org/api/rspeedy/rspeedy.dev.writetodisk.md |
|
||
| 生产预览 | `rspeedy preview`(需先 `rspeedy build`;官方说不要用于生产服务器) | https://lynxjs.org/zh/rspeedy/cli.md |
|
||
|
||
### 4.1 Rspeedy CLI 全貌
|
||
|
||
```text
|
||
Usage: rspeedy <command> [options]
|
||
|
||
Commands:
|
||
build [options] Build the project in production mode
|
||
dev [options] Run the dev server and watch for source file changes while serving.
|
||
inspect [options] View the Rsbuild config and Rspack config of the project.
|
||
preview [options] Preview the production build outputs locally.
|
||
help [command] display help for command
|
||
```
|
||
|
||
常用参数:`--environment <name...>`(只构建指定环境)、`--base <base>`、`-m --mode`、`-c --config`。
|
||
`rspeedy dev` 的 dev server 配置在 `dev` 段(`hmr` / `liveReload` / `assetPrefix` / `writeToDisk` / `watchFiles` / `progressBar`),`server` 段管 host/port/base/proxy/cors/headers。
|
||
|
||
### 4.2 开发环境产物结构(`dist/.rspeedy`)
|
||
|
||
```text
|
||
dist/
|
||
├── .rspeedy
|
||
│ ├── async/[name]/{debug-metadata.json,tasm.json,[name].css}
|
||
│ ├── [name]/
|
||
│ │ ├── background.js # 后台线程脚本
|
||
│ │ ├── main-thread.js # 主线程脚本
|
||
│ │ ├── debug-metadata.json
|
||
│ │ ├── [name].css
|
||
│ │ └── tasm.json
|
||
│ └── rspeedy.config.js
|
||
├── [name].lynx.bundle
|
||
└── static/{image,js}/...
|
||
```
|
||
来源:https://lynxjs.org/zh/rspeedy/output.md
|
||
|
||
### 4.3 完整可跑的流程
|
||
|
||
```bash
|
||
# 0) Node 版本检查(必须 ^20.19.0 || >=22.12.0)
|
||
node -v
|
||
|
||
# 1) 脚手架
|
||
npm create rspeedy@latest my-lynx-app
|
||
cd my-lynx-app
|
||
npm install
|
||
|
||
# 2) 改 lynx.config.ts,加 environments: { lynx: {}, web: {} }
|
||
|
||
# 3) 新建 public/index.html(内容见 §3.4,用 CDN 版 client.prod.js)
|
||
|
||
# 4) 起 dev server
|
||
npm run dev
|
||
# → 终端打印 http://localhost:3000
|
||
|
||
# 5) 浏览器打开
|
||
# http://localhost:3000/index.html
|
||
# (<lynx-view url="/main.web.bundle"> 会去拉 http://localhost:3000/main.web.bundle)
|
||
|
||
# 6) 生产构建 + 本地预览
|
||
npm run build # 产出 dist/main.lynx.bundle 和 dist/main.web.bundle
|
||
npx rspeedy preview # 预览产物
|
||
```
|
||
|
||
**关于「必须自己写 index.html」的替代品**:官方提供了一个现成的在线预览器 —— [Lynx Explorer Web 版](https://www.unpkg.com/@lynx-js/web-explorer@latest/index.html),把你产物可访问的 URL 填进去就能看生产模式下的效果,不用自己写 HTML。适合快速验证产物是否正确。
|
||
来源:https://lynxjs.org/zh/guide/start/integrate-with-existing-apps.md
|
||
|
||
---
|
||
|
||
## 5. 样式系统约束
|
||
|
||
### 5.1 display 与布局:不是「只支持 flex」
|
||
|
||
Lynx 里 `display` 支持 **5 个值**:`linear`、`flex`、`grid`、`relative`、`none`。
|
||
|
||
| 值 | 对应 Web | 说明 |
|
||
| --- | --- | --- |
|
||
| `linear` | 无直接对应(近似更简化的 Flex) | **Lynx 的默认布局**。主轴默认竖向,用 `linear-direction`、`linear-weight` 控制 |
|
||
| `flex` | CSS Flexbox | 绝大部分属性一致;**当前不支持 `min-content`**,子项收缩下限视作 `0px` |
|
||
| `grid` | CSS Grid | 支持常见行列定义与 gap;暂不支持 `grid-area`、line names |
|
||
| `relative` | Android RelativeLayout 心智 | **仅移动端有效**;用 `relative-*` 描述与兄弟/父节点的相对定位 |
|
||
| `none` | — | 不参与布局 |
|
||
|
||
所以准确说法是:**默认(`linear`)也受 `align-items` / `justify-content` 等对齐属性控制,但不要假设 Web flex 的默认值**。要 flex 行为就显式写 `display: 'flex'`。
|
||
|
||
- 所有元件默认 `box-sizing: border-box`,**且不具备 margin collapsing(外边距折叠)**。
|
||
⚠️ 与 Web 不一致:`width`/`height`/`max-width` 等描述的是**含边框大小**(包含 border 与 padding)。
|
||
- **不存在 `inline` / `block` 切换**,`display` 不接受这两个值。文本布局依赖 `<text>` 元件。
|
||
- 逻辑方向属性(`inset-inline-start` 等)**依赖 `direction` 且需要开启 CSS 继承功能**才可用。
|
||
- `<view>` 元件内**不能直接插入文字**。
|
||
|
||
来源:https://lynxjs.org/zh/guide/ui/layout/index.md 、https://lynxjs.org/zh/guide/ui/layout/linear-layout.md 、https://lynxjs.org/zh/llms.txt
|
||
|
||
### 5.2 长度单位
|
||
|
||
| 单位 | 语义 |
|
||
| --- | --- |
|
||
| `px` | 像素,等于「一物理像素 × 像素密度」,相当于 iOS 的 `pt` / Android 的 `dp`。高分辨率屏上一个 CSS px 往往占多个设备像素。约 96/英寸 |
|
||
| `ppx` | 物理像素(physical pixel),等于屏幕一实际像素的大小 |
|
||
| `rpx` | responsive pixel,按屏幕宽度自适应。**规定屏幕宽为 750rpx**。iPhone6 上屏宽 375px、共 750 个物理像素 ⇒ **750rpx = 375px = 750 物理像素,即 1rpx = 0.5px = 1 物理像素** |
|
||
| `%` | 相对于父元件 |
|
||
| `vw` / `vh` | 视口初始包含块宽度 / 高度的 1% |
|
||
| `rem` | 根元件的 `font-size` 大小;用在根元件 `font-size` 上时代表其初始值 |
|
||
| `em` | 元件的 `font-size` 计算值;用在 `font-size` 属性本身时表示继承来的 `font-size` |
|
||
|
||
**`0` 以外的数字必须带单位**,不支持无单位写法。
|
||
|
||
来源:https://lynxjs.org/zh/api/css/data-type/length.md
|
||
|
||
### 5.3 选择器支持范围
|
||
|
||
**支持:**
|
||
- 类型选择器 `view {}`、类选择器 `.a {}`、多类 `.a.b {}`、ID `#id {}`、通配 `* {}`
|
||
- 组合器:后代(空格)、子代 `>`、后续兄弟 `~`、紧邻兄弟 `+`、选择器列表 `,`
|
||
- 伪类:**仅 4 个** —— `:not()`、`:root`、`:active`、`:hover`
|
||
- 伪元素:**仅 `::selection`**
|
||
|
||
**关键差异 / 不支持:**
|
||
- 根节点选择器是 **`page`,不是 `body`**。(`:root` 也可用来匹配根节点)
|
||
- `:hover` 平台差异大:**Web 支持**(用浏览器原生 `:hover` 实现);Clay macOS/Windows 自 3.7 起支持;经典 Android/iOS/HarmonyOS 和 Clay iOS **不支持**。
|
||
- `::before` / `::after` **不在支持列表里**。
|
||
- `:first-child` / `:nth-child` 等**未在支持列表中**。
|
||
- `!important` **默认不支持**;从 `engineVersion 3.9` 起可在插件选项里把 `engineVersion` 设为 `'3.9'` 或更高来启用。
|
||
- 属性选择器 `[data-x]` **文档未列出**,不要依赖。
|
||
|
||
来源:https://lynxjs.org/zh/api/css/selectors.md
|
||
|
||
### 5.4 CSS 继承(默认不继承!)
|
||
|
||
- **常规(非自定义)CSS 属性默认不继承**,需要配置 `enableCSSInheritance` 开启。
|
||
- 开启后**默认只有这些属性可继承**:`direction`、`color`、`font-family`、`font-size`、`font-style`、`font-weight`、`letter-spacing`、`line-height`、`text-align`、`text-decoration`、`text-shadow`。
|
||
- 想加更多用 `customCSSInheritanceList`,例如 `"customCSSInheritanceList": ["font-size", "flex-direction"]`。**注意:一旦声明了这个列表,就只有列表里的属性可继承**。
|
||
- **CSS 变量(`--*`)默认可继承,无需配置**(遵循 Web 标准)。
|
||
- 继承的三个局限:
|
||
1. `position: fixed` 的元件**永远只继承 page 的属性**;
|
||
2. **不支持关键字 `inherit`**;
|
||
3. 除默认可继承属性外,只有类型为**枚举或布尔值**的 CSS 属性支持自定义继承。
|
||
|
||
来源:https://lynxjs.org/zh/guide/styling/custom-theming.md#按需使用-css-继承
|
||
|
||
在 `lynx.config.ts` 里开启:
|
||
|
||
```ts
|
||
import { pluginReactLynx } from '@lynx-js/react-rsbuild-plugin';
|
||
import { defineConfig } from '@lynx-js/rspeedy';
|
||
|
||
export default defineConfig({
|
||
plugins: [
|
||
pluginReactLynx({
|
||
enableCSSInheritance: true,
|
||
customCSSInheritanceList: ['font-size', 'flex-direction'],
|
||
}),
|
||
],
|
||
});
|
||
```
|
||
|
||
### 5.5 CSS 变量 `--*`
|
||
|
||
**完整支持**,Web 端也标 ✅ Yes。
|
||
|
||
- 声明:选择器里 `:root { --main-bg-color: yellow; }`,或内联 `style={{ '--main-bg-color': 'yellow' }}`。
|
||
- 引用:`background-color: var(--main-bg-color)`;支持嵌套引用 `--b: var(--a)`。
|
||
- 默认值:`width: var(--view-width, 100px)`。
|
||
- 与 `calc()` 配合:`height: calc(var(--main-height) - 100px)`(变量声明里不能直接做数学运算)。
|
||
- JS 改值:`lynx.getElementById('root').setProperty({ '--color': 'white', '--bg': 'black' })`(参数必须是 Object 或 String)。
|
||
- ⚠️ **坑**:用 JS `setProperty` 改过变量之后,**再切 class 就不会生效了**(官方示例里专门做了个错误提示:"Cannot change variables through the class after modifying them in JS!")。
|
||
|
||
来源:https://lynxjs.org/zh/api/css/properties/css-variable.md
|
||
|
||
主题切换两种官方写法(推荐 CSS 变量那种,更灵活):
|
||
|
||
```tsx
|
||
// 方式一:后代选择器切 class
|
||
<view className={`theme-${lynx.__globalProps.appTheme}`}>
|
||
<text className="content">Hello Theme</text>
|
||
</view>
|
||
|
||
// 方式二:CSS 变量 + 切顶层 class
|
||
// .theme-light { --color: black; --bg: white; }
|
||
// .theme-dark { --color: white; --bg: black; }
|
||
// .content { color: var(--color); background-color: var(--bg); }
|
||
<view id="root" className={`theme-${lynx.__globalProps.appTheme}`}>
|
||
<text className="content">Hello Variable</text>
|
||
</view>
|
||
```
|
||
|
||
### 5.6 `overflow` / `position`
|
||
|
||
- **不支持 `overflow: scroll` 做滚动**。`<view>` 没有滑动效果,只有 `<scroll-view>` 和 `<list>` 等滑动容器有。
|
||
- `overflow` 可以用于**裁切**溢出内容(如 `overflow: hidden`)。
|
||
- `position` 支持 **3 种**:`relative`、`absolute`、`fixed`。**`fixed` 的节点会被直接提升为根节点的直接子节点**。
|
||
|
||
来源:https://lynxjs.org/zh/guide/ui/scrolling.md 、https://lynxjs.org/zh/llms.txt
|
||
|
||
### 5.7 文本:必须包在 `<text>` 里
|
||
|
||
```tsx
|
||
// ❌ 不会生效
|
||
<view>hello world</view>
|
||
|
||
// ✅
|
||
<text>hello world</text>
|
||
```
|
||
|
||
- 行内混排靠**嵌套 `<text>`**:
|
||
```tsx
|
||
<text>
|
||
This is an
|
||
<text style={{ color: 'red', fontWeight: 'bold' }}>important word</text>
|
||
</text>
|
||
```
|
||
- `<text>` 里**还可以嵌套 `<view>`** 和 `<image>` 做图文混排(`<image>` 在行内用 `vertical-align` 调垂直位置;`<image>` 底部默认与文本基线对齐)。
|
||
- 文本截断:`text-maxline` + `text-overflow: ellipsis`;自定义截断处内容用 `<inline-truncation>`。
|
||
- 文本装饰:`text-shadow`、`text-stroke`(`textStroke` 类型尚未进 `@lynx-js/types`,示例里用了 `@ts-expect-error`)、`text-indent`、`word-break`、`text-align`、`line-height`。
|
||
- 字体:支持 `@font-face` 和 `lynx.addFont`,但**宿主需实现字体资源加载器**(原生侧)。Web 端由浏览器处理。
|
||
|
||
来源:https://lynxjs.org/zh/guide/styling/text-and-typography.md
|
||
|
||
### 5.8 `class` vs 内联 `style`
|
||
|
||
- **JSX 里用 `className`**(`<view className="a" />`);纯模板语法里写 `class`。两者都能让类选择器命中。
|
||
- **内联 `style` 支持对象**:`style={{ color: 'red', '--x': '1px' }}`;纯模板语法里写字符串:`style="width:200px;height:100px"`。
|
||
- 优先级遵循 CSS 层叠:**内联 `style` 覆盖样式规则**,权重高的选择器覆盖权重低的,文档中后出现的规则覆盖先出现的。
|
||
- 单个类名下也支持嵌套语法(Sass / postcss-nesting)。
|
||
|
||
来源:https://lynxjs.org/zh/guide/ui/styling.md 、https://lynxjs.org/zh/guide/ui/elements-components.md
|
||
|
||
### 5.9 `<image>` 用法
|
||
|
||
- **空元件**,不支持子节点。
|
||
- 要显示图片,`src` 非空**且**满足以下之一:宽高都 > 0 **或** `auto-size`。
|
||
- `mode` 取值(默认 `scaleToFill`):`scaleToFill` | `aspectFit` | `aspectFill` | `center`。
|
||
- 支持格式:`png`、`jpg`、`jpeg`、`bmp`、`gif`、`webp`。**不支持 svg**(要 SVG 得自己写自定义元件;或用 `<svg content={...} />` 元件)。
|
||
- 本地图片:`import logo from './assets/logo.png'` → 返回**表示静态资源的 URL 字符串**,直接传给 `src`。小于阈值(默认 **2KiB**)会自动内联为 base64。
|
||
- `?inline` 强制内联;`?url` 强制不内联。
|
||
- 类型声明来自 `src/lynx-rspeedy-env.d.ts` 里的 `/// <reference types="@lynx-js/rspeedy/client" />`。
|
||
- URL 前缀:dev 用 `dev.assetPrefix`,生产用 `output.assetPrefix`。
|
||
- 其他属性:`placeholder`、`blur-radius`、`prefetch-width`/`prefetch-height`、`cap-insets`、`tint-color`、`image-config`(Android)。
|
||
- 事件:`bindload`(`e.detail` 有 `width`/`height`)、`binderror`。
|
||
|
||
来源:https://lynxjs.org/zh/api/elements/built-in/image.md 、https://lynxjs.org/zh/rspeedy/assets.md
|
||
|
||
### 5.10 `<scroll-view>` 用法
|
||
|
||
⚠️ **注意**:官方 quick-start 的旧摘要里说「通过指定 `scroll-y` 或 `scroll-x` 属性之一来启用滚动」,但**当前 `<scroll-view>` API 文档里没有 `scroll-x`/`scroll-y` 这两个属性**,用的是 **`scroll-orientation`**。以 API 文档为准:
|
||
|
||
```tsx
|
||
<scroll-view scroll-orientation="vertical" style={{ width: '100%', height: '100%' }}>
|
||
{/* 直接子节点只支持 linear 与 sticky */}
|
||
</scroll-view>
|
||
```
|
||
|
||
- `scroll-orientation`:`vertical`(默认)| `horizontal`。**它同时决定滚动方向和子节点布局方向**。
|
||
- `<scroll-view>` **固定使用 linear 布局**。
|
||
- **必须给它一个确定的高度(或宽度)** —— 它本身不靠内容撑开。
|
||
- ⚠️ **直接子节点只支持 `linear` 与 `sticky`**。需要复杂布局时,**给它一个唯一的子 `<view>`,在里面随便写**:
|
||
```tsx
|
||
<scroll-view scroll-orientation="vertical">
|
||
<view> {/* 随便写 */}
|
||
</view>
|
||
</scroll-view>
|
||
```
|
||
- 其他属性:`enable-scroll`(默认 `true`)、`initial-scroll-offset`、`initial-scroll-to-index`(两者互相覆盖,不能一起用)、`bounces`(默认 `true`)、`upper-threshold`、`lower-threshold`、`scroll-bar-enable`(默认 `false`)、`sticky`(只能给直接子节点;Android 上还需 `flatten={false}`)。
|
||
- 事件:`bindscroll`、`bindscrolltoupper`、`bindscrolltolower`、`bindscrollend`、`bindcontentsizechanged`。
|
||
- 方法(走 `SelectorQuery` + `.invoke()` + `.exec()`):`scrollTo`、`autoScroll`、`scrollIntoView`、`scrollBy`。
|
||
- **性能**:`<scroll-view>` 会**一次性创建所有子节点**,且**没有任何复用机制**。数据超过 3 屏时优先用 `<list>`。
|
||
|
||
来源:https://lynxjs.org/zh/api/elements/built-in/scroll-view.md
|
||
|
||
### 5.11 不支持的 CSS 特性速查(避坑清单)
|
||
|
||
| 特性 | 状态 |
|
||
| --- | --- |
|
||
| `overflow: scroll` / `auto` 做滚动 | ❌ 用 `<scroll-view>` / `<list>` |
|
||
| `display: inline` / `block` | ❌ 不支持这两个取值 |
|
||
| margin collapsing(外边距折叠) | ❌ 不存在 |
|
||
| `box-sizing` 默认值 | ⚠️ 固定 `border-box`,且 width 含 border+padding |
|
||
| `min-content` | ❌ flex 下不支持 |
|
||
| `!important` | ⚠️ 默认不支持,需 `engineVersion >= '3.9'` |
|
||
| `inherit` 关键字 | ❌ 不支持 |
|
||
| `::before` / `::after` | ❌ 不在支持列表(仅 `::selection`) |
|
||
| `:first-child` / `:nth-child` | ❌ 未列出 |
|
||
| `:hover` | ⚠️ Web ✅、移动端经典渲染器 ❌ |
|
||
| `@font-face` | ✅ 但宿主需实现字体加载器 |
|
||
| `@media` | 文档未提及支持 |
|
||
| CSS 变量、`calc()`、`transform`、`linear-gradient`、`radial-gradient`、`border-radius`、`text-shadow` | ✅ 支持(官方示例中大量使用) |
|
||
| `display: grid` 的 `grid-area` / line names | ❌ 暂不支持 |
|
||
| 普通属性的 CSS 继承 | ⚠️ 默认关闭,需 `enableCSSInheritance` |
|
||
|
||
来源:https://lynxjs.org/zh/guide/ui/layout/index.md 、https://lynxjs.org/zh/api/css/selectors.md 、https://lynxjs.org/zh/guide/ui/scrolling.md 、https://lynxjs.org/zh/llms.txt 、https://lynxjs.org/zh/api/css/properties/css-variable.md
|
||
|
||
---
|
||
|
||
## 6. ReactLynx 与 React 的差异
|
||
|
||
### 6.1 基线:React 17 API + Preact 实现
|
||
|
||
- ReactLynx 是 React 的一个实现,**本质上基于 Preact**;可以用 `react` 换 `@lynx-js/react` 的方式迁移。
|
||
- **以 React 17 API 作为基础**。官方明确说明了原因:React 18 起的「并发 React 功能」(`useTransition`、`useDeferredValue`)对执行环境的调度方式和并发模型要求苛刻(依赖/覆写浏览器 Web API 等),Lynx 选 React 17 作基石。
|
||
|
||
注意 `@lynx-js/react` 的 `dependencies` 里写的是 `"preact": "npm:@lynx-js/internal-preact@11.0.0-rc.1-..."` —— Preact 被 alias 成了 Lynx 内部 fork。
|
||
|
||
来源:https://lynxjs.org/zh/api/react/index.md 、https://lynxjs.org/zh/react/introduction.md 、https://registry.npmjs.org/@lynx-js%2Freact/latest
|
||
|
||
### 6.2 支持的 hooks / API 清单(来自官方 API 索引)
|
||
|
||
**Hooks(全部可用):**
|
||
`useCallback`、`useContext`、`useDebugValue`、`useEffect`、`useImperativeHandle`、`useMemo`、`useReducer`、`useRef`、`useState`、`useSyncExternalStore`
|
||
|
||
**Lynx 特有:**
|
||
`useMainThreadRef`、`useGlobalProps`、`useGlobalPropsChanged`、`useInitData`、`useInitDataChanged`、`useLynxGlobalEventListener`
|
||
|
||
**其他 API:**
|
||
`cloneElement`、`createContext`、`createElement`、`createPortal`、`createRef`、`forwardRef`、`Fragment`、`isValidElement`、`lazy`、`memo`、`Suspense`、`Component`、`PureComponent`、`runOnBackground`、`runOnMainThread`、`withInitDataInState`、`root`、`useErrorBoundary`
|
||
组件级:`GlobalPropsProvider`/`GlobalPropsConsumer`、`InitDataProvider`/`InitDataConsumer`
|
||
|
||
**不支持:** `useLayoutEffect`(见下)、`useId`、`useTransition`、`useDeferredValue`、`useOptimistic`、`useActionState`(并发时代的 API 一律没有)。
|
||
来源:https://lynxjs.org/zh/api/react/index.md
|
||
|
||
### 6.3 `useLayoutEffect` —— 别名,不是真禁用
|
||
|
||
文档里有两处说法,以 API 参考为准:
|
||
|
||
- API 参考:**`useLayoutEffect` is now an alias of `useEffect`. Use `useEffect` instead.**
|
||
- 生命周期指南:所有生命周期钩子都在**后台线程异步执行**,不具备同步阻塞渲染的特性,因此 ReactLynx **不支持 `useLayoutEffect`**。
|
||
- 替代方案:用元件的 **`main-thread:bindlayoutchange`** 事件获取布局结果并设置属性;或 `main-thread:ref` + `MainThread.Element.invoke('boundingClientRect')`。
|
||
|
||
```tsx
|
||
const handleLayoutChange = (e: any) => {
|
||
"main thread";
|
||
e.currentTarget.setStyleProperty("transform", `translate3d(${x}px, ${y}px, 0)`);
|
||
};
|
||
// <view main-thread:bindlayoutchange={handleLayoutChange} />
|
||
```
|
||
来源:https://lynxjs.org/zh/react/lifecycle.md 、https://lynxjs.org/zh/api/react/index.md 、https://lynxjs.org/zh/llms.txt
|
||
|
||
### 6.4 `useEffect` 可用,但在后台线程
|
||
|
||
- **可用**,在**后台线程**执行,效果在「主线程 DOM 更新之后」运行且不阻塞它。
|
||
- 在 `useEffect`、`useImperativeHandle`、ref 回调、事件处理函数里调用 `NativeModules` / `lynx.getJSModule` 等原生 API 是**安全的**(因为都在后台线程)。
|
||
- 在**组件渲染逻辑/顶层**调用原生 API 会被标为**关键**错误 —— 那跑在主线程上,会阻塞 UI。
|
||
|
||
```tsx
|
||
// ❌ 组件顶层调用原生 API
|
||
function App() {
|
||
const data = NativeModules.Storage.get('key');
|
||
return <text>{data}</text>;
|
||
}
|
||
|
||
// ✅ 放 useEffect
|
||
useEffect(() => {
|
||
setData(NativeModules.Storage.get('key'));
|
||
}, []);
|
||
|
||
// ✅ 或用 'background only' 标记
|
||
function fetchData() {
|
||
'background only';
|
||
return NativeModules.Storage.get('key');
|
||
}
|
||
```
|
||
来源:https://lynxjs.org/zh/react/best-practices.md
|
||
|
||
### 6.5 事件绑定:`bindtap`,不是 `onClick`
|
||
|
||
**没有 `onClick`。** 命名规则:`bind*` / `catch*` / `capture-bind*` / `global-bind*`,主线程版加 `main-thread:` 前缀。
|
||
|
||
| 写法 | 行为 |
|
||
| --- | --- |
|
||
| `bindtap` | 冒泡到父节点 |
|
||
| `catchtap` | **拦截**,不再继续传播 |
|
||
| `capture-bindtap` | 捕获阶段 |
|
||
| `global-bindtap` | 跨元件监听(任意节点被点时都能收到);可用 `global-target` 限定只监听某些 `id` |
|
||
| `main-thread:bindtap` | 在主线程处理 |
|
||
|
||
```diff
|
||
- <button onTouchStart={...} />
|
||
+ <view bindtap={...} catchtap={...} />
|
||
```
|
||
|
||
**常用事件名**:`tap`、`longpress`、`touchstart`/`touchmove`/`touchend`、`input`、`change`、`scroll`/`scrolltoupper`/`scrolltolower`/`scrollend`、`load`/`error`(image)、`layoutchange`。
|
||
|
||
**事件对象**:
|
||
- **后台线程**的事件对象是**纯 JSON**,无法从事件对象拿到节点引用(要节点用 `SelectorQuery` + `selectUniqueID(e.currentTarget.uid)`)。
|
||
- **主线程**的事件对象是可操作的 Event Object,`e.currentTarget` 是 `MainThread.Element`,可直接 `setStyleProperty`。
|
||
- `e.stopPropagation()` / `e.preventDefault()` **只在主线程脚本中支持**。
|
||
- 通过 `data-*` 属性传数据,用 `event.currentTarget.dataset` 取(优先 `currentTarget` 而非 `target`;推荐用 dataset 而非闭包)。
|
||
来源:https://lynxjs.org/zh/guide/interaction/event-handling.md 、https://lynxjs.org/zh/guide/interaction/event-handling/event-propagation.md 、https://lynxjs.org/zh/react/best-practices.md 、https://lynxjs.org/zh/react/introduction.md
|
||
|
||
### 6.6 `'background only'` 与 `'main thread'` 指示符
|
||
|
||
- **双线程 React**:你的代码会在两个线程里跑。**事件、生命周期、`useEffect` 等副作用只从后台线程执行**。
|
||
- `'background only'`:给「只在后台线程运行」的函数加,必须是**函数体第一条语句**。单双引号都行。
|
||
- 框架能静态分析出部分直接情形;但引入了抽象和间接之后(**跨组件传事件回调、自定义 Hooks**),**必须显式加**。
|
||
- 模块级用 `import "background-only"` 表达「本模块预期只在后台线程运行」。
|
||
- `'main thread'`:主线程函数,函数体首行加该指令;只能通过 `main-thread:` 前缀的事件绑定 / `main-thread:ref` 使用。
|
||
限制:**捕获的外部变量是快照,必须可 JSON 序列化,因此无法直接调用函数**;**不能修改捕获的变量**;`MainThreadRef.current` 只能在主线程访问。
|
||
- 跨线程通信:`runOnMainThread` / `runOnBackground`(Promise 风格,参数需可 JSON 序列化)。
|
||
|
||
**仅在后台线程提供的 API**:`lynx.getJSModule`、`NativeModules`、`lynx.createSelectorQuery`、`lynx.getElementById`、`lynx.reload`、`lynx.performance.addTimingListener` 等、`fetch`、`setTimeout`/`clearTimeout`、`setInterval`/`clearInterval`、`requestAnimationFrame`/`cancelAnimationFrame`。
|
||
|
||
来源:https://lynxjs.org/zh/llms.txt(第 3、第 18 节)、https://lynxjs.org/zh/react/best-practices.md
|
||
|
||
### 6.7 状态管理建议
|
||
|
||
官方文档在「生态集成 → 状态管理」下给了三个方案:**Jotai、Valtio、Zustand**(以及路由 React Router、TanStack Router)。
|
||
- 更推荐优先用 `useState`/`useReducer`/Context;需要跨组件全局态再引第三方。
|
||
- 这些库的 store 读写要在**后台线程**上下文里做(事件处理函数、`useEffect` 内);如果封装成工具函数被自定义 Hook 间接调用,记得加 `'background only'`。
|
||
- 跨组件/跨宿主的事件广播用 `GlobalEventEmitter`(`lynx.getJSModule('GlobalEventEmitter')`,**仅后台线程**,有 `addListener`/`removeListener`/`removeAllListeners`/`toggle`/`trigger`),或 ReactLynx 的 `useLynxGlobalEventListener`。
|
||
- 全局状态更新**不要**用 `window.dispatchEvent`(没有 `window`),用上面的方案。
|
||
|
||
来源:https://lynxjs.org/zh/llms.txt(第 17 节速查表 + 生态链接)、https://lynxjs.org/zh/guide/interaction/event-handling.md
|
||
|
||
### 6.8 其他组件/能力差异
|
||
|
||
- **没有 `document` 和 `window`**(Web 端渲染时也是跑在 worker/沙箱里,你的业务代码拿不到)。任何依赖 `document`/`window` 的库都不能用。
|
||
- 替代:`lynx.reload` 代替 `window.location.reload()`;其余用 NativeModules / 自定义元件扩展。
|
||
- **保留字标签**:`view`、`text`、`image`、`scroll-view`、`list`/`list-item`、`page`、`frame`、`input`、`textarea`、`svg`、`inline-truncation` 等。
|
||
- `<page>` 是页面根节点(类比不可改尺寸的 `<body>`),大多数情况下无需手动添加 —— 但模板示例里加了。
|
||
- `createPortal` 可用(ReactLynx Portal,3.9 引入)。
|
||
- `lazy` / `Suspense` / `memo` / `forwardRef` / `Component` / `PureComponent` / ErrorBoundary(`useErrorBoundary`)都支持。
|
||
- React Compiler 支持(可自动提升静态 JSX)。
|
||
- `<list>` 子组件特殊:所有子组件的 JS 对象实例会一并创建,但真实 UI 节点要滚到位置才创建/复用。`useEffect` 和 `ref` 即使组件没显示也会触发;要精确反映 UI 状态用 `main-thread:ref`。
|
||
|
||
来源:https://lynxjs.org/zh/react/introduction.md 、https://lynxjs.org/zh/react/lifecycle.md 、https://lynxjs.org/zh/api/react/index.md
|
||
|
||
---
|
||
|
||
## 7. 常见坑清单
|
||
|
||
| # | 坑 | 症状 | 解决 |
|
||
| --- | --- | --- | --- |
|
||
| 1 | 用了 `npm create lynx@latest` | 装到一个 225 字节的无关包 | 用 `npm create rspeedy@latest` 或 `npm create @lynx-js/lynx@latest` |
|
||
| 2 | 忘了配 `environments.web` | `dist/` 里只有 `main.lynx.bundle`,`<lynx-view>` 404 | `lynx.config.ts` 加 `environments: { lynx: {}, web: {} }` |
|
||
| 3 | 给 `<lynx-view>` 传了 `.lynx.bundle` | 加载失败/解析错误 | 必须传 `.web.bundle` |
|
||
| 4 | 以为 Rspeedy 会生成 HTML | dev server 打开 `http://localhost:3000/` 是 404 或文件列表 | 自己写 `index.html`(放 `public/`),访问 `/index.html` |
|
||
| 5 | `<lynx-view>` 没设宽高 | 页面空白(CSS Containment 导致) | 必须给显式宽高;确实要自适应用 `width="auto"`/`height="auto"` |
|
||
| 6 | 照抄旧博客的 `thread-strategy` | 属性无效(0.20.0 起被移除) | 删掉;不要写 `thread-strategy` / `customTemplateLoader` / `inject-head-links` / `overrideLynxTagToHTMLTagMap` |
|
||
| 7 | `tsconfig` 少写 `jsxImportSource` | 类型全错,`<view>` 不认识 | `"jsx": "react-jsx"` + `"jsxImportSource": "@lynx-js/react"` |
|
||
| 8 | 从 `react` 导入 hooks | 构建能过但运行行为异常/类型错 | 一律从 `@lynx-js/react` 导入;类型从 `@lynx-js/types` 导入 |
|
||
| 9 | 用 `createRoot` | 报错 | 用 `import { root } from '@lynx-js/react'; root.render(<App />)` |
|
||
| 10 | 在 `<view>` 里直接写字符串 | 文字不显示 | 包进 `<text>` |
|
||
| 11 | 用 `onClick` / `onTouchStart` | 事件不触发 | 用 `bindtap` / `bindtouchstart` |
|
||
| 12 | 用 `overflow: scroll` 想滚动 | 不滚动 | 用 `<scroll-view scroll-orientation="vertical">` |
|
||
| 13 | `<scroll-view>` 里塞复杂布局 | 布局错乱 | 直接子节点只支持 `linear`/`sticky`;塞一个唯一子 `<view>` 再随便写 |
|
||
| 14 | 用了 `::before` / `::after` | 不生效 | 用真实子节点 |
|
||
| 15 | 用了 `:first-child` / `:nth-child` | 不生效 | 用 class 手动区分 |
|
||
| 16 | 假设普通 CSS 属性会继承 | 颜色/字号不继承 | 默认只有 11 个属性可继承;开 `enableCSSInheritance` 或显式设置 |
|
||
| 17 | 用了 `!important` | 不生效 | 默认不支持;需 `engineVersion: '3.9'`+ |
|
||
| 18 | 顶层组件里调 `NativeModules` / `lynx.getJSModule` | 阻塞 UI,官方标为**关键**错误 | 移到 `useEffect`/事件处理函数,或加 `'background only'` |
|
||
| 19 | 跨组件传事件回调 / 自定义 Hook 里调原生 API | 构建/运行报错或缺指令 | 函数体首行加 `'background only'` |
|
||
| 20 | 主线程函数里想调外部函数 | 不行 | 捕获变量是快照且需 JSON 可序列化;用 `runOnBackground` 回传 |
|
||
| 21 | `MainThreadRef.current` 在后台线程读 | 拿不到 | `MainThreadRef` 只能在主线程访问 |
|
||
| 22 | 用 JS `setProperty` 改了 CSS 变量后又切 class | class 切换失效 | 二选一,别混用 |
|
||
| 23 | `@media` / 响应式断点 | 文档未支持 | 用 `lynx.__globalProps` + `onWindowResize` 事件在 JS 侧处理 |
|
||
| 24 | `<image>` 没设宽高也没 `auto-size` | 图片不显示 | 给宽高之一(都 >0)或加 `auto-size` |
|
||
| 25 | 用 svg 文件给 `<image src>` | 不显示 | 不支持 svg;用 `<svg content={...} />` 或 `<svg src={url} />` |
|
||
| 26 | 大量数据用 `<scroll-view>` | OOM / 首屏极慢 | 超过 3 屏用 `<list>` |
|
||
| 27 | Node 版本过低 | Rspeedy 直接起不来 | `^20.19.0 || >=22.12.0` |
|
||
| 28 | `@lynx-js/react` 与插件版本不匹配 | peer dependency 警告/构建异常 | react `^0.126.0` + rspeedy `^0.18.0` + 插件 `^0.20.3` |
|
||
| 29 | `*.png` / `*.module.css` 导入报 TS2307 | 类型缺失 | 确保有 `/// <reference types="@lynx-js/rspeedy/client" />` 的 d.ts |
|
||
| 30 | `verbatimModuleSyntax` 下类型用普通 import 导出 | SWC 单独编译报错 | `export type { X } from './x.js'` 而不是 `export { X }` |
|
||
| 31 | Web 端跨域加载 bundle / Worker | `Uncaught SecurityError: Failed to construct 'Worker'...` | 同源部署;或按官方 FAQ 引入 `remote-web-worker`(必须在 `@lynx-js/web-core` **之前**引入) |
|
||
| 32 | 想用 Tailwind v4 | 不兼容 | **必须 Tailwind CSS v3** + `@lynx-js/tailwind-preset`(官方标注仍不稳定) |
|
||
| 33 | CSS Modules 命名 | 不生效 | 文件名必须是 `*.module.{css,scss,less,sass}` |
|
||
|
||
来源:本手册各节引用的官方页面。
|
||
|
||
---
|
||
|
||
## 8. 一键复制的完整最小示例
|
||
|
||
### `lynx.config.ts`
|
||
|
||
```ts
|
||
import { pluginReactLynx } from '@lynx-js/react-rsbuild-plugin';
|
||
import { defineConfig } from '@lynx-js/rspeedy';
|
||
|
||
export default defineConfig({
|
||
source: {
|
||
entry: {
|
||
main: './src/index.tsx',
|
||
},
|
||
},
|
||
environments: {
|
||
lynx: {},
|
||
web: {},
|
||
},
|
||
plugins: [pluginReactLynx()],
|
||
});
|
||
```
|
||
|
||
### `src/index.tsx`
|
||
|
||
```tsx
|
||
import { root } from '@lynx-js/react';
|
||
|
||
import { App } from './App.js';
|
||
|
||
root.render(<App />);
|
||
|
||
if (import.meta.webpackHot) {
|
||
import.meta.webpackHot.accept();
|
||
}
|
||
```
|
||
|
||
### `src/App.tsx`
|
||
|
||
```tsx
|
||
import { useCallback, useEffect, useState } from '@lynx-js/react';
|
||
|
||
import './App.css';
|
||
|
||
export function App() {
|
||
const [count, setCount] = useState(0);
|
||
|
||
useEffect(() => {
|
||
console.info('Hello, ReactLynx on Web');
|
||
}, []);
|
||
|
||
const onTap = useCallback(() => {
|
||
'background only';
|
||
setCount((c) => c + 1);
|
||
}, []);
|
||
|
||
return (
|
||
<page>
|
||
<view className="Header">
|
||
<text className="Title">Lynx on Web</text>
|
||
<text className="Subtitle">ReactLynx + Rspeedy</text>
|
||
</view>
|
||
|
||
<scroll-view scroll-orientation="vertical" className="Body">
|
||
<view className="BodyInner">
|
||
{Array.from({ length: 20 }).map((_, i) => (
|
||
<view className="Card" key={i}>
|
||
<text className="CardTitle">卡片 {i + 1}</text>
|
||
<text className="CardText">
|
||
文字必须包在 text 元件里;滚动必须用 scroll-view。
|
||
</text>
|
||
</view>
|
||
))}
|
||
</view>
|
||
</scroll-view>
|
||
|
||
<view className="Footer" bindtap={onTap}>
|
||
<text className="FooterText">点击了 {count} 次</text>
|
||
</view>
|
||
</page>
|
||
);
|
||
}
|
||
```
|
||
|
||
### `src/App.css`
|
||
|
||
```css
|
||
page {
|
||
display: flex;
|
||
flex-direction: column;
|
||
height: 100%;
|
||
background-color: #f5f6f8;
|
||
}
|
||
|
||
.Header {
|
||
padding: 16px;
|
||
background-color: #ffffff;
|
||
}
|
||
|
||
.Title {
|
||
font-size: 22px;
|
||
font-weight: bold;
|
||
color: #1a1a1a;
|
||
}
|
||
|
||
.Subtitle {
|
||
margin-top: 4px;
|
||
font-size: 14px;
|
||
color: #8a8f99;
|
||
}
|
||
|
||
.Body {
|
||
flex: 1;
|
||
height: 0; /* scroll-view 必须有确定高度 */
|
||
}
|
||
|
||
.BodyInner {
|
||
padding: 12px;
|
||
}
|
||
|
||
.Card {
|
||
margin-bottom: 12px;
|
||
padding: 16px;
|
||
border-radius: 12px;
|
||
background-color: #ffffff;
|
||
}
|
||
|
||
.CardTitle {
|
||
font-size: 16px;
|
||
font-weight: bold;
|
||
color: #1a1a1a;
|
||
}
|
||
|
||
.CardText {
|
||
margin-top: 6px;
|
||
font-size: 14px;
|
||
line-height: 20px;
|
||
color: #5a6068;
|
||
}
|
||
|
||
.Footer {
|
||
padding: 16px;
|
||
background-color: #1677ff;
|
||
}
|
||
|
||
.FooterText {
|
||
font-size: 16px;
|
||
color: #ffffff;
|
||
text-align: center;
|
||
}
|
||
```
|
||
|
||
### `public/index.html`
|
||
|
||
```html
|
||
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>Lynx on Web</title>
|
||
<link
|
||
rel="stylesheet"
|
||
href="https://unpkg.com/@lynx-js/web-core@0.26.2/client.prod.css"
|
||
/>
|
||
<script
|
||
type="module"
|
||
src="https://unpkg.com/@lynx-js/web-core@0.26.2/client.prod.js"
|
||
></script>
|
||
<style>
|
||
html,
|
||
body {
|
||
margin: 0;
|
||
height: 100%;
|
||
background: #e9ebee;
|
||
}
|
||
lynx-view {
|
||
display: block;
|
||
width: 100%;
|
||
max-width: 420px;
|
||
height: 100vh;
|
||
margin: 0 auto;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<lynx-view url="/main.web.bundle"></lynx-view>
|
||
</body>
|
||
</html>
|
||
```
|
||
|
||
### 运行
|
||
|
||
```bash
|
||
npm create rspeedy@latest my-lynx-app # React + TypeScript + Rspeedy
|
||
cd my-lynx-app
|
||
npm install
|
||
# 覆盖上面 4 个文件
|
||
npm run dev
|
||
# 浏览器打开 http://localhost:3000/index.html
|
||
```
|
||
|
||
---
|
||
|
||
## 9. 来源汇总
|
||
|
||
| 主题 | URL |
|
||
| --- | --- |
|
||
| 快速开始 | https://lynxjs.org/zh/guide/start/quick-start.html · https://lynxjs.org/zh/guide/start/quick-start.md |
|
||
| 集成到现有应用(含 Web 平台一节) | https://lynxjs.org/zh/guide/start/integrate-with-existing-apps.html |
|
||
| `<lynx-view>` API(含 0.20.0 迁移说明、宽高、FAQ) | https://lynxjs.org/zh/api/lynx-native-api/lynx-view/lynx-view.md |
|
||
| Rspeedy API 索引 | https://lynxjs.org/api/rspeedy/index.html · https://lynxjs.org/api/rspeedy/rspeedy.md |
|
||
| `environments` | https://lynxjs.org/api/rspeedy/rspeedy.config.environments.md |
|
||
| `output.filename.bundle`(`[platform]` 占位符) | https://lynxjs.org/api/rspeedy/rspeedy.filename.bundle.md |
|
||
| `server.port`(默认 3000) | https://lynxjs.org/api/rspeedy/rspeedy.server.port.md |
|
||
| `dev.writeToDisk` | https://lynxjs.org/api/rspeedy/rspeedy.dev.writetodisk.md |
|
||
| Rspeedy CLI | https://lynxjs.org/zh/rspeedy/cli.md |
|
||
| 构建输出文件 | https://lynxjs.org/zh/rspeedy/output.md |
|
||
| TypeScript / tsconfig | https://lynxjs.org/zh/rspeedy/typescript.md |
|
||
| 静态资源 | https://lynxjs.org/zh/rspeedy/assets.md |
|
||
| 样式(CSS Modules/Sass/PostCSS/Tailwind) | https://lynxjs.org/zh/rspeedy/styling.md |
|
||
| 开发前必读(总纲,含大量差异清单) | https://lynxjs.org/zh/llms.txt |
|
||
| 布局总览 | https://lynxjs.org/zh/guide/ui/layout/index.md |
|
||
| 线性布局 | https://lynxjs.org/zh/guide/ui/layout/linear-layout.md |
|
||
| 应用样式 / 层叠 | https://lynxjs.org/zh/guide/ui/styling.md |
|
||
| 选择器 API | https://lynxjs.org/zh/api/css/selectors.md |
|
||
| 长度单位 | https://lynxjs.org/zh/api/css/data-type/length.md |
|
||
| CSS 变量 | https://lynxjs.org/zh/api/css/properties/css-variable.md |
|
||
| 多主题 / CSS 继承 | https://lynxjs.org/zh/guide/styling/custom-theming.md |
|
||
| 文字排版 | https://lynxjs.org/zh/guide/styling/text-and-typography.md |
|
||
| 组合元件 | https://lynxjs.org/zh/guide/ui/elements-components.md |
|
||
| 管理滚动 | https://lynxjs.org/zh/guide/ui/scrolling.md |
|
||
| `<scroll-view>` API | https://lynxjs.org/zh/api/elements/built-in/scroll-view.md |
|
||
| `<image>` API | https://lynxjs.org/zh/api/elements/built-in/image.md |
|
||
| 事件处理 | https://lynxjs.org/zh/guide/interaction/event-handling.md |
|
||
| 事件传播 | https://lynxjs.org/zh/guide/interaction/event-handling/event-propagation.md |
|
||
| 直接操作节点 | https://lynxjs.org/zh/guide/interaction/event-handling/manipulating-element.react.md |
|
||
| ReactLynx 介绍 | https://lynxjs.org/zh/react/introduction.md |
|
||
| 渲染流程与生命周期 | https://lynxjs.org/zh/react/lifecycle.md |
|
||
| 编码建议('background only' 等) | https://lynxjs.org/zh/react/best-practices.md |
|
||
| `@lynx-js/react` API 索引(含 React 17 基线说明) | https://lynxjs.org/zh/api/react/index.md |
|
||
| lynx-stack 仓库 | https://github.com/lynx-family/lynx-stack |
|
||
| Web 平台 readme(产物差异原因) | https://github.com/lynx-family/lynx-stack/blob/main/packages/web-platform/readme.md |
|
||
| 脚手架模板(rspeedy-react-ts) | https://github.com/lynx-family/lynx-stack/tree/main/packages/rspeedy/create-lynx/template-rspeedy-react-ts |
|
||
| Web Explorer 的 index.html 参考 | https://github.com/lynx-family/lynx-stack/blob/main/packages/web-platform/web-explorer/index.html |
|
||
| npm registry 版本查询 | https://registry.npmjs.org/@lynx-js%2Freact/latest · https://registry.npmjs.org/@lynx-js%2Frspeedy/latest · https://registry.npmjs.org/@lynx-js%2Fweb-core/latest · https://registry.npmjs.org/@lynx-js%2Fweb-elements/latest · https://registry.npmjs.org/@lynx-js%2Flynx-ui/latest · https://registry.npmjs.org/@lynx-js%2Ftypes/latest · https://registry.npmjs.org/@lynx-js%2Fcreate-lynx/latest · https://registry.npmjs.org/create-rspeedy/latest |
|
||
| `@lynx-js/web-core` npm 页 | https://www.npmjs.com/package/@lynx-js/web-core |
|
||
| Lynx Explorer Web 版(在线预览产物) | https://www.unpkg.com/@lynx-js/web-explorer@latest/index.html |
|