Skip to content

Commit 2ec986e

Browse files
committed
refactor(start): move Rsbuild import protection transform to loader
1 parent b55ce55 commit 2ec986e

7 files changed

Lines changed: 517 additions & 332 deletions

File tree

packages/start-plugin-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"@babel/code-frame": "7.27.1",
8989
"@babel/core": "^7.28.5",
9090
"@babel/types": "^7.28.5",
91+
"@jridgewell/remapping": "^2.3.5",
9192
"@tanstack/router-core": "workspace:*",
9293
"@tanstack/router-generator": "workspace:*",
9394
"@tanstack/router-plugin": "workspace:*",

packages/start-plugin-core/src/rsbuild/INTERNALS-import-protection.md

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import-protection core in `src/import-protection/INTERNALS.md`.
77

88
Rsbuild owns:
99

10-
- post-transform enforcement through `api.transform({ order: 'post' })`
10+
- post-transform enforcement through a Rspack post-loader
1111
- virtual-module transport through `VirtualModulesPlugin`
1212
- compilation-truth reporting in `processAssets`
1313
- final graph reconstruction from Rspack compilation data
@@ -34,8 +34,10 @@ object:
3434
1. `onBeforeBuild`
3535
2. `onBeforeDevCompile`
3636
3. `modifyRspackConfig`
37-
4. `transform(..., { order: 'post' })`
38-
5. `processAssets(..., { stage: 'report' })`
37+
4. `processAssets(..., { stage: 'report' })`
38+
39+
`modifyRspackConfig` installs both the virtual-modules plugin and the
40+
environment-scoped import-protection post-loader.
3941

4042
## State Model
4143

@@ -50,11 +52,6 @@ Shared adapter state contains:
5052
- `vmPlugins`
5153
- `readyVmPlugins`
5254
- `pendingWrites`
53-
- `moduleByResource`
54-
55-
`moduleByResource` associates each loader resource with its Rspack module. The
56-
loader hook populates it, the matching post-transform hook consumes it, and
57-
durable marker metadata lives on `module.buildInfo`.
5855

5956
Notably absent compared to Vite:
6057

@@ -65,7 +62,10 @@ Notably absent compared to Vite:
6562

6663
## Transform Phase
6764

68-
Rsbuild enforcement runs after the Start compiler in a `post` transform.
65+
Rsbuild enforcement runs after the Start compiler in a Rspack loader with
66+
`enforce: 'post'`. The complete transform pipeline lives in
67+
`import-protection-loader.ts`; mutable configuration and per-environment state
68+
are passed through loader options.
6969

7070
That matters because many compiler-safe imports are already stripped by the time
7171
import protection runs. This naturally suppresses a large class of false
@@ -154,9 +154,16 @@ The Rsbuild adapter intentionally prefers native Rspack APIs where possible.
154154

155155
Transform-time:
156156

157-
- `ctx.resource`
158-
- `ctx.context`
159-
- `ctx.resolve(...)`
157+
- `loaderContext.resource`
158+
- `loaderContext.resourcePath`
159+
- `loaderContext.context`
160+
- `loaderContext.resolve(...)`
161+
- `loaderContext._module.buildInfo`
162+
163+
`_module` is a deprecated Rspack loader-context API. It is used deliberately
164+
because a loader invocation is bound to one exact module instance, including
165+
its layer. Keying modules by resource would collapse distinct modules that use
166+
the same resource in different layers. Do not add a resource-map fallback.
160167

161168
Compilation-time:
162169

@@ -175,11 +182,11 @@ Compilation-time:
175182
Unlike Vite, Rsbuild does not introduce plugin-owned virtual marker modules for
176183
normal operation.
177184

178-
The real package marker files are source-level markers. Rspack's loader hook
179-
records the module under the exact loader resource. The matching post-transform
180-
hook writes `{ kind, source }` to `module.buildInfo` before replacing a
181-
wrong-environment module. The metadata survives self-denial mocking and
182-
persistent-cache restores.
185+
The real package marker files are source-level markers. The post-loader writes
186+
`{ kind, source }` directly to its current `_module.buildInfo` before replacing
187+
a wrong-environment module. The metadata therefore stays attached to the exact
188+
resource-and-layer module and survives self-denial mocking and persistent-cache
189+
restores.
183190

184191
`processAssets` treats non-excluded, non-file-denied imports as possible marker
185192
modules, then checks their `buildInfo`. It does not infer marker kind from final

0 commit comments

Comments
 (0)