@financial-times/x-engine

中文文档
21.2.3发布于 2026/10/26ISCCommonJS近 30 天下载 1.3万

This module is a consolidation library to render x-dash components with any compatible runtime.

原始使用文档
文档目录10 节
  1. Installation
  2. Configuration
  3. Rendering
  4. Server-side
  5. Client-side
  6. FAQ
  7. This sounds complicated… is it a magic black box?
  8. What is a "factory function"?
  9. Which runtime should I use?
  10. What about Hyperscript?

x-engine

This module is a consolidation library to render x-dash components with any compatible runtime.

Installation

This module is supported on Node 12 and is distributed on npm.

npm install -S @financial-times/x-engine

You'll also need to install your chosen runtime and any related dependencies. Some compatible runtimes are:

* Usage of Hyperapp depends on a small modification to higher-order components to accept children as a second argument rather than receiving them appended to props.

Configuration

To start you must specify your runtime configuration within package.json. This instructs x-engine which modules to load for your environment. You may specify different runtimes for server and browser rendering depending on your needs.

This module includes several presets for popular tools and frameworks including React, Preact, and Hyperons. For example to use Hyperons on the server and Preact in the browser you may use the name of their preset:

{
    "x-dash": {
        "engine": {
            "server": "hyperons",
            "browser": "preact"
        }
    }
}

But if your chosen tool does not have a preset then the configuration for it can be provided with the expanded configuration format. The example below shows how to load the VDO module and use its createElement factory function*:

{
    "x-dash": {
        "engine": {
            "server": {
                "runtime": "vdo",
                "factory": "createElement"
            }
        }
    }
}

* A JSX factory function is a variadic function† with the signature fn(element, properties, ...children), examples include React.createElement and Preact.h. See the FAQ section for more information.

† Variadic means that the function accepts a variable number of arguments. The ... before the last arguments name is a rest parameter, meaning it will collect "the rest" of the arguments.

Rendering

Server-side

If your chosen runtime factory returns a string then you can pass properties to the component and immediately use the returned value:

const { Teaser } = require('@financial-times/x-teaser');

app.get('/teaser', (request, response) => {
    const properties = { … };
    response.send(Teaser(properties));
});

But if your factory method returns a framework-specific intermediary code then you'll need to load the specified render method to render the node into a string or stream:

const { render } = require('@financial-times/x-engine');
const { Teaser } = require('@financial-times/x-teaser');

app.get('/teaser', (request, response) => {
    const properties = { … };
    const nodes = Teaser(properties);
    response.send(render(nodes));
});

Client-side

To use components on the client-side you will first need to add the x-engine plugin to your Webpack configuration file. Under the hood this uses DefinePlugin to wire up your chosen runtime.

// webpack.config.js
const xEngine = require('@financial-times/x-engine/src/webpack');

module.exports = {
    plugins: [
        xEngine()
    ]
};

You can then install and use x- components in your client-side code:

import { h } from '@financial-times/x-engine';
import { Teaser } from '@financial-times/x-teaser';

export default const TeaserList = (props) => (
    <ul class="TeaserList">
        {props.items.map((item) => (
            <li className="TeaserList-Item">
                <Teaser {...item} layout="small" showImage={true} />
            </li>
        ))}
    </ul>
);

Client-side through n-ui

In order to configure n-ui to compile x-dash based .jsx files you will also need to add the following to n-ui-build.config.js.

// n-ui-build.config.js
const xEngine = require('@financial-times/x-engine/src/webpack');

module.exports = {
    plugins: [
        xEngine()
    ],
    pragma: 'h'
};

You will also need to import the factory function from x-engine in each .jsx file.

import { h } from '@financial-times/x-engine'

FAQ

This sounds complicated… is it a magic black box?

There is no magic. The source code for the server-side integration is less than 60 lines of unexciting code. The Webpack plugin for client-side usage is even smaller.

What is a "factory function"?

A factory function is a variadic function with the signature fn(element, properties, ...children), examples include React.createElement and Preact.h. It may return the framework's representation of a HTML node or a formatted string depending on the runtime you're using.

Which runtime should I use?

Whichever one you want! React, Preact, Rax, and Nerv are all largely compatible with one another. If you don't want the overhead of a framework, or are rendering static HTML, then it's worth investigating the Hyperons or VDO modules.

What about Hyperscript?

Hyperscript currently only supports passing a tag name (a string) as the first argument. This limitation means you cannot currently reference components inside other components.

版本与发布记录

@financial-times/x-engine 共发布 208 个版本,首次发布于 2018/04/12;过去 12 个月发布了 17 个版本。

版本发布日期距今
21.2.3latest2026/10/2619 天前
21.2.22026/09/012 个月前
21.2.12026/08/282 个月前
21.2.02026/08/272 个月前
21.1.02026/08/212 个月前
21.0.02026/08/133 个月前

其他发布通道:next → 1.0.0-1,pre-origami → 1.0.0-beta.24。

关于 @financial-times/x-engine 的常见问题

@financial-times/x-engine 是什么?

This module is a consolidation library to render x-dash components with any compatible runtime.

如何安装 @financial-times/x-engine?

在项目目录运行 npm install @financial-times/x-engine,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:webpack(>=4.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@financial-times/x-engine 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/financial-times__x-engine 是否存在。

@financial-times/x-engine 支持 ESM 还是 CommonJS?

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

@financial-times/x-engine 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js 16.x || 18.x || 20.x。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@financial-times/x-engine 可以商用吗?

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

@financial-times/x-engine 的下载量有多少?

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

@financial-times/x-engine 还在维护吗?

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