packages/start-plugin-core/src/rsbuild/INTERNALS-import-protection.md
This document covers the Rsbuild-specific orchestration around the shared
import-protection core in src/import-protection/INTERNALS.md.
Rsbuild owns:
api.transform({ order: 'post' })VirtualModulesPluginprocessAssetsShared AST analysis, rewrite logic, source extraction, usage lookup, source locations, trace formatting, and mock code generation are described in the shared internals doc.
Vite is primarily resolveId-driven. Rsbuild is primarily transform +
processAssets-driven.
That difference explains most of the adapter divergence.
Rsbuild does not emulate the Vite pending-queue state machine. It relies more directly on post-transform code and final compilation truth.
Import protection is attached through Rsbuild hooks, not a dedicated mini-plugin object:
onBeforeBuildonBeforeDevCompilemodifyRspackConfigtransform(..., { order: 'post' })processAssets(..., { stage: 'report' })Per environment, Rsbuild keeps a smaller runtime state than Vite:
resolveCacheseenViolationsbuildTransformResultsdeferredFileViolationsdeferredFileViolationKeysShared state is for virtual module transport and compiler fs access:
virtualModulesvmPluginsreadyVmPluginsinputFileSystemspendingWritesNotably absent compared to Vite:
pendingViolationspostTransformImportsdeferredBuildViolationsRsbuild enforcement runs after the Start compiler in a post transform.
That matters because many compiler-safe imports are already stripped by the time import protection runs. This naturally suppresses a large class of false positives without a Vite-style pending verification pass.
The transform phase is responsible for:
Rsbuild reuses the shared mock generators, but not the Vite id transport.
Instead it writes environment-scoped virtual files under:
<root>/node_modules/.virtual/import-protection/<env>/
The important forms are:
mock-silent.mjsmock-runtime-<base64>.mjsmock-edge-<base64>.mjsWrites may happen before the VirtualModulesPlugin instance is ready, so the
adapter queues them and flushes during compilation setup.
processAssets({ stage: 'report' }) is the authoritative reporting step.
It reconstructs the final view of the compilation from Rspack data by:
TransformResultProvider from compilation.modulesThis is the core Rsbuild-native replacement for Vite's generateBundle
verification plus dev pending-violation flow.
Rsbuild only needs explicit build deferral for file violations whose direct edge may disappear after compilation.
Specifier violations are rediscovered from surviving mock-edge virtual files. Marker violations are rediscovered from live compiled edges.
Only file violations need extra bookkeeping when the final compiled graph can no longer show the original denied edge directly.
The Rsbuild adapter intentionally prefers native Rspack APIs where possible.
Transform-time:
ctx.resourcectx.contextctx.resolve(...)compiler.inputFileSystem.readFile(...)Compilation-time:
module.nameForCondition?.()module.resourceResolveData?.resourcemodule.originalSource().sourceAndMap()sourcesContentcompilation.inputFileSystem.readFile(...)This keeps the adapter closer to Rsbuild/Rspack truth and avoids falling back to Node fs when the compilation already has the needed data.
Unlike Vite, Rsbuild does not introduce plugin-owned virtual marker modules for normal operation.
The real package marker files are used as source-level markers, and the adapter later infers marker kind from original source while reporting compiled edges.
When changing Rsbuild import protection, ask: