编译器

编译器方法

run

启动编译,并在编译完成或因错误中止时回调。

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;
警告

此 API 每次仅支持一个编译。请在 compiler.run 回调中调用 compiler.close 并等待其完成,然后再再次执行 compiler.run。并发编译会导致输出文件损坏。

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) => {});
  });
});

watch

监视文件和目录,在它们更改后启动编译过程,并在每次编译完成或因错误中止时回调。

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

此 API 每次仅支持一个编译。请在 compiler.close 回调中调用 compiler.watch 并等待其完成,然后再再次执行 compiler.watch。并发编译会导致输出文件损坏。

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

监视对象提供以下方法

  • watch:
    • 类型: (files: string[], dirs: string[], missing: string[]): void
    • 用法: 添加需要监视的文件和目录。
  • invalidate:
    • 类型: (callback: () => void): void
    • 用法: 立即结束本轮监视,并使用当前记录的文件更改启动编译,而不停止监视器。
  • suspend:
    • 类型: (): void
    • 用法: 进入仅监视状态,不会启动新的编译。
  • resume:
    • 类型: (): void
    • 用法: 退出仅监视状态,并使用当前记录的文件更改启动编译。
  • close:
    • 类型: (callback: () => void): void
    • 用法: 停止监视器。

compile

创建编译并运行它,这是 compiler.runcompiler.watch 依赖的基本方法。

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

close

关闭当前编译器,并在期间处理低优先级任务,例如缓存。

close(
  callback: (err: Error) => void // callback after closing
): void;

getInfrastructureLogger

创建一个记录器对象,该对象与任何编译无关,用于打印全局日志。

getInfrastructureLogger(name: string): Logger;

getCache

创建一个缓存对象,以在构建过程中共享数据。

getCache(name: string): CacheFacade;

purgeInputFileSystem

停止输入文件系统的读取循环,该循环在内部包含一个计时器,可能会导致进程在调用 compiler.close 后仍然无法退出。

purgeInputFileSystem(): void;

createChildCompiler

允许在 Rspack 内运行另一个 Rspack 实例。但是,作为具有不同设置和配置的子级应用。它从父级(或顶级编译器)复制所有钩子,并将插件复制到一个新的 Rspack 实例。返回创建的 Compiler

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

runAsChild

运行子编译器,该编译器将进行完整的编译并生成资产。

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;

isChild

此编译器是否为子编译器。

isChild(): boolean;

编译器属性

hooks

有关更多详细信息,请参阅编译器钩子

rspack

  • 类型: typeof rspack

获取 @rspack/core 的导出以获取相关的内部对象。这在您无法直接引用 @rspack/core 或存在多个 Rspack 实例时特别有用。

一个常见的例子是在 Rspack 插件中访问sources 对象

const { RawSource } = compiler.rspack.sources;
const source = new RawSource('console.log("Hello, world!");');

webpack

  • 类型: typeof rspack

等效于 compiler.rspack,此属性用于与 webpack 插件兼容。

如果您正在开发的 Rspack 插件需要与 webpack 兼容,您可以使用此属性而不是 compiler.rspack

console.log(compiler.webpack === compiler.rspack); // true

name

  • 类型: string

获取名称

  • 对于根编译器,它等效于name
  • 对于子编译器,它是传递给 createChildCompiler 的值。
  • 对于 MultiCompiler 以及 KV 形式,它是键。

context

当前项目根目录

  • 通过 new Compiler 创建,它是传递进来的值。
  • 通过 rspack({}) 创建,它是上下文配置

root

  • 类型: Compiler

获取子编译器树的根。

options

  • 类型: RspackOptionsNormalized

获取此编译器使用的完整选项。

watchMode

  • 类型: boolean

是否通过 compiler.watch 启动。

watching

  • 类型: Watching

获取监视对象,有关更多详细信息,请参阅watch 方法

running

  • 类型: boolean

编译是否正在执行。

inputFileSystem

  • 类型: InputFileSystem

获取用于从文件系统读取的代理对象,该对象内部具有缓存等优化,以减少对同一文件的重复读取。

outputFileSystem

  • 类型: OutputFileSystem

获取用于写入文件系统的代理对象,默认情况下为 fs

watchFileSystem

  • 类型: WatchFileSystem

获取用于监视文件或目录更改的代理对象,该对象提供一个 watch 方法来启动监视,并在回调中传入更改和删除的项。

多编译器

MultiCompiler 模块允许 Rspack 在独立的编译器中运行多个配置。如果 Rspack 的 JavaScript API 中的选项参数是一个选项数组,Rspack 将应用独立的编译器并在所有编译器执行后调用回调。

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');
  },
);

它也可以通过 new MultiCompiler 创建

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 还提供了一些 Compiler 的方法和属性。

多编译器方法

setDependencies

指定编译器之间的依赖关系,使用 compiler.name 作为标识符,以确保编译器的执行顺序。

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

validateDependencies

检查编译器之间的依赖关系是否合法。如果有循环或缺少依赖项,它将触发回调。

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

run

根据依赖关系执行每个编译器的 run 方法,以启动编译过程。

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

watch

根据依赖关系执行每个编译器的 watch 方法,以启动监视,并在文件更改后启动编译过程。

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

close

执行每个编译器的 close 方法以关闭它们,并在这段时间内处理缓存等低优先级任务。

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

purgeInputFileSystem

执行每个编译器的 purgeInputFileSystem 以停止文件系统的读取循环

purgeInputFileSystem(): void;

getInfrastructureLogger

创建一个记录器对象,该对象与任何编译无关,用于打印全局日志。

getInfrastructureLogger(name: string): Logger;

compilers[0].getInfrastructureLogger() 相同

多编译器属性

compilers

  • 类型: Compiler[]

获取所有包含的编译器。

options

只读
  • 类型: RspackOptionsNormalized[]

获取编译器使用的所有 完整选项

inputFileSystem

只写
  • 类型: InputFileSystem

设置用于从每个编译器的文件系统中读取的代理对象。

outputFileSystem

只写
  • 类型: OutputFileSystem

设置用于从每个编译器的文件系统中写入的代理对象。

watchFileSystem

只写
  • 类型: WatchFileSystem

设置用于监视每个编译器的文件或目录更改的代理对象。

running

  • 类型: boolean

编译是否正在执行。