diff --git a/e2e/cases/output/manifest-environment/index.test.ts b/e2e/cases/output/manifest-environment/index.test.ts index 14806c61d3..88e6eed376 100644 --- a/e2e/cases/output/manifest-environment/index.test.ts +++ b/e2e/cases/output/manifest-environment/index.test.ts @@ -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 | undefined = {}; + let nodeManifest: Record | 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(); +}); diff --git a/packages/core/src/server/devServer.ts b/packages/core/src/server/devServer.ts index edcdc58fcc..c63fb6b286 100644 --- a/packages/core/src/server/devServer.ts +++ b/packages/core/src/server/devServer.ts @@ -292,6 +292,7 @@ export async function createDevServer< await waitFirstCompileDone; return lastStats[environment.index]; }, + context: environment, loadBundle: async (entryName: string) => { if (!compilationManager) { throw new Error( diff --git a/packages/core/src/types/config.ts b/packages/core/src/types/config.ts index c2a859ed2a..7c50f8802b 100644 --- a/packages/core/src/types/config.ts +++ b/packages/core/src/types/config.ts @@ -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, @@ -1502,6 +1503,11 @@ export type EnvironmentAPI = { * Get the compiled HTML template. */ getTransformedHtml: (entryName: string) => Promise; + + /** + * Provides some context information about the current environment + */ + context: EnvironmentContext; }; }; diff --git a/packages/core/src/types/hooks.ts b/packages/core/src/types/hooks.ts index c379713551..7d8f398277 100644 --- a/packages/core/src/types/hooks.ts +++ b/packages/core/src/types/hooks.ts @@ -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 | ManifestData; }; diff --git a/website/docs/en/api/javascript-api/environment-api.mdx b/website/docs/en/api/javascript-api/environment-api.mdx index 7fdfdb26c3..a9e99325d9 100644 --- a/website/docs/en/api/javascript-api/environment-api.mdx +++ b/website/docs/en/api/javascript-api/environment-api.mdx @@ -198,6 +198,7 @@ You can use environment API via [rsbuild.createDevServer()](/api/javascript-api/ ```ts type EnvironmentAPI = { [name: string]: { + context: EnvironmentContext; getStats: () => Promise; loadBundle: (entryName: string) => Promise; getTransformedHtml: (entryName: string) => Promise; @@ -205,6 +206,20 @@ type EnvironmentAPI = { }; ``` +### 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. diff --git a/website/docs/en/config/output/manifest.mdx b/website/docs/en/config/output/manifest.mdx index cf7f479d3c..5678b6dabe 100644 --- a/website/docs/en/config/output/manifest.mdx +++ b/website/docs/en/config/output/manifest.mdx @@ -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: diff --git a/website/docs/zh/api/javascript-api/environment-api.mdx b/website/docs/zh/api/javascript-api/environment-api.mdx index 70ce672538..7d17ea932f 100644 --- a/website/docs/zh/api/javascript-api/environment-api.mdx +++ b/website/docs/zh/api/javascript-api/environment-api.mdx @@ -192,13 +192,14 @@ 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; loadBundle: (entryName: string) => Promise; getTransformedHtml: (entryName: string) => Promise; @@ -206,6 +207,20 @@ type EnvironmentAPI = { }; ``` +### context + +你可以通过 Environment API 获取和当前环境有关的上下文信息。 + +- **类型:** `EnvironmentContext` + +- **示例:** + +```ts +const webManifest = environments.web.context.manifest; + +console.log(webManifest.entries); +``` + ### getStats 获取当前环境的产物信息。 diff --git a/website/docs/zh/config/output/manifest.mdx b/website/docs/zh/config/output/manifest.mdx index 9638135abb..270a8db253 100644 --- a/website/docs/zh/config/output/manifest.mdx +++ b/website/docs/zh/config/output/manifest.mdx @@ -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 数据。 例如: