从一个最小项目开始:创建应用、启动开发服务器、构建静态产物,再理解每个目录负责什么。
推荐使用 Deno 2.7+。LessJS 是 Deno-first 项目,依赖通过 deno.json 管理,开发和构建命令都从 Deno task 进入。
deno run -A jsr:@lessjs/create my-app
cd my-app
生成的项目会包含页面路由、示例 island、Vite 配置和常用 Deno tasks。
deno task dev
开发模式通过 Vite 提供模块加载和刷新,通过生成的 Hono entry 提供 SSR/API 行为。 默认打开 http://localhost:5173。
deno task build
构建命令会依次生成 SSR bundle、client island entry 和 SSG HTML。 最终产物在 dist/,可以部署到任意静态托管平台。
deno task preview
预览命令用于检查最终静态产物,而不是开发服务器行为。部署前至少跑一次。
my-app/
|-- app/
| |-- routes/
| | |-- index.ts # page route for /
| | |-- about.ts # page route for /about
| | └-- api/
| | └-- status.ts # API route
| |-- islands/
| | └-- counter.ts # client-upgraded Custom Element
| └-- _renderer.ts # optional layout wrapper
|-- deno.json # tasks and imports
└-- vite.config.ts # LessJS plugin config
页面是一个 Web Component。SSR 会把它渲染成 Declarative Shadow DOM, 所以内容在 JavaScript 运行前就已经可见。
import { html, LitElement } from 'lit';
export class HomePage extends LitElement {
override render() {
return html`<main>Hello LessJS</main>`;
}
}
customElements.define('page-home', HomePage);
export default HomePage;
export const tagName = 'page-home';
把需要客户端行为的组件放进 app/islands。 页面 HTML 先输出,浏览器加载 island entry 后再升级组件。
<counter-island count="1"></counter-island>
下一步建议先读 Architecture, 再读 Routing、Rendering & SSG 和 Island Upgrade。