Skip to content

Commit

Permalink
docs: add compiler api (#7423)
Browse files Browse the repository at this point in the history
* docs: add compiler api

* docs: add compiler api

* docs: add compiler api
  • Loading branch information
LingyuCoder authored Aug 5, 2024
1 parent 4471e41 commit c75f8ff
Show file tree
Hide file tree
Showing 17 changed files with 899 additions and 8 deletions.
2 changes: 1 addition & 1 deletion website/docs/en/api/javascript-api/_meta.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["index", "compilation", "stats-json", "logger", "cache"]
["index", "compiler", "compilation", "stats-json", "logger", "cache"]
2 changes: 1 addition & 1 deletion website/docs/en/api/javascript-api/cache.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Collapse, CollapsePanel } from '@components/Collapse';
import Cache from '../../types/cache.mdx';
import Columns from '@components/Columns';

# Cache Object
# Cache

When writing Rspack plugins, you can use `compiler.getCache(name: string)` or `compilation.getCache(name: string)` to get the cache object which can share data in the build process. The cache data is stored on the `Compiler`, so it can be used in multiple `Compilation`s in the watch mode.

Expand Down
2 changes: 1 addition & 1 deletion website/docs/en/api/javascript-api/compilation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Collapse, CollapsePanel } from '@components/Collapse';

<WebpackLicense from="https://webpack.docschina.org/api/compilation-object/" />

# Compilation Object
# Compilation

This page will list the methods and properties available on the compilation object.

Expand Down
370 changes: 370 additions & 0 deletions website/docs/en/api/javascript-api/compiler.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,370 @@
import StatsType from '../../types/stats.mdx';
import CompilerType from '../../types/compiler.mdx';
import LoggerType from '../../types/logger.mdx';
import CacheType from '../../types/cache.mdx';
import ChunkType from '../../types/cache.mdx';
import InputFileSystemType from '../../types/input-file-system.mdx';
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';

# Compiler

## Compiler Methods

### run

Start a compilation, and callbacked when the compilation is completed or aborted due to an error.

```ts
run(callback: (
error: Error, // Only including compiler-related errors, such as configuration errors, not including compilation errors
stats: Stats, // detailed information generated during the compilation
) => void): void;
```

:::warning
This API only supports one compilation at a time. Please call `compiler.close` in the `compiler.run` callback and wait for it to finish before executing `compiler.run` again. Concurrent compilations will damage the output files.
:::

```js
compiler.run((err, stats) => {
// Deal with the compiler errors
handlerCompilerError(err);
// Deal with the compilation errors
handlerModuleErrors(stats.toJson().errors);
// Deal with the result
handleBuildResult(stats);
// End this compilation
compiler.close(closeErr => {
// Start a new compilation
compiler.run((err, stats) => {});
});
});
```

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

### watch

Watching files and directories, start a compilation process after they change, and callbacked every time the compilation is completed or aborted due to an error.

```ts
watch(
watchOptions: WatchOptions, // options for starting the watching
handler: (error: Error, stats: Stats) => void // callback when every compilation ends
): Watching; // watching controller
```

:::warning Warning
This API only supports one compilation at a time. Please call `compiler.close` in the `compiler.watch` callback and wait for it to finish before executing `compiler.watch` again. Concurrent compilations will damage the output files.
:::

```js
const watching = compiler.watch(
{
aggregateTimeout: 300,
poll: undefined,
},
(err, stats) => {
// Deal with the result
handleBuildResult(stats);
},
);
```

The Watching object provides the following methods:

- `watch`:
- **Type**: `(files: string[], dirs: string[], missing: string[]): void`
- **Usage**: Add the files and directories that need to be watched.
- `invalidate`:
- **Type**: `(callback: () => void): void`
- **Usage**: Immediately end this round of watching and start a compilation with the currently recorded file changes, without stopping the watcher.
- `suspend`:
- **Type**: `(): void`
- **Usage**: Enter the state of only watching and will not start a new compilation.
- `resume`:
- **Type**: `(): void`
- **Usage**: Exit the state of only watching and start a compilation with the currently recorded file changes.
- `close`:
- **Type**: `(callback: () => void): void`
- **Usage**: Stop the watcher.

<Collapse>
<CollapsePanel
className="collapse-code-panel"
header="WatchOptions.ts"
key="WatchOptions"
>

> See [watch options](/config/watch#watchoptions) for more details.
</CollapsePanel>
<CollapsePanel
className="collapse-code-panel"
header="Stats.ts"
key="Stats"
>
<StatsType />
</CollapsePanel>
</Collapse>

### compile

Create a compilation and run it, which is the basic method that `compiler.run` and `compiler.watch` depend on.

```ts
compile(
callback: (compilation: Compilation) => void // callback after this compilation
): void
```

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

### close

Close the current compiler, and handle low-priority tasks such as caching during this period.

```ts
close(
callback: (err: Error) => void // callback after closing
): 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;
```

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

### getCache

Create a cache object to share data in the build process.

```ts
getCache(name: string): CacheFacade;
```

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

### purgeInputFileSystem

Stop the read loop of the input file system, which internally contains a timer and may cause the process to still not be able to exit after calling `compiler.close`.

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

### createChildCompiler

Allows running another instance of Rspack inside of Rspack. However, as a child with different settings and configurations applied. It copies all hooks and plugins from the parent (or top-level compiler) and creates a child `Compiler` instance. Returns the created `Compiler`.

```ts
createChildCompiler(
compilation: Compilation,
compilerName: string,
compilerIndex: number,
outputOptions: OutputOptions,
plugins: RspackPlugin[]
): Compiler;
```

<Collapse>
<CollapsePanel
className="collapse-code-panel"
header="Compilation.ts"
key="Compilation"
>
<CompilationType />
</CollapsePanel>
<CollapsePanel
className="collapse-code-panel"
header="OutputOptions.ts"
key="OutputOptions"
>

> See [output options](/config/output) for more details.
</CollapsePanel>
<CollapsePanel
className="collapse-code-panel"
header="RspackPlugin.ts"
key="RspackPlugin"
>

> See [plugins options](/config/plugins) for more details
</CollapsePanel>
</Collapse>

### runAsChild

Running the child compiler, which will doing a complete compiling and generate the assets.

```ts
runAsChild(
callback(
err: Error, // error related to the child compiler
entries: Chunk[], // chunks generated by the child compiler
compilation: Compilation, // the compilation created by the child compiler
): void;
): void;
```

<Collapse>
<CollapsePanel className="collapse-code-panel" header="Chunk.ts" key="Chunk">
<ChunkType />
</CollapsePanel>
<CollapsePanel
className="collapse-code-panel"
header="Compilation.ts"
key="Compilation"
>
<CompilationType />
</CollapsePanel>
</Collapse>

### isChild

Whether this compiler is a child compiler.

```ts
isChild(): boolean;
```

## Compiler Properties

### hooks

See [compiler hooks](/api/plugin-api/compiler-hooks) for more details.

### rspack/webpack

**Type:** `typeof rspack`

Get the exports of @rspack/core to obtain the associated internal objects. This is usually very useful when Rspack is wrapped or there are multiple Rspack instances.

### name

**Type:** `string`

Get the name:

- For the root compiler, it is equivalent to [`name`](/config/other-options#name).
- For the child compiler, it is the value passed into `createChildCompiler`.
- For the MultiCompiler and in the KV form, it is the key.

### context

Current project root directory:

- Created through `new Compiler`, it is the value passed in.
- Created through `rspack({})`, it is [context configuration](/config/context).

### root

**Type:** `Compiler`

Get the root of the child compiler tree.

### options

**Type:** `RspackOptionsNormalized`

Get the full options used by this compiler.

### watchMode

**Type:** `boolean`

Whether started through `compiler.watch`.

### watching

**Type:** `Watching`

Get the watching object, see [watch method](#watch) for more details.

### running

**Type:** `boolean`

Whether the compilation is currently being executed.

### inputFileSystem

**Type:** `InputFileSystem`

Get the proxy object used for reading from the file system, which has optimizations such as caching inside to reduce duplicate reading of the same file.

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

### outputFileSystem

**Type:** `OutputFileSystem`

Get the proxy object used for writing to the file system, `fs` by default.

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

### watchFileSystem

**Type:** `WatchFileSystem`

Get the proxy object used for watching files or directories changes, which provides a `watch` method to start watching, and passes in the changed and removed items in the callback.

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

0 comments on commit c75f8ff

Please sign in to comment.