CC 4.0 License

The content of this section is derived from the content of the following links and is subject to the CC BY 4.0 license.

The following contents can be assumed to be the result of modifications and deletions based on the original contents if not specifically stated.

The Loader Context

The loader context represents the properties that are available inside of a loader assigned to the this property.


function addContextDependency(directory: string): void;

Add the directory as a dependency for the loader results so that any changes to the files in the directory can be listened to.


function addDependency(file: string): void;

Add a file as a dependency on the loader results so that any changes to them can be listened to. For example, sass-loader, less-loader use this trick to recompile when the imported style files change.


function dependency(file: string): void;

Alias of this.addDependency().


function addMissingDependency(file: string): void;

Add a non-existent file as a dependency on the loader results to make them listenable.


Tells Rspack that this loader will be called asynchronously. Returns this.callback.


A function that sets the cacheable flag:

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

By default, the processing results of the loader are marked as cacheable. Calling this method and passing false turns off the loader's ability to cache processing results.


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

A function that can be called synchronously or asynchronously in order to return multiple results. The expected arguments are:

  1. The first parameter must be Error or null, which marks the current module as a compilation failure.
  2. The second argument is a string or Buffer, which indicates the contents of the file after the module has been processed by the loader.
  3. The third parameter is a source map that can be processed by the loader.
  4. The fourth parameter is ignored by Rspack and can be anything (e.g. some metadata).


function clearDependencies(): void;

Removes all dependencies of the loader result.


The directory where the current module is located.

A data object shared between the pitch and the normal phase.


function emitError(error: Error): void;

Emit an error. Unlike throw and this.callback(err) in the loader, it does not mark the current module as a compilation failure, it just adds an error to Rspack's Compilation and displays it on the command line at the end of this compilation.

this.emitWarning(warning: Error)

function emitWarning(warning: Error): void;

Emit a warning.


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

Emit a file.


Extracts the given loader option, accepting an optional JSON schema as an argument.


function getResolve(options: ResolveOptions): resolve;

Create a resolver like this.resolve.


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

Resolve a request.

  • context must be the absolute path to a directory. This directory is used as the starting location for resolving.
  • request is the request to be resolved.
  • callback is a callback function that gives the resolved path.


The value of mode is read when Rspack is run.

The possible values are: 'production', 'development', 'none'

The value of target is read when Rspack is run.


The path string of the current module. For example '/abc/resource.js?query#hash'.


The path string of the current module, excluding the query and fragment parameters. For example '/abc/resource.js?query#hash' in '/abc/resource.js'.


The query parameter for the path string of the current module. For example '?query' in '/abc/resource.js?query#hash'.


The fragment parameter of the current module's path string. For example '#hash' in '/abc/resource.js?query#hash'.


The directory where the project is configured in config


Whether a source map should be generated.


function getLogger(name?: string): void;

Get the logger of this compilation, through which messages can be logged.