JavaScript API

Rsbuild provides a complete set of JavaScript APIs for developers to build higher level solutions on top of Rsbuild.

Access Example

Here is a basic example of how to access the Rsbuild JavaScript API.

1. Install Rsbuild

You need to install the @rsbuild/core package:

npm
yarn
pnpm
bun
npm add @rsbuild/core -D

2. Create an Rsbuild Instance

You can call the createRsbuild method to create an Rsbuild instance:

import { createRsbuild } from '@rsbuild/core';

const rsbuild = await createRsbuild();

The createRsbuild method provides some options, which you can learn more about in the API - createRsbuild.

3. Call Rsbuild Instance Method

The Rsbuild instance provides some methods, which you can use it according to the usage scenarios.

To start local development, it is recommended to use the rsbuild.startDevServer method, which will start a local Dev Server.

await rsbuild.startDevServer();

After successfully starting Dev Server, you can see the following logs:

> Local: http://localhost:3000 > Network: http://192.168.0.1:3000

To deploy the App to production environment, it is recommended to use the rsbuild.build method, which will build the production outputs.

await rsbuild.build();

For more introduction of Rsbuild instance methods, please read the Rsbuild Instance chapter.

After completing the above three steps, you have learned the basic usage of Rsbuild. Next, you can customize the build process through the Rsbuild plugin and Rsbuild configs.

Exports Format

Rsbuild provides exports in both ES Modules and CommonJS formats:

index.mjs
import { createRsbuild } from '@rsbuild/core';
index.cjs
const { createRsbuild } = require('@rsbuild/core');

It is recommended to use ES modules format, which is more in line with the community standards.