@farming-labs/farmjs

中文文档
0.2.146发布于 2026/11/14MITTypeScriptES Module近 30 天下载 4万

Farm.js adapter for @farming-labs/docs

原始使用文档
文档目录9 节
  1. Install
  2. Configure Farm
  3. Repeated production builds
  4. Prerendered pages
  5. Guarded pages
  6. Edge deployments
  7. Server wrapper
  8. Entrypoints
  9. License

@farming-labs/farmjs

The official Farm.js runtime adapter for @farming-labs/docs.

Install

npm install @farming-labs/docs @farming-labs/farmjs @farming-labs/theme

Configure Farm

Keep documentation settings in docs.config.ts, then enable the adapter from farm.config.ts:

import { defineConfig } from "@farm.js/core";
import { withDocs } from "@farming-labs/farmjs/config";

export default withDocs(
  defineConfig({
    preset: "vercel",
  }),
);

Farm discovers docs.config.ts from the application root. The wrapper writes an adapter runtime descriptor into Farm's docs configuration. Farm uses that versioned contract to load page rendering, server behavior, and MDX integration from @farming-labs/farmjs without selecting a docs theme itself. A different path can be provided explicitly:

export default withDocs(defineConfig({}), {
  configPath: "config/docs.config.ts",
});

Code highlighting is compiled by the adapter's MDX pipeline. Pick a specific Shiki palette at that boundary without replacing the rest of the adapter:

export default withDocs(defineConfig({}), {
  codeBlockThemes: {
    light: "github-light-default",
    dark: "vesper",
  },
});

Add the application's own MDX plugins in mdx in docs.config.ts. Each plugin is a package name or a path relative to the application root, with optional JSON options:

export default defineDocs({
  entry: "docs",
  mdx: {
    remarkPlugins: ["./lib/remark-docs.mjs"],
    rehypePlugins: [["rehype-katex", { strict: false }]],
    codeHighlighter: "./lib/rehype-docs-code.mjs",
  },
});
  • remarkPlugins and rehypePlugins run after the built-in plugins.
  • codeHighlighter replaces only the syntax highlighter; metadata and the table of contents stay enabled.
  • Node and edge targets compile pages with them.
  • Production builds compile every page while mdx is set, and buildCache: true alongside it throws.
  • withDocs(config, { config: { mdx } }) replaces the file's mdx.

Import the selected theme from the application-owned global stylesheet. This also gives the application a normal place for project-specific overrides:

/* src/app/globals.css */
@import "@farming-labs/theme/pixel-border/css";

:root {
  --color-fd-primary: #8b5cf6;
}

The theme factory in docs.config.ts and the CSS entrypoint must describe the same theme. This is the same unified CSS entrypoint used by the Next.js, TanStack Start, SvelteKit, Astro, and Nuxt integrations. The adapter does not ship or inject a second Farm-specific theme stylesheet.

Repeated production builds

The adapter reuses unchanged Markdown/MDX compilation and syntax highlighting in production builds. It stores the compiled code and source maps under <Vite cacheDir>/farming-docs/mdx-v1.json; Vite normally places cacheDir in node_modules/.vite. Preserve that directory in your CI build cache to reuse work across jobs. Deleting it only causes compilation to run again.

Cache entries are keyed by source, module path, mode, highlighting themes, Node version, and a content fingerprint of the compiler implementation and its dependencies. Vite still discovers and builds imports, components, CSS, routes, and deployment output normally. Development and watch builds do not use this cache. Missing, corrupt, incompatible, or unwritable cache files fall back to compilation; compiler errors still fail the build.

The cache retains at most 1,024 modules and approximately 32 MiB of payload. It does not cache rendered HTML or change client navigation or prerendering. To diagnose a build without MDX cache reuse:

export default withDocs(defineConfig({}), { buildCache: false });

The lower-level docsMdx({ buildCache: false }) option has the same behavior.

Prerendered pages

Set docs.prerender: true in farm.config.ts to have farm build render every docs page into a static file on Node and serverless targets:

export default withDocs(defineConfig({ docs: { prerender: true } }));

Farm asks the adapter's server entry for its page list (listFarmDocsPages). Prerendered pages link their Markdown copy relative to the page, or under the configured sitemap, llmsTxt, or robots baseUrl, so social cards need that base URL to stay absolute. Client navigation reads a static page's data from its HTML. A static file cannot negotiate Accept: text/markdown, so agents use the .md URL the page links to. Markdown, search, llms.txt, and MCP keep rendering on request.

Guarded pages

Guard a docs page as you guard app pages: a middleware.ts in its folder, a config middleware matcher, or an auth integration's protected routes. The adapter declares the guarded-pages capability, so Farm serves such pages with it and passes the app's guards to the runtime. A page a guard covers then stays out of search, llms.txt, llms-full.txt, the sitemap, MCP, Ask AI, and the content feed, which answer without running the guard. Its Markdown answers at its own URL (Accept: text/markdown) and at a .md URL only when the guard covers that URL too. Edge targets serve a snapshot that runs no guard, so Farm stops an edge build that has guarded docs pages.

Edge deployments

Farm edge targets use the adapter's build-time compiler automatically. It precompiles pages, client-navigation data, CSS, Markdown, search, sitemap, robots, and agent-discovery responses so the deployed Worker does not import the Node server runtime.

The snapshot cannot preserve features that need live server callbacks or mutable sessions. Disable the default MCP and production telemetry surfaces in an edge-specific docs configuration:

import { defineDocs } from "@farming-labs/docs";

export default defineDocs({
  entry: "docs",
  mcp: false,
  telemetry: false,
});

An edge build also stops with an actionable error when Ask AI, feedback, analytics, observability, i18n, or a function-backed custom search provider is enabled. Use a Node target when those runtime features are required.

Server wrapper

withDocs() is the normal integration. The lower-level server wrapper is available for custom runtimes, route testing, and framework development:

import docsConfig from "./docs.config";
import { createDocsServer } from "@farming-labs/farmjs/server";

const docs = createDocsServer({
  ...docsConfig,
  rootDir: process.cwd(),
});

const response = await docs.handle(request);

handle() returns null for non-docs requests so it can run in Farm's request pipeline without taking over application routes. The server also exposes load, GET, POST, and MCP for custom routing.

Entrypoints

EntrypointPurpose
@farming-labs/farmjs/configFarm config wrapper
@farming-labs/farmjs/serverPage loader and API, agent, and MCP request handlers
@farming-labs/farmjs/reactReact documentation page renderer
@farming-labs/farmjs/contentContent and navigation utilities
@farming-labs/farmjs/viteMDX compilation plugin
@farming-labs/farmjs/runtimeVersioned Farm runtime contract
@farming-labs/farmjs/edge-compilerBuild-time edge snapshot compiler used by Farm
@farming-labs/farmjs/api-referenceFarm API-reference handler

License

MIT

版本与发布记录

@farming-labs/farmjs 共发布 57 个版本,首次发布于 2026/08/06;过去 12 个月发布了 57 个版本。

版本发布日期距今
0.2.146latest2026/11/14今天
0.2.1452026/11/131 天前
0.2.1442026/11/131 天前
0.2.1432026/11/068 天前
0.2.1422026/11/068 天前
0.2.1412026/11/059 天前

其他发布通道:perf → 0.2.133-perf.1,beta → 0.2.139-beta.1。

关于 @farming-labs/farmjs 的常见问题

@farming-labs/farmjs 是什么?

Farm.js adapter for @farming-labs/docs

如何安装 @farming-labs/farmjs?

在项目目录运行 npm install @farming-labs/farmjs,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.2.0 || ^19.0.0)、react-dom(^18.2.0 || ^19.0.0)、@farm.js/core(^0.1.0-beta.106)、@farming-labs/docs(>=0.2.137)、@farming-labs/theme(<0.2.133 || >=0.2.133-perf.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@farming-labs/farmjs 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@farming-labs/farmjs 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@farming-labs/farmjs 可以商用吗?

@farming-labs/farmjs 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@farming-labs/farmjs 的下载量有多少?

根据 npm 官方下载统计,@farming-labs/farmjs 在 2026/10/15 – 2026/11/13 这 30 天内被下载 40,026 次,日均约 1,334 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@farming-labs/farmjs 还在维护吗?

最新版本 0.2.146 发布于 2026/11/14(今天);至今共发布 57 个版本,其中过去 12 个月发布了 57 个;首次发布于 2026/08/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。