Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add MultiCompiler API #7436

Merged
merged 5 commits into from
Aug 5, 2024
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
204 changes: 204 additions & 0 deletions website/docs/en/api/javascript-api/compiler.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import OutputFileSystemType from '../../types/output-file-system.mdx';
import WatchFileSystemType from '../../types/watch-file-system.mdx';
import CompilationType from '../../types/compilation.mdx';
import { Collapse, CollapsePanel } from '@components/Collapse';
import { Badge } from '@theme';

# Compiler

Expand Down Expand Up @@ -368,3 +369,206 @@ Get the proxy object used for watching files or directories changes, which provi
<WatchFileSystemType />
</CollapsePanel>
</Collapse>

## MultiCompiler

The `MultiCompiler` module allows Rspack to run multiple configurations in separate compilers. If the options parameter in the Rspack's JavaScript API is an array of options, Rspack applies separate compilers and calls the callback after all compilers have been executed.

```js
const rspack = require('@rspack/core');

rspack(
[
{ entry: './index1.js', output: { filename: 'bundle1.js' } },
{ entry: './index2.js', output: { filename: 'bundle2.js' } },
],
(err, stats) => {
process.stdout.write(stats.toString() + '\n');
},
);
```

It can also be created through `new MultiCompiler`:

```js
const compiler1 = new Compiler({
/* */
});
const compiler2 = new Compiler({
/* */
});

new MultiCompiler([compiler1, compiler2]);

new MultiCompiler([compiler1, compiler2], {
parallelism: 1, // the maximum number of parallel compilers
});

new MultiCompiler({
name1: compiler1,
name2: compiler2,
});
```

`MultiCompiler` also provides some methods and attributes of the `Compiler`.

### MultiCompiler Methods

#### setDependencies

Specify the dependency relationship between the compilers, using `compiler.name` as the identifier, to ensure the execution order of the compilers.

```ts
setDependencies(compiler: Compiler, dependencies: string[]): void;
```

#### validateDependencies

Check whether the dependency relationship between the compilers is legal. If there is a cycle or a missing dependency, it will trigger the callback.

```ts
validateDependencies(
callback: (err: Error) => void; // callback when there is an error
): boolean
```

#### run

Execute the `run` method of each compiler according to the dependency relationship to start the compilation process.

```ts
run(callback: (err: Error, stats: MultiStats) => void): void;
```

#### watch

Execute the `watch` method of each compiler according to the dependency relationship to start watching, and start a compilation process after the file changes.

```ts
watch(
watchOptions: WatchOptions,
handler: (err: Error, stats: MultiStats) => void,
): MultiWatching
```

#### close

Execute the `close` method of each compiler to close them, and handle low-priority tasks such as caching during this period.

```ts
close(callback: (err: Error) => void): void;
```

#### purgeInputFileSystem

Execute the `purgeInputFileSystem` of each compiler to stop the read loop of the file system

```ts
purgeInputFileSystem(): void;
```

#### getInfrastructureLogger

Create a [logger object](/api/javascript-api/logger) that is not associated with any compilation, which is used to print global logs.

```ts
getInfrastructureLogger(name: string): Logger;
```

> Same with `compilers[0].getInfrastructureLogger()`

<Collapse>
<CollapsePanel
className="collapse-code-panel"
header="Logger.ts"
key="Logger"
>
<LoggerType />
</CollapsePanel>
</Collapse>

### MultiCompiler Properties

#### compilers

**Type:** `Compiler[]`

Get all included compilers.

<Collapse>
<CollapsePanel
className="collapse-code-panel"
header="Compiler.ts"
key="Compiler"
>
<CompilerType />
</CollapsePanel>
</Collapse>

#### options

<Badge text="ReadOnly" type="info" />

**Type:** `RspackOptionsNormalized[]`

Get all the [full options](/config/index) used by the compilers.

#### inputFileSystem

<Badge text="WriteOnly" type="info" />

**Type:** `InputFileSystem`

Set the proxy object used for reading from the file system for each compiler.

<Collapse>
<CollapsePanel
className="collapse-code-panel"
header="InputFileSystem.ts"
key="InputFileSystem"
>
<InputFileSystemType />
</CollapsePanel>
</Collapse>

#### outputFileSystem

<Badge text="WriteOnly" type="info" />

**Type:** `OutputFileSystem`

Set the proxy object used for writing from the file system for each compiler.

<Collapse>
<CollapsePanel
className="collapse-code-panel"
header="OutputFileSystem.ts"
key="OutputFileSystem"
>
<OutputFileSystemType />
</CollapsePanel>
</Collapse>

#### watchFileSystem

<Badge text="WriteOnly" type="info" />

**Type:** `WatchFileSystem`

Set the proxy object used for watching files or directories changes for each compiler.

<Collapse>
<CollapsePanel
className="collapse-code-panel"
header="WatchFileSystem.ts"
key="WatchFileSystem"
>
<WatchFileSystemType />
</CollapsePanel>
</Collapse>

#### running

**Type:** `boolean`

Whether the compilation is currently being executed.
4 changes: 0 additions & 4 deletions website/docs/en/api/javascript-api/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,6 @@ rspack(
);
```

:::warning
Multiple configurations will **not be run in parallel**. Each configuration is only processed after the previous one has finished processing.
:::

## Error Handling

For good error handling, you need to account for these three types of errors:
Expand Down
30 changes: 30 additions & 0 deletions website/docs/en/types/multi-compiler.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<>

> See [MultiCompiler API](/api/javascript-api/compiler#multicompiler) for more details.

```ts
type MultiCompiler = {
constructor(compilers: Compiler[] | Record<string, Compiler>): MultiCompiler;

compilers: Compiler[];
running: boolean;
options: RspackOptionsNormalized[];
inputFileSystem: InputFileSystem;
outputFileSystem: OutputFileSystem;
watchFileSystem: WatchFileSystem;

setDependencies(compiler: Compiler, dependencies: string[]): void;
validateDependencies(callback: (err: Error) => void): boolean;

run(callback: (err: Error, stats: MultiStats) => void): void;
watch(
watchOptions: WatchOptions,
handler: (err: Error, MultiStats) => void,
): void;
close(callback: (err: Error) => void): void;
getInfrastructureLogger(name: string): Logger;
purgeInputFileSystem(): void;
};
```

</>
Loading
Loading