CC 4.0 许可证

本节内容源自以下链接的内容,并受 CC BY 4.0 许可证约束。

除非另有说明,以下内容可以假定为基于原始内容的修改和删除的结果。

加载器上下文

加载器上下文表示分配给this属性的加载器内部可用的属性。

this.addContextDependency()

function addContextDependency(directory: string): void;

将目录添加为加载器结果的依赖项,以便可以监听目录中任何文件的更改。

this.addDependency()

function addDependency(file: string): void;

将文件添加为加载器结果的依赖项,以便可以监听任何文件的更改。例如,sass-loaderless-loader 使用此技巧在导入的样式文件更改时重新编译。

this.dependency()

function dependency(file: string): void;

this.addDependency() 的别名。

this.addMissingDependency()

function addMissingDependency(file: string): void;

将一个不存在的文件添加为加载器结果的依赖项,使其可监听。

this.async()

告诉 Rspack 此加载器将异步调用。返回this.callback

this.cacheable()

一个设置可缓存标志的函数

function cacheable(flag: boolean = true): void;

默认情况下,加载器的处理结果被标记为可缓存的。调用此方法并传递false 将关闭加载器缓存处理结果的能力。

this.callback()

function callback(
  err: Error | null,
  content: string | Buffer,
  sourceMap?: SourceMap,
  meta?: any,
): void;

一个可以同步或异步调用以返回多个结果的函数。预期的参数是

  1. 第一个参数必须是Errornull,它将当前模块标记为编译失败。
  2. 第二个参数是stringBuffer,它表示模块在被加载器处理后文件的原始内容。
  3. 第三个参数是一个可以被加载器处理的源映射。
  4. 第四个参数被 Rspack 忽略,可以是任何东西(例如一些元数据)。
警告

如果调用此函数,您应该返回undefined 以避免加载器结果不明确。

传递给this.callback的值将传递给链中的下一个加载器。sourceMapmeta参数是可选的。如果它们没有被传递,下一个加载器将不会收到它们。

this.clearDependencies()

function clearDependencies(): void;

删除加载器结果的所有依赖项。

this.context

当前模块所在的目录。

this.data

在 pitch 和正常阶段之间共享的数据对象。

this.emitError()

function emitError(error: Error): void;

发出错误。与加载器中的throwthis.callback(err)不同,它不会将当前模块标记为编译失败,它只是向 Rspack 的编译添加一个错误,并在本次编译结束时在命令行上显示它。

this.emitWarning(warning: Error)

function emitWarning(warning: Error): void;

发出警告。

this.emitFile()

function emitFile(
  name: string,
  content: Buffer | string,
  sourceMap: SourceMap,
): void;

发出文件。

this.getOptions(schema)

提取给定的加载器选项,接受可选的 JSON 架构作为参数。

this.getResolve()

function getResolve(options: ResolveOptions): resolve;

创建类似于this.resolve的解析器。

this.resolve()

function resolve(
  context: string,
  request: string,
  callback: (err: Error | null, result: string) => void,
): void;

解析请求。

  • context必须是目录的绝对路径。此目录用作解析的起始位置。
  • request是要解析的请求。
  • callback是一个回调函数,它提供已解析的路径。

this.mode

当运行 Rspack 时,将读取mode的值。

可能的值为:'production''development''none'

this.target

当运行 Rspack 时,将读取target的值。

this.resource

当前模块的路径字符串。例如'/abc/resource.js?query#hash'

this.resourcePath

当前模块的路径字符串,不包括查询和片段参数。例如'/abc/resource.js?query#hash'中的'/abc/resource.js'

this.resourceQuery

当前模块的路径字符串的查询参数。例如'/abc/resource.js?query#hash'中的'?query'

this.resourceFragment

当前模块的路径字符串的片段参数。例如'/abc/resource.js?query#hash'中的'#hash'

this.rootContext

在配置中配置项目的目录

this.sourceMap

是否应该生成源映射。

this.getLogger()

function getLogger(name?: string): void;

获取此编译的日志记录器,通过它可以记录消息。