# 前端框架

acdev 不要求你改框架的配置. 构建时它按依赖认出框架, 用**框架自己的构建**, 再把产物装进包里. 先认出的先用:

| 依赖 | 构建命令 | 产物 |
| --- | --- | --- |
| `next` | `next build` (standalone) | `.next/standalone` 作为后端; `output: 'export'` 时是静态的 `out/` |
| `nuxt` (或 `nuxt3`) | `nuxt build` (`node-server` preset) | `.output/server/index.mjs` 作为后端, `.output/public` |
| `@sveltejs/kit` | `vite build` | 服务端包成后端, `client` 是静态资源 |
| `astro` | `astro build` | `dist/`; 有 `dist/server/entry.mjs` 时是后端 |
| `@react-router/dev` / `@remix-run/dev` | `react-router build` / `remix vite:build` | 有 `build/server/index.js` 时是后端 |
| `@angular/core` + `@angular/cli` | `ng build --configuration production --base-href /` | `dist/**/browser`; 有 `server/server.mjs` 时是后端 |
| `vite` | 在进程内调工程的 Vite `build()`, 读它的 `vite.config` | 静态页面 |
| `react-scripts` | `react-scripts build` | `build/` |
| `package.json` 里有 `build` 脚本 | acdev 跑在 Bun 上时是 `bun --bun run build`, 否则是 `<pm> run build` | `dist` / `build` / `out` / `www` / `.output/public` 里第一个有 HTML 的 |

有服务端渲染的框架, 页面由后端生成, 清单的 `entry` 会是 `/app/<id>/` (以 `/` 结尾).

没有 `node_modules` 时 acdev 先跑一次 `<pm> install`.

要用这些框架之一开始一个工程, `acdev new --template` 有 `next`、`nuxt`、`sveltekit` 和 `astro`, 以及 Vite 的模板 `react`、`vue`、`svelte`、`solid`、`preact` 和 `vanilla`; 见 [第一个应用](https://docs.dopame.ai/zh/start/first-app.md#新建).

## 在 acdev dev 里保存

`acdev dev` 在每次保存后跑同样的生产构建, 并**重新载入整个页面**, 除非这次构建只改了运行时能换进正在跑的页面里的样式表和脚本 (见 [重新载入](https://docs.dopame.ai/zh/guide/dev.md#重新载入)); 它不用框架的开发服务器, 也不用模块热替换 (HMR). 重新载入前后, acdev 会放回表单的值、滚动位置、焦点和选中的文字, 但**组件状态会丢掉**: React 或 Preact 里 `useState` 的计数器、Vue 的 `ref`、Svelte 和 Solid 的 signal, 以及其他只放在内存里的值, 都从头开始. 想在重新载入后还留着的状态, 放进 `localStorage` 或 IndexedDB. 见 [重新载入](https://docs.dopame.ai/zh/guide/dev.md#重新载入).

`acdev new --template react` (以及其他 Vite 模板) 生成的页面上写的是 "save to reload the page", Vite 自己的模板在那里写的是 "save to test HMR".

## 框架识别

`acde.config.jsonc` 的 `framework`:

- `auto` (默认): 按上表识别.
- `next`、`nuxt`、`sveltekit`、`astro`、`react-router`、`remix`、`angular`、`vite`、`cra`、`script`: 指定用哪个.
- `none`: 不用框架, acdev 自己打包 `entry`.

写了 `entry` 时 acdev 不认 Vite (自带模板带 Vite 依赖只是给 acdev 自己打包用的). 想用 Vite 构建, 去掉 `entry` 或写 `"framework": "vite"`.

## 不用框架

没有认出框架 (或 `"framework": "none"`) 时, acdev 从 `entry` 打包:

- HTML 入口: `base` 是 `/app/<id>/`, `acde:*` 保持为外部导入, `NODE_ENV` 是 `"production"`, `import.meta.env` 和 Vite 一样 (包括 `VITE_*` 变量).
- 模块入口: 打成 `<名字>.js`, 保留导出; 默认导出一个 `WindowApp`.

Bun 上用 `Bun.build`, Node.js 上用工程里的 Vite (没有时提示 `add it with <pm> add -D vite`). 规则两边一样.

## Vite 插件

acdev 用 Vite 构建工程时会加上自己的插件: `acde:*` 保持为外部导入. 这个插件随 acdev 提供, 不在 SDK 里, 所以工程自己的 `vite.config` 导入不了它; 单独的 `vite dev` (在普通浏览器里) 不认识 `acde:` 模块 (见下文).

## acde: 模块

如果同一份代码既要在浏览器里跑也要在 acdev 里跑, 动态导入模块; 模块拿不到的地方导入会 reject:

```js
const fs = await import('acde:fs').then((m) => m.default, () => null)
if (fs) console.log(await fs.roots())
```
