Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions e2e/cases/output/manifest-environment/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,66 @@ test('should allow to access manifest data in environment context after dev buil

await rsbuild.close();
});

test('should allow to access manifest data in environment API', async ({
page,
}) => {
let webManifest: Record<string, any> | undefined = {};
let nodeManifest: Record<string, any> | undefined = {};

const rsbuild = await dev({
cwd: fixtures,
page,
rsbuildConfig: {
output: {
filenameHash: false,
},
environments: {
web: {
output: {
manifest: true,
},
},
node: {
output: {
target: 'node',
manifest: 'manifest-node.json',
},
},
},
dev: {
setupMiddlewares: [
(middlewares, { environments }) => {
middlewares.unshift(async (_req, _res, next) => {
webManifest = environments.web.context.manifest;
nodeManifest = environments.node.context.manifest;
next();
});
},
],
},
},
});

// should visit base url correctly
await page.goto(`http://localhost:${rsbuild.port}`);

// main.js, main.js.map, index.html
expect(Object.keys(webManifest.allFiles).length).toBe(3);
expect(webManifest.entries.index).toMatchObject({
initial: {
js: ['/static/js/index.js'],
},
html: ['/index.html'],
});

// main.js, main.js.map
expect(Object.keys(nodeManifest.allFiles).length).toBe(2);
expect(nodeManifest.entries.index).toMatchObject({
initial: {
js: ['/index.js'],
},
});

await rsbuild.close();
});
1 change: 1 addition & 0 deletions packages/core/src/server/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export async function createDevServer<
await waitFirstCompileDone;
return lastStats[environment.index];
},
context: environment,
loadBundle: async <T>(entryName: string) => {
if (!compilationManager) {
throw new Error(
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type { FileDescriptor } from '../../compiled/rspack-manifest-plugin';
import type { BundleAnalyzerPlugin } from '../../compiled/webpack-bundle-analyzer/index.js';
import type { RsbuildDevServer } from '../server/devServer';
import type {
EnvironmentContext,
ModifyBundlerChainUtils,
ModifyChainUtils,
Routes,
Expand Down Expand Up @@ -1502,6 +1503,11 @@ export type EnvironmentAPI = {
* Get the compiled HTML template.
*/
getTransformedHtml: (entryName: string) => Promise<string>;

/**
* Provides some context information about the current environment
*/
context: EnvironmentContext;
};
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export type EnvironmentContext = {
* Manifest data. Only available when:
* - The `output.manifest` config is enabled
* - The build has completed, accessible in hooks like `onAfterBuild`,
* `onDevCompileDone` and `onAfterEnvironmentCompile`
* `onDevCompileDone` and `onAfterEnvironmentCompile` or via the environment API
*/
manifest?: Record<string, unknown> | ManifestData;
};
Expand Down
15 changes: 15 additions & 0 deletions website/docs/en/api/javascript-api/environment-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,28 @@ You can use environment API via [rsbuild.createDevServer()](/api/javascript-api/
```ts
type EnvironmentAPI = {
[name: string]: {
context: EnvironmentContext;
getStats: () => Promise<Stats>;
loadBundle: <T = unknown>(entryName: string) => Promise<T>;
getTransformedHtml: (entryName: string) => Promise<string>;
};
};
```

### context

You can get context information related to the current environment through the Environment API.

- **Type:** `EnvironmentContext`

- **Example:**

```ts
const webManifest = environments.web.context.manifest;

console.log(webManifest.entries);
```

### getStats

Get the build stats of current environment.
Expand Down
2 changes: 1 addition & 1 deletion website/docs/en/config/output/manifest.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type ManifestList = {

## Access via hooks

You can access the generated manifest data through Rsbuild's [hooks](/plugins/dev/hooks).
You can access the generated manifest data through Rsbuild's [hooks](/plugins/dev/hooks) and [Environment API](/api/javascript-api/environment-api).

For example:

Expand Down
17 changes: 16 additions & 1 deletion website/docs/zh/api/javascript-api/environment-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,35 @@ manifest 数据仅在构建完成后才能被访问,你可以在以下 hooks

## Environment API

Environment API 包了一些与多环境构建相关的 API。
Environment API 提供一些与多环境构建相关的 API。

你可以通过 [rsbuild.createDevServer()](/api/javascript-api/instance#rsbuildcreatedevserver) 或 [dev.setupMiddlewares](/config/dev/setup-middlewares) 使用 environment API,这允许你在服务端获取特定环境下的构建产物信息。

```ts
type EnvironmentAPI = {
[name: string]: {
context: EnvironmentContext;
getStats: () => Promise<Stats>;
loadBundle: <T = unknown>(entryName: string) => Promise<T>;
getTransformedHtml: (entryName: string) => Promise<string>;
};
};
```

### context

你可以通过 Environment API 获取和当前环境有关的上下文信息。

- **类型:** `EnvironmentContext`

- **示例:**

```ts
const webManifest = environments.web.context.manifest;

console.log(webManifest.entries);
```

### getStats

获取当前环境的产物信息。
Expand Down
2 changes: 1 addition & 1 deletion website/docs/zh/config/output/manifest.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type ManifestList = {

## 通过 hooks 访问

你可以通过 Rsbuild 的 [hooks](/plugins/dev/hooks) 访问生成的 manifest 数据。
你可以通过 Rsbuild 的 [hooks](/plugins/dev/hooks) 或 [Environment API](/api/javascript-api/environment-api) 访问生成的 manifest 数据。

例如:

Expand Down
Loading