@codepress/codepress-engine

中文文档
0.11.3发布于 2026/11/05SEE LICENSE IN LICENSETypeScriptCommonJS近 30 天下载 3.8万

CodePress engine - Babel and SWC plug-ins

原始使用文档
文档目录18 节
  1. CodePress Engine
  2. Requirements
  3. Installation
  4. Next.js usage
  5. How it works
  6. Bundle size impact
  7. When to use
  8. Babel plugin usage
  9. Project layout
  10. Build & test workflow
  11. Local development
  12. Publishing a built site as a CodePress App
  13. Getting a token
  14. Migrating from commit-code + publish scripts
  15. SWC package exports and WASM selection
  16. Feature comparison
  17. Additional references
  18. Contributing

CodePress Engine

Deprecated, and optional. CodePress visual editing works on cloud previews of a plain dev server with no build plugin: the preview runtime locates elements from their DOM position, text and framework hints, and edits are applied by an agent that commits to your branch. Local-device previews still need the plugin for now. You do not need to install this package for cloud previews. If your app already uses it, nothing changes: the editor still reads the codepress-data-fp attribute and uses it as the first hint for where an element lives. No new features are planned for the plugins.

TypeScript-powered instrumentation for the CodePress visual editor. The package ships a Babel plugin, SWC plugin, development server, and CLI so React or Next.js projects can expose file-level context to the editor.


Requirements

  • Node.js 18.17.0 or newer (Fastify v5 + diagnostics channel support)
  • npm 8+ (or any compatible package manager)
  • Git available on the host machine (repository & branch detection)

Automated tests run under Node 16 and skip Fastify-specific assertions when the runtime lacks the required APIs. For production usage we strongly recommend Node 18+.


Installation

npm install @codepress/codepress-engine

Next.js usage

// next.config.mjs
import { createSWCPlugin } from "@codepress/codepress-engine/swc";
import CodePressWebpackPlugin from "@codepress/codepress-engine/webpack-plugin";

const nextConfig = {
  // SWC plugin for code transformation (existing)
  experimental: {
    swcPlugins: [createSWCPlugin()],
  },

  // Webpack plugin for production module mapping (new)
  webpack: (config, { isServer, dev }) => {
    config.plugins.push(new CodePressWebpackPlugin({ isServer, dev }));
    return config;
  },
};

export default nextConfig;

How it works

The plugin:

  1. Automatically skips server builds (isServer: true) and dev builds (dev: true)
  2. Runs during webpack compilation for production client builds
  3. Builds a mapping: { 33: "react", 92: "react-dom", ... }
  4. Injects window.__CP_MODULE_MAP__ into the main bundle
  5. Enables O(1) module resolution instead of scanning all modules

Bundle size impact

  • Typical app (1000 modules): +3-4KB gzipped
  • Large app (3000 modules): +8-10KB gzipped

When to use

  • Required: Production builds where you need HMR to work
  • Optional: Development (already has named module IDs)
  • Alternative: Set moduleIds: 'named' in webpack config (larger bundle, exposes file structure)

Note: When using ESM (next.config.mjs), prefer the named import above due to CJS interop. For CommonJS configs you can use:

// next.config.cjs
const { createSWCPlugin } = require("@codepress/codepress-engine/swc");

The SWC plugin auto-detects your repository and branch from git and common CI env vars. WASM selection is automatic based on your Next.js / @swc/core version; see the WASM exports below for manual overrides.

Optional options for createSWCPlugin (all are optional; omit to use auto-detection):

OptionTypeDefaultPurpose
repo_namestringauto-detected from git remoteForce repository id in owner/repo form
branch_namestringauto-detected from env/gitForce branch name

Babel plugin usage

// babel.config.mjs
export default {
  plugins: ["@codepress/codepress-engine"],
};

Each JSX element receives a codepress-data-fp attribute whose value encodes the relative path and start/end line numbers. On the first root container (html, body, or div), the plugin also injects repository and branch metadata.

Optional options for the Babel plugin (all are optional; omit to use auto-detection):

OptionTypeDefaultPurpose
repo_namestringauto-detected from git remoteForce repository id in owner/repo form
branch_namestringauto-detected from env/gitForce branch name

Entry points exposed by the package:

ExportDescription
@codepress/codepress-engine/babelCompiled Babel plugin (CommonJS)
@codepress/codepress-engine/swcSWC plugin factory & WASM helpers
@codepress/codepress-engine/webpack-pluginWebpack plugin for production module map
@codepress/codepress-engine/serverFastify development server factory
@codepress/codepress-engine/cliCLI used by the codepress binary

Project layout

PathDetails
src/TypeScript sources for the Babel plugin, CLI, dev server, and utils
dist/Compiled JavaScript + declaration files (npm run build)
babel/Lightweight proxy that re-exports the compiled Babel plugin
swc/Versioned WASM binaries for the Next.js 14, 15.5+ within 15.x, and 16.3 SWC host ABIs, plus the plugin entry
tests/, test/Jest suites covering Babel, SWC, and server helpers

Build & test workflow

# Install dependencies
npm install

# Compile TypeScript into dist/
npm run build

# Run Jest (ts-jest powered)
npm test

The build step must run before publishing or linking locally because Babel and CLI entry points load from dist/. Jest automatically performs type-checking and skips Fastify integration tests when the runtime does not support diagnostics channels.


Local development

Run the local CodePress server alongside your app to visually edit code on disk (no GitHub commits):

# npm
npx codepress && npm start

# pnpm
pnpm dlx codepress && pnpm start

# yarn
yarn dlx codepress && yarn start

Useful commands:

# Show available commands
npx codepress help

# Launch the server explicitly on port 4321
npx codepress server

# Scaffold .env entries required by the server
npx codepress setup

Environment variables (from .env when present):

VariableDefaultPurpose
CODEPRESS_DEV_PORT4321Fastify listen port
CODEPRESS_BACKEND_HOSTlocalhostCodePress backend hostname
CODEPRESS_BACKEND_PORT8007Backend REST port
CODEPRESS_API_TOKENunsetAPI token used for authenticated proxy calls

The server performs git-aware writes and enriches responses with repository metadata. It writes changes to your local filesystem and is not used in production builds.


Publishing a built site as a CodePress App

codepress publish publishes a directory you have already built (it does not run your build) as a CodePress App:

codepress publish ./dist --org <org> --app <app> [--publish] [--message "..."] [--no-wait]
  • --org and --app take a UUID or a slug.
  • --publish also releases the deployed files as the App's public website.
  • --message / -m sets the commit message.
  • --no-wait prints the deployment id after finalize and exits 0 without waiting for the job. If the deployment had already ended without succeeding, for example expired during a long upload, it reports that and exits 1.

The command walks the directory and hashes every file. It then begins a deployment with a manifest of {path, sha256, size}, and uploads only the files the organization does not already hold, eight at a time, straight to storage. Finally it finalizes the deployment and prints each stage and its progress until the deployment finishes.

Exit codes. The command exits 0 when the deployment succeeds. It exits 1 when the deployment fails, is interrupted or expires, printing the error class and message, for example branch_moved: .... It also exits 1 when the API refuses a request, printing the typed reason, for example deployment_in_progress or deployment_unsupported_delivery_mode. The second one is for an organization that reviews generated Apps through pull requests; those organizations keep publishing from an agent session.

Reruns. Running the same directory again is cheap. Files the organization already holds are not uploaded. An identical manifest attaches to the deployment that is still open. Shortly after a success, it returns that deployment as deduped and does no new work. If an upload URL expires, the command re-mints it once and retries that file once.

What gets published. The directory is walked the way an agent's commit_app walks a build output. Every regular file is included, dotfiles too, and index.html must be at the root. A symlink or other special file is refused, so copy the asset in as a regular file. The server enforces the App limits: 512 files, 20 MiB per file and 50 MiB in total.

Status commands:

codepress deployments <app> --org <org> [--limit N]   # recent deployments, newest first
codepress deployment <deployment-id> --org <org> --app <app>
VariableDefaultPurpose
CODEPRESS_TOKENrequiredCredential, sent as Authorization: Bearer
CODEPRESS_API_URLhttps://api.codepress.devAPI origin (with or without /v1)
CODEPRESS_DEPLOY_POLL_INTERVAL_MS2000How often the command polls the deployment while it runs

Getting a token

The deployment API accepts a CodePress login token, the same session token the web app and CodePress Terminal use, or a platform API key (see CI / long-lived keys below). You can get a login token in either of these ways:

  • From CodePress Terminal. Signed-in Terminal keeps it as webAuthToken in ~/.codepress-terminal/settings.json.
  • From an email and password account. Call POST /v1/auth/jwt/login with {"email", "password"} and take access_token from the response.

A login token lasts 14 days. When it is close to expiring, the API returns a refreshed one in the X-New-Token header. The command uses that refreshed token for the rest of the run and prints a reminder, but it cannot update your environment, so replace CODEPRESS_TOKEN before the old one expires.

CI / long-lived keys. The command never inspects the token; it only sends it as a Bearer credential. For CI, use a CodePress platform API key (cpk_..., created under Settings > Integrations > Platform MCP) with both the apps:write scope (begin and finalize) and the apps:read scope (status and list). A 403 names the scope the key is missing. The slug lookups accept only a login token, so with a key the command asks for the organization and App UUIDs: pass UUIDs to --org and --app.

Migrating from commit-code + publish scripts

Scripts that read every file into one POST /v1/app-code/organizations/{org}/apps/{app}/commit-code request and then call POST /v1/websites/{app}/publish hold a request open for minutes on a large site. Replace both calls with:

CODEPRESS_TOKEN=... codepress publish ./dist --org <org> --app <app> --publish

--publish takes over from the separate website publish call, so you no longer read the generation and artifact SHA first. Nothing is sent inline: each file is uploaded once, and an unchanged file is never uploaded again. Any timeout you raised for the old commit request can go, because no request stays open while the server commits. The old routes keep working while you migrate.


SWC package exports and WASM selection

ExportTarget
./swc./dist/swc/index.js
./swc/wasm./swc/codepress_engine.v42.wasm
./swc/wasm-v42./swc/codepress_engine.v42.wasm
./swc/wasm-turbopack./swc/codepress_engine.turbopack.wasm
./swc/wasm-v73./swc/codepress_engine.v73.wasm
./swc/wasm-turbopack-v73./swc/codepress_engine.turbopack-v73.wasm
./swc/wasm-v26./swc/codepress_engine.v26.wasm
./swc/wasm-v0_82_87./swc/codepress_engine.v0_82_87.wasm

The helper automatically selects the correct WASM binary based on the detected Next.js / @swc/core version. Next.js 15.5 and later 15.x releases use the swc_core 42 exports; Next.js 16.3.x uses the swc_core 73 exports, with a dedicated Turbopack artifact in each family. Unsupported Next.js 16 minors fail with an actionable compatibility error instead of loading an ABI-mismatched binary. Override detection with CODEPRESS_SWC_WASM=<package specifier> or CODEPRESS_SWC_ABI_BAND=<v42|turbopack|v73|turbopack-v73|v26|v0_82_87> after verifying the selected artifact against the host compiler.


Feature comparison

CapabilityBabel pluginSWC plugin
Git-aware attributes✅✅
Encoded path securityXOR + base64XOR + base64
Line number tracking✅ start–end range✅ (optional)
PerformanceBaseline20–70× faster
Output mediumString literal attribute[wasmSpecifier, config] array

Additional references

  • INSTALL.md – linking the package locally & publishing guidance
  • MODULE_MAP_INTEGRATION.md – detailed guide for webpack plugin integration and troubleshooting
  • scripts/build-swc.mjs – rebuild the WASM binaries (requires Rust toolchain)
  • tests/ – examples of mocking git, fetch, and file IO when validating the plugin

Contributing

PRs are welcome. Please ensure:

  1. npm run build
  2. npm test

are both green before submitting. Mention in the PR description if Fastify integration tests were skipped due to running on Node < 18.

CodePress Engine is released under the MIT license (see LICENSE).

版本与发布记录

@codepress/codepress-engine 共发布 231 个版本,首次发布于 2025/10/14;过去 12 个月发布了 220 个版本。

版本发布日期距今
0.11.3latest2026/11/055 天前
0.11.22026/11/055 天前
0.11.12026/10/2318 天前
0.11.02026/10/1823 天前
0.10.152026/10/081 个月前
0.10.142026/10/071 个月前

其他发布通道:dev → 0.9.4-dev.tunnel-url.20260516041047。

关于 @codepress/codepress-engine 的常见问题

@codepress/codepress-engine 是什么?

CodePress engine - Babel and SWC plug-ins

如何安装 @codepress/codepress-engine?

在项目目录运行 npm install @codepress/codepress-engine,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 codepress,可以用 npx codepress 直接运行。

@codepress/codepress-engine 支持 TypeScript 吗?

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

@codepress/codepress-engine 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@codepress/codepress-engine 可以商用吗?

@codepress/codepress-engine 使用 SEE LICENSE IN LICENSE 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@codepress/codepress-engine 的下载量有多少?

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

@codepress/codepress-engine 还在维护吗?

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