> For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt.

# From Source to Bundle

Lynx builds use [Rsbuild](https://rsbuild.rs/) with [`pluginLynx`]. Use Rsbuild's TypeScript, CSS, static asset and plugin capabilities, while `pluginLynx` compiles code for both threads and encodes the bundles your host loads.

## Get started with Rsbuild \{#getting-started}

Create a project with the ReactLynx Rsbuild template:

```bash
npm create @lynx-js/lynx@latest -- --template rsbuild-react-ts
```

In the generated project directory, install dependencies and start the dev server:

```bash
npm install
npm run dev
```

Scan the QR code with [Lynx Explorer](/next/guide/start/quick-start.md) to run the app on a device. The dev server updates the app when you save your code.

The template's build configuration lives in `rsbuild.config.ts`. [`pluginReactLynx`](/next/api/packages/react-rsbuild-plugin/functions/pluginReactLynx.md) applies `pluginLynx` automatically, and [`pluginQRCode`](/next/api/packages/qrcode-rsbuild-plugin.md) provides the QR code:

```ts title="rsbuild.config.ts"
import { pluginQRCode } from '@lynx-js/qrcode-rsbuild-plugin';
import { pluginReactLynx } from '@lynx-js/react-rsbuild-plugin';
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
  source: {
    entry: {
      main: './src/index.tsx',
    },
  },
  plugins: [pluginQRCode(), pluginReactLynx()],
});
```

When development is complete, use `rsbuild build` to generate production files and `rsbuild preview` to preview them locally.

### Configure your app

`rsbuild.config.ts` accepts the full [Rsbuild configuration](https://rsbuild.rs/config/). Configure the capabilities your project needs:

- 
  **[TypeScript](/next/guide/build/typescript.md)**: build-time declarations and types for
  application data, elements and native modules.
- 
  **[CSS](/next/guide/build/styling.md)**: CSS Modules, global styles, pre-processors and
  PostCSS. For Tailwind, see the [Lynx preset
  setup](/next/guide/build/styling.md#tailwind-css).
- 
  **[Static assets](https://rsbuild.rs/guide/basic/static-assets)**: images,
  fonts, the `public/` directory, URL prefixes and inlining.
- 
  **[Module resolution](https://rsbuild.rs/config/resolve/alias)**: path aliases
  and dependency resolution. `pluginLynx` prefers dependencies' `lynx` export
  condition; see [`resolve.extensions`](/next/api/build/resolve/extensions.md) for file
  extensions.
- 
  **[Plugins](https://rsbuild.rs/plugins/list/)**: add Rsbuild plugins or extend
  the underlying build through `tools.rspack`.

The [`rsbuild.config.ts` reference](/next/api/build.md) describes Lynx-specific options and changed defaults. For example, to customize the bundle filename, explicitly add `pluginLynx({ output: { filename: { bundle: 'app.lynx.bundle' } } })` to `plugins`; it will not be applied twice.

### Optimize and debug

The build also provides ways to control loading, investigate performance and diagnose problems:

- **[Code splitting](/next/react/code-splitting.md)**: load some code on demand to reduce what the first screen needs.
- 
  **[External Bundle](/next/guide/build/external-bundle.md)**: build common dependencies into a
  bundle shared by several apps.
- **[Build profiling](https://rsbuild.rs/guide/debug/build-profiling)**: use tools such as Rsdoctor to understand build time and dependencies.
- 
  **[Runtime
  profiling](/next/guide/performance/analysis-performance/analysis-framework.md) and
  [bundle size
  analysis](/next/guide/performance/analysis-performance/analysis-bundle-size.md)**:
  investigate runtime and loading costs.
- **[Production error mapping](/next/guide/devtool/map-errors-to-source.md) and [UI source maps](/next/guide/devtool/ui-source-map.md)**: use build-generated debugging information to connect errors and rendered nodes to source code.

:::info Still using Rspeedy?
Rspeedy is still maintained and uses the same build engine as `pluginLynx`. Existing projects can keep using it; Rsbuild is recommended for new projects. See the [Rspeedy migration guide](/next/guide/build/rspeedy.md#migrating-an-rspeedy-project) for the differences and migration steps.
:::

## Understand the output \{#bundles}

A [_**Lynx Bundle**_][Lynx Bundle] is the deliverable
for a Lynx app: a binary file ending in `.lynx.bundle`. The host passes its URL
or contents to the [_**Lynx Engine**_][Lynx Engine], which loads it and renders
the page. A Lynx Bundle has much in common with a web app:



| Part    | Web app                                                                      | Lynx Bundle                                                                                                                                    |
| ------- | ---------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| Scripts | JavaScript files loaded at runtime                                           | Encoded into the bundle by default; **main-thread** code creates the **first screen**, while **background-thread** code runs application logic |
| Styles  | CSS files loaded at runtime                                                  | CSS parsed into structured data and encoded into the bundle                                                                                    |
| Assets  | Separate images, fonts and other files, with small assets optionally inlined | Also supports separate files and inlining small assets                                                                                         |
| Entry   | The browser loads HTML, then the scripts, styles and assets it references    | The host loads the bundle directly, including page configuration and other metadata                                                            |

### Output files \{#output-files}

#### Standard Lynx Bundle output

Build output goes into `dist/` by default:

```txt
dist/
├── [name].lynx.bundle  # application bundle
├── lazy-bundle/       # bundles loaded on demand
├── static/            # static assets and split scripts
└── .lynx/[name]/      # intermediates for debugging
```

Scripts are encoded into the bundle by default; code splitting can also produce files in `static/js/`. Set the bundle filename with [`output.filename.bundle`](/next/api/build/output/filename/bundle.md), other filenames with [`output.filename`](https://rsbuild.rs/config/output/filename), and output directories with [`output.distPath`](/next/api/build/output/dist-path.md). See the APIs for [source maps](/next/api/build/output/source-map.md) and [license comments](/next/api/build/output/legal-comments.md) for those options.

#### Web output

The same source can also target the web: the `web` environment produces a `.web.bundle`, loaded by `<lynx-view url="...">`. The `[platform]` placeholder in the default filename is the environment name, so the two environments produce `main.lynx.bundle` and `main.web.bundle` respectively.

#### From intermediate files to binary

The build first produces the **main-thread** script `main-thread.js`, the **background-thread** script `background.js` and styles. The `tasm.json` file organizes these contents and page configuration for [`@lynx-js/tasm`](https://www.npmjs.com/package/@lynx-js/tasm) to encode into the `.lynx.bundle` binary.

These intermediate files live in `.lynx/[name]/` and are kept in development builds by default; use `DEBUG=lynx` to retain them in production builds. Inspect `tasm.json` to see what the bundle encodes; `debug-metadata.json` supports [Production Error Mapping](/next/guide/devtool/map-errors-to-source.md).

### An app can have multiple bundles

Besides the main bundle, an app can load other bundles on demand or share them:

- **Lazy bundles**: load components through dynamic `import()`, with output in `lazy-bundle/`, to reduce what the **first screen** needs. See [Code Splitting](/next/react/code-splitting.md).
- **External Bundle**: build common dependencies into a separate bundle shared by several apps. See [External Bundle](/next/guide/build/external-bundle.md).

## Compatibility \{#compatibility}

This article explains how to ensure version compatibility between [Bundle] and [Lynx Engine], and how to handle challenges that come with Lynx Engine version evolution.

### Build from Source

A simple way to ensure compatibility is to **always build the Bundle with the Host application from source**.

- Bundle can fully utilize all features of the current Lynx Engine version
- Compatibility can be fully verified during development, what you see is what you get

### Multi-version Compatibility

However, in a complex project, the relationship between Bundle and Lynx Engine is not one-to-one:

- A Bundle might run in applications with different Lynx Engine versions

- An application can run multiple Bundles maintained by different teams

In this case, you need to set the [`engineVersion`] to ensure compatibility.

:::info Engine Version
When the Bundle's [`engineVersion`] is greater than the Lynx Engine version, it will not be able to run.
:::

For example: a Bundle with [`engineVersion`] 3.3 can run on Lynx Engine 3.3 and later versions, but cannot run on 3.2.

**Version Compatibility Diagram** 

graph TB
  subgraph Bundle["🎯 Bundle"]
      B10["Engine Version 3.3"]
  end
  subgraph Engine["⚙️ Lynx Engine"]
      E12["Version 3.5"]
      E11["Version 3.4"]
      E10["Version 3.3"]
      E09["Version 3.2"]
  end
  B10 -.->|❌ Not Supported| E09
  B10 ==>|✅ Can Run| E10
  B10 ==>|✅ Can Run| E11
  B10 ==>|✅ Can Run| E12
  %% Node styles
  style E12 fill:#2b5a83,stroke:#4a90e2,stroke-width:3px,rx:10px
  style E11 fill:#2b5a83,stroke:#4a90e2,stroke-width:3px,rx:10px
  style E10 fill:#2b5a83,stroke:#4a90e2,stroke-width:3px,rx:10px
  style E09 fill:#2b5a83,stroke:#4a90e2,stroke-width:2px,rx:10px,stroke-dasharray:5,5
  style B10 fill:#2d5a1e,stroke:#7ed321,stroke-width:3px,rx:10px
  %% Subgraph styles
  style Bundle fill:transparent,stroke:#7ed321,stroke-width:2px,rx:10px
  style Engine fill:transparent,stroke:#4a90e2,stroke-width:2px,rx:10px
  %% Default styles
  classDef default fill:#23272f,color:#ccc,font-family:system-ui
  %% Connection line label styles
  linkStyle default stroke-width:2px

When a Bundle needs to run on multiple Lynx Engine versions, its [`engineVersion`] must not exceed the oldest Lynx Engine version it targets.

When running multiple Bundles on a single Lynx Engine version, each Bundle can set different [`engineVersion`]s, but none can be greater than the Lynx Engine version.

#### Version Incompatibility Handling

If you try to run a Bundle on a lower version of the Lynx Engine, a Fatal level error with code [10204](/next/api/errors/error-code.md#10204-e_app_bundle_load_parse_failed) will occur, and the rendering process will stop. This mechanism prevents potential runtime issues caused by version incompatibility.

#### Configuring Engine Version

The [`engineVersion`] is a string containing two version numbers, and you can specify it in your configuration file:

```ts title="rsbuild.config.ts"
import { defineConfig } from '@rsbuild/core';
import { pluginReactLynx } from '@lynx-js/react-rsbuild-plugin';

export default defineConfig({
  plugins: [
    pluginReactLynx({
      engineVersion: '3.2',
    }),
  ],
});
```

### Upgrading Lynx Engine

As Lynx Engine evolves, new features are added and some features may be deprecated. Their compatibility is also managed by [`engineVersion`].

#### New Features

Some new features **_will not_** affect existing Bundles, but if [`engineVersion`] is lower than the version where they were introduced, runtime detection is needed, for example:

```js
// Detect if `lynx.newlyAddedMethod` can be called
if (lynx.newlyAddedMethod) {
  lynx.newlyAddedMethod();
}
```

While some new features cannot run on lower versions of the Lynx Engine at all, so they will only be enabled when [`engineVersion`] is greater than or equal to the corresponding version.

#### Deprecated Features

Some features may be deprecated during iteration, and their behavior may change when a higher [`engineVersion`] is set.

- If only the Lynx Engine version is upgraded without upgrading the Bundle's [`engineVersion`], there will be no compatibility issues
- If both Lynx Engine and Bundle's [`engineVersion`] are upgraded, you need to read the CHANGELOG to ensure there are no unexpected behavior changes


### Feature and API compatibility \{#feature-compatibility}

Check each feature, element, API or CSS property's reference for platform support. The example below shows cross-platform compatibility for CSS `gap`:

APISummary

**Compatibility Table**

**Query:** `css.properties.gap`

[Source](https://github.com/lynx-family/lynx/blob/develop/tools/css_generator/css_defines/205-gap.json) · [API Reference](/next/api/css/properties/gap) · [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/gap)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| gap | Yes (2.14) | Yes (3.4) | Yes (2.14) | Yes (2.14) | Yes (2.14) | Yes |
| Flex | Yes (2.14) | Yes (3.4) | Yes (2.14) | Yes (2.14) | Yes (2.14) | Yes |
| Grid | Yes (2.14) | Yes (3.4) | Yes (2.14) | Yes (2.14) | Yes (2.14) | Yes |

**`gap`**

**Specifications:**

**`Flex`**

Supported in Flexible Box Layout

[Source](https://github.com/lynx-family/lynx/blob/develop/tools/css_generator/css_defines/205-gap.json)

**`Grid`**

Supported in Grid Layout

[Source](https://github.com/lynx-family/lynx/blob/develop/tools/css_generator/css_defines/205-gap.json)


[Bundle]: /next/guide/spec.md#lynx-bundle-or-bundle

[Lynx Bundle]: /next/guide/spec.md#lynx-bundle-or-bundle

[Lynx Engine]: /next/guide/spec.md#engine

[`pluginLynx`]: /next/api/packages/rsbuild-plugin.md

[`engineVersion`]: /next/api/packages/react-rsbuild-plugin/interfaces/PluginReactLynxOptions.md#engineversion
