rsbuild-plugin-react-router 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/README.md +58 -208
  2. package/dist/511.js +371 -332
  3. package/dist/build-output-transforms.d.ts +30 -6
  4. package/dist/classic-mode.d.ts +56 -0
  5. package/dist/config-imports.d.ts +8 -2
  6. package/dist/constants.d.ts +3 -0
  7. package/dist/dev-background-resources.d.ts +3 -1
  8. package/dist/dev-generation.d.ts +2 -3
  9. package/dist/dev-hmr.d.ts +8 -2
  10. package/dist/dev-runtime-controller.d.ts +6 -1
  11. package/dist/dev-source-maps.d.ts +4 -0
  12. package/dist/effect-runtime.d.ts +17 -5
  13. package/dist/entry-paths.d.ts +16 -0
  14. package/dist/environment-output.d.ts +6 -0
  15. package/dist/export-utils.d.ts +2 -1
  16. package/dist/index.cjs +3964 -2217
  17. package/dist/index.d.ts +3 -1
  18. package/dist/index.js +3474 -1799
  19. package/dist/lazy-compilation-prewarm.d.ts +8 -5
  20. package/dist/manifest.d.ts +17 -5
  21. package/dist/mode-plan.d.ts +82 -0
  22. package/dist/modify-browser-manifest.d.ts +8 -4
  23. package/dist/plugin-utils.d.ts +27 -1
  24. package/dist/prerender-build.d.ts +5 -5
  25. package/dist/prerender.d.ts +1 -5
  26. package/dist/react-router-config.d.ts +11 -3
  27. package/dist/route-artifacts.d.ts +4 -2
  28. package/dist/route-chunks.d.ts +8 -2
  29. package/dist/route-component-transform.d.ts +0 -2
  30. package/dist/route-export-pruning.d.ts +4 -1
  31. package/dist/route-imports.d.ts +18 -0
  32. package/dist/route-transform-tasks.d.ts +5 -0
  33. package/dist/route-watch.d.ts +11 -6
  34. package/dist/rsc-dev-server.d.ts +26 -0
  35. package/dist/rsc-prerender.d.ts +54 -0
  36. package/dist/rsc-route-config.d.ts +6 -0
  37. package/dist/rsc-route-exports.d.ts +15 -0
  38. package/dist/rsc-route-transform-loader.cjs +43 -0
  39. package/dist/rsc-route-transform-loader.d.ts +30 -0
  40. package/dist/rsc-route-transform-loader.js +16 -0
  41. package/dist/rsc-route-transform-registration.d.ts +12 -0
  42. package/dist/rsc-route-transforms.d.ts +21 -0
  43. package/dist/rsc-support.d.ts +23 -0
  44. package/dist/rsc-virtual-modules.d.ts +19 -0
  45. package/dist/server-build-plan.d.ts +2 -1
  46. package/dist/server-build-resolution.d.ts +1 -2
  47. package/dist/server-utils.d.ts +4 -5
  48. package/dist/ssr-asset-relocation.d.ts +98 -0
  49. package/dist/templates/entry.rsc.client.d.ts +1 -0
  50. package/dist/templates/entry.rsc.client.js +61 -0
  51. package/dist/templates/entry.rsc.d.ts +9 -0
  52. package/dist/templates/entry.rsc.js +38 -0
  53. package/dist/templates/entry.rsc.ssr.d.ts +4 -0
  54. package/dist/templates/entry.rsc.ssr.js +24 -0
  55. package/dist/typegen.d.ts +4 -2
  56. package/dist/types.d.ts +30 -1
  57. package/package.json +69 -14
  58. package/src/build-output-transforms.ts +155 -21
  59. package/src/classic-mode.ts +258 -0
  60. package/src/config-imports.ts +153 -6
  61. package/src/constants.ts +6 -2
  62. package/src/dev-background-resources.ts +46 -85
  63. package/src/dev-generation.ts +52 -33
  64. package/src/dev-hmr.ts +112 -80
  65. package/src/dev-runtime-artifacts.ts +11 -12
  66. package/src/dev-runtime-controller.ts +75 -85
  67. package/src/dev-runtime-session.ts +14 -18
  68. package/src/dev-server.ts +2 -0
  69. package/src/dev-source-maps.ts +257 -0
  70. package/src/effect-runtime.ts +105 -57
  71. package/src/entry-paths.ts +80 -0
  72. package/src/environment-output.ts +55 -0
  73. package/src/export-utils.ts +15 -11
  74. package/src/index.ts +661 -496
  75. package/src/lazy-compilation-prewarm.ts +20 -4
  76. package/src/manifest.ts +186 -100
  77. package/src/mode-plan.ts +367 -0
  78. package/src/modify-browser-manifest.ts +131 -124
  79. package/src/plugin-utils.ts +103 -39
  80. package/src/prerender-build.ts +82 -104
  81. package/src/prerender.ts +23 -24
  82. package/src/react-router-config.ts +72 -26
  83. package/src/route-artifacts.ts +96 -66
  84. package/src/route-chunks.ts +196 -54
  85. package/src/route-component-transform.ts +14 -21
  86. package/src/route-export-pruning.ts +4 -3
  87. package/src/route-imports.ts +100 -0
  88. package/src/route-transform-tasks.ts +39 -25
  89. package/src/route-watch.ts +166 -188
  90. package/src/rsc-dev-server.ts +112 -0
  91. package/src/rsc-prerender.ts +362 -0
  92. package/src/rsc-route-config.ts +175 -0
  93. package/src/rsc-route-exports.ts +67 -0
  94. package/src/rsc-route-transform-loader.ts +65 -0
  95. package/src/rsc-route-transform-registration.ts +145 -0
  96. package/src/rsc-route-transforms.ts +995 -0
  97. package/src/rsc-runtime.d.ts +143 -0
  98. package/src/rsc-support.ts +116 -0
  99. package/src/rsc-virtual-modules.ts +113 -0
  100. package/src/server-build-plan.ts +14 -3
  101. package/src/server-build-resolution.ts +37 -47
  102. package/src/server-utils.ts +21 -35
  103. package/src/ssr-asset-relocation.ts +183 -0
  104. package/src/ssr-externals.ts +8 -26
  105. package/src/templates/entry.rsc.client.tsx +168 -0
  106. package/src/templates/entry.rsc.ssr.tsx +45 -0
  107. package/src/templates/entry.rsc.tsx +80 -0
  108. package/src/typegen.ts +40 -23
  109. package/src/types.ts +39 -1
  110. package/src/warnings/warn-on-client-source-maps.ts +6 -10
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  <a href="https://rsbuild.dev" target="blank"><img src="https://github.com/web-infra-dev/rsbuild/assets/7237365/84abc13e-b620-468f-a90b-dbf28e7e9427" alt="Rsbuild Logo" /></a>
5
5
  </p>
6
6
 
7
- A Rsbuild plugin that provides seamless integration with React Router, supporting both client-side routing and server-side rendering (SSR).
7
+ React Router Framework Mode for Rsbuild.
8
8
 
9
9
  ## Features
10
10
 
@@ -19,6 +19,21 @@ A Rsbuild plugin that provides seamless integration with React Router, supportin
19
19
  - ☁️ Cloudflare Workers deployment support
20
20
  - 🔗 Module Federation support (experimental)
21
21
 
22
+ ## Framework Mode
23
+
24
+ The plugin lets an Rsbuild application use the standard React Router Framework
25
+ Mode conventions:
26
+
27
+ - `react-router.config.*` for application configuration
28
+ - `app/routes.ts` and `@react-router/dev/routes` for route configuration
29
+ - Route Module exports such as `loader`, `action`, `meta`, `links`, and
30
+ `shouldRevalidate`
31
+ - generated `./+types/*` types
32
+ - standard `entry.client` and `entry.server` entrypoints
33
+
34
+ Some React Router build-tool integrations are not supported 1:1. See the
35
+ configuration and examples below for the currently supported behavior.
36
+
22
37
  ## Installation
23
38
 
24
39
  ```bash
@@ -29,18 +44,6 @@ yarn add rsbuild-plugin-react-router
29
44
  pnpm add rsbuild-plugin-react-router
30
45
  ```
31
46
 
32
- ## Local development
33
-
34
- For the federation examples and Playwright e2e tests, use Node 22 and the
35
- repo-pinned pnpm version:
36
-
37
- ```bash
38
- nvm install
39
- nvm use
40
- corepack enable
41
- corepack prepare pnpm@9.15.3 --activate
42
- ```
43
-
44
47
  ## Usage
45
48
 
46
49
  Add the plugin to your `rsbuild.config.ts`:
@@ -60,6 +63,17 @@ export default defineConfig({
60
63
  });
61
64
  ```
62
65
 
66
+ Continue using the standard React Router Framework Mode project structure:
67
+
68
+ ```text
69
+ app/
70
+ root.tsx
71
+ routes.ts
72
+ routes/
73
+ react-router.config.ts
74
+ rsbuild.config.ts
75
+ ```
76
+
63
77
  ## Configuration
64
78
 
65
79
  React Router application settings live in `react-router.config.*`. The Rsbuild
@@ -82,9 +96,9 @@ pluginReactRouter({
82
96
  | `customServer` | `false` | Disables the built-in development SSR middleware. Enable this when an app owns the server with `createDevServer()` or an adapter. |
83
97
  | `serverOutput` | Derived | Emitted Rsbuild server format: `'module'` or `'commonjs'`. When omitted, React Router's `serverModuleFormat` selects the format (`'esm'` -> `'module'`, `'cjs'` -> `'commonjs'`); setting `serverOutput` overrides it. |
84
98
  | `lazyCompilation` | `true` | Optional Rsbuild dev lazy-compilation config. When enabled here or through `dev.lazyCompilation`, React Router hydration-critical modules stay eager so the browser manifest and route modules are not replaced by lazy proxies. |
85
- | `unstableLazyCompilationPrewarm` | `false` | Experimental prewarm for emitted Rspack lazy-compilation proxy modules after dev compiles. Enable with `true` when route JS proxy startup should happen shortly after compiler readiness. |
99
+ | `unstableLazyCompilationPrewarm` | `false` | Experimental prewarm for emitted lazy-compilation proxy modules after dev compiles. Enable with `true` when route JS proxy startup should happen shortly after compiler readiness. |
86
100
  | `logPerformance` | `false` | Logs structured React Router plugin timing information. |
87
- | `parallelRouteTransform` | `undefined` | Controls worker-thread route transforms. `undefined` and `false` keep transforms inline, `true` uses Rspack's default worker count, and a positive integer sets the maximum worker count. |
101
+ | `parallelRouteTransform` | `undefined` | Controls worker-thread route transforms. `undefined` and `false` keep transforms inline, `true` uses an automatic worker count, and a positive integer sets the maximum worker count. |
88
102
  | `onRouteTopologyChange` | `undefined` | Notification for programmatic/custom dev servers. Recreate the Rsbuild server when route files are added, removed, or moved. The callback is not awaited. |
89
103
  | `federation` | `false` | Enables the plugin's experimental Module Federation integration. |
90
104
 
@@ -111,10 +125,11 @@ export default {
111
125
  ```
112
126
 
113
127
  Use `ReactRouterRsbuildConfig` for Rsbuild projects so plugin-supported
114
- configuration such as `splitRouteModules` stays typed. The underlying route
115
- and config types come from `@react-router/dev`, which framework-mode apps
116
- already install for `routes.ts` helpers and typegen; it is declared as an
117
- optional peer dependency.
128
+ configuration such as `splitRouteModules` stays typed across React Router 7
129
+ and 8 releases. The underlying route and config types come from
130
+ `@react-router/dev`, which framework-mode apps already install for
131
+ `routes.ts` helpers and typegen; it is declared as an optional peer
132
+ dependency.
118
133
 
119
134
  Commonly used options:
120
135
 
@@ -146,29 +161,18 @@ The plugin will look for `react-router.config` with any supported JS/TS extensio
146
161
 
147
162
  If none are found, it falls back to defaults.
148
163
 
149
- ### Framework Mode
150
-
151
- React Router "Framework Mode" is implemented as a Vite plugin, but this Rsbuild
152
- plugin aims to provide equivalent **framework-mode behaviors** (typegen, Route
153
- Module API types, route module splitting, SPA/SSR/prerender strategies) on top
154
- of Rsbuild/Rspack.
155
-
156
- In practice, you should be able to use the `@react-router/dev/*` config + routes
157
- APIs, import generated `./+types/*` in route modules, and use the standard
158
- `entry.client`/`entry.server` entrypoints like you would in the official setup.
159
-
160
164
  ### FAQ
161
165
 
162
166
  #### rsbuild-plugin-react-router vs ModernJS
163
167
 
164
- This plugin is a lightweight adapter to run React Router on Rsbuild. It does
165
- not aim to replace ModernJS or its higher-level framework features. If your
166
- goal is a full framework or advanced microfrontend support, ModernJS may be
167
- a better fit.
168
+ This plugin is a focused React Router Framework Mode adapter for Rsbuild. It
169
+ does not aim to replace ModernJS or its higher-level framework features. If
170
+ your goal is a broader application framework or advanced microfrontend
171
+ support, ModernJS may be a better fit.
168
172
 
169
173
  ### SPA Mode (`ssr: false`)
170
174
 
171
- React Router's SPA Mode still requires a build-time server render of the root route to generate a hydratable `index.html` (this is how the official React Router Vite plugin works).
175
+ React Router's SPA Mode still requires a build-time server render of the root route to generate a hydratable `index.html` (this is how this Rsbuild integration mirrors React Router framework behavior).
172
176
 
173
177
  When `ssr: false`:
174
178
 
@@ -242,10 +246,10 @@ Rsbuild source maps for faster local debugging, prefer a cheap JS map:
242
246
 
243
247
  Lazy compilation prewarming is disabled by default. When enabled alongside
244
248
  `lazyCompilation`, the plugin fetches emitted browser entry and route JS assets,
245
- extracts activation keys from Rspack's generated lazy-compilation client calls,
246
- and POSTs those keys to Rspack's configured lazy trigger endpoint after dev
247
- compiles. It does not request application routes or run route loaders. Because
248
- the key extraction depends on Rspack's generated client code shape, opt in with
249
+ extracts activation keys from the generated lazy-compilation client calls, and
250
+ POSTs those keys to the configured lazy trigger endpoint after dev compiles. It
251
+ does not request application routes or run route loaders. Because the key
252
+ extraction depends on the generated client code shape, opt in with
249
253
  `unstableLazyCompilationPrewarm: true`.
250
254
 
251
255
  Subresource Integrity is disabled by default. Enable it with
@@ -486,163 +490,10 @@ Then update your `package.json` scripts:
486
490
 
487
491
  ## Cloudflare Workers Deployment
488
492
 
489
- To deploy your React Router app to Cloudflare Workers:
490
-
491
- 1. **Configure Rsbuild** (`rsbuild.config.ts`):
492
-
493
- ```ts
494
- import { defineConfig } from '@rsbuild/core';
495
- import { pluginReact } from '@rsbuild/plugin-react';
496
- import { pluginReactRouter } from 'rsbuild-plugin-react-router';
497
-
498
- export default defineConfig({
499
- environments: {
500
- node: {
501
- performance: {
502
- chunkSplit: { strategy: 'all-in-one' },
503
- },
504
- tools: {
505
- rspack: {
506
- experiments: { outputModule: true },
507
- externalsType: 'module',
508
- output: {
509
- chunkFormat: 'module',
510
- chunkLoading: 'import',
511
- workerChunkLoading: 'import',
512
- wasmLoading: 'fetch',
513
- library: { type: 'module' },
514
- module: true,
515
- },
516
- resolve: {
517
- conditionNames: [
518
- 'workerd',
519
- 'worker',
520
- 'browser',
521
- 'import',
522
- 'require',
523
- ],
524
- },
525
- },
526
- },
527
- },
528
- },
529
- plugins: [pluginReactRouter({ customServer: true }), pluginReact()],
530
- });
531
- ```
532
-
533
- 2. **Configure Wrangler** (`wrangler.toml`):
534
-
535
- ```toml
536
- workers_dev = true
537
- name = "my-react-router-worker"
538
- compatibility_date = "2024-11-18"
539
- main = "./build/server/static/js/app.js"
540
- assets = { directory = "./build/client/" }
541
-
542
- [vars]
543
- VALUE_FROM_CLOUDFLARE = "Hello from Cloudflare"
544
-
545
- # Optional build configuration
546
- # [build]
547
- # command = "npm run build"
548
- # watch_dir = "app"
549
- ```
550
-
551
- 3. **Create Worker Entry** (`server/index.ts`):
552
-
553
- ```ts
554
- import { createRequestHandler } from 'react-router';
555
-
556
- declare global {
557
- interface CloudflareEnvironment extends Env {}
558
- interface ImportMeta {
559
- env: {
560
- MODE: string;
561
- };
562
- }
563
- }
564
-
565
- declare module 'react-router' {
566
- export interface AppLoadContext {
567
- cloudflare: {
568
- env: CloudflareEnvironment;
569
- ctx: ExecutionContext;
570
- };
571
- }
572
- }
573
-
574
- // @ts-expect-error - virtual module provided by React Router at build time
575
- import * as serverBuild from 'virtual/react-router/server-build';
576
-
577
- const requestHandler = createRequestHandler(serverBuild, import.meta.env.MODE);
578
-
579
- export default {
580
- fetch(request, env, ctx) {
581
- return requestHandler(request, {
582
- cloudflare: { env, ctx },
583
- });
584
- },
585
- } satisfies ExportedHandler<CloudflareEnvironment>;
586
- ```
587
-
588
- 4. **Update Package Dependencies**:
589
-
590
- ```json
591
- {
592
- "dependencies": {
593
- "@react-router/node": "^7.13.0",
594
- "@react-router/serve": "^7.13.0",
595
- "react-router": "^7.13.0"
596
- },
597
- "devDependencies": {
598
- "@cloudflare/workers-types": "^4.20241112.0",
599
- "@react-router/cloudflare": "^7.13.0",
600
- "@react-router/dev": "^7.13.0",
601
- "wrangler": "^3.106.0"
602
- }
603
- }
604
- ```
605
-
606
- 5. **Setup Deployment Scripts** (`package.json`):
607
-
608
- ```json
609
- {
610
- "scripts": {
611
- "build": "rsbuild build",
612
- "deploy": "npm run build && wrangler deploy",
613
- "dev": "rsbuild dev",
614
- "start": "wrangler dev"
615
- }
616
- }
617
- ```
618
-
619
- ### Key Configuration Notes:
620
-
621
- - The `workers_dev = true` setting enables deployment to workers.dev subdomain
622
- - `main` points to your Worker's entry point in the build output
623
- - `assets` directory specifies where your static client files are located
624
- - Environment variables can be set in the `[vars]` section
625
- - The `compatibility_date` should be kept up to date
626
- - TypeScript types are provided via `@cloudflare/workers-types`
627
- - Development can be done locally using `wrangler dev`
628
- - Deployment is handled through `wrangler deploy`
629
-
630
- ### Development Workflow:
631
-
632
- 1. Local Development:
633
-
634
- ```bash
635
- # Start local development server
636
- npm run dev
637
- # or
638
- npm start
639
- ```
640
-
641
- 2. Production Deployment:
642
- ```bash
643
- # Build and deploy
644
- npm run deploy
645
- ```
493
+ Start from the tested [Cloudflare example](./examples/cloudflare). It contains
494
+ the complete Rsbuild configuration, Worker entry, Wrangler configuration, and
495
+ deployment scripts. The example uses `customServer: true` so the Worker owns
496
+ the React Router request handler.
646
497
 
647
498
  ## Development
648
499
 
@@ -653,6 +504,15 @@ The plugin automatically:
653
504
  - Handles route-based code splitting
654
505
  - Manages client and server builds
655
506
 
507
+ For local development, use Node 22 and the repository's pinned pnpm version:
508
+
509
+ ```bash
510
+ nvm install
511
+ nvm use
512
+ corepack enable
513
+ corepack prepare pnpm@9.15.3 --activate
514
+ ```
515
+
656
516
  ### Benchmarking
657
517
 
658
518
  Run the focused local suite for plugin regression checks:
@@ -667,18 +527,6 @@ See the [benchmark guide](./benchmarks/README.md) for JSON output, pull-request
667
527
  comparisons and comments, `BENCHMARK_PLUGIN_ROOT`, and optional CodSpeed
668
528
  publication.
669
529
 
670
- ## React Router Framework Mode
671
-
672
- React Router "Framework Mode" wraps Data Mode using a Vite plugin. This Rsbuild
673
- plugin aims to match the important behaviors without depending on Vite:
674
-
675
- - Typegen + Route Module API types (`./+types/*`)
676
- - Route module splitting (`splitRouteModules`)
677
- - SPA mode (`ssr: false`), SSR mode, and static prerendering (`prerender`)
678
-
679
- Some Vite-specific integrations (for example Vite's environment API + critical
680
- CSS endpoint) are not supported 1:1.
681
-
682
530
  ## Examples
683
531
 
684
532
  The repository includes several examples demonstrating different use cases:
@@ -691,6 +539,8 @@ The repository includes several examples demonstrating different use cases:
691
539
  | [custom-node-server](./examples/custom-node-server) | Custom Express server with SSR | 3003 | `pnpm dev` |
692
540
  | [cloudflare](./examples/cloudflare) | Cloudflare Workers deployment | 3004 | `pnpm dev` |
693
541
  | [client-only](./examples/client-only) | `.client` modules with SSR hydration | 3010 | `pnpm dev` |
542
+ | [react-router-8](./examples/react-router-8) | React Router 8 framework-mode SSR | 3020 | `pnpm dev` |
543
+ | [rsc-mode](./examples/rsc-mode) | Experimental RSC Framework Mode | 3021 | `pnpm dev` |
694
544
  | [epic-stack](./examples/epic-stack) | Full-featured Epic Stack example | 3005 | `pnpm dev` |
695
545
  | [federation/epic-stack](./examples/federation/epic-stack) | Module Federation host | 3006 | `pnpm dev` |
696
546
  | [federation/epic-stack-remote](./examples/federation/epic-stack-remote) | Module Federation remote | 3007 | `pnpm dev` |