Compiler hooks

Overview

environment

Called while preparing the compiler environment, right after initializing the plugins in the configuration file.

  • Type: SyncHook<[]>

afterEnvironment

Called right after the environment hook, when the compiler environment setup is complete.

  • Type: SyncHook<[]>

entryOption

Called after the entry configuration from Rspack options has been processed.

  • Type: SyncBailHook<[string, EntryNormalized]>
  • Arguments:
    • string: same with context
    • EntryNormalized: normalized entry

afterPlugins

Called after setting up initial set of internal plugins.

  • Type: SyncHook<[Compiler]>
  • Arguments:
    • Compiler: current compiler instance

afterResolvers

Triggered after resolver setup is complete.

  • Type: SyncHook<[Compiler]>
  • Arguments:
    • Compiler: current compiler instance

initialize

Called when a compiler object is initialized.

  • Type: SyncHook<[]>

beforeRun

Adds a hook right before running the compiler.

NOTE

This hook is only triggered when calling compiler.run() (which is used by the rspack build command), and will not be executed in watch mode. You can use the watchRun hook in watch mode.

  • Type: AsyncSeriesHook<[Compiler]>
  • Arguments:
    • Compiler: current compiler instance
  • Example: Sync operation
class ExamplePlugin {
  apply(compiler) {
    compiler.hooks.beforeRun.tap('ExamplePlugin', compiler => {
      console.log('Build is about to start...');
    });
  }
}
  • Example: Async operation
class ExamplePlugin {
  apply(compiler) {
    compiler.hooks.beforeRun.tapPromise(
      'ExamplePlugin',
      (compiler) => {
        console.log('Build is about to start...');

        await someAsyncOperation();
      },
    );
  }
}

run

Called at the beginning of a build execution.

NOTE

This hook is only triggered when calling compiler.run() (which is used by the rspack build command), and will not be executed in watch mode. You can use the watchRun hook in watch mode.

  • Type: AsyncSeriesHook<[Compiler]>
  • Arguments:
    • Compiler: current compiler instance
  • Example: Sync operation
class ExamplePlugin {
  apply(compiler) {
    compiler.hooks.beforeRun.tap('ExamplePlugin', compiler => {
      console.log('Build start...');
    });
  }
}
  • Example: Async operation
class ExamplePlugin {
  apply(compiler) {
    compiler.hooks.beforeRun.tapPromise(
      'ExamplePlugin',
      (compiler) => {
        console.log('Build start...');

        await someAsyncOperation();
      },
    );
  }
}

watchRun

Executes a plugin during watch mode after a new compilation is triggered but before the compilation is actually started.

You can use compiler.modifiedFiles and compiler.removedFiles to get the changed file paths and removed file paths.

NOTE

This hook is only triggered when calling compiler.watch(), and will not be called in non-watch mode. You can use the run or beforeRun hook in non-watch mode.

  • Type: AsyncSeriesHook<[Compiler]>
  • Arguments:
    • Compiler: current compiler instance
  • Example: Sync operation
class ExamplePlugin {
  apply(compiler) {
    compiler.hooks.watchRun.tap('ExamplePlugin', compiler => {
      const { modifiedFiles, removedFiles } = compiler;
      if (modifiedFiles) {
        console.log('Changed files:', Array.from(modifiedFiles));
      }
      if (removedFiles) {
        console.log('Removed files:', Array.from(removedFiles));
      }
    });
  }
}
  • Example: Async operation
class ExamplePlugin {
  apply(compiler) {
    compiler.hooks.watchRun.tapPromise('ExamplePlugin', compiler => {
      await someAsyncOperation();
    });
  }
}

beforeCompile

Executes a plugin after compilation parameters are created.

  • Type: AsyncSeriesHook<[]>

compile

Called right after beforeCompile, before a new compilation is created.

  • Type: SyncHook<[]>

thisCompilation

Called while initializing the compilation, right before calling the compilation hook.

  • Type: SyncHook<[Compilation]>
  • Arguments:

compilation

Runs a plugin after a compilation has been created.

  • Type: SyncHook<[Compilation]>
  • Arguments:

make

Called before the make phase.

In the make phase, Rspack will build the module graph starting from the entry, and use the loader to handle each module.

  • Type: AsyncParallelHook<[Compilation]>
  • Arguments:

finishMake

Called after finishing the make phase.

In the make phase, Rspack builds the module graph starting from the entry and uses loaders to handle each module. This hook is called when that process completes.

  • Type: AsyncSeriesHook<[Compilation]>
  • Arguments:

afterCompile

Called after the make phase and before the seal phase.

In the seal phase, Rspack will create chunk graph from the module graph and then generate the assets.

  • Type: AsyncSeriesHook<[Compilation]>
  • Arguments:

shouldEmit

Called before emitting assets. Should return a boolean telling whether to emit.

  • Type: SyncBailHook<[Compilation]>
  • Arguments:

emit

Called right before emitting assets to output dir.

  • Type: AsyncSeriesHook<[Compilation]>
  • Arguments:

afterEmit

Called after emitting assets to output directory.

  • Type: AsyncSeriesHook<[Compilation]>
  • Arguments:

done

Called when the compilation has completed.

  • Type: AsyncSeriesHook<Stats>
  • Arguments:
    • Stats: generated stats object

afterDone

Called after done hook.

  • Type: SyncHook<Stats>
  • Arguments:
    • Stats: generated stats object

failed

Called if the compilation fails.

  • Type: SyncHook<[Error]>

invalid

Executed when a watching compilation has been invalidated. This hook is not copied to child compilers.

  • Type: SyncHook<[string | null, number]>

  • Arguments:

    • fileName: the file path of the invalid file
    • changeTime: the change time of the invalid file

When triggering a re-compilation, this hook can be used to get the changed file path and change time, for example:

compiler.hooks.invalid.tap('MyPlugin', (fileName, changeTime) => {
  console.log(`Changed file: ${fileName}, change time: ${changeTime}`);
});

watchClose

Called when a watching compilation has stopped.

  • Type: SyncHook<[]>

shutdown

Called when the compiler is closing.

  • Type: AsyncSeriesHook<[]>