Skip to content

@celljs/vite ​

Not translated yet

This page is only written in Chinese so far; the original is shown below.

Cell 4 的构建核心:把一个 Cell 项目(组件图、DI 模块、cell.yml 配置)交给 Vite 构建成 .cell/output。一般通过 @celljs/cli 的 cell build 使用,也可以单独使用。

  • ProjectLoader:只读 package.json 与 cell*.yml,生成可 JSON 序列化的项目模型(组件、mode、各目标的 DI 模块、入口、合并后的配置),不执行项目代码。
  • CellPlugin / cell():Vite 插件,提供 client(前端,输出 public/)和 server(后端,输出单个 server/index.mjs)两个环境;DI 装配代码、合并后的配置和项目模型同时写到 .cell/generated/。
  • CellBuilder:一次构建全部目标,失败时抛 CellError(@celljs/kit)。
ts
import { CellBuilder } from '@celljs/vite';

const project = await new CellBuilder({
    root: process.cwd(),
    mode: ['production'],
    entryName: 'http',          // 后端入口名,从组件的 cell.entries 中选
    format: 'esm',
    outDir: '.cell/output',
}).build();

构建规则:

  • 后端依赖全部内联;组件声明的 cell.optionalDependencies 解析不到时 external;原生模块和 external 按依赖关系拷到 server/node_modules。产物可以拷到项目外单独运行。
  • 后端配置里的 ${...} 在运行时求值,前端配置和 index.html 在构建时求值。
  • CELL_BACKEND__* 环境变量由服务端在启动时覆盖配置,CELL_FRONTEND__* 在构建时覆盖前端配置,都早于 ${...} 求值(@celljs/core 的 EnvConfigUtil);结果记在 TargetModel.envOverrides。
  • 后端配置带 paths.public(默认 public,相对输出目录),服务端启动时解析成绝对路径。
  • 用户源码由 Oxc 转译,开启 legacy 装饰器和 emitDecoratorMetadata。
  • 前端的 Node 内置模块 polyfill 只在项目安装了对应 polyfill 包时启用。

需要 Node.js ^20.19.0 || >=22.12.0。