website/docs/en/api/plugin-api/external-module-hooks.mdx
import { Collapse, CollapsePanel } from '@components/Collapse'; import ChunkType from '../../types/chunk.mdx'; import CompilationType from '../../types/compilation.mdx';
ExternalModule represents a dependency whose implementation is provided by the runtime environment instead of being bundled by Rspack. It provides hooks for controlling how external modules are assigned to chunks.
You can obtain these hooks as follows:
export default {
plugins: [
{
apply(compiler) {
const { ExternalModule } = compiler.rspack;
compiler.hooks.compilation.tap('MyPlugin', (compilation) => {
const hooks = ExternalModule.getCompilationHooks(compilation);
// ...
});
},
},
],
};
chunkConditionSyncBailHook<[Chunk, Compilation], boolean | undefined>
Controls whether an external module can be included in a chunk. Return true to allow the external module in the chunk, false to disallow it, or undefined to continue to subsequent taps and then use Rspack's default behavior.
The following example only allows external modules to be included in entry chunks:
hooks.chunkCondition.tap('MyPlugin', (chunk, compilation) => {
return compilation.chunkGraph.getNumberOfEntryModules(chunk) > 0;
});