Skip to content

Commit

Permalink
feat: export more sub-types of Rsbuild config (#3351)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Sep 2, 2024
1 parent 71f8ae4 commit 971c7df
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 33 deletions.
4 changes: 2 additions & 2 deletions e2e/cases/server/ssr/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
type RequestHandler,
type ServerAPIs,
type SetupMiddlewaresServer,
defineConfig,
logger,
} from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';

export const serverRender =
(serverAPI: ServerAPIs): RequestHandler =>
(serverAPI: SetupMiddlewaresServer): RequestHandler =>
async (_req, res, _next) => {
const indexModule = await serverAPI.environments.ssr.loadBundle<{
render: () => string;
Expand Down
22 changes: 20 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,42 @@ export type {
BundlerPluginInstance,
CacheGroup,
CacheGroups,
Charset,
ClientConfig,
ConfigChain,
ConfigChainWithContext,
ConsoleType,
CreateCompiler,
CreateCompilerOptions,
CreateRsbuildOptions,
ResolvedCreateRsbuildOptions,
CrossOrigin,
CSSLoaderOptions,
CSSModules,
CSSModulesLocalsConvention,
DataUriLimit,
Decorators,
DevConfig,
DistPathConfig,
EnvironmentContext,
EnvironmentConfig,
FilenameConfig,
HistoryApiFallbackContext,
HistoryApiFallbackOptions,
HtmlConfig,
HtmlRspackPlugin,
HtmlBasicTag,
HtmlFallback,
HtmlTagHandler,
HtmlTagDescriptor,
HtmlTagContext,
InspectConfigOptions,
InspectConfigResult,
InlineChunkConfig,
InlineChunkTest,
InlineChunkTestFunction,
LegalComments,
MetaAttrs,
MetaOptions,
Minify,
ModifyBundlerChainFn,
ModifyBundlerChainUtils,
Expand Down Expand Up @@ -104,10 +114,14 @@ export type {
PostCSSLoaderOptions,
PostCSSPlugin,
PreconnectOption,
ProxyOptions,
ProxyDetail,
PrintUrls,
PublicDir,
PublicDirOptions,
ProgressBarConfig,
RequestHandler,
ResolvedCreateRsbuildOptions,
RsbuildConfig,
RsbuildContext,
RsbuildEntry,
Expand All @@ -121,10 +135,14 @@ export type {
RsbuildTarget,
RspackChain,
RspackRule,
SriOptions,
SriAlgorithm,
ScriptInject,
ScriptLoading,
SecurityConfig,
ServerAPIs,
SourceMap,
SetupMiddlewaresFn,
SetupMiddlewaresServer,
ServerConfig,
SourceConfig,
SplitChunks,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/rspack/RsbuildHtmlPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type {
EnvironmentContext,
HtmlBasicTag,
HtmlTag,
HtmlTagContext,
HtmlTagDescriptor,
HtmlTagUtils,
ModifyHTMLTagsFn,
} from '../types';

Expand Down Expand Up @@ -182,7 +182,7 @@ const applyTagConfig = (
...formatTags(data.bodyTags, { head: false }),
];

const utils: HtmlTagUtils = {
const context: HtmlTagContext = {
hash: compilationHash,
entryName,
outputName: data.outputName,
Expand All @@ -191,7 +191,7 @@ const applyTagConfig = (

for (const item of tagConfig.tags) {
if (isFunction(item)) {
tags = item(tags, utils) || tags;
tags = item(tags, context) || tags;
} else {
tags.push(item);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/server/getDevMiddlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type {
EnvironmentAPI,
RequestHandler,
Rspack,
ServerAPIs,
ServerConfig,
SetupMiddlewaresServer,
} from '../types';
import type { UpgradeEvent } from './helper';
import {
Expand All @@ -20,7 +20,7 @@ import {

export type CompileMiddlewareAPI = {
middleware: RequestHandler;
sockWrite: ServerAPIs['sockWrite'];
sockWrite: SetupMiddlewaresServer['sockWrite'];
onUpgrade: UpgradeEvent;
close: () => void;
};
Expand All @@ -44,7 +44,7 @@ const applySetupMiddlewares = (
) => {
const setupMiddlewares = dev.setupMiddlewares || [];

const serverOptions: ServerAPIs = {
const serverOptions: SetupMiddlewaresServer = {
sockWrite: (type, data) => compileMiddlewareAPI?.sockWrite(type, data),
environments,
};
Expand Down
27 changes: 12 additions & 15 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ export type HtmlTag = HtmlBasicTag & {
head?: boolean;
};

export type HtmlTagUtils = {
export type HtmlTagContext = {
hash: string;
entryName: string;
outputName: string;
Expand All @@ -947,7 +947,7 @@ export type HtmlTagUtils = {

export type HtmlTagHandler = (
tags: HtmlTag[],
utils: HtmlTagUtils,
context: HtmlTagContext,
) => HtmlTag[] | void;

export type HtmlTagDescriptor = HtmlTag | HtmlTagHandler;
Expand Down Expand Up @@ -1099,14 +1099,22 @@ export type EnvironmentAPI = {
};
};

export type ServerAPIs = {
export type SetupMiddlewaresServer = {
sockWrite: (
type: string,
data?: string | boolean | Record<string, any>,
) => void;
environments: EnvironmentAPI;
};

export type SetupMiddlewaresFn = (
middlewares: {
unshift: (...handlers: RequestHandler[]) => void;
push: (...handlers: RequestHandler[]) => void;
},
server: SetupMiddlewaresServer,
) => void;

export type ClientConfig = {
/**
* The path for the WebSocket request.
Expand Down Expand Up @@ -1176,18 +1184,7 @@ export interface DevConfig {
/**
* Provides the ability to execute a custom function and apply custom middlewares.
*/
setupMiddlewares?: Array<
(
/** Order: `unshift` => internal middlewares => `push` */
middlewares: {
/** Use the `unshift` method if you want to run a middleware before all other middlewares */
unshift: (...handlers: RequestHandler[]) => void;
/** Use the `push` method if you want to run a middleware after all other middlewares */
push: (...handlers: RequestHandler[]) => void;
},
server: ServerAPIs,
) => void
>;
setupMiddlewares?: SetupMiddlewaresFn[];
/**
* Controls whether the build output from development mode is written to disk.
* @default false
Expand Down
4 changes: 2 additions & 2 deletions website/docs/en/config/dev/setup-middlewares.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- **Type:**

```ts
type ServerAPIs = {
type SetupMiddlewaresServer = {
sockWrite: (
type: string,
data?: string | boolean | Record<string, any>,
Expand Down Expand Up @@ -36,7 +36,7 @@ type SetupMiddlewares = Array<
unshift: (...handlers: RequestHandler[]) => void;
push: (...handlers: RequestHandler[]) => void;
},
server: ServerAPIs,
server: SetupMiddlewaresServer,
) => void
>;
```
Expand Down
4 changes: 2 additions & 2 deletions website/docs/en/config/html/tags.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ The final insertion position on the page is as follows:
## Tags Handler

```ts
type HtmlTagUtils = {
type HtmlTagContext = {
hash: string;
entryName: string;
outputName: string;
Expand All @@ -114,7 +114,7 @@ type HtmlTagUtils = {

type HtmlTagHandler = (
tags: HtmlTag[],
utils: HtmlTagUtils,
context: HtmlTagContext,
) => HtmlTag[] | void;
```

Expand Down
4 changes: 2 additions & 2 deletions website/docs/zh/config/dev/setup-middlewares.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- **类型:**

```ts
type ServerAPIs = {
type SetupMiddlewaresServer = {
sockWrite: (
type: string,
data?: string | boolean | Record<string, any>,
Expand Down Expand Up @@ -37,7 +37,7 @@ type SetupMiddlewares = Array<
unshift: (...handlers: RequestHandler[]) => void;
push: (...handlers: RequestHandler[]) => void;
},
server: ServerAPIs,
server: SetupMiddlewaresServer,
) => void
>;
```
Expand Down
4 changes: 2 additions & 2 deletions website/docs/zh/config/html/tags.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default {
## 函数形式

```ts
type HtmlTagUtils = {
type HtmlTagContext = {
hash: string;
entryName: string;
outputName: string;
Expand All @@ -113,7 +113,7 @@ type HtmlTagUtils = {

type HtmlTagHandler = (
tags: HtmlTag[],
utils: HtmlTagUtils,
context: HtmlTagContext,
) => HtmlTag[] | void;
```

Expand Down

0 comments on commit 971c7df

Please sign in to comment.