Webpack / Rspack Plugin Configuration
Alloy ships explicit webpack and Rspack adapters for applications on those bundlers. They use the same AlloyPluginOptions as alloy-di/vite and generate the same virtual:alloy-container module.
Webpack
js
// webpack.config.mjs
import alloy from "alloy-di/webpack";
export default {
plugins: [
alloy({
sourceDirs: ["src"],
}),
],
};Generate declarations before type-checking with the webpack config loader:
bash
alloy generate --bundler webpackRspack
js
// rspack.config.mjs
import alloy from "alloy-di/rspack";
export default {
plugins: [
alloy({
sourceDirs: ["src"],
}),
],
};Generate declarations before type-checking with the Rspack config loader:
bash
alloy generate --bundler rspackBehavior
virtual:alloy-containeris resolved to an Alloy-generated cache module undernode_modules/.cache/alloy-di.- Configured
sourceDirsare rescanned for each build/watch compilation. - Provider files and configured source directories are added to the bundler watch graph where the compiler exposes those dependency sets.
- Rebuilds regenerate the cache module before compilation, so imports from
virtual:alloy-containersee the latest discovered service graph.
Current Limitations
- The webpack and Rspack adapters regenerate the container on rebuild; they do not hot-swap the DI graph inside a running page. If the service graph changes during development, refresh the browser after the rebuild completes.
alloy generateloads JavaScript bundler config files (.js,.mjs,.cjs). TypeScript webpack/Rspack configs need a JavaScript wrapper, or you can pass--config falseand callgenerate()fromalloy-di/generatewith explicit options.- The adapters do not transpile TypeScript or styles. Keep using your bundler's normal loaders for
.ts,.tsx, CSS, Sass, assets, and framework-specific transforms.
CLI Options
bash
alloy generate --bundler webpack --config webpack.config.mjs --mode development
alloy generate --bundler rspack --config rspack.config.mjs --mode production--bundleracceptsvite,webpack,rspack, ornone.--modeis passed to webpack/Rspack config functions and defaults toproduction.--config falseskips config loading and is equivalent to--bundler none.